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

a callback function use itself

asked 2017-07-14 09:21:34 -0500

Tomas yuan gravatar image

Here is the problem , I have a callback function ,and in this funciton ,there are some loop work.In order to response to other work , I use spinOnce() somewhere in the loop. And it would step out of the loop to process the callback function.It will be back to the recent callback function until finish the current callback function . So,I want to just finish the current funcion and not do any recent job.Can I ?
for example :

 void callback(const mulprocess::hello & msg) {  
  printf("in the callback\n");;

  for(int i=0;i<10;i++)
    printf("sleep end and data is%s\n",data.c_str());

And the output is like following picture. When first in call back it show "in the callback" ,and it spinOnce() step out to response the second callback,it also show "in the callback".Then it finish the second one ,and return back to the former. I want just response the lastest one and end.

image description

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2017-07-14 11:47:07 -0500

allenh1 gravatar image

As a general rule, I would refrain from calling ros::spinOnce() in a callback (except for, perhaps, some very specific scenarios).

ros::spinOnce() will process the next message in the queue, which is why you are getting the duplication there (because the callback is now calling itself).

For more info, I'll refer you to the following roscpp tutorial.

edit flag offensive delete link more

Question Tools



Asked: 2017-07-14 09:21:34 -0500

Seen: 164 times

Last updated: Jul 14 '17