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

Pause execution of program from callback function

asked 2016-04-12 07:11:24 -0500

bergercookie gravatar image

updated 2016-04-12 07:13:19 -0500

gvdhoorn gravatar image

In my main, I have defined a callback function with the following code:

// initialize a Subscribing for reading the current pose
CallbackHandler cb_handler(slam, &node_list, &node_tstamps);
ros::Subscriber sub = nh.subscribe("/pose2D", 1000, &CallbackHandler::checkOdometricConstraint, &cb_handler);

After that I call ros::spin() to wait for callbacks


Is it possible to pause the system execution from within the callback function? I tried using cpp approaches like system("pause") or std::cin.get() but that didn't work.

edit retag flag offensive close merge delete


can you tell us why you want to do that (just trying to avoid an xy-problem)?

gvdhoorn gravatar image gvdhoorn  ( 2016-04-12 07:13:55 -0500 )edit

I incrementally add elements to an std::vector when the callback function is called, and each time this happens I need some time to investigate the added element. I could use ros::Duration(x).sleep() for this purpose but it would be handier if I could pause execution as long as I needed..

bergercookie gravatar image bergercookie  ( 2016-04-12 07:18:24 -0500 )edit

With "I need some time to investigate" are you really talking about you, the person, or the rest of your program?

gvdhoorn gravatar image gvdhoorn  ( 2016-04-12 10:43:00 -0500 )edit

I am talking about me watching the program output. My goal is to see what is the output of the program, for debugging reasons.

bergercookie gravatar image bergercookie  ( 2016-04-12 14:22:44 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2018-04-07 04:28:31 -0500

dotcom gravatar image

updated 2018-04-07 04:44:58 -0500

  1. You could pause outside of a callback, if you really need to; store the value you will add to the vector in the callback.
  2. By using simulation time you can make your clock server pause, just like you pause a ROS bag from being played.

For debugging reasons (this one is a clear case of this, from your comments) you should really use a debugger, not print the contents of a vector. You will be glad you did it if you put some time into it. Super easy if you have an IDE (for gdb see this).

First of all start here. If you find yourself really needing to use a launch file, such that you can't run your node from the IDE, maybe your IDE provides a way to attach the debugger to the already running node.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2016-04-12 07:11:24 -0500

Seen: 1,202 times

Last updated: Apr 07 '18