From a09c76d30d1703c0434cd8abba7f6016a68ef3f9 Mon Sep 17 00:00:00 2001 From: Jacob Dahl Date: Tue, 31 Mar 2026 14:49:27 -0800 Subject: [PATCH] fix(mpl3115a2): correct timestamp_sample to integration midpoint The MPL3115A2 ADC conversion at OSR 2 (ratio 4) takes ~18ms. The driver polls until the conversion completes, so the read time is at the end of the integration window. Correct timestamp_sample to the midpoint by subtracting CONVERSION_TIME / 2. --- src/drivers/barometer/mpl3115a2/MPL3115A2.cpp | 10 +++++++++- src/drivers/barometer/mpl3115a2/MPL3115A2.hpp | 2 ++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/drivers/barometer/mpl3115a2/MPL3115A2.cpp b/src/drivers/barometer/mpl3115a2/MPL3115A2.cpp index 8acc202465..8c4348b705 100644 --- a/src/drivers/barometer/mpl3115a2/MPL3115A2.cpp +++ b/src/drivers/barometer/mpl3115a2/MPL3115A2.cpp @@ -50,6 +50,7 @@ #define MPL3115A2_CONVERSION_INTERVAL 10000 /* microseconds */ #define MPL3115A2_OSR 2 /* Over Sample rate of 4 18MS Minimum time between data samples */ +#define MPL3115A2_CONVERSION_TIME 18000 /* ADC conversion time at OSR 2 (ratio 4), microseconds */ #define MPL3115A2_CTRL_TRIGGER (CTRL_REG1_OST | CTRL_REG1_OS(MPL3115A2_OSR)) MPL3115A2::MPL3115A2(const I2CSPIDriverConfig &config) : @@ -200,6 +201,8 @@ int MPL3115A2::measure() perf_count(_comms_errors); } + _measure_start_time = hrt_absolute_time(); + perf_end(_measure_perf); return PX4_OK; @@ -228,7 +231,12 @@ int MPL3115A2::collect() */ uint8_t b[3 + 2] {}; uint8_t reg = OUT_P_MSB; - const hrt_abstime timestamp_sample = hrt_absolute_time(); + + /* Correct for measurement integration delay: the conversion was + * started in measure(), so the effective sample midpoint is half the + * conversion time after the start. Using the stored start time avoids + * jitter from the polling interval. */ + const hrt_abstime timestamp_sample = _measure_start_time + MPL3115A2_CONVERSION_TIME / 2; ret = transfer(®, 1, &b[0], sizeof(b)); if (ret == -EIO) { diff --git a/src/drivers/barometer/mpl3115a2/MPL3115A2.hpp b/src/drivers/barometer/mpl3115a2/MPL3115A2.hpp index 9bf8bf7f51..ba0ebe9159 100644 --- a/src/drivers/barometer/mpl3115a2/MPL3115A2.hpp +++ b/src/drivers/barometer/mpl3115a2/MPL3115A2.hpp @@ -81,6 +81,8 @@ private: bool _collect_phase{false}; + hrt_abstime _measure_start_time{0}; + perf_counter_t _sample_perf; perf_counter_t _measure_perf; perf_counter_t _comms_errors;