Skip to content
gunkslab

Primers

ROS 2

ROS 2, the Robot Operating System, isn’t an operating system. It’s a set of libraries and tools for writing robot software as many small programs that talk to each other, and a shared vocabulary for what they say: a speed command, a distance reading, a map. It runs on Linux computers, and on macOS and Windows with more work. Many research labs and robotics companies build on it, so a robot that speaks ROS 2 can use tools and code that others have already written.

Pick your level at the top of the page.

High school

A team of small programs

A robot’s software has a lot of jobs: read the distance sensor, work out where the robot is, plan a route, turn the motors. ROS 2 splits them into small programs called nodes, one job each, like a kitchen where one cook does the sauces and another the grill. Each node can be written, tested and swapped on its own.

Topics: channels everyone can tune into

Nodes talk by publishing messages on topics, named channels like /scan or /cmd_vel. Any node that wants them subscribes. It’s the same idea as MQTT, with one difference: there’s no broker in the middle. Nodes find each other on the network by themselves, and send straight to each other.

Standard forms

Every topic carries one kind of message, with a fixed layout, like a form with set boxes. ROS 2 comes with forms everyone uses: a speed command, a distance reading, a battery’s state, a position. Because everyone fills in the same form, a tool someone wrote for their robot works on yours.

The speed command is called Twist. It says how fast to go forward and how fast to turn, and nothing about which wheels or motors. Each robot works out its own wheels from that.

Asking, and asking for a job

Not everything is a stream of messages:

  • A service is a question with one answer. “What’s your battery?” “7.6 volts.”
  • An action is a job that takes a while, like ordering food for delivery: you get updates on the way (“halfway there”), a result at the end, and you can cancel it. “Drive to the door” is an action.

Left of what?

“The obstacle is 20 cm to the left” means nothing until you know left of what. Left of the sensor? The robot’s middle? The room? Every part of a robot has its own frame, and ROS 2 keeps track of how they all sit relative to each other. So a reading from a sensor on the robot’s nose can be put in the right place on a map of the room.

Check yourself

  1. Why split a robot’s software into many small programs instead of one big one?
  2. What’s the big difference between how ROS 2 nodes and MQTT programs find each other?
  3. “Drive to the door” is an action, not a service. Why?

<details><summary>Answers</summary>

  1. Each one is simpler to write and test, one can crash without taking everything with it, and you can swap one, say a better route planner, without touching the rest.
  2. MQTT programs all connect to one broker. ROS 2 nodes find each other themselves, with no broker.
  3. It takes time, you want to hear how it’s going, and you might need to cancel it. A service just answers once.

</details>

College

The graph

A running ROS 2 system is a graph: nodes, and the topics, services and actions between them. ros2 node list, ros2 topic list and rqt_graph show it. Nodes are written with a client library, rclpy for Python or rclcpp for C++, and each runs callbacks: one fires when a message arrives, another on a timer.

Topics, types and QoS

Each topic has one message type, defined in a .msg file and turned into Python and C++ classes when it’s built. A publisher and a subscriber match only if the topic name and the type agree.

Each publisher and subscriber also has a QoS profile, like MQTT’s QoS but with more settings:

Setting Choices Like in MQTT
Reliability reliable, or best effort QoS 1, or QoS 0
Durability volatile, or transient local (late joiners get the last messages kept) a retained message
History keep the last N, or keep all (no equivalent: MQTT retains one)

The two ends must be compatible. A subscriber asking for reliable gets nothing from a best-effort publisher, and it’s a common first bug: the topic is there, the types match, and no messages come. ros2 topic info -v shows each end’s profile. Sensor drivers often publish best effort, the “sensor data” profile.

Services, actions, parameters

  • A service is a request and a response, with a .srv type.
  • An action has a goal, feedback while it runs and a result, and can be cancelled (an .action type). Nav2’s “navigate to a pose” is one.
  • Parameters are a node’s typed settings, read and set from outside while it runs: ros2 param set /node max_speed 0.3.

