ROS 2 Lyrical Luth · Lesson 25 of 86
ros2 bag: recording and playback
Recording topics, playing them back, storage formats and using bags in tests.
Estimated time · 45–75 min
Learning goals
What you will understand
- Explain ros2 bag: recording and playback in your own words
- Use it in a small robotics example
- Verify the result instead of assuming it worked
Understand the idea first
The mental model
A bag records messages with timestamps so you can replay the exact same data later. ros2 bag record -o name /topic1 /topic2 stores selected topics (or -a for all). ros2 bag info shows duration, message counts and topics; ros2 bag play replays them, with options such as --rate and --loop.
Bags make bugs reproducible: record a failing run once and debug it offline. When replaying, use --clock and set use_sim_time so nodes follow bag time. Record only what you need, because sensor data grows quickly.
Guided lesson
Work through these steps
- 1.
Source the ROS 2 Lyrical environment in a fresh terminal.
- 2.
Read the ros2 bag: recording and playback example and identify what each line does.
- 3.
Build only the affected package, source the workspace overlay, and run the example.
- 4.
Inspect the result from a second terminal with the ros2 CLI or RViz2 and compare it with the expected result.
Worked example
Read it before you run it
ros2 bag record -o run1 /scan /odom /tf
# Record three topics into the folder run1.
ros2 bag info run1
# Show duration, size and message counts per topic.
ros2 bag play run1 --clock
# Replay the data and publish /clock.
ros2 bag play run1 --rate 0.5 --loop
# Replay at half speed, endlessly.
ros2 param set /my_node use_sim_time true
# Make a node follow bag time.What the example is doing
- 1.The ros2 commands query the running graph; they do not prove that a node's internal behavior is correct.
- 2.Build from the workspace root, then source install/setup.bash so the shell can discover the new package.
- 3.Use a second sourced terminal to inspect nodes and interfaces while the example runs.
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 ros2 bag: recording and playback. Expected result: Messages: 20 (about 20 seconds of /chatter)
Practice code
Now type it yourself
Copy the starter into your ROS 2 package, replace every TODO, then build with colcon build and run it from a sourced terminal.
# Terminal A: ros2 run demo_nodes_cpp talker
# Terminal B
# TODO: record the topic /chatter into a bag folder named run1
# After Ctrl+C: ros2 bag info run1Expected output
Messages: 20 (about 20 seconds of /chatter)Stuck? Show the full solution (try it yourself first)
# Terminal A: ros2 run demo_nodes_cpp talker
# Terminal B
ros2 bag record -o run1 /chatter
# After Ctrl+C: ros2 bag info run1Expected result
How you know it worked
- ROS_DISTRO is lyrical in every terminal used for the exercise.
- The complete package from the exercise builds and the expected graph entity appears in a second terminal.
Practical exercise
Apply the lesson
Record /chatter for 20 seconds, stop the talker, and replay the bag while a listener runs. Then replay at 2× rate and compare the message timing with ros2 topic hz.
Before you continue
- ROS_DISTRO reports lyrical in every terminal you used
- The example builds and runs without errors
- You checked the result with a ros2 command, RViz2 or a test, not just by reading the code
If something goes wrong
Source /opt/ros/lyrical/setup.bash, build from the workspace root, then source install/setup.bash in that terminal.
Confirm both terminals use Lyrical, the same ROS_DOMAIN_ID, and compatible network/discovery settings.