ros provides the rosbag tool for recording and playing data. It records the data in the specified rostopic into a data package with a .bag suffix, which facilitates offline analysis and processing of the data.
The commands of rosbag are as follows:
Commonly used ones are record and play, which are recording messages and playing messages respectively.
The command format of rosbag record recording message is,
rosbag record /topic_name
Among them, /topic_name is the name of the topic where the message needs to be recorded. If the parameter -a is provided, for example,
xxxxxxxxxx
rosbag record -a
With the parameter -a, it means recording all current topic data without specifying a certain topic. Of course, you can also record multiple topics that you are interested in, for example,
xxxxxxxxxx
rosbag record /topic_name1 /topic_name2
When you want to stop recording, press ctrl c to stop. The data packet will be named after the current time, with the suffix .bag, and saved in the terminal directory where the recording command is run. If you want to specify the name of the generated data packet, add the parameter -O on the command line, for example,
xxxxxxxxxx
rosbag record -O save_topic.bag /topic_name
The command format of rosbag info to view packet information is,
xxxxxxxxxx
rosbag info file_name.bag
Open the terminal in the folder where the data package is located, and then enter the above command to see the information of the data package, including topic name, size, data type, etc., for example,
You can also use the parameter -y to convert it into YAML format for viewing.
xxxxxxxxxx
rosbag info -y file_name.bag
for example,
The command format of rosbag play to play data packets is,
xxxxxxxxxx
rosbag play file_name.bag
Open the terminal in the folder where the data package is located, and then enter the above command to play the topic information recorded in the data package. It can be understood as a publisher publishing information according to the default frequency.If you want to change the rate of message publishing, you need to add -r, followed by a number corresponding to the playback rate.
xxxxxxxxxx
rosbag play -r 2 file_name.bag
If you want to loop playback, add the -l parameter.
xxxxxxxxxx
rosbag play -l file_name.bag
If the recorded data packet contains multiple data and you only want to play the topic of interest, you need to add the --topic command.
xxxxxxxxxx
rosbag play file_name.bag --topic /topic_name
During the playback command, if you want to pause playback midway, you can press the space bar to pause or continue playback.
We first run the program to control the little turtle, and record the speed topic of the little turtle at this time, and then play it again and enter in the terminal,
xxxxxxxxxx
roslaunch learn_launch turtle_ctrl.launch
Then enter the following command to record the speed topic data of the little turtle,
xxxxxxxxxx
rosbag record /turtle1/cmd_vel
Click on the terminal that starts the launch file, and press up, down, left, and right to control the movement of the little turtle. After the exercise is completed, click on the terminal to start the command to record the topic, ctrl c to close the recording, and a data packet (.bag suffix) with time as the unit will be generated in the terminal directory. Here I am in the ~/Desktop directory. The generated package name is 2023-10-24-20-06-18.bag.
Also use ctrl c to close turtle_ctrl.launch, then run only the small turtle node and enter in the terminal,
xxxxxxxxxx
roslaunch learn_launch turtle_node.launch
Open another terminal, use the cd command to switch to the directory of the data packet just recorded, and enter,
xxxxxxxxxx
rosbag play 2023-10-24-20-06-18.bag #Replace the bag here with the name you just recorded
As shown in the figure above, the terminal that plays the data packet will continue to play (publish) the data. After receiving the speed control instruction, the little turtle starts moving according to the speed value.
You can use rosbag info to check the relevant information of this data packet and enter it in the terminal.
xxxxxxxxxx
rosbag info 2023-10-24-20-06-18.bag #Replace the bag here with the name you just recorded