From b4f9388cd9ab34973e552c30aac323dbaeffb169 Mon Sep 17 00:00:00 2001 From: Jacob Dahl <37091262+dakejahl@users.noreply.github.com> Date: Fri, 28 Aug 2026 21:13:44 -0600 Subject: [PATCH] fix(drivers/imu): clip FIFO samples against the declared range, not the int16 rail (#28310) * fix(drivers/imu): clip FIFO samples against the declared range, not the int16 rail updateFIFO() counted a sample as clipped only within 1 LSB of INT16_MIN or INT16_MAX, while update() compares against _clip_limit (range/scale). The two agree only for sensors whose sensitivity is exactly range/32768. ST parts leave headroom in the word: the LSM6DSV80X high-g channel at +/-80 g and 3.904 mg/LSB saturates at 20492 counts, its gyro at +/-4000 dps and 140 mdps/LSB at 28571, the LSM9DS1 and ADIS16607 are similar, and the BMI055's 12-bit accel word never reaches the rail at all. None of them could report a clip on the FIFO path, so the EKF's delta-velocity clipping handling never engaged on those sensors. Compare against _clip_limit on the FIFO path as well. For rail-scaled sensors the threshold moves from 32766 to 32735 counts (the existing 0.999 margin), which is what the non-FIFO path already used. * docs(msg): SensorGyroFifo/SensorAccelFifo counts are raw, not SI x/y/z are int16 counts; SI is count * scale. The comments called them rad/s and m/s^2, which is what sensor_gyro/sensor_accel carry. Signed-off-by: Jacob Dahl --------- Signed-off-by: Jacob Dahl --- msg/SensorAccelFifo.msg | 8 ++++---- msg/SensorGyroFifo.msg | 8 ++++---- .../drivers/accelerometer/PX4Accelerometer.cpp | 18 ++++++++++-------- src/lib/drivers/gyroscope/PX4Gyroscope.cpp | 18 ++++++++++-------- 4 files changed, 28 insertions(+), 24 deletions(-) diff --git a/msg/SensorAccelFifo.msg b/msg/SensorAccelFifo.msg index 1eae5dd149c..84b5629a913 100644 --- a/msg/SensorAccelFifo.msg +++ b/msg/SensorAccelFifo.msg @@ -4,10 +4,10 @@ uint64 timestamp_sample uint32 device_id # unique device ID for the sensor that does not change between power cycles float32 dt # delta time between samples (microseconds) -float32 scale +float32 scale # m/s^2 per count uint8 samples # number of valid samples -int16[32] x # acceleration in the FRD board frame X-axis in m/s^2 -int16[32] y # acceleration in the FRD board frame Y-axis in m/s^2 -int16[32] z # acceleration in the FRD board frame Z-axis in m/s^2 +int16[32] x # FRD board frame X-axis raw counts (m/s^2 = x * scale) +int16[32] y # FRD board frame Y-axis raw counts (m/s^2 = y * scale) +int16[32] z # FRD board frame Z-axis raw counts (m/s^2 = z * scale) diff --git a/msg/SensorGyroFifo.msg b/msg/SensorGyroFifo.msg index 2e77ef07eb2..f1c398d9901 100644 --- a/msg/SensorGyroFifo.msg +++ b/msg/SensorGyroFifo.msg @@ -4,12 +4,12 @@ uint64 timestamp_sample uint32 device_id # unique device ID for the sensor that does not change between power cycles float32 dt # delta time between samples (microseconds) -float32 scale +float32 scale # rad/s per count uint8 samples # number of valid samples -int16[32] x # angular velocity in the FRD board frame X-axis in rad/s -int16[32] y # angular velocity in the FRD board frame Y-axis in rad/s -int16[32] z # angular velocity in the FRD board frame Z-axis in rad/s +int16[32] x # FRD board frame X-axis raw counts (rad/s = x * scale) +int16[32] y # FRD board frame Y-axis raw counts (rad/s = y * scale) +int16[32] z # FRD board frame Z-axis raw counts (rad/s = z * scale) uint8 ORB_QUEUE_LENGTH = 4 diff --git a/src/lib/drivers/accelerometer/PX4Accelerometer.cpp b/src/lib/drivers/accelerometer/PX4Accelerometer.cpp index da11b0239c2..3c619730f60 100644 --- a/src/lib/drivers/accelerometer/PX4Accelerometer.cpp +++ b/src/lib/drivers/accelerometer/PX4Accelerometer.cpp @@ -50,15 +50,13 @@ static constexpr int32_t sum(const int16_t samples[], uint8_t len) return sum; } -static constexpr uint8_t clipping(const int16_t samples[], uint8_t len) +static constexpr uint8_t clipping(const int16_t samples[], uint8_t len, int16_t clip_limit) { unsigned clip_count = 0; for (int n = 0; n < len; n++) { - // - consider data clipped/saturated if it's INT16_MIN/INT16_MAX or within 1 - // - this accommodates rotated data (|INT16_MIN| = INT16_MAX + 1) - // and sensors that may re-use the lowest bit for other purposes (sync indicator, etc) - if ((samples[n] <= INT16_MIN + 1) || (samples[n] >= INT16_MAX - 1)) { + // symmetric about zero: rotation negates samples and |INT16_MIN| = INT16_MAX + 1 + if ((samples[n] <= -clip_limit) || (samples[n] >= clip_limit)) { clip_count++; } } @@ -181,9 +179,13 @@ void PX4Accelerometer::updateFIFO(sensor_accel_fifo_s &sample) _last_sample[1] = sample.y[N - 1]; _last_sample[2] = sample.z[N - 1]; - report.clip_counter[0] = clipping(sample.x, N); - report.clip_counter[1] = clipping(sample.y, N); - report.clip_counter[2] = clipping(sample.z, N); + // The declared range is not the int16 rail on every part (ST high-g and dps scales leave + // headroom in the word), so clip against range/scale like update() does. The 0.999 margin + // in _clip_limit also covers sensors that reuse the lowest bit for a sync flag. + const int16_t clip_limit = static_cast(math::min(_clip_limit, (float)(INT16_MAX - 1))); + report.clip_counter[0] = clipping(sample.x, N, clip_limit); + report.clip_counter[1] = clipping(sample.y, N, clip_limit); + report.clip_counter[2] = clipping(sample.z, N, clip_limit); report.samples = N; report.timestamp = hrt_absolute_time(); diff --git a/src/lib/drivers/gyroscope/PX4Gyroscope.cpp b/src/lib/drivers/gyroscope/PX4Gyroscope.cpp index 740e3196bbd..99731d4c39c 100644 --- a/src/lib/drivers/gyroscope/PX4Gyroscope.cpp +++ b/src/lib/drivers/gyroscope/PX4Gyroscope.cpp @@ -50,15 +50,13 @@ static constexpr int32_t sum(const int16_t samples[], uint8_t len) return sum; } -static constexpr uint8_t clipping(const int16_t samples[], uint8_t len) +static constexpr uint8_t clipping(const int16_t samples[], uint8_t len, int16_t clip_limit) { unsigned clip_count = 0; for (int n = 0; n < len; n++) { - // - consider data clipped/saturated if it's INT16_MIN/INT16_MAX or within 1 - // - this accommodates rotated data (|INT16_MIN| = INT16_MAX + 1) - // and sensors that may re-use the lowest bit for other purposes (sync indicator, etc) - if ((samples[n] <= INT16_MIN + 1) || (samples[n] >= INT16_MAX - 1)) { + // symmetric about zero: rotation negates samples and |INT16_MIN| = INT16_MAX + 1 + if ((samples[n] <= -clip_limit) || (samples[n] >= clip_limit)) { clip_count++; } } @@ -180,9 +178,13 @@ void PX4Gyroscope::updateFIFO(sensor_gyro_fifo_s &sample) _last_sample[1] = sample.y[N - 1]; _last_sample[2] = sample.z[N - 1]; - report.clip_counter[0] = clipping(sample.x, N); - report.clip_counter[1] = clipping(sample.y, N); - report.clip_counter[2] = clipping(sample.z, N); + // The declared range is not the int16 rail on every part (ST high-g and dps scales leave + // headroom in the word), so clip against range/scale like update() does. The 0.999 margin + // in _clip_limit also covers sensors that reuse the lowest bit for a sync flag. + const int16_t clip_limit = static_cast(math::min(_clip_limit, (float)(INT16_MAX - 1))); + report.clip_counter[0] = clipping(sample.x, N, clip_limit); + report.clip_counter[1] = clipping(sample.y, N, clip_limit); + report.clip_counter[2] = clipping(sample.z, N, clip_limit); report.samples = N; report.timestamp = hrt_absolute_time();