ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
The easiest way to do what you're looking for is to call spinOnce
in a loop. This is non-blocking, and allows you to do other operations in "parallel" with the callback processing. Note that this is not truly parallel, since this is a single-threaded model, but many applications are fine without truly multi-threaded operations.
See here for more discussion of various spinning methods.
Try something like this:
my_callback(){something};
int main(){
sub = node.subscribe("topic", &my_callback);
ros::Rate r(10); // 10 hz
while (ros::ok())
{
do_callback_independent_processing();
if (new_callback_data)
do_callback_dependent_processing();
ros::spinOnce();
r.sleep();
}
}
2 | add hint for ASyncSpinner |
The easiest way to do what you're looking for is to call spinOnce
in a loop. This is non-blocking, and allows you to do other operations in "parallel" with the callback processing. Note that this is not truly parallel, since this is a single-threaded model, but many applications are fine without truly multi-threaded operations.
See here for more discussion of various spinning methods.
Try something like this:
my_callback(){something};
int main(){
sub = node.subscribe("topic", &my_callback);
ros::Rate r(10); // 10 hz
while (ros::ok())
{
do_callback_independent_processing();
if (new_callback_data)
do_callback_dependent_processing();
ros::spinOnce();
r.sleep();
}
}
Edit:
If you don't like the idea of calling spinOnce
in a loop, then probably the next-best choice is to use ASyncSpinner
. Trying to use multiple threads and multiple calls to ros::spin()
is messy, and probably won't work. ros::spin
explicitly states that it is designed for single-threaded nodes only.