Note: This tutorial assumes that you have completed the previous tutorials: ROS Tutorials. |
Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags. |
Accessing Private Names from a NodeHandle
Description: This tutorial will show you how to access private Names with roscpp's NodeHandle API.Tutorial Level: BEGINNER
Next Tutorial: 使用类中的“方法(method)”作为“回调函数(callback)”
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:
Where should a private name resolve? Some options would be:
/my_node_handle_namespace/my_node_name/name
my_node_name/my_node_handle_namespace/name
/my_node_handle_namespace/name
- Something else entirely
For this reason, NodeHandle does not allow passing private names directly to its methods, or to constructors that take a NodeHandle as an argument.
Accessing Private Names
The solution is to construct a NodeHandle with a private name as its namespace:
nh1's namespace is /my_node_name, and nh2's namespace is /my_node_name/foo.
So instead of doing this:
You do this:
Next Tutorial: 使用类中的“方法(method)”作为“回调函数(callback)”