refactor(timesync): express the filter state in seconds

Keep the offset in s and the skew in s/s so all tuning constants share
SI units instead of mixing us and s. offset() is the only conversion
back to us for the interface. The estimate is unchanged.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Andrea Bernasconi <andrea.bernasconi@auterion.com>
This commit is contained in:
Andrea Bernasconi committed 2026-11-19 13:51:13 +01:00
1 parent 40b5efe9c1
commit 26cc1b32a3
2 files changed
+19 -18

No files matched your search

+7 -6
View File
@@ -54,7 +54,7 @@ void Timesync::update(const uint64_t now_us, const int64_t remote_timestamp_ns,
uint64_t rtt_us = now_us - (originate_timestamp_ns / 1000ULL);
// Calculate the difference of this sample from the current estimate
uint64_t deviation = llabs((int64_t)_time_offset - offset_us);
uint64_t deviation = llabs(offset() - offset_us);
if (rtt_us < MAX_RTT_SAMPLE) { // Only use samples with low RTT
@@ -100,7 +100,7 @@ void Timesync::update(const uint64_t now_us, const int64_t remote_timestamp_ns,
tsync_status.source_protocol = _source;
tsync_status.remote_timestamp = remote_timestamp_ns / 1000ULL;
tsync_status.observed_offset = offset_us;
tsync_status.estimated_offset = (int64_t)_time_offset;
tsync_status.estimated_offset = offset();
tsync_status.round_trip_time = rtt_us;
tsync_status.timestamp = hrt_absolute_time();
@@ -112,7 +112,7 @@ uint64_t Timesync::sync_stamp(uint64_t usec)
{
// Only return synchronised stamp if we have converged to a good value
if (sync_converged()) {
return usec + (int64_t)_time_offset;
return usec + offset();
} else {
return hrt_absolute_time();
@@ -121,13 +121,14 @@ 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]. The skew is a rate [us/s], so the prediction
// 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).
static constexpr double R = MEASUREMENT_NOISE_STD * MEASUREMENT_NOISE_STD;
const double offset_s = offset_us * 1e-6;
if (_sequence == 0) {
// First offset sample
_time_offset = offset_us;
_time_offset = offset_s;
_time_skew = 0.0;
_var_offset = R;
_cov_offset_skew = 0.0;
@@ -143,7 +144,7 @@ void Timesync::add_sample(int64_t offset_us, uint64_t now_us)
_var_skew += SKEW_RANDOM_WALK * dt;
// Update
const double innovation = offset_us - _time_offset;
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);
+12 -12
View File
@@ -58,17 +58,17 @@ static constexpr time_t PX4_EPOCH_SECS = 1234567890ULL;
// 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 [us], mostly caused
// 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 [(us/s)^2/s]. Larger values let
// 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 [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;
// INITIAL_SKEW_STD : Prior uncertainty of the skew [s/s], i.e. the expected 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;
// Number of exchanged timesync packets before the estimate is considered converged.
static constexpr uint32_t CONVERGENCE_WINDOW = 500;
@@ -102,7 +102,7 @@ public:
*/
uint64_t sync_stamp(uint64_t usec);
int64_t offset() const { return (int64_t)_time_offset; }
int64_t offset() const { return (int64_t)(_time_offset * 1e6); } // [us]
/**
* Return true if the timesync algorithm converged to a good estimate,
@@ -127,13 +127,13 @@ private:
uint32_t _sequence{0};
// Timesync statistics
double _time_offset{0}; // [us]
double _time_skew{0}; // [us/s]
double _time_offset{0}; // [s]
double _time_skew{0}; // [s/s]
// Filter covariance
double _var_offset{0};
double _cov_offset_skew{0};
double _var_skew{0};
double _var_offset{0}; // [s^2]
double _cov_offset_skew{0}; // [s]
double _var_skew{0}; // [-]
uint64_t _last_sample_us{0};
// Outlier rejection and filter reset