Theme
Stage 3 · lesson 4 of 7 · 40 min

Topics, services and actions

You will: Choose the right ROS 2 communication primitive, and inspect each on a live robot.

Builds on Drive it yourself.

You know message buses, RPC and job queues. ROS 2 gives you three primitives that map onto them closely, and choosing the wrong one is the most common design mistake:

primitive like use for cannot
topic pub/sub continuous state, many listeners report failure
service synchronous RPC a short request needing an answer be cancelled
action a job with progress and cancel anything that takes seconds be cheap

This robot's whole public interface:

name primitive why
/joint_states topic continuous, many subscribers, losing one sample is harmless
/controller_manager/switch_controller service short, needs success or failure
/arm_controller/follow_joint_trajectory action takes seconds, needs feedback, must be cancellable
/gripper_controller/gripper_cmd action same
/arm_controller/joint_trajectory topic fire-and-forget targets, the latest wins: what the teleop uses

Motion is the only thing here that takes time and can be interrupted, so it is the only thing that is an action. That is the whole heuristic. The trajectory topic is the exception that proves it: the teleop wants to fire a target and move on, not wait for a result.

Every one of these came free from ros2_control. None was designed by hand, and there is no private API anywhere.

Two gotchas are worth an hour each. Types must match exactly: a publisher and a subscriber on the same topic name with different message types never connect, and nothing logs an error. A mobile base on this project's first robot once sat motionless because a config parameter meant to choose the right type did not exist on this ROS version — it was accepted and silently ignored. And ros2 topic pub stamps messages with time zero, which TF treats as ancient, so a transform you published by hand appears not to exist.

In the simulation

Two ways to command the same arm. A named pose behaves like an action goal: one target, seconds of motion, a clear end. Game mode (G) is a stream: velocities while a key is held, latest wins, and it brakes when the stream stops. The ,/. jogs are short position targets, what the teleop publishes on the trajectory topic.

Kinematic: joints move at their real speed limit, but there is no gravity, no load and no physics. That is what the steps below are for.

The SO-101 loads as you reach it.

In this lesson

  1. Open Inspect, then /joint_states, and press pick. Watch the velocities rise, fall, and end at zero.
  2. Press G for Game mode and hold the up arrow to push the gripper out past x = 0.30 m. Let go, and it stops.
  3. Press G again to leave Game mode, press 5 for wrist_roll, and press . until it is past 0.3 rad. Each press is one target, and the arm holds between presses.

Do it

  1. With make play running, open a shell and list what is running.

    make shell
    ros2 node list
    ros2 topic list -t
    
  2. Read one joint state message, then measure the rate.

    ros2 topic echo /joint_states --once
    ros2 topic hz /joint_states
    

    Expect: Names, positions and velocities for shoulder_pan through wrist_roll and the gripper; then a rate well below the configured 50 Hz, roughly 50 times the real-time factor, because the controllers run on simulated time. Ctrl-C to stop.

  3. List the long-running interfaces.

    ros2 action list -t
    

    Expect: The arm's trajectory action and the gripper's command action.

You will know it worked when: For each of the robot's public interfaces, you can name its primitive and say why it is that one.

When it goes wrong

ros2 topic list shows almost nothing while the simulation is clearly running.

Why: Stale shared-memory files left by a process that was killed hard. The middleware's shared-memory transport finds them and gives up.

Fix: make stop removes them. The container also runs the middleware over UDP only, which avoids the problem at the cost of a little speed.

A transform you published with ros2 topic pub never shows up in tf2_echo.

Why: ros2 topic pub stamps messages with time zero, and TF treats them as ancient.

Fix: Publish from a small node that stamps with the current clock.

pkill -f joint_state_publisher kills your own shell.

Why: The pattern matches the pkill command line itself.

Fix: Bracket one character: pkill -f 'joint_state_pub[l]isher'.

Terms

Topic
A named stream of messages, published and subscribed — pub/sub. No reply, no failure reporting.
Service
A request with a single response — synchronous RPC. Blocks the caller, and cannot be cancelled.
Action
A goal with feedback, a result and cancellation — for anything that takes time, like motion.
Message type
The schema of a topic's messages. Publisher and subscriber must agree exactly, or they silently never connect.

Every term in the course: glossary.

Check yourself

Answer in your head first, then open it.

Why is moving the arm an action and not a service?

A motion takes seconds. A service would block the caller for all of it, could not report progress, and could not be cancelled if something got in the way.

Why is /joint_states a topic and not a service the dashboard could call?

It is continuous state with many consumers. Each would otherwise poll; with a topic they all receive the same stream, and missing a sample costs nothing.

A subscriber gets nothing and no error is logged anywhere. What is your first command?

ros2 topic info <topic> — compare the publisher and subscriber types and counts.

Stretch

You need a new capability: "measure the distance from the gripper to the target". Should it be a topic, a service or an action? Argue for your choice, then argue for one of the other two. Which argument is stronger, and what would change your mind?

Going deeper