The command interaction tool is basically introduced, and these debugging processes can be recorded for subsequent analysis.
Record the data published on a topic so that it can be played back and viewed at any time.
ros2 bag is a command line tool for recording topic data published in the system. It accumulates the data passed to any number of topics and saves it in the database. Then, the data can be replayed to reproduce the results of tests and experiments. Recording a theme is also a great way to share your work and allow others to recreate it.
Preliminary function package
- sudo apt install ros-foxy-ros2bag ros-foxy-rosbag2-converter-default-plugins ros-foxy-rosbag2-storage-default-plugins
1 Start the node
The keyboard input will be recorded in the turtlesim system for later saving and replay, so start the /turtlesim and /teleop_turtle nodes first.
Open a new terminal and run:
- ros2 run turtlesim turtlesim_node
Open another terminal and run:
- ros2 run turtlesim turtle_teleop_key
It is also a good practice to create a new directory to store the saved files:
- mkdir bag_files
- cd bag_files
If you do not create a folder, it will be saved in the current path.
2 Choose a theme
ros2 bag can only record the data of the published topic. To view the list of system topics, open a new terminal and run the following command:
- ros2 topic list
Shown as follows:
In the topic tutorial, I learned that the /turtle_teleop node issues commands on the /turtle1/cmd_vel topic to make the robot move in turtlesim.
To view the data being published by /turtle1/cmd_vel, run the following command:
- ros2 topic echo /turtle1/cmd_vel
Nothing will be displayed at first, because teleop did not publish any data. Go back to the terminal running Teleop and select it to make it active. Use the arrow keys to move the robot, and you will see the data published on the terminal running ros2 topic echo.
After using the keyboard remote control:
To record the data posted to the topic, use the following command syntax:
- ros2 bag record <topic_name>
Before running this command on the selected theme, open a new terminal and enter the bag_files directory created earlier, because the rosbag file will be saved in the directory where it was run.
Run the command:
- ros2 bag record /turtle1/cmd_vel
You will see the following message in the terminal (the date and time will be different):
Now ros2 bag is recording the data published on the topic /turtle1/cmd_vel. Return to the teleop terminal and move the robot again. It doesn’t matter how you move, but try to make a recognizable pattern so you can check it later when you replay the data.
Press Ctrl+C to stop recording.
The data will be accumulated in a bag file named rosbag2_year_month_day-hour_minute_second mode.
Record multiple data subjects
You can also record multiple topics, and change the ros2 bag and save it to a folder with a specified name.
Run the following command:
- ros2 bag record -o subset /turtle1/cmd_vel /turtle1/pose
The -o option allows the package file to choose a unique name. The following strings (in this case, a subset) are file names.
To record multiple topics at once, just list each topic and separate it with a space.
You will see the following message confirming that these two topics are being recorded.
When finished, you can move the robot and press Ctrl+C .
You can add another option -a to the command, which records all topics on the system.
4 package information
You can view detailed information about the recording by running the following command:
- ros2 bag info <bag_file_name>
Running this command on a subset package file will return a list of information about that file:
- ros2 bag info subset
To view a single message, you must open the database (sqlite3 in this case) to view it, which is beyond the scope of ROS 2.
5 pack play
Before replaying the package file, enter Ctrl+C in the terminal running teleop. Then make sure that the turtlesim window is visible so that you can see the running package file.
input the command:
- ros2 bag play rosbag2_2021_06_03-11_09_25
The terminal will return a message:
The robot will follow the same path entered during the previous recording (although not 100% accurate; turtlesim is sensitive to small changes in system timing).
If you use subset
Because the file records the subject of /turtle1/pose, the ros2 bag play command will not exit as long as turtlesim is running, even if it is not moved.
This is because as long as the /turtlesim node is active, it will periodically publish data on the subject of /turtle1/pose. You may have noticed that in the result of the ros2 bag info example above, the Count information for the subject /turtle1/cmd_vel is only 0; this is the number of times we pressed the arrow keys while recording.
Note that the Count value of /turtle1/pose exceeds 20000; at the time of recording, the data on the subject was published 25561 times.
To find out how often location data is published, you can run the following command:
- ros2 topic hz /turtle1/pose
to sum up
You can use the ros2 bag command to record the topic data transferred in the ROS 2 system. Whether you are sharing your work with others or reviewing your own experiments, it is a great tool.
Completed the "Beginner: CLI Tools" tutorial!
The following tutorial will consolidate these CLI tools through the 3D environment robot, and then carry out more in-depth learning.