From e253c1e20cbd2290a4e5c32cb497b1f742c11acc Mon Sep 17 00:00:00 2001 From: Jacob Dahl Date: Tue, 31 Mar 2026 14:48:41 -0800 Subject: [PATCH] fix(ms5837): correct timestamp_sample to integration midpoint Same fix as MS5611: the MS5837 ADC conversion at OSR 1024 takes ~2.28ms, but the data is read after a 10ms scheduling delay. Correct timestamp_sample by subtracting (CONVERSION_INTERVAL - CONVERSION_TIME/2) from the read time. --- src/drivers/barometer/ms5837/MS5837.cpp | 9 +++++++-- src/drivers/barometer/ms5837/ms5837_registers.h | 1 + 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/drivers/barometer/ms5837/MS5837.cpp b/src/drivers/barometer/ms5837/MS5837.cpp index 4bd4d11c9a..365df5d822 100644 --- a/src/drivers/barometer/ms5837/MS5837.cpp +++ b/src/drivers/barometer/ms5837/MS5837.cpp @@ -269,8 +269,13 @@ int MS5837::_collect() perf_begin(_sample_perf); - /* read the most recent measurement - read offset/size are hardcoded in the interface */ - const hrt_abstime timestamp_sample = hrt_absolute_time(); + /* Correct for measurement integration delay: the conversion was + * started CONVERSION_INTERVAL ago and took OSR1024_CONVERSION_TIME + * to integrate, so the effective sample midpoint is + * (CONVERSION_INTERVAL - OSR1024_CONVERSION_TIME/2) before now. */ + const hrt_abstime now = hrt_absolute_time(); + static constexpr hrt_abstime correction = MS5837_CONVERSION_INTERVAL - MS5837_OSR1024_CONVERSION_TIME / 2; + const hrt_abstime timestamp_sample = (now > correction) ? (now - correction) : now; int ret = read(0, (void *)&raw, 0); if (ret < 0) { diff --git a/src/drivers/barometer/ms5837/ms5837_registers.h b/src/drivers/barometer/ms5837/ms5837_registers.h index 75ef7a6ddb..e664e5f1fe 100644 --- a/src/drivers/barometer/ms5837/ms5837_registers.h +++ b/src/drivers/barometer/ms5837/ms5837_registers.h @@ -83,6 +83,7 @@ * conversion finished */ #define MS5837_CONVERSION_INTERVAL 10000 /* microseconds */ +#define MS5837_OSR1024_CONVERSION_TIME 2280 /* max ADC conversion time at OSR 1024, microseconds */ #define MS5837_MEASUREMENT_RATIO 3 /* pressure measurements per temperature measurement */ namespace ms5837