0
$\begingroup$

Rosanswers logo

I have a code where I need 4 callback functions (4 subscribers) for each wheel of my robot. What will be the best option to create those subscribers?

My idea was to create 4 threads that can run simultaneously:

ros::MultiThreadedSpinner spinner(4); 

But from my understanding, the spinner will be blocking one and 4 threads won't use callbacks at the same time so I might just use a regular spinOnce();, am I correct?

Can you suggest may be something?


Originally posted by EdwardNur on ROS Answers with karma: 115 on 2019-04-29

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

The regular ros::spin() can handle multiple ROS topic subscribers. Just set up four subscribers in your main function and call ros::spin():

ros::Subscriber sub1 =  nh.subscribe("topic1", 1, callback1);
ros::Subscriber sub2 =  nh.subscribe("topic2", 1, callback2);
ros::Subscriber sub3 =  nh.subscribe("topic3", 1, callback3);
ros::Subscriber sub4 =  nh.subscribe("topic4", 1, callback4);
ros::spin();

Just make sure your callback functions don't block or take a long time to finish, or your other callbacks won't get scheduled properly.


Originally posted by robustify with karma: 956 on 2019-04-30

This answer was ACCEPTED on the original site

Post score: 0


Original comments

Comment by EdwardNur on 2019-05-01:
@robustify If I have 4 wheels, wouldn't it make my wheels to start async? Meaning that I can get a bit different trajectory because some wheels start considerably quicker than others?

Comment by gvdhoorn on 2019-05-01:
If the callbacks don't take too long (and they shouldn't) there should be at most a few milliseconds between the callbacks. That sort of delay typically "drowns" in delays introduced by motion controllers, velocity ramp-up profiles and general sluggishness due to inertia, etc.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.