节点的启动与关闭 ros::init()解析(c++)
1.初始化roscpp 节点
ros::init() API链接:http://docs.ros.org/api/roscpp/html/init_8h.html
在node代码中在调用其它roscpp函数前,首先调用ros::init()函数
例如:
ros::init(argc, argv, "my_node_name");
ros::init(argc, argv, "my_node_name", ros::init_options::AnonymousName);
函数原型:
void ros::init(<command line or remapping arguments>, std::string node_name, uint32_t options);
argc和argv:解析命令行重映射参数
node_name:节点名,在ros系统中,节点名必须唯一;如果重名节点被运行,那之前的节点就会自动关闭。
options:选项参数,明确roscpp的一些具体的行为
初始化节点只是简单地从命令行参数和环境中确定像节点名、名字空间以及重映射。它不会与主节点master交互。
1) 初始化选项
ros::init_options API链接:
http://docs.ros.org/api/roscpp/html/namespaceros_1_1init__options.html
2)获取你的命令行参数
ros::removeROSArgs() API链接:
http://docs.ros.org/api/roscpp/html/init_8h.html
2.启动roscpp节点
最常用的启动roscpp节点的方式就是创建ros::NodeHandle实例:
ros::NodeHandle nh;
二、节点的关闭
1.关闭节点
节点会在调用ros::shutdown()函数时被关闭,这将杀死所有该节点相关的订阅者,发布者,服务请求,服务提供...
2.关闭测试
有两种测试关闭状态的方法。
最常用的方法是ros::ok(),一旦ros::ok()返回false,节点就已经被关闭了。
使用方法:
while(ros::ok())
{
...
...
...
}
另一种是使用ros::isShutDown()方法。当ros::shutdown()函数被调用时,该函数返回true.
在一个持久的服务回调函数中使用该测试函数测试是否应该结束,这时候不能使用ros::ok()来测试,因为节点在回调函数运行时无法完成shutdown
自定义的SIGINT句柄:信号中断句柄。
使用方法:
#include <ros/ros.h>
#include <signal.h>
void mySigintHandler(int sig)
{
// Do some custom action.
// For example, publish a stop message to some other nodes.
// All the default sigint handler does is call shutdown()
ros::shutdown();
}
int main(int argc, char** argv)
{
ros::init(argc, argv, "my_node_name", ros::init_options::NoSigintHandler);
ros::NodeHandle nh;
// Override the default ros sigint handler.
// This must be set after the first NodeHandle is created.
signal(SIGINT, mySigintHandler);
//...
ros::spin();
return ;
}
最新文章
- Bringing Whoops Back to Laravel 5
- [LeetCode] Binary Tree Right Side View 二叉树的右侧视图
- 手机web页面开发-第一弹
- Win7系统修改hosts文件不能保存的解决方法
- struts理解
- ArrayList vs LinkedList vs Vector
- RFS一些基本概念
- 学号20145220 《Java程序设计》第5周学习总结
- 关于kafka连接不上别的机器问题Connection refused
- sublime text3设置文件类型(CR/LF)
- win8、win8.1官方版本、及安装密钥
- Best Time to Buy and Sell Stock 解答
- Android 使用PopupWindow实现弹出菜单
- vconfig 的使用
- xmlns:xsi ——是指xml文件遵守xml规范,xsi全名:xml schema instance
- Dictionary导致CPU暴涨
- Unity 简易的UI背景昼夜轮替效果
- python3 练习题(用函数完成登录注册以及购物车的功能)
- 20154305 齐帅 PC平台逆向破解
- [PHP] 看博客学习观察者模式