Getting Started with ROS2: Understanding Nodes

Part 6 of our “Getting Started with ROS2” Series

Sagar Kumar
Spinor
6 min readJul 16, 2024

--

Welcome, fellow readers, to our “Getting Started with ROS2” series! In this series, we aim to provide you with a comprehensive introduction to ROS 2 and will guide you through the fundamentals, its key concepts, and practical applications. If you have never used the Robot Operating System(ROS) before, even ROS 1, or if you want a practical and quick refreshment of the basics then this series is for you. Feel free to explore the links provided for a more in-depth understanding of the concepts discussed.

Prerequisites

Before we start, you must have a basic understanding of ROS2 concepts. Please refer to the following articles in our series:

  1. Getting Started with ROS2: An Introduction — Provides a foundational overview of ROS2.
  2. Getting Started with ROS2: Why ROS2? — Explains the advantages and motivations for using ROS2.
  3. Getting Started with ROS2: Install and Setup ROS2 Humble on Ubuntu 22.04(LTS) — Guides you through installing and setting up ROS2 on Ubuntu.
  4. Getting Started with ROS2: Overview of ROS2 Workspaces, Packages and NodesGives an overview of the essential components of ROS2.
  5. Getting Started with ROS2: Create and Set Up a WorkspaceExplains how to create and configure a ROS2 workspace for your projects.

Having a solid grasp of these basics will help you follow along and understand the steps involved in creating and managing workspaces in ROS2.

Introduction to ROS2 Nodes

Nodes are the fundamental building blocks of a ROS2 system. A node is essentially a single process that performs computation. Each node is designed to perform a specific task within a robotic system, such as controlling a motor, processing sensor data, or communicating with other nodes. This modular approach allows for the development of complex robotic systems by combining multiple small, reusable, and independent nodes.

Nodes communicate with each other using a publish-subscribe model, services, and actions, enabling them to share information and request tasks from one another. By breaking down a system into individual nodes, developers can create more manageable, maintainable, and scalable robotics applications.

To better understand how nodes work in ROS2, let’s look at an example using the pre-existing packages demo_nodes_cpp and demo_nodes_py. These packages contain example nodes that can help illustrate the publish-subscribe communication model.

Publisher/Subscriber Example Using demo_nodes_cpp and demo_nodes_py

The demo_nodes_cpp package contains example nodes written in C++, and the demo_nodes_py package contains example nodes written in Python. We will use these packages to demonstrate a simple publisher/subscriber interaction.

In this example, the Python node (talker from demo_nodes_py) will act as the publisher, and the C++ node (listener from demo_nodes_cpp) will act as the subscriber.

  • Running the Publisher Node (talker from demo_nodes_py)
ros2 run demo_nodes_py talker

This command starts the talker node, which publishes messages to the /chatter topic. The talker node continuously sends messages such as "Hello World: [count]".

talker node
  • Running the Subscriber Node (listener from demo_nodes_cpp)
ros2 run demo_nodes_cpp listener

This command starts the listener node, which subscribes to the /chatter topic and prints the received messages.

listener node

Using rqt_graph to Visualize Running Nodes

To better understand the relationships between nodes and topics in your ROS2 system, you can use the rqt_graph tool. rqt_graph provides a graphical interface that displays the nodes and the topics they are publishing to or subscribing from.

  • Installing rqt_graph: First, ensure that rqt_graph is installed. If it's not already installed, you can install it using the following command:
sudo apt install ros-humble-rqt-graph
  • Running rqt_graph: Once rqt_graph is installed, type the following command in a separate terminal to visualize your running nodes:
rqt_graph
  • Viewing the Graph: The rqt_graph window will open, displaying a graph of your ROS2 system. You will see nodes represented as ellipses and topics as arrows connecting these nodes.
    For the example with talker and listener, you should see:
    - A node named /talker (the publisher).
    - A node named /listener (the subscriber).
    - An arrow representing the /chatter topic connecting the talker node to the listener node.
rqt_graph visualizing the nodes and the topic

This visualization helps you understand the structure and communication flow of your ROS2 system, making it easier to debug and optimize your setup. Using rqt_graph is an excellent way to ensure your nodes are interacting as expected and to diagnose any issues in your ROS2 network.

Multi-Node Communication with Turtlesim Package

Turtlesim is a beginner-friendly simulation package within the Robot Operating System (ROS) ecosystem, designed to introduce users to key concepts of robotic control and simulation. It features a simple graphical interface where users can interact with a turtle that moves within a virtual environment. Turtlesim allows users to control the turtle’s movements using commands published over ROS topics, making it an excellent tool for learning about ROS’s publish-subscribe communication model. The package includes nodes for simulating the turtle’s behavior, such as `turtlesim_node`, and utilities like `turtle_teleop_key` for keyboard control. It’s widely used for educational purposes, providing a hands-on approach to understanding robotic concepts such as teleoperation, node interaction, and graphical visualization within ROS environments.

To demonstrate a multi-node communication in a ROS2 system using the turtlesim package, we will run several nodes, including turtlesim_node, turtle_teleop_key, and use rqt_graph to visualize their interactions with other nodes in the background.

  • Install the Turtlesim Package: If you don’t already have the turtlesim package installed, you can install it using the following command:
sudo apt install ros-humble-turtlesim
  • Start the Turtlesim Node: Open a terminal and run the following command to start the turtlesim_node, which launches the turtlesim simulator:
ros2 run turtlesim turtlesim_node

You should see a window open with a turtle in the center.

  • Control the Turtle with the Keyboard: Open another terminal and run the following command to start the turtle_teleop_key node. This node allows you to control the turtle using the keyboard.
ros2 run turtlesim turtle_teleop_key

You can now use the arrow keys on your keyboard to move the turtle around in the turtlesim window.

  • Visualize with rqt_graph: Open a third terminal and run the following command to start rqt_graph:
rqt_graph

The rqt_graph window will open, displaying a graph of the running nodes and their connections.

Video Demo: For a visual walkthrough of the demo described above, I’ve recorded a video capturing the screen while running the turtlesim package with turtle_teleop_key and rqt_graph. Watch the video below to see how nodes interact in a ROS2 environment.

Understanding the Turtlesim Demo

  • turtlesim_node: This node provides the simulation environment. It subscribes to the /turtle1/cmd_vel topic to receive velocity commands and updates the turtle's position accordingly.
  • turtle_teleop_key: This node reads keyboard input and publishes velocity commands to the /turtle1/cmd_vel topic. When you press the arrow keys, this node sends the corresponding velocity commands to the turtlesim_node.
  • Other Nodes: In addition to turtlesim_node and turtle_teleop_key, there are other nodes running in the background that facilitate various functionalities such as rendering the GUI, handling internal communications, and managing other aspects of the ROS2 system.

For more details follow this link.

What’s Next?

In the upcoming article, you will learn how to create custom packages and understand how to structure them effectively to encapsulate functionality. You’ll also learn how to write custom nodes that communicate seamlessly within the ROS2 framework, enabling you to expand the capabilities of your robotic applications.

If you’ve made it this far, it means you’re not just interested — you’re committed, and we’re delighted to have you here!

Your interest and engagement inspire us to create more content and share knowledge with fellow learners, the community of robotics, and tech enthusiasts who share our passion.

So, thank you for being a part of this journey with us. Stay tuned for more insightful articles, tutorials, and practical examples as we continue our exploration of ROS 2 together.

References

--

--

Sagar Kumar
Spinor

Sagar is a computer vision and robotics expert with a focus on Perception & Localization | Twitter: twitter.com/sagarcadet | Linkedin: linkedin.com/in/sagark30