Skip to content
On this page

Recording and playing

Recording data using ROS2 (Robot Operating System 2) can be done with the ros2 bag command-line tool, which allows you to record and play back data from ROS2 topics. This is useful for logging sensor data, debugging, and testing your robotic applications. Here's a step-by-step guide to recording data using ROS2:

  1. Install ros2bag and rosbag2:

    First, ensure that you have the necessary packages installed. If you haven't already, install ros2bag and the storage backend (e.g., rosbag2 for SQLite3) using the following command:

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

Replace <distro> with the name of your ROS2 distribution (e.g., foxy, galactic, rolling).

  1. Source your ROS2 workspace:

Before using ROS2 commands, you need to source your ROS2 workspace. To do this, run the following command in your terminal:

source /opt/ros/<distro>/setup.bash
source /opt/ros/<distro>/setup.bash

Replace <distro> with the name of your ROS2 distribution.

  1. Start your ROS2 system:

Launch your ROS2 nodes or system so that the topics you want to record are being published. This can be done using the ros2 launch command or by starting individual nodes manually.

  1. Record data with ros2 bag:

To record data from specific topics, use the following command:

ros2 bag record -o <output_directory> <topic1> <topic2> ...
ros2 bag record -o <output_directory> <topic1> <topic2> ...

Replace <output_directory> with the desired directory for storing the recorded data and <topic1>, <topic2>, ... with the names of the topics you want to record.

If you want to record data from all available topics, use the following command:

ros2 bag record -a -o <output_directory>
ros2 bag record -a -o <output_directory>
  1. Stop the recording:

To stop the recording, press Ctrl+C in the terminal where the ros2 bag record command is running. The recorded data will be saved as a .db3 file in the specified output directory.

  1. Play back recorded data (optional):

If you want to play back the recorded data, use the following command:

ros2 bag play <output_directory>/<bag_file>
ros2 bag play <output_directory>/<bag_file>

Replace <output_directory> with the directory where the recorded data is stored and <bag_file> with the name of the .db3 file containing the recorded data.

Keep in mind that the ros2 bag tool can be configured with additional options, such as compression, filtering, or specifying a custom storage backend. You can learn more about these options by referring to the ROS2 documentation or running ros2 bag --help in your terminal.