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

shared variable between callbacks

asked 2015-05-05 10:21:52 -0500

mark_vision gravatar image

Hi all, I have a ros node that is supposed to accept two sources (IMU and PointCloud) with two different frequecies (Imu is ~200 Hz, PointCloud is ~30 Hz). So expect around 7 IMU messages for every cloud message.

If I use a message filter, I can collect in a single callback the two closest signals.

Instead, I would like to independently collect all the occurences of IMU between two point cloud messages, so that I can use them to get a better interpolation of the IMU signal to synchronize the two signals.

Would two callbacks + a global variable (a std::vector) suffice? Should I implement some blocking mechanism to prevent the global variable from being modified when I'm inside the cloud callback?

Are the callbacks sequential? In that case, how can I make them concurrent?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2015-05-05 11:00:43 -0500

updated 2015-05-05 11:04:02 -0500

The answer depends on whether you are using roscpp or rospy. It seems you are using roscpp because you mentioned std::vector. If you are simply using ros::spin or ros::spinOnce there is only a single thread handling callbacks so there should be no need for any sort of mutex.

If you'd like to use a multi-threaded approach to processing your callbacks, roscpp includes the ros::MultiThreadedSpinner and ros::AsyncSpinner. If using one of these spinners, you should take care to prevent access collisions.

This information was pulled from the Callbacks and Spinning roscpp Overview page.


IMO, it can be a bit difficult to find documentation on the rospy threading model (and I'm apparently too lazy to write a wiki page about it), but this ROS answers post has a clear and concise description.

edit flag offensive delete link more

answered 2015-05-05 11:03:56 -0500

Wolf gravatar image

In a typical ros node without Asynch/Multithreaded Spinners or something you do not have to take care about thread safety, i. e. you don't need blocking mechanisms. Any of your callbacks can only be called when your main process loop is calling ros::spin() or ros::spinOnce() and only one of our callbacks will be called at the same time (no parallel callbacks).

If you need parallel callbacks you should at this:

Keep in mind that if you use asynch/multithreaded spinning your callbacks might be executed parallel, i. e. you need blocking mechanisms, mutexes, condition variables and stuff.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2015-05-05 10:21:52 -0500

Seen: 3,242 times

Last updated: May 05 '15