Naming in ROS
Naming in ROS
Understanding Names
Overview
- Every item in the ROS Computation Graph has a Graph Resource Name (ROS 1)
- ROS 2 Topic and Service Names discusses the design of names in ROS 2.
- Names are substantially similar between ROS 1 and ROS 2
- When creating nodes, subscribers, clients, actions, and services, each of these entities is provided a name as one of the arguments.
- The name is used by ROS Client Libraries to register objects with the appropriate entity in the ROS graph (e.g., publisher/subscriber with a topic).
- Names provided in code are best thought of as placeholder default values: they can be changed by users when the node runs.
- Every ROS 2 node exists within a =/namespace/
- Namespaces are like directories on Linux: they start from the global namespace (called
/) and can be nested (e.g.,/ns1/ns2/ns3).
- Namespaces are like directories on Linux: they start from the global namespace (called
Names as Paths
Just like paths on Linux, ROS names can be relative or absolute
- An absolute name is any name that starts with a
/- No two entities in the ROS graph can share the same absolute name
- A relative name is any name that does not start with a
/. - Relative names are interpreted relative the namespace of the node
- In some contexts (such as starting a node with
ros2 runan absolute name must be used.
- In some contexts (such as starting a node with
- The base name is the name of the entity (e.g., node, topic, etc.) without any preceding namespaces.
- In a sense this is equivalent to the name of a
fileon a Linux system
- In a sense this is equivalent to the name of a
- A
~makes a nameprivate.- The
~gets expanded to the full absolute name of the node (including the base name). - Private is enforced by convention and just means that the node namespace and name are prepended.
- The use of this symbol is inspired by its use for the home directory on Linux
- The
Example
Suppose we have a node called /this/is/mynode
- The base name of the node is
mynode. - The namespace of the node is
/this/is. - The
isnamespace is nested under the/thisnamespace
Suppose /this/is/mynode creates a publisher on the hello topic and a subscriber on the /foo/data topic and a service server called ~/help:
- The node (by default) will publish to
/this/is/hello. - The node (by default) will subscribe to
/foo/data. - The node will offer a service (by default) on
/this/is/mynode/help.
Comparison with ROS 1
- In ROS 1, parameters were global and followed the same naming conventions as other entities.
- In ROS 2, parameters are always associated with a specific node and within that node can optionally have their own parameter namespace.
Manipulating Names
Name Mapping
- Resources can be remapped to any name that you want using Node Arguments
- Remapping is like an
mvoperation in the Linux filesystem - The ability to remap enables multiple copies of a node to run in parallel but use different topics/services etc.
- Remapping is like an
- Any name referred to in the node (including absolute and private names) can be remapped when starting the node.
- To remap a specific name use
ros2 run package node --ros-args -r old_name:=new_name- Multiple
-rarguments can be passed after--ros-argsto rename multiple entities
- Multiple
- The node can be renamed using
-r __node:=new_node_namein the above - The namespace can be changed using
-r __ns:=/new_namespace.- The
namespacemust start with a/
- The
- To remap a specific name use
- When the node's namespace is changed, all entities (e.g., topics) that used relative names will be moved relative to the provided namespace, but entities that used absolute names will not move.
- This behavior makes it easy to remap groups of related names.
- Namespaces also allow running multiple groups of nodes simultaneously with the same topics
- In practice, ROS nodes should be written with
remappingin mind:- Generally, use simple, relative base names for ROS nodes, the services they offer, and the topics they publish and subscribe to (so do not include the leading
/).- Following this convention allows your node to easily be run under a different namespace without needing to remap each topic individually
- Using absolute node names makes your node less flexible and harder for others to use.
- It is common for users of node to remap topics and place nodes in namespaces
- Generally, use simple, relative base names for ROS nodes, the services they offer, and the topics they publish and subscribe to (so do not include the leading
Example
The turtlesim_node supports having multiple turtles. Each turtle's topics are mapped to it's own namespace.
For example there is /turtle1/cmd_vel and /turtle2/cmd_vel to control the velocity and /turtle1/pose and /turtle2/pose to get the turtle's pose.
Imagine we make a node called control that we use to control the turtles. There are a few options
- Absolute Paths (Generally Don't Use These)
Assume
controlsubscribes to/poseand publishes to/cmd_vel. To use two copies of this node we need to- Explicitly name the node two different names (e.g.,
control1andcontrol2) - Explicitly remap both topics on both nodes so they control the correct turtle
- For complicated nodes this quickly becomes annoying and error-prone
- Explicitly name the node two different names (e.g.,
- Relative Paths and Namespaces (Use These)
- The node subcribes to
poseand publishes tocmd_vel(both relative names) - We launch one
controlnode in the/turtle1namespace and one control node in the/turtle2namespace - Everything connects properly: no renaming of nodes, no remapping.
- The node subcribes to
A Node's ROS API
- The publishers, subscribers, services, parameters, and actions a node declares comprise it's ROS API
- They determine how other nodes interact with your node, on a ROS level
- These ROS inter-process communication mechanisms are how you link nodes together, much like how modules, functions, and classes are used from within python.
- These items should be documented in a docstring at the top of the file that defines your node.