ros2 run and ros2 node – Start and Debug your ROS2 Nodes

In this tutorial you’ll learn more about ROS2 command line tools to start and debug your nodes: ros2 run and ros2 node.

I will try to remove some confusions you may have when you start, and give you practical tips you can use to improve your efficiency when developing with ROS2.

Let’s get to it!

The code we’ll use

Here’s a very minimal code for a ROS2 node (in Python), written in a file named “my_program.py”, inside a “ros2_tutorials_py” package.

#!/usr/bin/env python3
import rclpy
from rclpy.node import Node

class MyNode(Node):
    def __init__(self):
        super().__init__("my_node")
        self.get_logger().info("Node has been started.")

def main(args=None):
    rclpy.init(args=args)
    node = MyNode()
    rclpy.spin(node)
    rclpy.shutdown()

if __name__ == "__main__":
    main()

This will just start a node named “my_node” and print a log on the screen. That’s all we need to practice on command line tools for ROS2 nodes.

So, first install this node by adding one line into the setup.py file of your Python package:


You are learning ROS2...

As a complete beginner? Check out ROS2 For Beginners and learn ROS2 in 1 week.

As a ROS1 developer? Check out Learn ROS2 as a ROS1 Developer and Migrate Your ROS Projects.


...
    entry_points={
        'console_scripts': [
            "minimal_node = ros2_tutorials_py.my_program:main"
        ],
...

Note: if you were using Cpp code, you would use the CMakeLists.txt.

If you’re completely new to ROS2 nodes, check out how to write a minimal node: Python | Cpp.

Compile your package with colcon build, source your ROS2 workspace, and you’re all set for the following.

ros2 run – Start your Node from the terminal

You might have already used ros2 run before, but maybe not everything is clear for you.

So, ROS2 comes with a lot of useful command line tools. Among them, the run command allows you to start a node from any installed package (from your global ROS2 installation, and from your own ROS2 workspace).

To start a ROS2 program from the terminal, you will use:

ros2 + run + name of the package + name of the executable.

Between each step you can press TAB twice to see all available options.

And there is often a confusion about the executable name.

In the example we’re using, we are using 3 different names for:

  • file: my_program.py
  • node: my_node
  • executable: minimal_node

You can choose to use a different name for the file, node, and executable. You can also choose to use the same name for the 3 of them. But make sure you know the difference between them.

So, here, if you want to start the node “my_node” from the “my_program.py” file, you’ll have to use the executable name “minimal_node” with ros2 run.

$ ros2 run ros2_tutorials_py minimal_node 
[INFO] [1593588614.171412010] [my_node]: Node has been started.

The same applies for Cpp code: let’s say the name of the node is also “my_node”, the name of the file is “my_program.cpp”, the package name is “ros2_tutorials_cpp”, and the executable name is “minimal_node”. Then you’ll execute ros2 run ros2_tutorials_cpp minimal_node.

And to kill the node, simply press CTRL+C on the terminal where you executed ros2 run.

Change the name of a Node with ros2 run – at run time

You can add many arguments to the ros2 run command. Among them there is one allowing you to directly change the node’s name at run time, without having to re-write/re-compile anything.

The first thing to add is --ros-args. Use --ros-args only once, and put all arguments after it.

To change the node’s name from “my_node” to “another_node”, use -r __node:=...:

$ ros2 run ros2_tutorials_py minimal_node --ros-args -r __node:=another_node
[INFO] [1593588911.947569209] [another_node]: Node has been started.

You can see on the log line: the name of the node has been changed!

This feature will be very useful when you want to launch multiple nodes with different names. For example if you have a “single_wheel_controller” node, you can create a “right_wheel_controller” and a “left_wheel_controller” node, using the same code and executable.

And this is important: you can’t start 2 nodes with the same name, or else expect to see some weird behavior.

ros2 node list – List all Nodes in your graph

Let’s start 2 nodes, using the same executable, but different names.

$ ros2 run ros2_tutorials_py minimal_node --ros-args -r __node:=node_1
[INFO] [1593589221.422757460] [node_1]: Node has been started.

--> in another terminal

$ ros2 run ros2_tutorials_py minimal_node --ros-args -r __node:=node_2
[INFO] [1593589224.588230634] [node_2]: Node has been started.

So, you can start a node and modify its name. But now it could be nice to be able to debug what’s going on in the ROS2 graph.

ros2 node list will give you the list of all the nodes you’ve launched in the same graph/network.

$ ros2 node list
/node_1
/node_2

Great, we can see the 2 nodes we’ve just started.

ros2 node info – Get more details about a Node

Now that you know the name of nodes in your graph, you can use ros2 node info on a given node to get more details.

$ ros2 node info /node_1 
/node_1
  Subscribers:

  Publishers:
    /parameter_events: rcl_interfaces/msg/ParameterEvent
    /rosout: rcl_interfaces/msg/Log
  Service Servers:
    /node_1/describe_parameters: rcl_interfaces/srv/DescribeParameters
    /node_1/get_parameter_types: rcl_interfaces/srv/GetParameterTypes
    /node_1/get_parameters: rcl_interfaces/srv/GetParameters
    /node_1/list_parameters: rcl_interfaces/srv/ListParameters
    /node_1/set_parameters: rcl_interfaces/srv/SetParameters
    /node_1/set_parameters_atomically: rcl_interfaces/srv/SetParametersAtomically
  Service Clients:

  Action Servers:

  Action Clients:

Here you’ll see 6 different categories, each listing a different kind of ROS2 communication feature:

  • Subscribers and Publishers (Topic).
  • Service Servers and Service Clients.
  • Action Servers and Action Clients.

For each existing communication on the node, you’ll get the name of the interface (topic name, service name, or action name), and the type to use.

As you can see with this node, we already have some publishers and service servers, even if we didn’t start anything from the code we wrote. Those communications will be automatically started for every node you start:

  • One publisher to the /rosout topic. Nodes send their log to this topic, so all your logs from your application are centralized into one place, and then can be written into a file.
  • One publisher and 6 service servers to handle the parameters of the node.

Going further with ros2 run and ros2 node

At anytime, if you need help with a specific command, you can add -h to see a help message: ros2 run -h, ros2 node -h, ros2 node list -h, etc.

When developing with ROS2, you will use those 2 command line tools all the time. With this tutorial you already have a good overview of what to expect from them, and what info you can get to efficiently debug your application.

As you saw here, ros2 node info gives you the list of topics, services, and actions for one node. Well, you will also be able to debug those communication features from the terminal. Keep “ros2” first, and then add the name of the communication:

Want to learn how to program with ROS2?

Don't miss this opportunity:

ROS2 For Beginners - Step by Step Course


>> Learn ROS2 in 1 Week <<

...or are you already a ROS1 Developer?

ROS2 For ROS1 Developers and Migrate Your ROS Projects


>> Learn ROS2 as a ROS1 Developer and Migrate Your ROS Projects <<

LEARN HOW TO PROGRAM ROBOTS

Did you find this tutorial useful?

Do you want to become better at programming robots, with Arduino, Raspberry Pi, or ROS2?

If yes, subscribe to receive exclusive content and special offers!