diff --git a/src/lib/timesync/Timesync.cpp b/src/lib/timesync/Timesync.cpp index 3faa8093129..0bf6b528a49 100644 --- a/src/lib/timesync/Timesync.cpp +++ b/src/lib/timesync/Timesync.cpp @@ -121,8 +121,7 @@ uint64_t Timesync::sync_stamp(uint64_t usec) void Timesync::add_sample(int64_t offset_us, uint64_t now_us) { - // Kalman filter with state [offset, skew] in seconds. The skew is a rate [s/s], so the prediction - // stays consistent when the interval between samples changes (e.g. fast initial sync, then 1 Hz). + // Kalman filter with state [offset, skew] in seconds static constexpr double R = MEASUREMENT_NOISE_STD * MEASUREMENT_NOISE_STD; const double offset_s = offset_us * 1e-6; @@ -137,13 +136,15 @@ void Timesync::add_sample(int64_t offset_us, uint64_t now_us) } else { const double dt = (now_us - _last_sample_us) * 1e-6; - // Predict + // Predict: x = F x, P = F P F^T + Q in Horner form, with F = [1 dt; 0 1]. + // Q adds the skew random walk q over dt + // Q = q * integral_0^dt [s^2, s; s, 1] ds = q [dt^3/3, dt^2/2; dt^2/2, dt]. _time_offset += _time_skew * dt; - _var_offset += dt * (2.0 * _cov_offset_skew + dt * _var_skew) + SKEW_RANDOM_WALK * dt * dt * dt / 3.0; - _cov_offset_skew += dt * _var_skew + SKEW_RANDOM_WALK * dt * dt / 2.0; - _var_skew += SKEW_RANDOM_WALK * dt; + _var_offset += dt * (2.0 * _cov_offset_skew + dt * (_var_skew + dt * SKEW_RANDOM_WALK / 3.0)); + _cov_offset_skew += dt * (_var_skew + dt * SKEW_RANDOM_WALK / 2.0); + _var_skew += dt * SKEW_RANDOM_WALK; - // Update + // Update with H = [1 0] (Kalman filter measurement update) const double innovation = offset_s - _time_offset; const double k_offset = _var_offset / (_var_offset + R); const double k_skew = _cov_offset_skew / (_var_offset + R); diff --git a/src/lib/timesync/Timesync.hpp b/src/lib/timesync/Timesync.hpp index a76b8d49b63..cd3a675c343 100644 --- a/src/lib/timesync/Timesync.hpp +++ b/src/lib/timesync/Timesync.hpp @@ -52,20 +52,11 @@ using namespace time_literals; static constexpr time_t PX4_EPOCH_SECS = 1234567890ULL; -// Kalman filter tuning -// -// The offset and skew are estimated by a two-state Kalman filter (offset, skew) whose -// prediction uses the actual time between samples, so its dynamics are defined in seconds -// and do not depend on how often the caller exchanges timesync packets. -// -// MEASUREMENT_NOISE_STD : Standard deviation of a single observed offset [s], mostly caused -// by asymmetric link delays. Bounded by MAX_RTT_SAMPLE / 2. -// -// SKEW_RANDOM_WALK : Spectral density of the skew random walk [1/s]. Larger values let -// the filter follow skew changes and clock corrections on the remote system faster, -// at the cost of a noisier estimate. -// -// INITIAL_SKEW_STD : Prior uncertainty of the skew [s/s], i.e. the expected crystal tolerance. +// Kalman filter tuning. State: offset [s] and skew [s/s], predicted with the actual time +// between samples, so the response does not depend on the timesync rate. +// MEASUREMENT_NOISE_STD : std of an observed offset [s], mostly asymmetric link delay +// SKEW_RANDOM_WALK : skew random walk density [1/s], higher follows clock corrections faster +// INITIAL_SKEW_STD : prior skew std [s/s], i.e. crystal tolerance static constexpr double MEASUREMENT_NOISE_STD = 1e-3; static constexpr double SKEW_RANDOM_WALK = 1e-12; static constexpr double INITIAL_SKEW_STD = 100e-6;