Ubuntu Linux for Robotics · Lesson 45 of 96
Publishers and subscribers
Building a talker and listener pair and checking them with the ros2 tools.
Estimated time · 60–90 min
Learning goals
What you will understand
- Explain publishers and subscribers in your own words
- Use it in a small workstation example
- Verify the result instead of assuming it worked
Understand the idea first
The mental model
A publisher writes typed messages to a topic and a subscriber receives them. Create them in the node's constructor with create_publisher and create_subscription and keep the returned pointers as members. A wall timer usually drives publishing.
On Ubuntu, check the pair from the terminal: ros2 topic list -t to see the type, ros2 topic echo to see values, ros2 topic hz to check the rate and ros2 topic info -v to compare QoS on each end. If nothing arrives, compare QoS and ROS_DOMAIN_ID before reading the code.
Guided lesson
Work through these steps
- 1.
Open a terminal and record the current state before changing anything.
- 2.
Read the publishers and subscribers commands and predict what each one does.
- 3.
Run the commands one at a time and compare each output with your prediction.
- 4.
Write the commands that worked in a setup note you can repeat later.
Worked example
Read it before you run it
ros2 run robot_nodes talker
# Terminal A: start the publisher.
ros2 run robot_nodes listener
# Terminal B: start the subscriber.
ros2 topic list -t
# Terminal C: the topic and its message type.
ros2 topic echo /chatter --once
# Print a single message.
ros2 topic hz /chatter
# Measure how often messages arrive.
ros2 topic info /chatter -v
# Compare the QoS of publisher and subscriber.What the example is doing
- 1.Lines without sudo only inspect or change files you own; sudo authorizes a system-wide change.
- 2.Read command output before moving to the next line. Stop if names, versions, disks, or paths differ from the lesson.
- 3.Comments beginning with # explain safety checks and are not executed by the shell.
Watch it run · Messages between nodes
See what this code does
The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.
Demo for Publishers and subscribers. Expected result: average rate: 2.000
Practice code
Now type it yourself
Open a terminal, run the starter line by line (or save it as practice.sh and run bash practice.sh), and replace every TODO with your own command.
# TODO: measure the publishing rate of /chatter with ros2 topic hzExpected output
average rate: 2.000Stuck? Show the full solution (try it yourself first)
ros2 topic hz /chatterExpected result
How you know it worked
- Each command completes without an unexplained error.
- Your notes contain the command, the important output, and what it proves.
Practical exercise
Apply the lesson
Write the talker and listener, run them in two terminals, use topic hz to confirm the rate, change the timer period and observe the new rate, then start the listener with a different domain ID and note what happens.
Before you continue
- You recorded the starting state
- Every command and output is understood
- The change can be repeated from your notes
If something goes wrong
Check ownership and permissions first. Do not add sudo unless the command changes a documented system location.
Check spelling, Ubuntu version, enabled repositories, and whether the previous installation command succeeded.