ros节点暂停

1. ros运行单位:

Ros程序运行的单位是ros node。

2. ros 节点的启动:

(1)初始化ros节点:通过调用ros::init()接口实现;可以通过参数指定默认节点名字,之所以是默认,是因为在命令行执行程序时,是可以修改节点名字的。

(2)启动ros节点:通过调用ros::NodeHnadle创建NodeHnadle实例nh实现,第一次创建实例时,会调用ros::start(),此时启动本节点。

3. ros节点关闭:

(1)通过调用ros::shutdown()接口,关闭ros节点,此接口会关闭订阅话题、发布话题、请求服务以及提供服务。

(2)在最后一个ros::NodeHnadle对象销毁时,会自动调用ros::shutdown(),以此关闭ros节点。

4. 检测ros节点是否已经关闭:

(1)ros::ok()接口:返回false,说明节点已经关闭。

(2)ros::isShuttingDown()接口:返回true,说明ros::shutdown()被调用,但是节点比一定结束。

注:所以一般使用ros::ok()判断,但是在不会结束的节点,例如“ prolonged service callback”中,则只能使用ros::isShuttingDown(),因为此节点不会关闭,疑惑这是什么节点?

5. 中断:

(1)在ros::init()选项中,有一选项ros::init_options::NoSigintHandler,决定是否安装默认SIGINT处理函数,若不带此参数,则安装,程序在收到SIGINT信号(ctrl+c)时,ros::ok()返回false,节点可以走关闭流程。若带此参数,需要自己安装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_INFO("in mySigintHandler.");
  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_INFO("before spin.");
  ros::spin();
  ROS_INFO("after spin.");
  return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_63761958/article/details/130039569