diff --git a/src/lib/timesync/Timesync.cpp b/src/lib/timesync/Timesync.cpp index 9d8d798f619..d5c52655a8a 100644 --- a/src/lib/timesync/Timesync.cpp +++ b/src/lib/timesync/Timesync.cpp @@ -73,21 +73,8 @@ void Timesync::update(const uint64_t now_us, const int64_t remote_timestamp_ns, } else { - // Filter gain scheduling - if (!sync_converged()) { - // Interpolate with a sigmoid function - const float progress = (float) _sequence / (float) CONVERGENCE_WINDOW; - double p = (double)(1.f - expf(0.5f * (1.f - 1.f / (1.f - progress)))); - _filter_alpha = p * ALPHA_GAIN_FINAL + (1.0 - p) * ALPHA_GAIN_INITIAL; - _filter_beta = p * BETA_GAIN_FINAL + (1.0 - p) * BETA_GAIN_INITIAL; - - } else { - _filter_alpha = ALPHA_GAIN_FINAL; - _filter_beta = BETA_GAIN_FINAL; - } - // Perform filter update - add_sample(offset_us); + add_sample(offset_us, now_us); // Increment sequence counter after filter update _sequence++; @@ -132,24 +119,43 @@ uint64_t Timesync::sync_stamp(uint64_t usec) } } -void Timesync::add_sample(int64_t offset_us) +void Timesync::add_sample(int64_t offset_us, uint64_t now_us) { - // Online exponential smoothing filter. The derivative of the estimate is also - // estimated in order to produce an estimate without steady state lag: - // https://en.wikipedia.org/wiki/Exponential_smoothing#Double_exponential_smoothing - double time_offset_prev = _time_offset; + // Kalman filter with state [offset, skew]. The skew is a rate [us/s], so the prediction + // stays consistent when the interval between samples changes (e.g. fast initial sync, then 1 Hz). + static constexpr double R = MEASUREMENT_NOISE_STD * MEASUREMENT_NOISE_STD; if (_sequence == 0) { // First offset sample _time_offset = offset_us; + _time_skew = 0.0; + _var_offset = R; + _cov_offset_skew = 0.0; + _var_skew = INITIAL_SKEW_STD * INITIAL_SKEW_STD; } else { - // Update the clock offset estimate - _time_offset = _filter_alpha * offset_us + (1.0 - _filter_alpha) * (_time_offset + _time_skew); + const double dt = (now_us - _last_sample_us) * 1e-6; - // Update the clock skew estimate - _time_skew = _filter_beta * (_time_offset - time_offset_prev) + (1.0 - _filter_beta) * _time_skew; + // Predict + _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; + + // Update + const double innovation = offset_us - _time_offset; + const double k_offset = _var_offset / (_var_offset + R); + const double k_skew = _cov_offset_skew / (_var_offset + R); + + _time_offset += k_offset * innovation; + _time_skew += k_skew * innovation; + + _var_skew -= k_skew * _cov_offset_skew; + _var_offset *= (1.0 - k_offset); + _cov_offset_skew *= (1.0 - k_offset); } + + _last_sample_us = now_us; } void Timesync::reset_filter() @@ -158,8 +164,10 @@ void Timesync::reset_filter() _sequence = 0; _time_offset = 0.0; _time_skew = 0.0; - _filter_alpha = ALPHA_GAIN_INITIAL; - _filter_beta = BETA_GAIN_INITIAL; + _var_offset = 0.0; + _cov_offset_skew = 0.0; + _var_skew = 0.0; + _last_sample_us = 0; _high_deviation_count = 0; _high_rtt_count = 0; } diff --git a/src/lib/timesync/Timesync.hpp b/src/lib/timesync/Timesync.hpp index 4844713442d..8da2c6502dd 100644 --- a/src/lib/timesync/Timesync.hpp +++ b/src/lib/timesync/Timesync.hpp @@ -52,27 +52,25 @@ using namespace time_literals; static constexpr time_t PX4_EPOCH_SECS = 1234567890ULL; -// Filter gains +// Kalman filter tuning // -// Alpha : Used to smooth the overall clock offset estimate. Smaller values will lead -// to a smoother estimate, but track time drift more slowly, introducing a bias -// in the estimate. Larger values will cause low-amplitude oscillations. +// 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. // -// Beta : Used to smooth the clock skew estimate. Smaller values will lead to a -// tighter estimation of the skew (derivative), but will negatively affect how fast the -// filter reacts to clock skewing (e.g cause by temperature changes to the oscillator). -// Larger values will cause large-amplitude oscillations. -static constexpr double ALPHA_GAIN_INITIAL = 0.05; -static constexpr double BETA_GAIN_INITIAL = 0.05; -static constexpr double ALPHA_GAIN_FINAL = 0.003; -static constexpr double BETA_GAIN_FINAL = 0.003; +// MEASUREMENT_NOISE_STD : Standard deviation of a single observed offset [us], mostly caused +// by asymmetric link delays. Bounded by MAX_RTT_SAMPLE / 2. +// +// SKEW_RANDOM_WALK : Spectral density of the skew random walk [(us/s)^2/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 [us/s], i.e. the expected crystal tolerance. +static constexpr double MEASUREMENT_NOISE_STD = 1000.0; +static constexpr double SKEW_RANDOM_WALK = 1.0; +static constexpr double INITIAL_SKEW_STD = 100.0; -// Filter gain scheduling -// -// The filter interpolates between the INITIAL and FINAL gains while the number of -// exhanged timesync packets is less than CONVERGENCE_WINDOW. A lower value will -// allow the timesync to converge faster, but with potentially less accurate initial -// offset and skew estimates. +// Number of exchanged timesync packets before the estimate is considered converged. static constexpr uint32_t CONVERGENCE_WINDOW = 500; // Outlier rejection and filter reset @@ -121,20 +119,22 @@ public: private: /** - * Online exponential filter to smooth time offset + * Kalman filter update of time offset and skew */ - void add_sample(int64_t offset_us); + void add_sample(int64_t offset_us, uint64_t now_us); uORB::PublicationMulti _timesync_status_pub{ORB_ID(timesync_status)}; uint32_t _sequence{0}; // Timesync statistics - double _time_offset{0}; - double _time_skew{0}; + double _time_offset{0}; // [us] + double _time_skew{0}; // [us/s] - // Filter parameters - double _filter_alpha{ALPHA_GAIN_INITIAL}; - double _filter_beta{BETA_GAIN_INITIAL}; + // Filter covariance + double _var_offset{0}; + double _cov_offset_skew{0}; + double _var_skew{0}; + uint64_t _last_sample_us{0}; // Outlier rejection and filter reset uint32_t _high_deviation_count{0};