Introduction to ROS2 - Recording/Playback Data

_Sometimes we want to develop on someone else's existing datasets or reproduce scene data that we've built before, then we can use ROS2 ros2 bag tool. ros2 bag is a command line tool that can record and playback topic data in ROS2 system. The selected data will be packaged into a database file, which can be used to playback all topic data according to the timeline in the future.
_Before using rosbag, you need to confirm whether it is installed, because when you install ROS2 via desktop version, you do not have a bag package installed, you need to install it yourself. Run this sentence:

sudo apt-get install ros-<distro>-ros2bag ros-<distro>-rosbag2*

_The blogger uses the version of eloquent, so the following commands are required to install it:

sudo apt-get install ros-eloquent-ros2bag ros-eloquent-rosbag2*

1 Configuration

_Or take the small turtle simulator as an example, we try to record the speed command that controls the movement of the turtle and achieve playback.
_Open a new terminal and run:

ros2 run turtlesim turtlesim_node

_Open another terminal and run:

ros2 run turtlesim turtle_teleop_key

_Next, create a new folder to save database files in the future:

mkdir bag_files
cd bag_files

2 Select a topic

_ros2 bag can only record topic data being published by the current system. To view a list of current topics on the system, open a new terminal and run the following command:

ros2 topic list

_will output the following information:


_We already know that the/teleop_turtle node issues commands on the/turtle1/cmd_level topic to make the turtle move in the emulator. If you listen to this topic and control the movement of the turtle through the keyboard, you can see the message of the speed command:

ros2 topic echo /turtle1/cmd_vel

_At first nothing will be displayed because the/teleop_turtle node does not publish any data. Return to the terminal running/turtle_teleop_key and select it to make it active. Use the arrow keys to move the turtle and you will see the data published on the terminal running ros2 topic echo.

  x: 2.0
  y: 0.0
  z: 0.0
  x: 0.0
  y: 0.0
  z: 0.0

3 ros2 bag record topic data

To record data published to the topic, use the following commands:

ros2 bag record <topic_name>

_Before running this command on the selected topic, open a new terminal and move to the bag_files directory you created earlier, because the rosbag file will be saved in the directory where you run it.
Run the following command:

ros2 bag record /turtle1/cmd_vel

_You can see the log prompt to start recording data:

[INFO] [rosbag2_storage]: Opened database 'rosbag2_2021_09_22-01_03_32/rosbag2_2021_09_22-01_03_32_0.db3' for READ_WRITE.
[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...

_ros2 bag is now recording messages on / Turtle1 / cmd_veltopic. Go back to the keyboard-controlled window and control the turtle to complete some movement. It doesn't matter how it moves. It's important to record all these moving instructions.

_After the recording is completed, Ctrl+C can stop the recording in the terminal running ros2 bag, and the recorded data will be saved in the file named "rosbag2_year_month_day-hour_minute_second".

_You can also record multiple topics and change the file name saved by ros2 bag. Run the following command:

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

The-o parameter is used to set the database file name, which is called subset. Multiple topic names are separated by spaces. Enter a few to record several. You can see when recording:

[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...

_Note: If you feel troubled, you can record data on all topics with the -a parameter, but this can easily lead to circular dependency, which may cause the system to crash.

4 View database files

_We can use this command to view the information of the database file:

ros2 bag info <bag_file_name>

_Output the following information:

[INFO] [rosbag2_storage]: Opened database 'subset_0.db3' for READ_ONLY.

Files:             subset_0.db3
Bag size:          96.0 KiB
Storage id:        sqlite3
Duration:          20.12s
Start:             Sep 22 2021 01:15:30.652 (1632244530.652)
End                Sep 22 2021 01:15:50.664 (1632244550.664)
Messages:          1294
Topic information: Topic: /turtle1/cmd_vel | Type: geometry_msgs/msg/Twist | Count: 41 | Serialization Format: cdr
                   Topic: /turtle1/pose | Type: turtlesim/msg/Pose | Count: 1253 | Serialization Format: cdr

_To view a single message, you must open the database (in this case, sqlite3) to examine it, which is outside the scope of ROS2.

5 ros2 bag playback data

_Close the previously running keyboard control node to ensure that no node is sending a turtle run command at this time, and then use the following command to play back the recorded data:

ros2 bag play <bag_file_name>

_terminal will prompt:

[INFO] [rosbag2_storage]: Opened database 'rosbag2_2021_09_22-01_03_32_0.db3' for READ_ONLY.

_The turtle simulator can see that the turtle is moving according to previously controlled instructions. (Although not 100% accurate; turtlesim is very sensitive to small changes in system timing).
_You can use the ros2 bag command to record the data passed to the topic in the ROS2 system. This is a good tool whether you share your work with others or rethink your own experiments.

Posted on Tue, 21 Sep 2021 13:36:20 -0400 by fellow21