Understanding of ros::Rate and ros::spin() in ros

1. About the use and understanding of ros::Rate rate() and rate.sleep()

These two functions are used to control the release frequency, which controls the cycle frequency, not the release frequency of messages and services. By controlling the cycle frequency, the release frequency can be controlled

ros::Rate rate(50) //Define a frequency

rate.sleep() //sleep

Often ros::Rate rate(50) is written outside the loop, while rate.sleep() is inside the loop to control the frequency of topic publishing.

This frequency refers to the time between running the last loop.sleep() and the next loop.sleep() . Usually, the code runs faster than the set frequency, so if you run to the next loop.sleep If it does not reach 0.02s (1/50Hz) after (), it will start to sleep, and then execute the next program after 0.02s.

2. About the use and understanding of ros::spin() and ros::spinonce()

The overall effect is to centrally process all callback functions in this node

When we want to subscribe to a topic and use subscribe, we must use ros::spin() or ros::spinonce() to call the callback function. In topic subscription communication, when Publish publishes a topic, sub will subscribe to the same topic. At the same time, there is a queue_size parameter in the subscribe function, which is the length of the callback function list. Any publication and subscription will have its corresponding publication The cache area and the subscription cache area, just like the publisher and subscriber functions, have parameters for the number of caches (queue length). Why is there this parameter?

This is because in the process of publishing and subscribing, if the publishing frequency is too fast and the processing time in the callback function is very long, the callback of the topic published later will be queued. This queue is the subscription buffer, and wait until the last callback After the end, subscribe will find the one with the earliest timestamp in the callback function queue to call back, which can prevent missing

Usually in use, if you have high requirements for real-time performance and want to only process the published information at this moment each time, then it is best to set queue_size to 1, so that each callback function calls back the currently published topic.

If you don't want to miss all the published topics, you can set the queue_size slightly larger

If queue_size is 0, it indicates that the callback function queue is infinite, and the callback of the topic coming later can be filled all the time

例如:rospy.Subscriber('usb_cam/image_raw', Image, callback,queue_size=1)

When there is a subscription topic in the node, the ros::spin() and ros::spinonce() functions must be used. Generally speaking, these two functions are in the wake-up callback function.

So what is the difference between these two functions?

ros::spin():

When ros::spin() is processed, it will always go to the topic subscription buffer to check if there is a callback function. If there is, it will process the callback function. If not, continue to check and wait, so it is processed to ros::spin() Will not execute anything other than the callback function, the node will only wait and process the callback function at this time

praise::spinonce():

When ros::spinonce() is processed, it will go to the topic subscription buffer to check if there is a callback function. If there is, it will process the callback function. If not, it will continue to execute. There is a difference here, spinonce will not always Stuck at the callback function, he just checks to see if there is a callback function in the buffer, instead of waiting for the callback function to arrive

Guess you like

Origin blog.csdn.net/weixin_62705892/article/details/127678310
Recommended