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

timer callback

asked 2013-01-21 18:52:40 -0500

alexalspach gravatar image

updated 2014-01-28 17:14:56 -0500

ngrennan gravatar image

Hello, all,

Been searching for a while but I figured I'd ask directly. What are the pros/cons of using rate.sleep() to control loop frequency versus using a timer callback?

From what I have gathered so far, it seems that sleep should be used if you no other processes need to run while waiting for the next control iteration. Using a callback obviously allows the main loop to continue running while we wait for the next callback.

Mainly I'm interested in the reliability of either. I am running a control loop at only 333Hz with very little control code in between callbacks. What happens if the control loop takes longer than the desired period of the iteration?

Please advise. General information or pointers to the proper resources are greatly appreciated.

Thank you!

Alex

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
3

answered 2013-01-22 03:51:56 -0500

Your last question gets at the main difference in my mind. With a timer, if your code takes longer than the timeout, it will still be called again anyway in a new thread. Using rate.sleep(), the next iteration will not start until the previous one has finished, and a warning will be issued saying the loop has missed its desired rate. Which behavior is best depends on the situation.

edit flag offensive delete link more
2

answered 2013-01-22 13:33:40 -0500

Thomas gravatar image

updated 2013-01-22 16:11:50 -0500

It depends on your spinner (see this question). If you have a blocking spinner then if a timer callback is taking too long, then, as the spin call is blocking there won't be any other call to the callback. If you use an Async spinner, a new callback will be called in parallel, given that all the threads are not busy.

I would definitively use a ros::Rate object in your case as it makes the code easier to read and to maintain. IMHO it is easier when the main processing is run in the main thread while callbacks only handle as little as possible.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-01-21 18:52:40 -0500

Seen: 2,504 times

Last updated: Jan 22 '13