Skip to content

Record and replay

Goal: record a run of the robot, then play it back.

The idea

Robots are hard to test: the simulator takes time to start, and a real robot may be somewhere else. ROS 2 can record the messages that flow on some topics into a folder, called a bag. Later you replay them: the same messages come out again, at the same times, even with the simulator closed.

A bag is a recording of messages, not a video.

Record while the robot runs, replay later without it.

Look at it live

  • Run ros2 bag --help and read the list of commands: record, info and play are the three you need.

Your mission

This challenge has no code file. You work in the terminal.

  • Start your demo from Challenge 6, so the robot is driving.
  • In a second terminal, record the topics below. Let the robot reach an obstacle so it BONKs, then stop the recording with Ctrl+C (about 30 seconds in all).
ros2 bag record -o /tmp/bags/my_first_run /bumperbot_controller/odom /scan /cmd_vel /tf /tf_static /clock
  • Look at the recording with ros2 bag info /tmp/bags/my_first_run.
  • Stop the simulator and your nodes. Replay with ros2 bag play /tmp/bags/my_first_run. In another terminal, run ros2 topic echo /cmd_vel and watch the zero appear at the BONK.
  • Replay at half speed and at double speed, using --rate 0.5 and --rate 2.0.

Recording limits

A recording stops by itself after 2 minutes or 100 MB. Record into /tmp/bags, which is emptied when your workspace restarts.

Check your recording. In the output of ros2 bag info, divide the number of /scan messages by the duration in seconds. You should get about 5, the lidar's rate.

What you should see

ros2 bag info lists the topics you recorded, with their message counts and the duration. The replay brings your BONK moment back exactly as it happened, with the simulator closed: /cmd_vel shows the zero at the BONK, twice as slow at 0.5 and twice as fast at 2.0.

Stuck?

Hint 1

A recording is a folder. ls /tmp/bags/my_first_run shows the data file and metadata.yaml.

Hint 2

Could not find metadata means the recording was cut before it closed. Record again and stop with Ctrl+C.

Hint 3

ros2 bag record refuses to write into a folder that already exists. Use a new name, or delete the old folder.

Submit

When it works, open the Submit tab, choose 7 · Record and Replay, and submit. Paste the output of ros2 bag info in your answer.

You finished Module 1

You now know the five building blocks of ROS 2 and have used each one on Bumperbot: nodes and topics (publish and subscribe), parameters, services and launch files, and you can record and replay a run.