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 topic
ros2 bag record /topic-name
# Record multiple topics
ros2 bag record topic-name1 topic-name2
# Record all topics
ros2 bag record -a
Other options
-o name Customize the name of the output file
xxxxxxxxxx
ros2 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_23
ros2 bag info rosbag2_2023_10_31-07_58_23
Then we can replay the data and use the following command to play the data
xxxxxxxxxx
ros2 bag play rosbag2_2023_10_31-07_58_23
Use the ros2 topic command to view data
xxxxxxxxxx
ros2 topic echo /chatter
The -r option can modify the playback rate, such as -r value, such as -r 10, which means playing the topic at 10 times speed.
xxxxxxxxxx
ros2 bag play rosbag2_2023_10_31-07_58_23 -r 10
This is the single cycle
xxxxxxxxxx
ros2 bag play rosbag2_2023_10_31-07_58_23 -l
xxxxxxxxxx
ros2 bag play rosbag2_2023_10_31-07_58_23 --topics /chatter
xxxxxxxxxx
ros2 run demo_nodes_py talker
xxxxxxxxxx
# Record all topics
ros2 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
xxxxxxxxxx
ros2 bag play rosbag2_2023_10_31-07_58_23 -l
Open another terminal to view the topic:
xxxxxxxxxx
ros2 topic echo /chatter