Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
Back to tool/Back to issues
#34540·ardupilot

SITL JSON backend: clock truncates each frame's time step and drifts behind physics time

Author: izmaylovdevCreated Sep 30, 2026Updated Sep 30, 2026

Bug

With the JSON backend, SITL's clock falls steadily behind the physics time it is given: 47 ms after under 4 minutes of simulation, about 750 ms after an hour, with 1 ms physics steps. Nothing is lost in transport; it is the arithmetic in JSON::update (libraries/SITL/SIM_JSON.cpp, line 514 on master, line 364 on Plane-4.5):

cpp
double deltat;
...
    deltat = state.timestamp_s - last_timestamp_s;
...
time_now_us += deltat * 1.0e6;

time_now_us is a uint64_t. The difference of two double timestamps is almost never exactly the physics step: for 1 ms steps it comes out as 999.99999999… or 1000.00000001… µs. The compound assignment converts the sum back to uint64_t by truncation, so every frame whose deltat lands just below 1 ms adds 999 µs instead of 1000. How often that happens depends on the spacing of doubles near the timestamp, so the drift comes in phases that change at powers of two of the simulation time (about 225 µs per second between 64 and 256 s, none from about 290 to 2048 s, about 450 µs per second between 2048 and 4096 s).

Reproduction without a simulator

This mirrors ardupilot_gazebo (ArduPilotPlugin::PostUpdate sends simTime as seconds in a double, which rapidjson writes exactly) and the lines above:

cpp
// g++ -O2 -std=c++17 sitl_clock_drift.cpp -o sitl_clock_drift && ./sitl_clock_drift
#include <chrono>
#include <cinttypes>
#include <cstdint>
#include <cstdio>

int main()
{
    uint64_t time_now_us = 0;       // SIM_Aircraft::time_now_us
    double last_timestamp_s = 0.0;  // JSON::last_timestamp_s
    for (int64_t ms = 1; ms <= 3600000; ms++) {
        // ardupilot_gazebo: simTime (ns) -> seconds as double
        const std::chrono::nanoseconds sim_time(ms * 1000000);
        const double timestamp_s = std::chrono::duration_cast<std::chrono::duration<double>>(sim_time).count();

        // SIM_JSON.cpp, JSON::update
        const double deltat = timestamp_s - last_timestamp_s;
        time_now_us += deltat * 1.0e6;
        last_timestamp_s = timestamp_s;

        if (ms % 256000 == 0 || ms == 3600000) {
            const int64_t err_us = int64_t(time_now_us) - ms * 1000;
            printf("sim %4" PRId64 " s   SITL clock - sim time = %+8.3f ms\n", ms / 1000, err_us / 1000.0);
        }
    }
    return 0;
}

Output (g++ 13.3, x86-64):

sim  256 s   SITL clock - sim time =  -51.382 ms
sim  512 s   SITL clock - sim time =  -58.678 ms
...
sim 2048 s   SITL clock - sim time =  -58.678 ms
sim 2304 s   SITL clock - sim time = -173.366 ms
sim 2560 s   SITL clock - sim time = -288.054 ms
...
sim 3600 s   SITL clock - sim time = -753.974 ms

Seen in a real run

ArduPlane Plane-4.5 SITL, ardupilot_gazebo main, Gazebo Harmonic, lockstep, 1 ms physics step, ROS 2 Jazzy with Gazebo's clock bridged to /clock. MAVROS 2.16.0 with timesync_mode: PASSTHROUGH stamps messages with the flight controller's own time (time_boot_ms), so a ROS node on /clock can compare SITL's clock with physics time directly. Median of stamp − /clock on arrival for ATTITUDE, next to what the reproduction above predicts:

sim time measured predicted
58 s −9 ms −8.4 ms
107 s −23 ms −22.8 ms
155 s −35 ms −34.4 ms
229 s −47 ms −46.8 ms

The remaining ~1 ms is transport to the measuring node. After a reboot in flight, SITL's clock came back to physics time instead of 0: last_timestamp_s restarts at 0, so the first deltat is the whole simulation time. That also reset the accumulated error.

Effect

SITL's time_boot_ms, time_usec and its logs drift from the physics time in the JSON packets, and so from anything else on that clock, such as ROS nodes on /clock. Anything that lines SITL's timestamps up with the simulator's, or compares them with a companion computer's clock, is off by the accumulated error: tens of milliseconds within minutes, most of a second within an hour.

Possible fixes

Both keep SITL's clock exactly on physics time in the reproduction above (worst error 0 µs over an hour of 1 ms steps). Not tested inside SITL.

  1. Round each step: time_now_us += uint64_t(llround(deltat * 1.0e6));. Exact whenever the step is a whole number of microseconds; otherwise the error is bounded by 0.5 µs per frame, but can still accumulate.

  2. Take the clock from the absolute timestamp instead of adding steps, keeping a base for the physics-reset case:

    cpp
    if (state.timestamp_s < last_timestamp_s) {
        // physics reset: keep SITL's clock running from where it is
        base_time_us = time_now_us;
        base_timestamp_s = state.timestamp_s;
    }
    time_now_us = base_time_us + uint64_t(llround((state.timestamp_s - base_timestamp_s) * 1.0e6));

    No error can accumulate this way, whatever the step. deltat would still be computed as now, for adjust_frame_time() and time_advance(). On the first packet the base is 0, so SITL's clock starts at the physics time, as it does today.

Related

  • #29921 replaced the same line, time_now_us += delta_time * 1.0e6, in the RealFlight backend with absolute time, and #11217 did the same for JSBSim (time_now_us = fdm.cur_time). The JSON backend still accumulates.
  • #15680 reported time_boot_ms drift in SITL, but against the wall clock with AirSim, and was AirSim's clock. This is a different cause: the drift here is against the physics time in the JSON packets, and deterministic.

Source: ArduPilot/ardupilot

View original on GitHubView discussion on GitHub