当前位置:网站首页>ros常用的函数——ros::ok(),ros::Rate,ros::spin()和ros::spinOnce()
ros常用的函数——ros::ok(),ros::Rate,ros::spin()和ros::spinOnce()
2022-04-23 17:36:00 【后厂村路蔡徐坤】
(一)ros::ok()
用于检查系统状态,适用于在while循环中判断状态
语法:
while(ros::ok())
{
//...
}
ros::ok()在以下几种情况下会返回false:
1、按下Ctrl-C时。
2、我们被一个同名同姓的节点从网络中踢出。
3、ros::shutdown()被应用程序的另一部分调用。
4、所有的ros::NodeHandles都被销毁了。
5、一旦ros::ok()返回false,所有的ROS调用都会失败
(二)ros::Rate
用于设置循环频率,适用于Publisher程序无限循环
ros::Rate对象可以允许你指定自循环的频率
ros::Rate loop_rate(10);//10Hz
//...
loop_rate.sleep();
(三)ros::spin()和ros::spinOnce()
用于处理ROS的回调函数,适用于Subscriber处理回调数据。
两者区别在于ros::spin()调用后不会再返回,而ros::spinOnce()在调用后还可以继续执行之后的程序。
ros::spin()用于非循环场景,ros::spinOnce()用于有循环的场景。
什么时候用ros::spin()和ros::spinOnce()呢,如果仅仅只是响应topic,就用ros::spin()。当程序中除了响应回调函数还有其他重复性工作的时候,那就在循环中做那些工作,然后调用ros::spinOnce()。
1、ros::spin()
int main(int argc, char **argv)
{
ros::init(argc, argv, "listener");
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);
ros::spin();
return 0;
}
2、ros::spinOnce()
这句话的意思是监听反馈函数(callback)。只能监听反馈,不能循环。所以当你需要监听一下的时候,就调用一下这个函数。
这个函数比较灵活,尤其是我想控制接收速度的时候。配合ros::ok()效果极佳。
ros::Rate loop_rate(10);
while(ros::ok())
{
ros::spinOnce();
loop_rate.sleep();
}
版权声明
本文为[后厂村路蔡徐坤]所创,转载请带上原文链接,感谢
https://blog.csdn.net/zeye5731/article/details/124314347
边栏推荐
- Deep understanding of control inversion and dependency injection
- Use of shell cut command
- If you start from zero according to the frame
- 2. Electron's HelloWorld
- [WPF binding 3] listview basic binding and data template binding
- Conversion between hexadecimal numbers
- MySQL installation
- Clickhouse table engine
- . net type transfer
- 958. 二叉树的完全性检验
猜你喜欢
Compare the performance of query based on the number of paging data that meet the query conditions

Qt error: /usr/bin/ld: cannot find -lGL: No such file or directory

01 - get to know the advantages of sketch sketch

Webapi + form form upload file

ASP. Net core JWT certification

Double pointer advanced -- leetcode title -- container with the most water

Why do some people say SCM is simple and I have to learn it so hard?

STM32 entry development board choose wildfire or punctual atom?

Clickhouse table engine

470. 用 Rand7() 实现 Rand10()
随机推荐
Webapi + form form upload file
ClickHouse-表引擎
matlab如何绘制已知公式的曲线图,Excel怎么绘制函数曲线图像?
tidb-server 的配置文件在哪里?
快时钟同步慢时钟域下的异步控制信号slow clk to fast clk
给 el-dialog 增加拖拽功能
402. 移掉 K 位数字-贪心
Qt 修改UI没有生效
How does matlab draw the curve of known formula and how does excel draw the function curve image?
Header built-in object
嵌入式系统中,FLASH中的程序代码必须搬到RAM中运行吗?
QT modification UI does not take effect
Using quartz under. Net core -- job attributes and exceptions of [4] jobs and triggers
ECMAScript history
uni-app黑马优购项目学习记录(下)
JS failed to change all variables and changed to the return method. Finally, the problem was solved
Shell-awk命令的使用
How to use the input table one-way service to send (occupy less) picture files (body transmission)? FileReader built-in object involved
Halo 开源项目学习(二):实体类与数据表
Come out after a thousand calls