ROS2 - Humble - Basics with Node, Topic and Service in Python

We are going to walk through the essential building blocks of ROS2.

Rather than building a complex industrial system, we are using a battery simulator as a clear, practical example to master the three fundamental elements of robotic communication: Nodes, Topics, and Services.

Let's get started.

Shopping list

The Three Pillars of ROS 2

Before looking at the code, we must understand these three components, as they are the DNA of any ROS2 application:

  • The Node: A Node is an individual process that performs a specific task.
    In our lab, we have one node for the battery, one for monitoring, and one for charging. This "modular" approach means that if one node fails, the rest of the robot stays functional.

  • The Topic: Topics are used for continuous, one-way data streaming.
    It follows a Publish/Subscribe model.
    Imagine a radio station: the Battery node "broadcasts" its level, and any node that "tunes in" (Subscribes) can receive that data in real-time.

  • The Service: Services are for occasional, two-way Request/Response interactions.
    It’s like a phone call: a "Client" node calls a "Server" node, asks it to perform an action (like "Reset"), and waits for a confirmation that the task is complete.

The files

Our project is divided into four files to demonstrate a clean architecture.

node_battery.py

This is our data provider and simulates the physical battery hardware.

  • Topic (Publisher): It constantly sends out the battery percentage.
  • Service (Server): It hosts the "Reset" command, waiting to be told to refill.

node_monitor.py

This node shows how to interpret data and make safety decisions.

  • Topic (Subscriber): It listens to the battery broadcast.
  • Logic: It watches the numbers and if the battery hits 0%, it triggers a safety message.

node_charger.py

This is a "one-shot" node created to perform a specific task and then disappear.

  • Service (Client): It sends a single request to the Battery node to reset the level and shuts down once it receives the "Success" response.

utils.py

This isn't a node, but a shared library.

It centralizes the initialization and shutdown logic, ensuring all our nodes follow the same standards.

How to use it

First thing, once your project is cloned from Github and reopened as a container (with the DevContainer extension), go to the following directory:

  • /workspaces/ros2_ws

and type the following command:

colcon build

It will compile your project into a package and create the necessary folders: 

  • build
  • install
  • log

Then, if everything went well, open a new terminal for the Battery node and type:

ros2 run pkg_python node_battery

It will generate a quick message for the battery level from 100 % to 0 %, then slow down to every 10 seconds (to protect the battery).

If it's still OK, open a third terminal and type this for Monitor node:

ros2 run pkg_python node_monitor

It will wait the end of the 10 seconds, from your node battery terminal, to display a message of your battery level.

Finally open a fourth terminal for the Charger node and type this:

 ros2 run pkg_python node_charger

It will send a message to the Battery node to simulate the full charge being available again.

The Battery and Monitors terminal nodes should then restart from beginning and display the battery level from 100 to 0 %.

Conclusion​

By completing this tutorial, you aren't just running scripts, you are managing a distributed system.

You now have the skills to make different programs work together to control a robot behavior.

Good job, you did it laugh

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.