ROS2 learning note 10 -- recording and playback data using ros2 bag

Posted by moose4204l on Sun, 23 Jan 2022 02:20:28 +0100

Summary: This article mainly introduces the use of ros2 bag to record and playback data

Environment: Ubuntu 20 04,ros2-foxy,vscode

Finally, if there are no problems encountered in the actual operation, it means that the chapter can be reproduced normally by myself

2.1.10 recording and playback data (original text: https://docs.ros.org/en/foxy/Tutorials/Ros2bag/Recording-And-Playing-Back-Data.html )

>>Tutorials > > recording and playback data

You are reading the documentation of the older version of ros2 (Foxy), but it still supports. For information on the latest version, please see the Galactic version link( https://docs.ros.org/en/galactic/Tutorials.html )

Record and playback data

Goal: record the data released by the topic, and you can play back and check it at any time

Course level: Elementary

Duration: 10min

catalogue

1. Background
2. Preparatory knowledge
3. Steps
3.1 setting
3.2 select a topic
3.3 ros2 bag record
3.4 ros2 bag info
3.5 ros2 bag play
4. Summary
5. Next step
6. Relevant contents

1. Background

ros2 bag is a command-line tool used to record the data released by the system topic. It can record the data of any topic and save it to a database. You can replay these data to reproduce the results of tests or experiments. Recording topics is also a good way to share your work content and let others recreate (your work)

2. Preparatory knowledge

You should use ros2 bag as the general configuration of ros2, which has been installed before

If you have installed from a linux distribution, but the system does not recognize this instruction, (you can also) install it like this:

sudo apt-get install ros-foxy-ros2bag \
                     ros-foxy-rosbag2-converter-default-plugins \
                     ros-foxy-rosbag2-storage-default-plugins

Some concepts mentioned in this course are introduced in previous courses, such as nodes and topics. The turnlesim package is also used here

As the old rule, don't forget to open the configuration file (environment variable) in the new terminal source (personally, if the environment variable of ros2 is written in the startup script. bashrc, there is no need to source again)

3. Steps

3.1 setting

You will record the keyboard input of the turnlesim system, save it and use it for later playback, so before starting, you need to start the node / turnlesim and node / teleop_turtle

New terminal operation:

ros2 run turtlesim turtlesim_node

Operation of another terminal:

ros2 run turtlesim turtle_teleop_key

Create a directory to store record data. Good exercise:

mkdir bag_files
cd bag_files

3.2 select a topic

ros2 bag can only record the data released from the topic. Look at the topic list of your system, open the terminal and enter:

ros2 topic list

return:

/parameter_events
/rosout
/turtle1/cmd_vel
/turtle1/color_sensor
/turtle1/pose

In the topic course, you know / turtle_ The teleop node issues instructions to the topic / turnle1 / CMD_ Vel makes the little turtle move in the simulation window

To view / turnle1 / CMD_ Vel is publishing data. Enter the command:

ros2 topic echo /turtle1/cmd_vel

At first, there was no data because teleop did not publish any data. Return to the terminal running teleop and click the window to activate. Type the specified letter to move the little turtle. If the terminal uses the ros2 topic echo command, you will see that there is data published

linear:
  x: 2.0
  y: 0.0
  z: 0.0
angular:
  x: 0.0
  y: 0.0
  z: 0.0
  ---

3.3 ros2 bag record

In order to record the data published by the topic, use the following instruction syntax:

ros2 bag record <topic_name>

Before running the command, open a new terminal and enter the previously created directory bag_files (run the command again), because the rosbag file is stored in the directory where you run the command

Operation instruction:

ros2 bag record /turtle1/cmd_vel

You will see the following information (time and data will be different):

[INFO] [rosbag2_storage]: Opened database 'rosbag2_2019_10_11-05_18_45'.
[INFO] [rosbag2_transport]: Listening for topics...
[INFO] [rosbag2_transport]: Subscribed to topic '/turtle1/cmd_vel'
[INFO] [rosbag2_transport]: All requested topics are subscribed. Stopping discovery...

Now, the ros2 bag command is being recorded and published in / turnle1 / CMD_ Vel topic data. Return to the teleop terminal and drive the little turtle again. It doesn't matter how to move, but try to run a reasonable figure to facilitate playback and viewing later

Enter ctrl+c to stop recording

The collected data is saved in the bag file, and its name style is like rosbag2_year_month_day-hour_minute_second.

3.3.1 recording multiple topics

You can also record multiple topics, (remember) modify the package name saved by running the ros2 bag command

Run the following command:

ros2 bag record -o subset /turtle1/cmd_vel /turtle1/pose

-o operation can let your package get a unique name. The next string is the file name. In this case, it is subset

Record multiple topics at the same time, and simply separate each topic with a space

You see the following message to prove that all topics (selected) have been recorded

[INFO] [rosbag2_storage]: Opened database 'subset'.
[INFO] [rosbag2_transport]: Listening for topics...
[INFO] [rosbag2_transport]: Subscribed to topic '/turtle1/cmd_vel'
[INFO] [rosbag2_transport]: Subscribed to topic '/turtle1/pose'
[INFO] [rosbag2_transport]: All requested topics are subscribed. Stopping discovery...

You can move the little turtle. When you are finished, you can type ctrl+c

be careful:
Another operation can also be added to the command, - a represents all system topics of the recording system

3.4 ros2 bag info

You can view the recordings by running:

ros2 bag info <bag_file_name>

Run this command on the subset package to obtain the file information list:

ros2 bag info subset
Files:             subset.db3
Bag size:          228.5 KiB
Storage id:        sqlite3
Duration:          48.47s
Start:             Oct 11 2019 06:09:09.12 (1570799349.12)
End                Oct 11 2019 06:09:57.60 (1570799397.60)
Messages:          3013
Topic information: Topic: /turtle1/cmd_vel | Type: geometry_msgs/msg/Twist | Count: 9 | Serialization Format: cdr
                 Topic: /turtle1/pose | Type: turtlesim/msg/Pose | Count: 3004 | Serialization Format: cdr

In order to see more personalized information, you must open the database to view. This is a lightweight database, which is not within the scope of ros2 knowledge

3.5 ros2 bag play

Before playing back the data set, enter ctrl+c to terminate the teleop window. Then ensure that the turnlesim simulation window is always visible so that you can see the recording package in action

Input command:

ros2 bag play subset

The terminal will return the following information:

[INFO] [rosbag2_storage]: Opened database 'subset'.

Your tortoise will follow the path you entered (although it is not 100% in fact. Due to the time characteristics of the system, there are some small changes in the little tortoise simulation)

Because the subset file records the topic of / turnle1 / pose, as long as your turnlesim keeps running, the ros2 bag play command will not exit unless you stop running

This is because as long as the / turnlesim node is active, it (recording package) periodically reads data to the topic / turnle1 / pose. You may notice the result of the above ros2 bag info example, topic / turnle1 / CMD_ The number of vel messages is only 9. This indicates the number of times to press letters during recording

Note that the statistical value of / turtle 1 / pose exceeds 3000; When we record, the data is sent to the topic 3000 times

In order to understand the frequency of location information release, you can run the command:

ros2 topic hz /turtle1/pose

4. Summary

In the ros2 system, you can use the ros2 bag command to record the data transmitted to the topic. Whether you share your work with others or study your own experiments, it is a good tool

5. Next step

You have completed the "initial stage: CLI command line interface tools" course! The next step is the "initial stage: Client Library" course. The first course is to create a workspace

6. Relevant contents

Here( https://github.com/ros2/rosbag2 )More thorough analysis of ros2 bag. For more information about QoS compatibility and ros2 bag, you can see the course, rosbag2: subverting QoS strategy( https://docs.ros.org/en/foxy/Guides/Overriding-QoS-Policies-For-Recording-And-Playback.html#ros2bag-qos-override).

other

I think the key points are:

ros2 bag record <topic_name> #Record the data information of the topic, and the packet name is automatically generated according to the date

ros2 bag record -o subset /turtle1/cmd_vel /turtle1/pose #Record multiple topics at the same time, and the custom packet name is subset

ros2 bag info <bag_file_name>  #View package specification parameters

ros2 bag play <bag_file_name>  #Playback package content

Problems encountered:

os2 topic echo /turtle1/cmd_ When vel acts, there is still no data output; The little turtle can only turn

This course was held for more than ten days after the graduation certificate. My roommate asked, why are you still fooling around here because online translation is so powerful now? I said, my goal is to take a good look, understand and toss myself. At present, I think of the best way to get the best effect, even if the translation is a little awkward, ha ha ha

#####################
No silicon steps, no even thousands of miles
A good memory is better than a bad pen
If you feel a little harvest, please praise the collection

Topics: ROS2