ROS 2
Introduction to ROS 2
By Juan Felipe Martínez Bedoya
This tutorial covers the basics of ROS 2 and draws on The Construct’s ROS2 Basics with Python course (The Construct).
ROS 2 is the current version of ROS (Robot Operating System) and was created to overcome the limitations of its predecessor. Its advantages include real-time communication, built-in security, communication over DDS (a standard for distributed data, very useful in systems with many robots) and more industrial applications.
If you are not comfortable with the terminal yet, start with the Linux commands.
Running a program
Setting up the environment
Before using ROS 2 in a terminal you must load its environment. Otherwise the system will not recognise the ros2 command:
source /opt/ros/humble/setup.bash
Replace humble with the name of your ROS 2 distribution (for example, jazzy). To avoid repeating this in every terminal, add that line to the end of ~/.bashrc.
Running programs
In ROS 2 everything runs through the ros2 command. There are two ways to start a program:
- Run an executable from a package directly.
- Use a launch file, which can start several programs at once with their configuration.
Executables:
ros2 run <packageName> <executable>
Launch files:
ros2 launch <packageName> <launchFile>
ROS 2 structure
Workspace
A workspace is a folder that holds packages and everything needed to run them. It has this structure:
src/: the source code of all packages.build/: intermediate build files.install/: what gets installed when building; programs run from here.log/: build logs.
Packages
A package is a folder with everything a ROS 2 program needs to work:
- Programs:
.cppfor C++ or.pyfor Python. - Configuration and build files.
- Launch files.
- Parameter files.
Building a workspace
From the root of the workspace:
colcon build
To build a single package:
colcon build --packages-select <packageName>
After every build, load the workspace environment:
source ~/ros2_ws/install/setup.bash
Nodes
Nodes handle the different parts of a robot: one controls the wheels, another reads a laser, another the ultrasonic sensor, and so on. A robotic system is made of several nodes working together, and in ROS 2 one executable can contain one or more nodes.

Nodes communicate in three ways:
- Topics: a publisher node sends messages to a topic (for example,
/cmd_vel) and any subscriber node receives them. It is continuous, one-to-many communication. - Services: a client sends a request to a server and waits for the response. While waiting, it cannot do anything else.
- Actions: similar to services, but for long tasks. The client can keep working while the action runs, receives feedback during execution and can cancel it.
To list active nodes:
ros2 node list
To see a node’s information (use the name as it appears in the list):
ros2 node info /<nodeName>
Interfaces
Interfaces define the structure of the messages nodes exchange. They are written in a standard language (IDL, interface definition language), and ROS 2 ships with many predefined ones.
ros2 interface list # list available interfaces
ros2 interface show <interfaceName> # show an interface's structure
ros2 interface proto <interfaceName> # show a sample message ready to fill in
Topics in detail
To list active topics:
ros2 topic list
To see a topic’s information:
ros2 topic info /<topic>
The output shows three items:
- Type: the topic’s interface (message type).
- Publisher count: how many publishers are connected.
- Subscription count: how many subscribers are connected.
To subscribe to a topic from the terminal and see incoming messages:
ros2 topic echo /<topic>
You can also publish messages without writing a program, using pub. Check the message structure with ros2 interface proto:
ros2 topic pub <topic> <interface> "<message>"
Here <topic> is the topic you send to, <interface> is its type and <message> is the content in YAML. For example, to move the turtlesim turtle:
ros2 topic pub /turtle1/cmd_vel geometry_msgs/msg/Twist "{linear: {x: 2.0}, angular: {z: 1.8}}"
Continue with ROS 2 with Python to create your own packages and nodes.
Translated from Spanish.