Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 38 ยท ROS and Software Architecture

ROS 2 Concepts: Nodes, Topics, Services, Actions

Learn the core ideas of ROS 2: nodes, topics, services, actions, parameters, launch files, tf and QoS, with runnable Python stand-ins of the message patterns.

A real robot is many programs at once: one reads the lidar, one estimates the pose, one plans a path, one drives the wheels, one watches the battery. They must exchange data constantly, start and stop independently, and keep working if one crashes. Writing all that communication yourself would take months.

ROS 2 (Robot Operating System 2) is the middleware most of the robotics world uses for this. It is not an operating system; it is a set of libraries, tools, and conventions that give you message passing between processes, hardware drivers, and ready-made algorithms (navigation, SLAM, arm planning). This lesson covers the concepts. The next one shows the code.

ROS 2 runs on Linux (Ubuntu is the reference), and also on Windows and macOS with limitations. It cannot run in your browser, so this lesson uses read-only ROS snippets and small runnable Python stand-ins that behave like the real communication patterns. Each stand-in is labelled. Distribution names (Humble, Jazzy, ...) change every year; the concepts do not.

The computation graph

A ROS 2 system is a graph of nodes connected by named channels.

  • A node is one process doing one job (lidar_driver, planner, motor_controller). Nodes are small and reusable.
  • Nodes talk through three styles of channel: topics, services, and actions.
  • Nodes find each other automatically using DDS (Data Distribution Service), the underlying middleware. There is no central master process in ROS 2, unlike ROS 1.

Three ways nodes talk

Topic Publish and subscribe. Streams of data.
Service Request and reply. One quick answer.
Action Long goal with feedback and cancel.
1 / 2

Topics: publish and subscribe

A topic is a named stream with a message type. A node publishes to /scan (type sensor_msgs/msg/LaserScan) and any number of nodes subscribe. The publisher does not know who listens; the subscribers do not know who sends. This loose coupling is the superpower: you can swap a simulated lidar for a real one without touching the planner.

Typical topics: /cmd_vel (velocity commands), /odom (odometry), /scan (lidar), /camera/image_raw, /tf, /map.

Here is a tiny stand-in (plain Python, not ROS) that mimics a topic bus so you can see the pattern.

Python Stand-in: a topic bus with publishers and subscribers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Three things to notice: the lidar code is unaware of the safety node and the logger, the logger was added without changing anything else, and the data flows one way (topics are one-to-many streams).

Services: request and response

A service is a synchronous-style call: a client sends a request, a server computes a response. Use it for quick, one-off questions or commands: "reset the odometry," "set the LED color," "what is the map's resolution?" A service is a poor choice for anything that takes many seconds, because the caller waits.

Actions: long tasks with feedback

An action is for work that takes time and may be cancelled: "navigate to this pose," "grasp the cup." An action client sends a goal; the server sends periodic feedback (distance remaining), and finally a result. The client can cancel midway. Nav2's NavigateToPose and MoveIt's trajectory execution are actions.

Python Stand-in: an action with feedback and cancel

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Comparison

Topic Service Action
Pattern Publish/subscribe Request/response Goal, feedback, result
Direction One-way stream Two-way, once Two-way, over time
Duration Continuous Short Long, cancellable
Example /scan, /cmd_vel /reset_odom NavigateToPose
Receivers Many One server One server

Parameters, launch files and tf2

  • Parameters are named values on a node (max_speed: 0.5, robot_radius: 0.22) that you set from a YAML file or the command line and can change while running.
  • Launch files (Python, XML or YAML) start a set of nodes with their parameters and remappings, so ros2 launch my_robot bringup.launch.py brings up the whole robot.
  • tf2 keeps the tree of coordinate frames (map -> odom -> base_link -> laser) and answers "where is the laser in the map frame at time t?" Any node can ask. Frames, and their time stamps, are a major source of beginner bugs.
  • Remapping lets you rename a topic without changing code, for example /scan to /front_lidar/scan.

Quality of Service (QoS)

Real networks lose packets. ROS 2 lets each topic choose a QoS profile:

Setting Options Typical use
Reliability reliable or best_effort Commands reliable, camera frames best effort
Durability volatile or transient_local Maps use transient_local so late subscribers get the last message
History/depth keep last N Small N for sensors
Deadline, lifespan Time limits Detect stalled publishers

A publisher and subscriber must have compatible QoS or they will not connect. Sensor drivers often publish best_effort; subscribing with reliable there silently produces nothing, a very common problem.

Tools you will use daily

These are read-only command examples (they need a ROS 2 installation):

Terminal
ros2 node list              # which nodes are running
ros2 topic list             # which topics exist
ros2 topic echo /scan       # print messages as they arrive
ros2 topic hz /scan         # measure the publishing rate
ros2 service call /reset std_srvs/srv/Empty
ros2 param get /planner max_speed
ros2 bag record /scan /odom # record data to replay later
rviz2                       # 3D visualization of everything

Bags (recorded topic data) let you debug a failure at your desk by replaying exactly what the robot saw. RViz2 shows frames, maps, scans, and paths. Gazebo and other simulators supply fake sensors and physics so you can test without hardware.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A robot has to 'navigate to the kitchen', which takes a minute and may be canceled. Which ROS 2 mechanism fits?

    Actions are made for long-running goals that give feedback and can be canceled.
  2. 2. Why is publishing to a topic considered loosely coupled?

    Nodes connect by topic name and type only, so you can add or replace nodes without changing the others.
  3. 3. Your subscriber receives nothing from a camera driver that publishes best_effort. Likely cause?

    A reliable subscriber is incompatible with a best_effort publisher, so no connection is made.
  4. 4. What does tf2 provide?

    tf2 tracks the relationships between frames such as map, odom and base_link.

Summary

  • A ROS 2 system is a graph of small nodes connected without a central master.
  • Topics stream data (publish/subscribe), services answer quick requests, actions run long goals with feedback and cancel.
  • Parameters, launch files and tf2 configure, start and coordinate the system; QoS must be compatible between endpoints.
  • ros2 command-line tools, bags, RViz2 and simulators are the daily toolkit.

Next: ROS 2 in Code: Publishers, Subscribers and Services