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 0;
}
本文深入讲解ROS节点的初始化与关闭过程,包括初始化选项、获取命令行参数、节点启动及关闭测试方法,同时介绍如何自定义SIGINT句柄,实现节点的精细控制。
731

被折叠的 条评论
为什么被折叠?