Frames and tf2

tf2 keeps a tree of coordinate frames and how each sits in its parent, timestamped. A mobile robot’s usual chain (REP 105) is:

map → odom → base_link → each sensor’s frame

odom is where odometry says the robot is: smooth, but drifting (see encoders and odometry). map is corrected by landmarks: it can jump, but doesn’t drift. Ask tf2 for “this sensor’s reading, in map, at the time it was taken”, and it chains the transforms.

ROS’s units (REP 103) are SI: metres, radians, seconds. x is forward, y is left, z is up.

From Twist to wheels

geometry_msgs/msg/Twist asks for a forward speed v (linear.x, m/s) and a turn rate ω (angular.z, rad/s). For a differential drive with track width b, each side’s speed is:

left = v − ωb/2, right = v + ωb/2

These are speeds. A robot whose motor drivers take power, not speed, has to hold each side’s speed itself, with encoders and a speed controller, before it can obey a Twist.

Check yourself

  1. A node subscribes to /scan with reliable QoS. ros2 topic list shows /scan, but nothing arrives. What would you check first?
  2. Why does a mobile robot have both odom and map frames?
  3. With a 10 cm track, what are the side speeds for v = 0.2 m/s and ω = 1 rad/s?
  4. Which MQTT feature is transient-local durability most like?

<details><summary>Answers</summary>

  1. The publisher’s QoS: a best-effort publisher and a reliable subscriber don’t match. ros2 topic info -v /scan.
  2. odom is smooth for control but drifts. map is right in the long run but can jump when a landmark corrects it. Controllers want the first, planners the second.
  3. Left 0.2 − 0.05 = 0.15 m/s, right 0.25 m/s.
  4. A retained message: a late subscriber gets the last value at once.

</details>

University

The middleware layer

ROS 2 doesn’t define its own transport. Its client libraries sit on rcl, which talks to a middleware through the RMW interface. The usual middleware is DDS (the Object Management Group’s Data Distribution Service): Fast DDS by default in recent releases, with Cyclone DDS and others selectable through RMW_IMPLEMENTATION. Zenoh is a newer alternative. Messages are serialised as CDR. Each .msg file is compiled by rosidl into language types and the type support each middleware needs.

Discovery

DDS’s simple discovery finds participants by multicast UDP: every process announces itself and what it publishes and subscribes to, and the endpoints that match connect peer to peer. There’s no broker to fail, but discovery traffic grows with the number of endpoints, and multicast often doesn’t cross Wi-Fi or a busy network well. Fixes include a discovery server, peers listed by hand, or a different middleware. ROS_DOMAIN_ID splits a network into separate ROS worlds, so two groups on one LAN don’t see each other. On a classroom network that matters.

Compare MQTT: a broker is a single point of failure and an extra hop, but discovery is trivial, it crosses networks, and it’s one place to put access control. Microcontrollers sit in between: micro-ROS on a board such as an ESP32 talks to an agent on a computer, which speaks DDS for it.

QoS as a contract

DDS QoS is request versus offered: each policy has an order, and a match needs the publisher to offer at least what the subscriber requests. Reliable > best effort; transient local > volatile. Beyond reliability, durability and history:

  • Deadline: the longest gap expected between messages. A missed deadline raises an event at both ends.
  • Lifespan: a message older than this isn’t delivered.
  • Liveliness: how a publisher shows it’s alive, and how long before it’s declared not. This is DDS’s closest thing to MQTT’s last will.

Time and transforms

tf2 keeps a buffer of timestamped transforms and interpolates between them. To put a sensor reading in map, you ask for the transform at the reading’s own timestamp, not now: at 0.3 m/s, 100 ms of latency is 3 cm of error. That needs the clocks to agree. A message stamped when it arrives, rather than when it was measured, carries the network’s delay as error.

