Atlas Robotics
ROS2 ToolsBasic

ros2 bag — record and replay robot data

Atlasbot · 17/8/2026 · 11 min read

ros2 bag — record and replay robot data
Record a bag in the field and replay it at your desk

A SLAM bug appears only when the robot passes a specific corridor corner in the field — returning to the site to reproduce it every time you debug is impractical. ros2 bag records all topic data (LiDAR, odometry, TF...) from that run into files, then replays it identically at your desk as many times as needed.

Key concepts

ros2 bag record -o test_hanh_lang /scan /odom /tf /tf_static

-o test_hanh_lang names the output directory. Record only the topics you need to debug — -a (all topics) can create unnecessarily huge files if the system has large image/video topics.

ros2 bag play test_hanh_lang

This replays the recorded data on the original topics with the recorded timing. To subscribing nodes (such as slam_toolbox), bag data looks exactly like a running physical robot's data; they do not need to know whether the source is a bag or a robot.

In short: ros2 bag turns a hard-to-reproduce field bug into a file you can replay indefinitely at your desk — it is the most important tool for systematic SLAM/Nav2 debugging.

How it works

Inspect a recorded bag:

ros2 bag info test_hanh_lang
Files:             test_hanh_lang_0.db3
Duration:          47.3s
Messages:          14208
Topic information:
  /scan      : 473 msgs @ 10.0 Hz
  /odom      : 4730 msgs @ 100.0 Hz
  /tf        : 9005 msgs

A quick reading: if /scan was recorded at only 5Hz rather than the LiDAR's actual 10Hz, the recording computer was probably overloaded (the CPU could not keep up). This is an important clue before concluding that the SLAM algorithm is at fault.

Replay slowly or repeatedly for closer debugging:

ros2 bag play test_hanh_lang --rate 0.5   # replay at half speed
ros2 bag play test_hanh_lang --loop       # loop continuously

Technical details

Formulas

Estimate bag size using the bandwidth formula from the Topic article, multiplied by the recording duration:

Bag size ≈ Σ (bandwidth per topic) × recording duration

Example: record /scan (~60KB/s, calculated in the Topic article) + /odom (small, ~5KB/s) for 5 minutes (300s)
Size ≈ (60 + 5) × 300 ≈ 19,500 KB ≈ ~19MB

Adding a camera topic (for example, ~2MB/s uncompressed) over the same 5 minutes:
Additional size ≈ 2000 × 300 = 600,000 KB ≈ ~600MB

The large difference between recording with and without image topics directly explains the recommendation to record only necessary topics, discussed under Key concepts.

Parameters

ParameterValue
Default storage formatSQLite3 (.db3)
Alternative format (higher performance, common in newer distributions)MCAP
Flag to record all topics-a (avoid with large image/point cloud topics)

Practical applications

Beyond reproducing bugs, ros2 bag is the standard tool for fair comparisons between SLAM configurations: record field data once, then test different SLAM Toolbox/Cartographer parameter sets on exactly the same bag. Every comparison uses identical input data, eliminating variation from repeated physical robot runs that never follow exactly the same trajectory 100% of the time. This is the standard benchmarking approach for objectively evaluating which algorithm produces better maps.

Practical example

$ ros2 bag record -o test_hanh_lang /scan /odom /tf /tf_static --max-bag-size 500000000
Output:
[INFO] [rosbag2_recorder]: Recording...
[INFO] [rosbag2_recorder]: Subscribed to topic '/scan'
[INFO] [rosbag2_recorder]: Subscribed to topic '/odom'
[INFO] [rosbag2_recorder]: Subscribed to topic '/tf'
[INFO] [rosbag2_recorder]: Subscribed to topic '/tf_static'

--max-bag-size 500000000 (bytes, ~500MB) automatically splits the bag into parts above that threshold. This is useful for long sessions to avoid a single file becoming too large to handle or vulnerable to corruption if recording is interrupted.

Common mistakes

  • Recording -a with large image/point cloud topics quickly fills the disk — as calculated under Formulas, image topics make a huge difference to file size. Always select the necessary topics, especially for long recordings.
  • Forgetting /tf_static when recording — static transform data (sensor mounting positions, covered in the TF2 article) is published once at startup. If recording begins afterward without explicitly recording /tf_static, replay will lack these critical transforms and cause TF lookup errors during debugging.
  • Replaying a bag while real nodes still publish to the same topics — two sources (bag and physical robot) writing to one topic create confusing mixed data. Stop real drivers (or use separate namespaces/remaps) before replaying bags for debugging.
  • Not synchronizing use_sim_time during replay for nodes requiring simulation time — some nodes (especially Nav2/SLAM) need to use the bag's simulated time rather than the real system clock. Forgetting use_sim_time causes timestamp mismatches and TF extrapolation errors, as discussed in the TF2 article.

Conclusion

ros2 bag converts hard-to-reproduce field bugs into replayable data and provides a standard way to benchmark algorithm configurations fairly on the same dataset. Always select the required topics and remember /tf_static to avoid missing replay data. The next and final article in this category covers RViz2 — the robot data visualization tool commonly used alongside bags to see data rather than just read raw numbers.

Get new articles and lessons by email

When a new technical article or course module is published, Atlas Robotics sends you a short email. No spam.

Unsubscribe any time with the link in each email. See our Privacy policy.

ros2 bag — record and replay robot data — Atlas Robotics