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 <dahl.jakejacob@gmail.com>

---------

Signed-off-by: Jacob Dahl <dahl.jakejacob@gmail.com>
This commit is contained in:
Jacob Dahl authored and GitHub committed 2026-08-28 21:13:44 -06:00
1 parent 243936abe2
commit b4f9388cd9
4 files changed
+28 -24

No files matched your search

+4 -4
View File
@@ -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)
+4 -4
View File
@@ -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
@@ -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<int16_t>(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();
+10 -8
View File
@@ -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<int16_t>(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();