ros之initialization與shutdown
Initialization
方法一:
ros::init(argc, argv, "my_node_name");
方法二:
ros::init(argc, argv, "my_node_name", ros::init_options::AnonymousName);
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 astd::vector<std::pair<std::string, std::string> >
.
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.
start a ros node
ros::NodeHandle nh;
ros::NodeHandle priv_nh("~");
Shutting Down
在程式中使用ros::shutdown()
ctrl + c
會終止所有的節點。
Custom SIGINT Handler
#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;
}
注: 當程式捕捉到ctrl + c
之後,會進入訊號處理函式,處理完後會終止ros節點。
如何使用shutdown()終止所有的節點
在launch檔案中,有個required引數,設為true時,當該節點shutdown後,其餘節點也會被shutdown。
required="true"(optional)
If node dies, kill entire roslaunch.
注:這個功能在程式中還是很有用的。