ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Having 4 callback functions

asked 2019-04-29 02:11:01 -0600

EdwardNur gravatar image

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?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2019-04-30 22:44:58 -0600

robustify gravatar image

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.

edit flag offensive delete link more

Comments

@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?

EdwardNur gravatar image EdwardNur  ( 2019-05-01 05:10:38 -0600 )edit

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.

gvdhoorn gravatar image gvdhoorn  ( 2019-05-01 05:53:05 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2019-04-29 02:11:01 -0600

Seen: 160 times

Last updated: Apr 30 '19