Welcome to Jetson Orin NANO Developer Kit repository
20.ROS2 recording and playback tool
20. ROS 2 Recording and Playback Tool
1. Introduction to the Recording and Playback Tool
The commonly used recording and playback tool in ROS 2—bag2—is used to record topic data. We can use this command to store topic data as a file; afterwards, without starting a node, we can directly publish the topic data in the bag file.
This tool is very useful when we build a real robot. For example, we can record a segment of topic data when a problem occurs on the robot. After recording, we can publish it multiple times for testing and experimentation, and we can also share the topic data with others to verify algorithms.
Let us try using the bag tool to record topic data and replay it a second time.
2. Usage Tutorial
2.1. Start the Topic Node to Be Recorded
For example, the talker in the ros2 demo:
ros2 run demo_nodes_py talker
2.2. Recording
/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 Customize the name of the output file
xxxxxxxxxxros2 bag record -o file-name topic-name-s Storage format
ROS 2 Jazzy uses the MCAP storage format by default. You can also use -s to specify an installed storage plugin, for example sqlite3:
xxxxxxxxxxros2 bag record -s sqlite3 /topic-name2.3. Viewing the Information of the Recorded Topics
Before playing a video, we can check the relevant information of the video through the file information, such as the recording time, size, type, and number of the topics
xxxxxxxxxx# Assume the recorded file is rosbag2_2023_10_31-07_58_23ros2 bag info rosbag2_2023_10_31-07_58_232.4. Playing and Viewing
2.4.1. Playing
Next we can replay the data. Use the following command to play the data
xxxxxxxxxxros2 bag play rosbag2_2023_10_31-07_58_232.4.2. Viewing
Use the ros2 topic command to view the data
xxxxxxxxxxros2 topic echo /chatter2.4.3. Playback Options
Playback at Multiples of Speed -r
The -r option can modify the playback rate, for example -r value; for example, -r 10 means 10x speed, playing the topic at ten times the speed
xxxxxxxxxxros2 bag play rosbag2_2023_10_31-07_58_23 -r 10Loop Playback -l
This is the 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 /chatter3. Example
3.1. Running the talker Node
xxxxxxxxxxros2 run demo_nodes_py talker3.2. Recording
xxxxxxxxxx# Record all topicsros2 bag record -a
How do you stop recording? Just use the Ctrl+C command in the terminal to interrupt the recording
Then you will find an extra 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. Playing and Viewing
Here we play in a loop
xxxxxxxxxxros2 bag play rosbag2_2023_10_31-07_58_23 -l Open another terminal to view the topic:
xxxxxxxxxxros2 topic echo /chatter
