Skip to content

Why flights differ, and how one doesn't

Two flights from the same recipe differ by a fraction of a percent. Before building anything to remove that, it was worth finding out where it comes from. Each of the claims below was measured on the stock arduplane quadplane SITL by flying it and diffing the dataflash logs record by record.

Boot two SITLs with the same parameters, one at speedup 10 and one at speedup 4, side by side so they compete for the CPU. Leave them idle for 45 sim seconds, logging from boot. The two logs agree on all 32,116 IMU samples, every estimator state, every GPS fix, every barometer reading and every truth record.

So the physics, the sensor noise and the estimator are all repeatable, and so is the speedup. The noise generators are seeded, and the main loop advances by sim time, never by wall time. Computation costs no sim time, so load doesn’t show.

An input sent over MAVLink, such as a mode change, an arm command or an RC override, waits in a socket buffer until the main loop next reads it. Which iteration that is depends on the wall clock, and at speedup 10 a millisecond of scheduling becomes ten milliseconds of flight. An arm 30 ms later means a different phase against the GPS updates and the estimator’s steps, and the flight that follows differs slightly. The batch control on the home page, six identical hovers in two clusters 0.8% apart, came from exactly this.

Poking an idle SITL at sim time 20 s shows which inputs do damage. A parameter write, a parameter read, a mode change, a mission upload and a burst of RC overrides each left the timeline bit-identical. None of them changed the vehicle’s state on the ground. What matters is a change in what the aircraft does, applied at a wall-clock moment.

The obvious fix is to move the inputs into the autopilot as a Lua script that acts on the vehicle’s own clock. It doesn’t work. SITL steps physics only from the main thread. Every other thread, scripting included, sleeps with usleep on the wall clock, so a script wakes some wall time after its sim deadline. Two identical scripts that armed at “40 s” armed at 40.045 s and 40.075 s.

The main loop is deterministic, and a mission runs inside it. Two of its items let a mission start without outside help:

  • NAV_DELAY until a UTC time of day. With SITL’s start time pinned (--start-time), UTC is a pure function of sim time, so the delay ends on the same iteration every run. It holds only while disarmed, which is exactly when it’s needed.
  • DO_AUX_FUNCTION with ArmDisarm. It arms from inside the mission.

The upload and the switch to AUTO can arrive at any time before the anchor. Two runs that switched to AUTO 15 s apart, at different speedups, flew bit-identical flights.

A log opened at arming moved the whole flight. With the default LOG_DISARMED 0, arming opens a new log. SITL then steps physics while the main thread waits on the logger thread’s lock (its semaphore_wait_hack), and every later sample shifted by 6.66 ms, eight physics steps. Logging from boot removes the wait.

Switching to AUTO before the vehicle settles leaves a mark. The first live test of fly_exact failed its own replay. The flights matched for 14 s after the anchor, then parted by 0.01° of desired pitch where the mission handed over from a loiter to a landing. They split into two classes by when the switch to AUTO came. Every flight that switched at 10.3 s matched every other one, and every flight that switched at 11 s or later matched the rest of its class. Both moments fall before the estimator sets its origin, at about 20 s, and entering AUTO that early captures navigation state at the switch. Switches at 24 s and 39 s, after the origin was set, gave identical flights. A second boundary turned up on a vehicle whose parameters boot it in QRTL. Its land detector ran while disarmed and finished about 2.7 s after the estimator settled, and switches before its “Land complete” parted the same way. It was bracketed on a custom airframe build. fly_exact therefore waits for the vehicle to be quiescent before it uploads anything, and for 3 sim seconds of it: EKF_STATUS_REPORT showing an absolute position, EXTENDED_SYS_STATE on the ground, and in QLAND or QRTL a “Land complete”. With that wait, flights whose call came immediately, after an idle pause, or late came out identical. To check a new vehicle, fly the same exact recipe twice with the call at different moments, and logcompare.identical will show whether anything else still settles.

RC overrides that stop expire at a wall-clock moment. ArduPilot drops an override RC_OVERRIDE_TIME after the last one arrived, and the channels fall back to SITL’s radio, flight-mode channel included. An exact session therefore never sends any.

A Lua script can be loaded in an exact flight, but once the flight starts it can’t act on anything. A TerraHawk script that wrote SIM_WIND_* and SIM_ENGINE_* every 20 ms, from the aircraft’s true height over the pad, parted two second-boot flights 0.9 s after arming. That was the moment it began writing new values, as the aircraft lifted off. Until then they had matched record for record. The script wakes on simulated time, but it runs on its own thread, so which physics step its param:set lands in depends on host scheduling. The same goes for anything else a script does to the flight: an RC override, a mode change, a servo output. Scripts that only read, or that change nothing during the flight, leave it exact, as TerraHawk’s adopted scripts did. fly_exact says so in warnings whenever scripts are loaded. An effect that has to change during an exact flight belongs in SITL’s C++, on the physics step, where TerraHawk’s dock wind is going.

All of the above was measured on a calm pad, since SITL’s default wind profile (SIM_WIND_T 0) weakens the wind to nothing at the ground. With a uniform wind, the flights parted at the anchor’s first tick, by 1e-11 m of position. Steady wind alone did it. The wind pushes on the parked aircraft, and the steps before the anchor land at wall-clock moments: the mission upload, and the switch to AUTO, which moves the control surfaces in that airflow. The aircraft therefore reaches the anchor in a slightly different state each time.

So nothing may happen on the pad at a wall-clock moment. fly_exact stores the mission in the running SITL and boots it again on that eeprom, in AUTO from its first tick. A mission started at boot can’t wait for a UTC time straight away, because ArduPlane’s NAV_DELAY reads a clock that GPS sets about 9 s in, and it would get a zero delay and arm at once. It holds a fixed number of seconds first. That is just as deterministic, since the mission starts on a fixed boot tick. Two second boots, run at the same time on two instances with 3 m/s on the pad, agreed on every record from boot to touchdown, 122,266 of them.

The first boot isn’t exact, and its eeprom is the second one’s starting state. fly_exact compares the parameters both boots logged at start and refuses a flight when the first boot saved anything that matters. The first real run caught two things, both expected: the mission’s length (MIS_TOTAL) and the airspeed offset the first boot calibrated, which the second boot recalibrates on its own clock.

Comparing the logs turned up one more trap. ArduPilot’s logger drops a streaming record when its buffer is full, as in the burst at boot or on a busy disk. logcompare.identical paired records by position, so one drop misaligned every record after it and reported a difference that wasn’t there. It now pairs them by time and instance, and counts the drops separately under unmatched.

Why exact sessions take everything at open

Section titled “Why exact sessions take everything at open”

Some writes before the anchor were measured harmless: a mode change on the ground, a mission upload, and starting SITL’s turbulence at 20 s or at 23 s. Others are plainly not. A gain or an estimator parameter changed on the ground changes the history the flight starts from. The server can’t tell the two kinds apart for a parameter it has never seen, so an exact session takes its parameters and wind at open_sitl and nothing afterwards. The rule is stricter than the physics needs, and simple enough to hold without exceptions.

Speedup is the exception, since it changes how fast SITL runs and never what it computes. Four speedup changes in mid-flight, at wall-clock moments, left a flight bit-identical. Idle pacing therefore stays on in exact sessions.