Welcome to RDK S600 Developer Kit repository
20.ROS2 recording and playback tool
20. ROS2 Recording and Playback Tool
Note: The accompanying reference video uses the ROS Humble version, for reference only
1. Recording and Playback Tool Overview
A commonly used recording and playback tool in ROS2 - bag2. This tool is used to record topic data. We can use this command to store topic data as a file. Later, we don't need to start nodes, and can directly publish topic data from the bag file.
This tool is very useful when working with a real robot. For example, we can record a segment of topic data when a problem occurs with the robot. After recording, we can publish it multiple times for testing and experiments. We can also share topic data with others for algorithm verification, etc.
Let's try using the bag tool to record topic data and replay it.
2. Usage Tutorial
2.1. Start the Topic Node to Record
Like talker in ros2 demo:
ros2 run demo_nodes_py talker
2.2. Record
/topic-name is the topic name:
xxxxxxxxxx# Record a single topicros2 bag record /topic-name# Record multiple topicsros2 bag record topic-name1 topic-name2# Record all topicsros2 bag record -aOther options:
-o name: Custom output file name
xxxxxxxxxxros2 bag record -o file-name topic-name-s: Storage format
Currently only supports sqlite3, others are still being expanded.
2.3. View Recorded Topic Information
Before playing a video, we can view video-related information such as topic recording time, size, type, and quantity:
xxxxxxxxxx# Assuming the recorded file is rosbag2_2023_10_31-07_58_23ros2 bag info rosbag2_2023_10_31-07_58_232.4. Play and View
2.4.1. Play
Next, we can replay the data. Use the following command to play data:
xxxxxxxxxxros2 bag play rosbag2_2023_10_31-07_58_232.4.2. View
Use ros2 topic commands to view data:
xxxxxxxxxxros2 topic echo /chatter2.4.3. Playback Options
Speed playback -r
The -r option can modify playback speed. For example, -r 10 means 10x speed playback:
xxxxxxxxxxros2 bag play rosbag2_2023_10_31-07_58_23 -r 10Loop playback -l
This is for single-track loop:
xxxxxxxxxxros2 bag play rosbag2_2023_10_31-07_58_23 -lPlay a single topic:
xxxxxxxxxxros2 bag play rosbag2_2023_10_31-07_58_23 --topics /chatter
3. Examples
3.1. Run talker Node
xxxxxxxxxxros2 run demo_nodes_py talker3.2. Record
xxxxxxxxxx# Record all topicsros2 bag record -a
How to stop recording? We can directly interrupt recording using Ctrl+C in the terminal.
Then you will see a new folder in the terminal named rosbag2_2023_10_31-08_21_21.
Open the folder and you can see the contents:

This completes the recording.
3.3. Play and View
Here we play in loop:
xxxxxxxxxxros2 bag play rosbag2_2023_10_31-07_58_23 -lOpen another terminal to view the topic:
xxxxxxxxxxros2 topic echo /chatter
