ros2 recording ai_generated true

ROS2 bag recording timestamps don't match message timestamps when using sim_time

ID: ros2/bag-recording-sim-time-clock-skew

Also available as: JSON · Markdown
82%Fix Rate
85%Confidence
3Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
any active

Root Cause

ros2 bag record uses wall clock for recording timestamps by default, even when nodes use sim_time. Messages are recorded with wall-clock receive time, not the sim_time publish time. This causes misalignment on playback.

generic

Workarounds

  1. 88% success Use --use-sim-time with a reliable clock source (e.g., Gazebo clock)
    ros2 bag record --use-sim-time /topic1 /topic2 /clock  # always record /clock topic
  2. 92% success Always record the /clock topic alongside other topics
    ros2 bag record -a --include-hidden-topics  # or explicitly: ros2 bag record /clock /cmd_vel /scan
  3. 85% success Use message header timestamps (not bag timestamps) for time-critical analysis
    When processing bag data, use msg.header.stamp not the bag receive timestamp for accurate timing

Dead Ends

Common approaches that don't work:

  1. Record bag and assume timestamps match simulation time 85% fail

    ros2 bag record uses wall clock for the bag metadata timestamps. Sim_time message headers have correct sim_time but the bag index uses wall clock.

  2. Use --use-sim-time flag on ros2 bag record 78% fail

    This changes the bag recorder's clock to sim_time but can cause recording gaps if clock publishes are delayed or irregular.