当前位置:网站首页>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().
边栏推荐
- Clion编译catkin_ws(ROS工作空间包的简称)加载CMakeLists.txt出现的问题
- Chinese (Simplified) language pack
- transforms.RandomCrop()的输入只能是PIL image 不能是tensor
- properties文件乱码
- Case study of row lock and isolation level
- Row lock analysis and deadlock
- JS cast
- In and exceptions, count (*) query optimization
- Regular match same character
- sqlite数据库的系统表sqlite_master
猜你喜欢
随机推荐
ROS的发布消息Publishers和订阅消息Subscribers
next(iter(dataloader))的一点点体会
A little experience of next (ITER (dataloader))
Deep understanding of MySQL lock and transaction isolation level
Properties file garbled
JS common regular expressions
你了解如何比较两个对象吗
wm_concat()和group_concat()函数
Get and set settings in 26class
[unity] use C in unity to execute external files, such as Exe or bat
Introduction to Ethereum Technology Architecture
DVD-数字通用光盘
How does Guosen Securities open an account? Is it safe to open a stock account through the link
Handwritten promise all
transforms.RandomCrop()的输入只能是PIL image 不能是tensor
How pycharm modifies multiline annotation shortcuts
Analysis of deep security definition and encryption technology
利用递归实现求n位所有格雷码
Eigen库计算两个向量夹角
Ethereum技术架构介绍









