当前位置:网站首页>Usage and difference between ros:: spinonce() and ros:: spin()

Usage and difference between ros:: spinonce() and ros:: spin()

2022-06-26 18:13:00 Maccy37

       When running the program , subscribe /scan The topic information has not been transmitted into the laser data , Later, I struggled to ask my friend and found that he didn't write ros::spinOnce(), Unable to call the callback function to pass in laser data . The following is after solving the problem , Refer to other people's blogs , Written study notes .

1. About ros Tutorial on message publishers and subscribers :http://wiki.ros.org/ROS/Tutorials/WritingPublisherSubscriber(c++)

2. After reading the tutorial in the link , We know :

        The message publisher is in a while It has been sending circularly “hello world” To the topic (topic)chatter On . Once the message subscriber knows chatter It has data, I'll put this data Pass in as a parameter callback Function , But it has not been implemented yet callback function , But the callback Function is placed in a callback function queue . So when the publisher keeps sending data To chatter Up there , There will be corresponding callback Function into the queue , They have the same function name , But the arguments are different .

So when will it be implemented callback Function? ?

Namely ros::spinOnce() and ros::spin() Thing .

ros::spinOnce()

       This sentence means to monitor the feedback function (callback). Only listen for feedback , You can't cycle . When spinOnce When the function is called ,spinOnce Will call the first in the callback function queue callback function , here callback Function is executed , Then wait until next time spinOnce When the function is called again , The second in the callback function queue callback The function will be called , And so on .

      therefore , There will be a problem . Because the length of the callback function queue is limited , If the publisher sends data too fast ,spinOnce Too few function calls , This will cause the queue to overflow , some callback Functions will be squeezed out , As a result, it is not implemented to .

ros::spin()

     Loop and listen to feedback functions (callback). Once entered spin function , It will not return , It's like it's looping in its own function . As long as the callback function queue has callback Function in , It will immediately execute callback function . If not , It will block , Will not occupy CPU.

for example :

    This sentence means to monitor the feedback function (callback). Only listen for feedback , You can't cycle . So when you need to monitor , Just call this function .  This function is more flexible , Especially when I want to control the receiving speed . coordination ros::ok() Excellent results .  

ros::Rate loop_rate(10);
while(ros::ok())
{
    ros::spinOnce();
    loop_rate.sleep();
}

Can be controlled 10Hz Speed , function callback function , Very convenient . 

If only

while(ros::ok())
{
    ros::spinOnce();
}

This is equal to ros::spin().

Be careful :

These two functions are only related to the receive callback function (callback) of , It has nothing to do with the release . If you want to publish circularly , Can only write circularly publish().

原网站

版权声明
本文为[Maccy37]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261807206898.html