roscpp编程--Node的创建和管理

来源:互联网 发布:js的非阻塞sleep函数 编辑:程序博客网 时间:2024/06/05 10:43

from:http://www.ros.org/wiki/roscpp/Overview/Initialization%20and%20Shutdown


Initialization and Shutdown


There are two levels of initialization for a roscpp Node:

  1. Initializing the node through a call to one of theros::init() functions. This provides command line arguments to ROS, and allows you to name your node and specify other options.

  2. Starting the node is most often done through creation of a ros::NodeHandle, but in advanced cases can be done in different ways.

Each of these is described below

Initializing the roscpp Node

Before calling any other roscpp functions in a node you must call one of theros::init() functions. The two most commoninit() invokations are:

ros::init(argc, argv, "my_node_name");

and

ros::init(argc, argv, "my_node_name", ros::init_options::AnonymousName);

In general, the form ofros::init() conforms to:

void ros::init(<command line or remapping arguments>, std::string node_name, uint32_t options);

Let's go over the arguments in order:

  • argc andargv

    • ROS uses these to parse remapping arguments from the command line. It also modifies them so that they no longer contain any remapping arguments, so that if you callros::init() before processing your command line you will not need to skip those arguments yourself.

    node_name

    • This is the name that will be assigned to your node unless it's overridden by one of theremapping arguments. Node names must beunique across the ROS system. If a second node is started with the same name as the first, the first will be shutdown automatically. In cases where you want multiple of the same node running without worrying about naming them uniquely, you may use theinit_options::AnonymousName option described below.

    options

    • This is an optional argument that lets you specify certain options that change roscpp's behavior. The field is a bitfield, so multiple options can be specified. The options are described in theInitialization Options section.

There are other forms of ros::init() that do not take argc/argv, instead taking explicit remapping options. Specifically, there are versions that take astd::map<std::string, std::string> and a std::vector<std::pair<std::string, std::string> >.

Initializing the node simply reads the command line arguments and environment to figure out things like the node name, namespace and remappings. It doesnot contact the master. This lets you useros::master::check() and other ROS functions after callingros::init() to check on the status of the master. The node only full spins up once it has been started, which is described in theStarting the roscpp Node section.

Initialization Options

See also: ros::init_options code API

  • ros::init_options::NoSigintHandler

    • Don't install a SIGINT handler. You should install your own SIGINT handler in this case, to ensure that the node gets shutdown correctly when it exits. Note that the default action for SIGINT tends to be to terminate the process, so if you want to do your own SIGTERM handling you will also have to use this option.

    ros::init_options::AnonymousName

    • Anonymize the node name. Adds a random number to the end of your node's name, to make it unique.

    ros::init_options::NoRosout

    • Don't broadcast rosconsole output to the /rosout topic.

Accessing Your Command Line Arguments

As mentioned above, calling ros::init() with argc and argv will remove ROS arguments from the command line. If you need to parse the command line before callingros::init(), you can call the (new in ROS 0.10) ros::removeROSArgs() function.

Starting the roscpp Node

The most common way of starting a roscpp node is by creating aros::NodeHandle:

ros::NodeHandle nh;

When the first ros::NodeHandle is created it will call ros::start(), and when the lastros::NodeHandle is destroyed, it will callros::shutdown(). If you want to manually manage the lifetime of the node you may callros::start() yourself, in which case you should callros::shutdown() before your program exits.

Shutting Down

Shutting Down the Node

At any time you may call the ros::shutdown() function to shutdown your node. This will kill all open subscriptions, publications, service calls, and service servers.

By default roscpp also installs a SIGINT handler which will detectCtrl-C and automatically shutdown for you.

Testing for Shutdown

There are two methods to check for various states of shutdown. The most common isros::ok(). Onceros::ok() returns false, the node has finished shutting down. A common use ofros::ok():

   1 while (ros::ok())   2 {   3   ...   4 }


The other method to check for shutdown is the ros::isShuttingDown() method. This method will turn true as soon as ros::shutdown() is called, not when it is done. Use ofros::isShuttingDown() is generally discouraged, but can be useful in advanced cases. For example, to test inside of a prolonged service callback whether the node is requested to shut down and the callback should therefore quit now,ros::isShuttingDown() is needed.ros::ok() would not work here because the node can't finish its shutdown as long as the callback is running.

原创粉丝点击