How to Record and Play Back Data in ROS 2 Foxy Fitzroy

cover-ros2-playback

In this post, we will learn how to record and play back data that is published on topics in your ROS 2 application.

The official tutorial is located in the ROS 2 Foxy documentation, but we’ll run through the entire process step-by-step below.


You Will Need

In order to complete this tutorial, you will need:

Prerequisites

It is helpful if you’ve completed this tutorial on the Turtlesim package in ROS 2, but it is not required.

Setup

Open a new terminal window, and type the following command to launch the turtlesim simulation:

ros2 run turtlesim turtlesim_node

Open another terminal window, and launch the keyboard executable, which will enable us to navigate the robot using the keyboard.

ros2 run turtlesim turtle_teleop_key

Let’s create a new directory to save the data that is published to topics. ROS 2 calls these recordings “bag files”. In another terminal window, type the following commands:

mkdir bag_files
cd bag_files

Select a Topic You Want to Record

Let’s see what the currently active topics are.

ros2 topic list
1-list-ros2-topicsJPG

Type the following command to see the relationship between active nodes and topics.

rqt_graph
2-rqt-graphJPG

You can see that the /teleop_turtle node (which is the keyboard-related node) publishes velocity commands to the /turtle1/cmd_vel topic. The /turtlesim node subscribes to these velocity commands, and the turtle moves around the screen accordingly.

Press CTRL + C in the terminal window to close rqt_graph.

Let’s explore the data that is being published to the /turtle1/cmd_vel topic. To do this, we need to use the ros2 topic echo command.

ros2 topic echo /turtle1/cmd_vel

Select the terminal window where you launched the turtle_teleop_key node.

3-turtle-teleop-key-nodeJPG

Move the turtle around using the keyboard.

Return to the terminal window where you launched the ros2 topic echo /turtle1/cmd_vel command.

You should see a printout of the velocity commands.

4-print-out-of-velocity-commandsJPG

Record Data

Let’s record the data that is published to the /turtle1/cmd_vel topic. Open a new terminal window, and navigate to the bag_files directory you created earlier in this tutorial.

cd bag_files
ros2 bag record /turtle1/cmd_vel

The data that is published to /turtle1/cmd_vel is now being recorded.

5-record-bag-dataJPG

Go back to the teleop terminal window, and move the turtle around.

6-move-turtlesim-aroundJPG

Go back to the terminal where you ran the ros2 bag command, and press CTRL + C to stop recording.

Let’s see if the bag file is recorded properly. Inside the bag_files directory on your computer, type the following command to see what files are inside.

dir
7-bag-file-recorded-properlyJPG

The format for the bag file is:

rosbag2_year_month_day-hour_minute_second

To see detailed information about this bag file type:

ros2 bag info <name_of_bag_file>

8-ros2-bag-infoJPG

Play Back Data

Let’s play back the data that was recorded in the bag file.

Return to the teleop window.

Press CTRL + C to stop keyboard control.

Navigate to the bag_files directory, and play back the bag file you recorded earlier. Make sure you can see the turtlesim window.

ros2 bag play <name_of_bag_file>
9-ros-playbackJPG

You will see the turtle replicate the motion you commanded earlier. 

That’s it for bag files. Keep building!