The commonly used recording and playback tool in ROS2 - rosbag2, is used to record topic data. We can use this command to store the topic data as a file. Later, we can directly publish the topic data in the bag file without starting the node.
This tool is very useful when we are making a real robot. For example, we can record a piece of topic data when the robot has problems. After the recording is completed, it can be released multiple times for testing and experimentation.You can also share topic data with others for verification of algorithms, etc.
We try to use the bag tool to record topic data and replay it twice.
Such as the talker in ros2 demo:
ros2 run demo_nodes_py talker
/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
Currently only supports sqllite3, others also have extensions
Before we play a video, we can check the relevant information of the video through the file information, such as the time, size, type, and quantity of the topic record.
xxxxxxxxxx# Assume that the recorded file is rosbag2_2023_10_31-07_58_23ros2 bag info rosbag2_2023_10_31-07_58_23Then we can replay the data and use the following command to play the data
xxxxxxxxxxros2 bag play rosbag2_2023_10_31-07_58_23Use the ros2 topic command to view data
xxxxxxxxxxros2 topic echo /chatterThe -r option can modify the playback rate, such as -r value, such as -r 10, which means playing the topic at 10 times speed.
xxxxxxxxxxros2 bag play rosbag2_2023_10_31-07_58_23 -r 10This is the single cycle
xxxxxxxxxxros2 bag play rosbag2_2023_10_31-07_58_23 -lxxxxxxxxxxros2 bag play rosbag2_2023_10_31-07_58_23 --topics /chatter
xxxxxxxxxxros2 run demo_nodes_py talkerxxxxxxxxxx# Record all topicsros2 bag record -a
How to stop recording? We can directly use the Ctrl+C command in the terminal to interrupt the recording.
Then you will find multiple folders in the terminal named rosbag2_2023_10_31-08_21_21
Open the folder and you can see the contents
This completes the recording.
Here we play it 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