The map → odom transform is where localisation’s correction lives: odometry publishes odom → base_link smoothly at a high rate, and a localiser (a SLAM system, or an overhead camera) publishes map → odom, the accumulated drift.

Execution

Callbacks run in executors: single-threaded by default, so a slow callback delays every other in that node. Callback groups and multi-threaded executors allow concurrency. Managed (lifecycle) nodes move through unconfigured, inactive, active and finalised states, so a launch can bring a system up in order. Nav2 uses them. Nodes in one process can pass messages without copying.

Check yourself

  1. Two classes on one Wi-Fi network run ROS 2 desks and see each other’s robots. Give two fixes.
  2. A subscriber requests transient local; the publisher offers volatile. What happens, and why is that the right rule?
  3. A program stamps an overhead camera’s poses when they reach it, about 80 ms after the frame was taken. A robot drives at 0.25 m/s. How far off is its plotted position, and how would you fix it?
  4. A robot must stop if its speed commands stop for 500 ms. Is that closer to the deadline policy or to liveliness, and why?

<details><summary>Answers</summary>

  1. Different ROS_DOMAIN_IDs, or limit discovery (a discovery server, or peers listed by hand) so each desk only finds its own.
  2. They don’t match, and the subscriber gets nothing. The subscriber asked to be sent history on joining, and a volatile publisher can’t promise it: failing to match is better than silently not meeting it.
  3. About 0.25 × 0.08 = 2 cm behind. Stamp each pose with the frame’s capture time, and sync the camera’s clock with the receiver’s.
  4. Deadline: it’s about the gap between messages on one topic, and it acts when that gap runs past 500 ms. Liveliness is about whether the publisher is alive at all, which a publisher that’s alive but has stopped sending would pass. Either way, the stop itself belongs on the robot, so it happens even when the network is gone.

</details>

The tools around it

  • RViz shows what a robot sees and thinks in 3D: its position, its sensor readings, its map, its planned path.
  • Gazebo simulates robots and their world.
  • Nav2 navigates: give it a map and a goal, and it plans a route around obstacles and drives it.
  • slam_toolbox builds a map from a robot’s movement and its distance sensor while the robot drives (SLAM: simultaneous localisation and mapping).
  • micro-ROS is a small version for microcontrollers such as the ESP32, for when a board should speak ROS 2 itself.

Releases

A new release, a distribution, comes out each May with an alphabetical name. The ones from even-numbered years are supported for five years. Jazzy Jalisco (2024) is supported until May 2029, and Lyrical Luth (2026) until May 2031.

ROS 2 is open source. Most of it is under the Apache 2.0 licence.

Try it

You need only a container runtime. Start a shell with ROS 2 Jazzy in it:

container

container run --rm -it docker.io/library/ros:jazzy

docker

docker run --rm -it ros:jazzy

podman

podman run --rm -it docker.io/library/ros:jazzy

The first time, it downloads the image, which takes a few minutes. Then you’re at a prompt inside it. Publish a message twice a second in the background, and listen to it:

ros2 topic pub --rate 2 /desk/greeting std_msgs/msg/String "{data: hello from the desk}" > /dev/null &
ros2 topic echo /desk/greeting

You’ll see the message arrive twice a second:

data: hello from the desk
---
data: hello from the desk
---

Press Ctrl-C to stop listening. Now ask what’s there:

ros2 topic list
ros2 topic info /desk/greeting

It prints the topics, and that /desk/greeting carries std_msgs/msg/String with one publisher:

/desk/greeting
/parameter_events
/rosout
Type: std_msgs/msg/String
Publisher count: 1
Subscription count: 0

Look at the layout of a speed command, the message a planner sends to drive a robot:

ros2 interface show geometry_msgs/msg/Twist

It prints two sets of x, y and z: linear, the speed along each axis, and angular, the turn rate about each. A car on a desk uses linear.x, forward, and angular.z, turning.

Type exit to leave. The container goes with it.

Learn more