ROS学习笔记81(从NodeHandle访问私有名称)

原文链接: http://wiki.ros.org/roscpp_tutorials/Tutorials/AccessingPrivateNamesWithNodeHandle

1 Why not just ~name?

The introduction of NodeHandles in roscpp created something of a conundrum when dealing with private names. If I create a NodeHandle with its own namespace:

ros::init(argc, argv, "my_node_name");
ros::NodeHandle nh("/my_node_handle_namespace");

私有名字应该怎么解析?有些选项是: 

  • /my_node_handle_namespace/my_node_name/name

  • my_node_name/my_node_handle_namespace/name

  • /my_node_handle_namespace/name

  • Something else entirely

出于这个原因,NodeHandle不允许直接传递私有名称给它的方法,或构造函数一个NodeHandle作为参数。

2 获取私有名称

The solution is to construct a NodeHandle with a private name as its namespace:

ros::init(argc, argv, "my_node_name");
ros::NodeHandle nh1("~");  // must be in main()
ros::NodeHandle nh2("~foo");

nh1's 名称空间是 /my_node_name, and nh2's 名称空间是  /my_node_name/foo.

So instead of doing this:

ros::NodeHandle nh;
nh.getParam("~name", ... );

你可以这样做

ros::NodeHandle nh("~");
nh.getParam("name", ... );

 

你可能感兴趣的:(ROS学习笔记)