ROS 2 Nodes
A node is a participant in the ROS 2 graph, which uses a client library to communicate with other nodes. Nodes can communicate with other nodes within the same process, in a different process, or on a different machine. Nodes are typically the unit of computation in a ROS graph; each node should do one logical thing.
Nodes can publish to named topics to deliver data to other nodes, or subscribe to named topics to get data from other nodes. They can also act as a service client to have another node perform a computation on their behalf, or as a service server to provide functionality to other nodes. For long-running computations, a node can act as an action client to perform it, or as an action server to have another node perform it. Nodes can provide configurable parameters to change behavior during run-time.
Connections between nodes are established through a distributed discovery process.
Each node in ROS should be responsible for a single, modular purpose, e.g. controlling the wheel motors or publishing the sensor data from a laser range-finder. Each node can send and receive data from other nodes via topics, services, actions, or parameters.
A full robotic system is comprised of many nodes working in concert. In ROS 2, a single executable (C++ program, Python program, etc.) can contain one or more nodes.
ROS 2 Node Commands:
ros2 run
The command ros2 run launches an executable from a package.
ros2 run <package_name> <executable_name>
To run turtlesim, open a new terminal, and enter the following command:
ros2 run turtlesim turtlesim_node
The turtlesim window will open, where the package name is turtlesim and the executable name is turtlesim_node.
We still don’t know the node name, however. You can find node names by using ros2 node list
ros2 node list
ros2 node list will show you the names of all running nodes. This is especially useful when you want to interact with a node, or when you have a system running many nodes and need to keep track of them.
Open a new terminal while turtlesim is still running in the other one, and enter the following command:
ros2 node list
The terminal will return the node name:
/turtlesim
Open another new terminal and start the teleop node with the command:
ros2 run turtlesim turtle_teleop_key
Here, we are referring to the turtlesim package again, but this time we target the executable named turtle_teleop_key.
Return to the terminal where you ran ros2 node list and run it again. You will now see the names of two active nodes:
/turtlesim
/teleop_turtle
ros2 node info
Now that you know the names of your nodes, you can access more information about them with:
ros2 node info <node_name>
To examine your latest node, my_turtle, run the following command:
ros2 node info /my_turtle
ros2 node info returns a list of subscribers, publishers, services, and actions. i.e. the ROS graph connections that interact with that node. The output should look like this:
/my_turtle
Subscribers:
/parameter_events: rcl_interfaces/msg/ParameterEvent
/turtle1/cmd_vel: geometry_msgs/msg/Twist
Publishers:
/parameter_events: rcl_interfaces/msg/ParameterEvent
/rosout: rcl_interfaces/msg/Log
/turtle1/color_sensor: turtlesim/msg/Color
/turtle1/pose: turtlesim/msg/Pose
Service Servers:
/clear: std_srvs/srv/Empty
/kill: turtlesim/srv/Kill
/my_turtle/describe_parameters: rcl_interfaces/srv/DescribeParameters
/my_turtle/get_parameter_types: rcl_interfaces/srv/GetParameterTypes
/my_turtle/get_parameters: rcl_interfaces/srv/GetParameters
/my_turtle/list_parameters: rcl_interfaces/srv/ListParameters
/my_turtle/set_parameters: rcl_interfaces/srv/SetParameters
/my_turtle/set_parameters_atomically: rcl_interfaces/srv/SetParametersAtomically
/reset: std_srvs/srv/Empty
/spawn: turtlesim/srv/Spawn
/turtle1/set_pen: turtlesim/srv/SetPen
/turtle1/teleport_absolute: turtlesim/srv/TeleportAbsolute
/turtle1/teleport_relative: turtlesim/srv/TeleportRelative
Service Clients:
Action Servers:
/turtle1/rotate_absolute: turtlesim/action/RotateAbsolute
Action Clients:
Now try running the same command on the /teleop_turtle node, and see how its connections differ from my_turtle.