Bags
ROS2 bags let you record messages published to topics and replay them later. This is useful for debugging, testing nodes with real data, and sharing work. With the ros2 bag command-line tool, you can record bags, play them back, and convert them to include different topics and to different formats. See the Additional Resources for more info.
Playback
You can use any of these commands to start the playback of a bag. While the bag is playing, you should see the available topics with ros2 topic list, and can echo them with ros2 topic echo. See this section of the rosbag2 README for more info.
- Play the bag once:
ros2 bag play <bag_name>- Loop playback of the bag forever until cancelled:
ros2 bag play -l <bag_name>- Play a specific range of the bag. For example, from 1:00 to 1:30:
ros2 bag play --start-offset 60 --playback-duration 30 <bag_name>TIP
You can use these hotkeys during playback:
| Hotkey | Action |
|---|---|
| Space | Toggle pause and resume playback |
| Right Arrow | Play the very next message (useful while paused) |
| Up Arrow | Increase message playback rate by 10% |
| Down Arrow | Decrease message playback rate by 10% |
| Ctrl + C | Stop playback |
Recording
Before recording, be sure to change into the directory where you would like the bag to be saved. After that, run the ros2 bag record command to capture the data being published to a topic (or topics). The bag will be named in the format of rosbag2_year_month_day-hour_minute_second, unless the -o parameter is used to specify a name for your bag. You can press Ctrl + C to stop the recording once finished. See this section of the rosbag2 README for more info. Here are some example commands:
- Record a single topic:
ros2 bag record <topic_name>- Record multiple topics:
ros2 bag record /topicA /topicB /topicC- Record multiple topics to a bag named
my_cool_bag:
ros2 bag record -o my_cool_bag /topicA /topicB /topicC- Record ALL topics:
ros2 bag record -aReducing/Converting
Sometimes it may be useful to reduce the number of topics in a bag, or modify it to have a smaller file size so it is easier to handle. For example, you may want to extract a few topics of interest from a previous bag that was recorded with ros2 bag record -a. This is where the ros2 bag convert command can be used. With convert, you can modify bags by merging two or more bags, splitting a bag into multiple bags, trimming the start and end times, or changing storage methods, among other things. See this section of the rosbag2 README for more info.
These steps will show what was done to create the reduced versions of the example bags.
- Record data from ALL topics to a bag named
first_run:
cd ~/bags
ros2 bag record -o first_run -a- Create a configuration YAML file in the same directory as the bag. Inside this file, choose a name for the output bag, as well as the list of topics. (See the example
out.yamlbelow):
touch out.yaml
nano out.yamlout.yaml
output_bags:
- uri: first_run_reduced # name of your output bag
topics: [ # list of topics, comma-separated
# tf
/tf,
/tf_static,
/robot_description,
# odom
/fusion/odom,
/fusion/pose,
/diff_cont/odom,
/joint_states,
# imu
/esp/imu,
# gnss
/gnss/fix,
/ntrip_client/rtcm,
# lidar
/scan_filtered,
/velodyne_points,
/depth_camera/filtered/points,
# cmd_vel
/diff_cont/cmd_vel,
# nav2
/plan,
/local_costmap/costmap,
/global_costmap/costmap,
/local_costmap/published_footprint,
/global_costmap/published_footprint,
# camera
/zed/zed_node/rgb/color/rect/image,
/zed/zed_node/point_cloud/cloud_registered,
]- Run this command to convert the bag:
ros2 bag convert -i first_run -o out.yaml- Optionally, compress the bag with
zstdif the file size is large:
tar --zstd -cf first_run_reduced.tar.zst first_run_reducedAdditional Resources
- ROS 2's Official Documentation
- rosbag2 repo
- The README contains plenty of other usage guides and examples
- rqt_bag
- A handy UI tool for displaying and replaying bag files
