0
$\begingroup$

Rosanswers logo

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


Originally posted by alexalspach on ROS Answers with karma: 11 on 2013-01-21

Post score: 1

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

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.


Originally posted by Dan Lazewatsky with karma: 9115 on 2013-01-22

This answer was ACCEPTED on the original site

Post score: 3

$\endgroup$
0
$\begingroup$

Rosanswers logo

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.


Originally posted by Thomas with karma: 4478 on 2013-01-22

This answer was NOT ACCEPTED on the original site

Post score: 2

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.