Learning about nodes - tutorial

Nodes are core components that perform specific functions within a robotic system. This tutorial explains how nodes fit into the ROS graph and how to inspect them with command-line tools. After completing it, you will be able to run nodes, list active nodes, and view their connections.

Area: nodes, framework | Content type: tutorial | Level: beginner

Nodes are the fundamental building blocks of a robotic system, where each node is responsible for a single task. This article walks you through how to run nodes with the ros2 command-line tools and inspect how they are shown on the ROS graph. A hands-on exercise gives you practice listing active nodes and examining their connections.

Area: Nodes, Framework | Content-type: tutorial | Experience: beginner

Summary

Each node in ROS serves a single, modular purpose in a robotics system. For more information, see About nodes.

Nodes communicate with other nodes through topics, services, actions, and parameters. A single executable can contain one or more nodes. For more information, see How ROS works.

The commands used in this tutorial were:

  • To run turtlesim, use the command ros2 run turtlesim turtlesim_node.

  • To display the names of all running nodes, use the command ros2 node list.

  • To reassign the name of the /turtlesim node using remapping, use the command ros2 run turtlesim turtlesim_node --ros-args --remap __node:=my_turtle.

Prerequisites

Using turtlesim, ros2, and rqt shows you how to install the turtlesim package used here.

Note

Make sure to source ROS in every new terminal you open.

For more information, see Configuring environment.

Steps

1 Launch an executable

Nodes run inside executables. To add a node to the ROS graph, launch an executable from a package with the ros2 run command, in the following format:

$ ros2 run <package_name> <executable_name>

To run the turtlesim_node executable from the turtlesim package, open a new terminal and enter the following command:

$ ros2 run turtlesim turtlesim_node

The Turtlesim window opens, as shown in Using turtlesim, ros2, and rqt.

In this example, the package name is turtlesim and the executable name is turtlesim_node. The executable name is not always the same as the node name on the ROS graph. You can find node names by using ros2 node list.

2 List all running nodes

ros2 node list shows you the names of all running nodes. This is especially useful when you want to interact with a node, or when your system runs many nodes and you need to keep track of them.

While Turtlesim is still running in the other terminal, open a new terminal and enter the following command:

$ ros2 node list

The terminal returns the node name:

/turtlesim

To see how the list changes when another node starts, in another new terminal, start a teleoperation node:

$ 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:

$ ros2 node list

You should see the names of two running nodes:

/turtlesim
/teleop_turtle

2.1 Remap a node name

Remapping allows you to reassign default node properties, such as node name, topic names, or service names, to custom values. In Using turtlesim, ros2, and rqt, you use remapping on turtle_teleop_key to change the cmd_vel topic and target turtle2.

You can also remap the node name when you start a new node. In a new terminal, run:

$ ros2 run turtlesim turtlesim_node --ros-args --remap __node:=my_turtle

This starts a second Turtlesim node named my_turtle. It does not rename the /turtlesim node that is already running. Another Turtlesim window opens for the new node.

Return to the terminal where you ran ros2 node list and run it again:

$ ros2 node list

You will see three node names:

/my_turtle
/turtlesim
/teleop_turtle

3 Access more information about nodes

Now that you know the names of your nodes, you can see how a node connects to the rest of the system. The ros2 node info command shows a node’s publishers, subscribers, services, and actions. These are the ROS graph connections that interact with that node.

$ ros2 node info <node_name>

To examine your latest node, my_turtle, run the following command:

$ ros2 node info /my_turtle

You should see output similar to:

/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_msgs/msg/Color
    /turtle1/pose: turtlesim_msgs/msg/Pose
  Service Servers:
    /clear: std_srvs/srv/Empty
    /kill: turtlesim_msgs/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_msgs/srv/Spawn
    /turtle1/set_pen: turtlesim_msgs/srv/SetPen
    /turtle1/teleport_absolute: turtlesim_msgs/srv/TeleportAbsolute
    /turtle1/teleport_relative: turtlesim_msgs/srv/TeleportRelative
  Service Clients:

  Action Servers:
    /turtle1/rotate_absolute: turtlesim_msgs/action/RotateAbsolute
  Action Clients:

Now try running the same command on the /teleop_turtle node, and see how its connections differ from my_turtle.

Next steps

To learn how to start multiple nodes at once without opening a separate terminal for each, see Launching nodes - how-to.

FAQs

Can a single executable contain more than one node?

Yes. In ROS, a single executable, such as a C++ or Python program, can contain one or more nodes.

Why does another Turtlesim window open when I remap the node name?

Each call to ros2 run turtlesim turtlesim_node starts a new node, and Turtlesim opens a window for it. Remapping __node sets the name of that new node. It does not rename or replace the /turtlesim node that is already running.

What is the difference between ros2 node list and ros2 node info?

ros2 node list shows the names of all running nodes. ros2 node info shows the connections of a single node, including its publishers, subscribers, services, and actions.