19.Topic message recording and playback

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.

19.1. rosbag command

The commands of rosbag are as follows:

image-20231024192036060

Commonly used ones are record and play, which are recording messages and playing messages respectively.

19.1.2 record recording message

The command format of rosbag record recording message is,

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,

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,

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,

19.1.3 View packet information

The command format of rosbag info to view packet information is,

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,

image-20231024193711156

You can also use the parameter -y to convert it into YAML format for viewing.

for example,

image-20231024193908356

19.1.4. Play data packets

The command format of rosbag play to play data packets is,

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.

If you want to loop playback, add the -l parameter.

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.

During the playback command, if you want to pause playback midway, you can press the space bar to pause or continue playback.

19.2 rosbag instance operation

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,

Then enter the following command to record the speed topic data of the little turtle,

image-20231024200656639

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,

Open another terminal, use the cd command to switch to the directory of the data packet just recorded, and enter,

image-20231024202050356

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.

image-20231024202653591