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/drivers/imu/invensense/icm42688p/ICM42688P.cpp b/src/drivers/imu/invensense/icm42688p/ICM42688P.cpp index 14cf4b945ff..9e41f97c1bc 100644 --- a/src/drivers/imu/invensense/icm42688p/ICM42688P.cpp +++ b/src/drivers/imu/invensense/icm42688p/ICM42688P.cpp @@ -417,13 +417,18 @@ bool ICM42688P::Configure() // For the 688 the only FSR settings that are operational are ±2000dps for gyroscope and ±16g for accelerometer // For the 686 the only FSR settings that are operational are ±4000dps for gyroscope and ±32g for accelerometer + // data is published from the 16 bit FIFO registers (data[19:4]), which always span the full range if (isICM686) { _px4_accel.set_range(32.f * CONSTANTS_ONE_G); _px4_gyro.set_range(math::radians(4000.f)); + _px4_accel.set_scale(32.f * CONSTANTS_ONE_G / 32768.f); + _px4_gyro.set_scale(math::radians(4000.f / 32768.f)); } else { _px4_accel.set_range(16.f * CONSTANTS_ONE_G); _px4_gyro.set_range(math::radians(2000.f)); + _px4_accel.set_scale(16.f * CONSTANTS_ONE_G / 32768.f); + _px4_gyro.set_scale(math::radians(2000.f / 32768.f)); } return success; @@ -626,33 +631,12 @@ void ICM42688P::FIFOReset() _drdy_timestamp_sample.store(0); } -static constexpr int32_t reassemble_20bit(const uint32_t a, const uint32_t b, const uint32_t c) -{ - // 0xXXXAABBC - uint32_t high = ((a << 12) & 0x000FF000); - uint32_t low = ((b << 4) & 0x00000FF0); - uint32_t lowest = (c & 0x0000000F); - - uint32_t x = high | low | lowest; - - if (a & Bit7) { - // sign extend - x |= 0xFFF00000u; - } - - return static_cast(x); -} - void ICM42688P::ProcessAccel(const hrt_abstime ×tamp_sample, const FIFO::DATA fifo[], const uint8_t samples) { sensor_accel_fifo_s accel{}; accel.timestamp_sample = timestamp_sample; accel.samples = 0; - // 18-bits of accelerometer data, sent as 20-bits, 2 least significant bits always 0 - bool scale_20bit = false; - - // first pass for (int i = 0; i < samples; i++) { uint16_t timestamp_fifo = combine_uint(fifo[i].TimeStamp_h, fifo[i].TimeStamp_l); @@ -664,75 +648,22 @@ void ICM42688P::ProcessAccel(const hrt_abstime ×tamp_sample, const FIFO::DA accel.dt = (float)timestamp_fifo * FIFO_TIMESTAMP_SCALING; } - // 20 bit hires mode - // Sign extension + Accel [19:12] + Accel [11:4] + Accel [3:2] (20 bit extension byte) - // Accel data is 18 bit () - int32_t accel_x = reassemble_20bit(fifo[i].ACCEL_DATA_X1, fifo[i].ACCEL_DATA_X0, - fifo[i].Ext_Accel_X_Gyro_X & 0xF0 >> 4); - int32_t accel_y = reassemble_20bit(fifo[i].ACCEL_DATA_Y1, fifo[i].ACCEL_DATA_Y0, - fifo[i].Ext_Accel_Y_Gyro_Y & 0xF0 >> 4); - int32_t accel_z = reassemble_20bit(fifo[i].ACCEL_DATA_Z1, fifo[i].ACCEL_DATA_Z0, - fifo[i].Ext_Accel_Z_Gyro_Z & 0xF0 >> 4); + // The 16 bit FIFO registers hold data[19:4] of the 20 bit hires sample, which always spans + // the full range (scale set in Configure()). The 20 bit extension nibble is unused so the + // published scale stays constant instead of toggling with batch content. + const int16_t accel_x = combine(fifo[i].ACCEL_DATA_X1, fifo[i].ACCEL_DATA_X0); + const int16_t accel_y = combine(fifo[i].ACCEL_DATA_Y1, fifo[i].ACCEL_DATA_Y0); + const int16_t accel_z = combine(fifo[i].ACCEL_DATA_Z1, fifo[i].ACCEL_DATA_Z0); - // sample invalid if -524288 - if (accel_x != -524288 && accel_y != -524288 && accel_z != -524288) { - // check if any values are going to exceed int16 limits - static constexpr int16_t max_accel = INT16_MAX; - static constexpr int16_t min_accel = INT16_MIN; - - if (accel_x >= max_accel || accel_x <= min_accel) { - scale_20bit = true; - } - - if (accel_y >= max_accel || accel_y <= min_accel) { - scale_20bit = true; - } - - if (accel_z >= max_accel || accel_z <= min_accel) { - scale_20bit = true; - } - - // shift by 2 (2 least significant bits are always 0) - accel.x[accel.samples] = accel_x / 4; - accel.y[accel.samples] = accel_y / 4; - accel.z[accel.samples] = accel_z / 4; + // sample invalid if -32768 (16 bit truncation of the hires invalid marker -524288) + if (accel_x != INT16_MIN && accel_y != INT16_MIN && accel_z != INT16_MIN) { + accel.x[accel.samples] = accel_x; + accel.y[accel.samples] = accel_y; + accel.z[accel.samples] = accel_z; accel.samples++; } } - if (!scale_20bit) { - // On the 686, if highres enabled accel data is always 4096 LSB/g - // On the 688, if highres enabled accel data is always 8192 LSB/g - if (isICM686) { - _px4_accel.set_scale(CONSTANTS_ONE_G / 4096.f); - - } else { - _px4_accel.set_scale(CONSTANTS_ONE_G / 8192.f); - } - - } else { - // 20 bit data scaled to 16 bit (2^4) - for (int i = 0; i < samples; i++) { - // 20 bit hires mode - // Sign extension + Accel [19:12] + Accel [11:4] + Accel [3:2] (20 bit extension byte) - // Accel data is 18 bit () - int16_t accel_x = combine(fifo[i].ACCEL_DATA_X1, fifo[i].ACCEL_DATA_X0); - int16_t accel_y = combine(fifo[i].ACCEL_DATA_Y1, fifo[i].ACCEL_DATA_Y0); - int16_t accel_z = combine(fifo[i].ACCEL_DATA_Z1, fifo[i].ACCEL_DATA_Z0); - - accel.x[i] = accel_x; - accel.y[i] = accel_y; - accel.z[i] = accel_z; - } - - if (isICM686) { - _px4_accel.set_scale(CONSTANTS_ONE_G / 1024.f); - - } else { - _px4_accel.set_scale(CONSTANTS_ONE_G / 2048.f); - } - } - // correct frame for publication for (int i = 0; i < accel.samples; i++) { // sensor's frame is +x forward, +y left, +z up @@ -756,10 +687,6 @@ void ICM42688P::ProcessGyro(const hrt_abstime ×tamp_sample, const FIFO::DAT gyro.timestamp_sample = timestamp_sample; gyro.samples = 0; - // 19-bits of gyroscope data sent as 20-bits, LSB bit is 0 - bool scale_20bit = false; - - // first pass for (int i = 0; i < samples; i++) { uint16_t timestamp_fifo = combine_uint(fifo[i].TimeStamp_h, fifo[i].TimeStamp_l); @@ -771,60 +698,20 @@ void ICM42688P::ProcessGyro(const hrt_abstime ×tamp_sample, const FIFO::DAT gyro.dt = (float)timestamp_fifo * FIFO_TIMESTAMP_SCALING; } - // 20 bit hires mode - // Gyro [19:12] + Gyro [11:4] + Gyro [3:0] (bottom 4 bits of 20 bit extension byte) - int32_t gyro_x = reassemble_20bit(fifo[i].GYRO_DATA_X1, fifo[i].GYRO_DATA_X0, fifo[i].Ext_Accel_X_Gyro_X & 0x0F); - int32_t gyro_y = reassemble_20bit(fifo[i].GYRO_DATA_Y1, fifo[i].GYRO_DATA_Y0, fifo[i].Ext_Accel_Y_Gyro_Y & 0x0F); - int32_t gyro_z = reassemble_20bit(fifo[i].GYRO_DATA_Z1, fifo[i].GYRO_DATA_Z0, fifo[i].Ext_Accel_Z_Gyro_Z & 0x0F); + // The 16 bit FIFO registers hold data[19:4] of the 20 bit hires sample, which always spans + // the full range (scale set in Configure()). The 20 bit extension nibble is unused so the + // published scale stays constant instead of toggling with batch content. + const int16_t gyro_x = combine(fifo[i].GYRO_DATA_X1, fifo[i].GYRO_DATA_X0); + const int16_t gyro_y = combine(fifo[i].GYRO_DATA_Y1, fifo[i].GYRO_DATA_Y0); + const int16_t gyro_z = combine(fifo[i].GYRO_DATA_Z1, fifo[i].GYRO_DATA_Z0); - // check if any values are going to exceed int16 limits - static constexpr int16_t max_gyro = INT16_MAX; - static constexpr int16_t min_gyro = INT16_MIN; - - if (gyro_x >= max_gyro || gyro_x <= min_gyro) { - scale_20bit = true; + // sample invalid if -32768 (16 bit truncation of the hires invalid marker -524288) + if (gyro_x != INT16_MIN && gyro_y != INT16_MIN && gyro_z != INT16_MIN) { + gyro.x[gyro.samples] = gyro_x; + gyro.y[gyro.samples] = gyro_y; + gyro.z[gyro.samples] = gyro_z; + gyro.samples++; } - - if (gyro_y >= max_gyro || gyro_y <= min_gyro) { - scale_20bit = true; - } - - if (gyro_z >= max_gyro || gyro_z <= min_gyro) { - scale_20bit = true; - } - - // shift by 1 (least significant bit is always 0) - gyro.x[gyro.samples] = gyro_x / 2; - gyro.y[gyro.samples] = gyro_y / 2; - gyro.z[gyro.samples] = gyro_z / 2; - gyro.samples++; - } - - if (!scale_20bit) { - // published data is the 20 bit value shifted right by 1, so it spans the full range in - // 2^18 counts: 65.536 LSB/dps on the 686, 131.072 LSB/dps on the 688 - if (isICM686) { - _px4_gyro.set_scale(math::radians(4000.f / 262144.f)); - - } else { - _px4_gyro.set_scale(math::radians(2000.f / 262144.f)); - } - - } else { - // 20 bit data scaled to 16 bit (2^4) - for (int i = 0; i < samples; i++) { - gyro.x[i] = combine(fifo[i].GYRO_DATA_X1, fifo[i].GYRO_DATA_X0); - gyro.y[i] = combine(fifo[i].GYRO_DATA_Y1, fifo[i].GYRO_DATA_Y0); - gyro.z[i] = combine(fifo[i].GYRO_DATA_Z1, fifo[i].GYRO_DATA_Z0); - } - - if (isICM686) { - _px4_gyro.set_scale(math::radians(4000.f / 32768.f)); - - } else { - _px4_gyro.set_scale(math::radians(2000.f / 32768.f)); - } - } // correct frame for publication diff --git a/src/drivers/imu/invensense/iim42652/IIM42652.cpp b/src/drivers/imu/invensense/iim42652/IIM42652.cpp index 757f8c2113b..39f7f1a4bcc 100644 --- a/src/drivers/imu/invensense/iim42652/IIM42652.cpp +++ b/src/drivers/imu/invensense/iim42652/IIM42652.cpp @@ -411,8 +411,11 @@ bool IIM42652::Configure() // 20-bits data format used // the only FSR settings that are operational are ±2000dps for gyroscope and ±16g for accelerometer + // data is published from the 16 bit FIFO registers (data[19:4]), which always span the full range _px4_accel.set_range(16.f * CONSTANTS_ONE_G); _px4_gyro.set_range(math::radians(2000.f)); + _px4_accel.set_scale(16.f * CONSTANTS_ONE_G / 32768.f); + _px4_gyro.set_scale(math::radians(2000.f / 32768.f)); return success; } @@ -614,33 +617,12 @@ void IIM42652::FIFOReset() _drdy_timestamp_sample.store(0); } -static constexpr int32_t reassemble_20bit(const uint32_t a, const uint32_t b, const uint32_t c) -{ - // 0xXXXAABBC - uint32_t high = ((a << 12) & 0x000FF000); - uint32_t low = ((b << 4) & 0x00000FF0); - uint32_t lowest = (c & 0x0000000F); - - uint32_t x = high | low | lowest; - - if (a & Bit7) { - // sign extend - x |= 0xFFF00000u; - } - - return static_cast(x); -} - void IIM42652::ProcessAccel(const hrt_abstime ×tamp_sample, const FIFO::DATA fifo[], const uint8_t samples) { sensor_accel_fifo_s accel{}; accel.timestamp_sample = timestamp_sample; accel.samples = 0; - // 18-bits of accelerometer data - bool scale_20bit = false; - - // first pass for (int i = 0; i < samples; i++) { uint16_t timestamp_fifo = combine_uint(fifo[i].TimeStamp_h, fifo[i].TimeStamp_l); @@ -652,64 +634,22 @@ void IIM42652::ProcessAccel(const hrt_abstime ×tamp_sample, const FIFO::DAT accel.dt = (float)timestamp_fifo * FIFO_TIMESTAMP_SCALING; } - // 20 bit hires mode - // Sign extension + Accel [19:12] + Accel [11:4] + Accel [3:2] (20 bit extension byte) - // Accel data is 18 bit () - int32_t accel_x = reassemble_20bit(fifo[i].ACCEL_DATA_X1, fifo[i].ACCEL_DATA_X0, - fifo[i].Ext_Accel_X_Gyro_X & 0xF0 >> 4); - int32_t accel_y = reassemble_20bit(fifo[i].ACCEL_DATA_Y1, fifo[i].ACCEL_DATA_Y0, - fifo[i].Ext_Accel_Y_Gyro_Y & 0xF0 >> 4); - int32_t accel_z = reassemble_20bit(fifo[i].ACCEL_DATA_Z1, fifo[i].ACCEL_DATA_Z0, - fifo[i].Ext_Accel_Z_Gyro_Z & 0xF0 >> 4); + // The 16 bit FIFO registers hold data[19:4] of the 20 bit hires sample, which always spans + // the full range (scale set in Configure()). The 20 bit extension nibble is unused so the + // published scale stays constant instead of toggling with batch content. + const int16_t accel_x = combine(fifo[i].ACCEL_DATA_X1, fifo[i].ACCEL_DATA_X0); + const int16_t accel_y = combine(fifo[i].ACCEL_DATA_Y1, fifo[i].ACCEL_DATA_Y0); + const int16_t accel_z = combine(fifo[i].ACCEL_DATA_Z1, fifo[i].ACCEL_DATA_Z0); - // sample invalid if -524288 - if (accel_x != -524288 && accel_y != -524288 && accel_z != -524288) { - // check if any values are going to exceed int16 limits - static constexpr int16_t max_accel = INT16_MAX; - static constexpr int16_t min_accel = INT16_MIN; - - if (accel_x >= max_accel || accel_x <= min_accel) { - scale_20bit = true; - } - - if (accel_y >= max_accel || accel_y <= min_accel) { - scale_20bit = true; - } - - if (accel_z >= max_accel || accel_z <= min_accel) { - scale_20bit = true; - } - - // shift by 2 (2 least significant bits are always 0) - accel.x[accel.samples] = accel_x / 4; - accel.y[accel.samples] = accel_y / 4; - accel.z[accel.samples] = accel_z / 4; + // sample invalid if -32768 (16 bit truncation of the hires invalid marker -524288) + if (accel_x != INT16_MIN && accel_y != INT16_MIN && accel_z != INT16_MIN) { + accel.x[accel.samples] = accel_x; + accel.y[accel.samples] = accel_y; + accel.z[accel.samples] = accel_z; accel.samples++; } } - if (!scale_20bit) { - // if highres enabled accel data is always 8192 LSB/g - _px4_accel.set_scale(CONSTANTS_ONE_G / 8192.f); - - } else { - // 20 bit data scaled to 16 bit (2^4) - for (int i = 0; i < samples; i++) { - // 20 bit hires mode - // Sign extension + Accel [19:12] + Accel [11:4] + Accel [3:2] (20 bit extension byte) - // Accel data is 18 bit () - int16_t accel_x = combine(fifo[i].ACCEL_DATA_X1, fifo[i].ACCEL_DATA_X0); - int16_t accel_y = combine(fifo[i].ACCEL_DATA_Y1, fifo[i].ACCEL_DATA_Y0); - int16_t accel_z = combine(fifo[i].ACCEL_DATA_Z1, fifo[i].ACCEL_DATA_Z0); - - accel.x[i] = accel_x; - accel.y[i] = accel_y; - accel.z[i] = accel_z; - } - - _px4_accel.set_scale(CONSTANTS_ONE_G / 2048.f); - } - // correct frame for publication for (int i = 0; i < accel.samples; i++) { // sensor's frame is +x forward, +y left, +z up @@ -733,10 +673,6 @@ void IIM42652::ProcessGyro(const hrt_abstime ×tamp_sample, const FIFO::DATA gyro.timestamp_sample = timestamp_sample; gyro.samples = 0; - // 20-bits of gyroscope data - bool scale_20bit = false; - - // first pass for (int i = 0; i < samples; i++) { uint16_t timestamp_fifo = combine_uint(fifo[i].TimeStamp_h, fifo[i].TimeStamp_l); @@ -748,48 +684,20 @@ void IIM42652::ProcessGyro(const hrt_abstime ×tamp_sample, const FIFO::DATA gyro.dt = (float)timestamp_fifo * FIFO_TIMESTAMP_SCALING; } - // 20 bit hires mode - // Gyro [19:12] + Gyro [11:4] + Gyro [3:0] (bottom 4 bits of 20 bit extension byte) - int32_t gyro_x = reassemble_20bit(fifo[i].GYRO_DATA_X1, fifo[i].GYRO_DATA_X0, fifo[i].Ext_Accel_X_Gyro_X & 0x0F); - int32_t gyro_y = reassemble_20bit(fifo[i].GYRO_DATA_Y1, fifo[i].GYRO_DATA_Y0, fifo[i].Ext_Accel_Y_Gyro_Y & 0x0F); - int32_t gyro_z = reassemble_20bit(fifo[i].GYRO_DATA_Z1, fifo[i].GYRO_DATA_Z0, fifo[i].Ext_Accel_Z_Gyro_Z & 0x0F); + // The 16 bit FIFO registers hold data[19:4] of the 20 bit hires sample, which always spans + // the full range (scale set in Configure()). The 20 bit extension nibble is unused so the + // published scale stays constant instead of toggling with batch content. + const int16_t gyro_x = combine(fifo[i].GYRO_DATA_X1, fifo[i].GYRO_DATA_X0); + const int16_t gyro_y = combine(fifo[i].GYRO_DATA_Y1, fifo[i].GYRO_DATA_Y0); + const int16_t gyro_z = combine(fifo[i].GYRO_DATA_Z1, fifo[i].GYRO_DATA_Z0); - // check if any values are going to exceed int16 limits - static constexpr int16_t max_gyro = INT16_MAX; - static constexpr int16_t min_gyro = INT16_MIN; - - if (gyro_x >= max_gyro || gyro_x <= min_gyro) { - scale_20bit = true; + // sample invalid if -32768 (16 bit truncation of the hires invalid marker -524288) + if (gyro_x != INT16_MIN && gyro_y != INT16_MIN && gyro_z != INT16_MIN) { + gyro.x[gyro.samples] = gyro_x; + gyro.y[gyro.samples] = gyro_y; + gyro.z[gyro.samples] = gyro_z; + gyro.samples++; } - - if (gyro_y >= max_gyro || gyro_y <= min_gyro) { - scale_20bit = true; - } - - if (gyro_z >= max_gyro || gyro_z <= min_gyro) { - scale_20bit = true; - } - - gyro.x[gyro.samples] = gyro_x / 2; - gyro.y[gyro.samples] = gyro_y / 2; - gyro.z[gyro.samples] = gyro_z / 2; - gyro.samples++; - } - - if (!scale_20bit) { - // published data is the 20 bit value shifted right by 1, so it spans the full range in - // 2^18 counts: 131.072 LSB/dps - _px4_gyro.set_scale(math::radians(2000.f / 262144.f)); - - } else { - // 20 bit data scaled to 16 bit (2^4) - for (int i = 0; i < samples; i++) { - gyro.x[i] = combine(fifo[i].GYRO_DATA_X1, fifo[i].GYRO_DATA_X0); - gyro.y[i] = combine(fifo[i].GYRO_DATA_Y1, fifo[i].GYRO_DATA_Y0); - gyro.z[i] = combine(fifo[i].GYRO_DATA_Z1, fifo[i].GYRO_DATA_Z0); - } - - _px4_gyro.set_scale(math::radians(2000.f / 32768.f)); } // correct frame for publication diff --git a/src/drivers/imu/invensense/iim42653/IIM42653.cpp b/src/drivers/imu/invensense/iim42653/IIM42653.cpp index 6b6e590381f..961675073d2 100644 --- a/src/drivers/imu/invensense/iim42653/IIM42653.cpp +++ b/src/drivers/imu/invensense/iim42653/IIM42653.cpp @@ -411,9 +411,12 @@ bool IIM42653::Configure() } // 20-bits data format used - // IIM-42653 FSR: ±4000 dps gyroscope and ±32 g accelerometer (default full-scale) + // the only FSR settings that are operational are ±4000dps for gyroscope and ±32g for accelerometer + // data is published from the 16 bit FIFO registers (data[19:4]), which always span the full range _px4_accel.set_range(32.f * CONSTANTS_ONE_G); _px4_gyro.set_range(math::radians(4000.f)); + _px4_accel.set_scale(32.f * CONSTANTS_ONE_G / 32768.f); + _px4_gyro.set_scale(math::radians(4000.f / 32768.f)); return success; } @@ -615,33 +618,12 @@ void IIM42653::FIFOReset() _drdy_timestamp_sample.store(0); } -static constexpr int32_t reassemble_20bit(const uint32_t a, const uint32_t b, const uint32_t c) -{ - // 0xXXXAABBC - uint32_t high = ((a << 12) & 0x000FF000); - uint32_t low = ((b << 4) & 0x00000FF0); - uint32_t lowest = (c & 0x0000000F); - - uint32_t x = high | low | lowest; - - if (a & Bit7) { - // sign extend - x |= 0xFFF00000u; - } - - return static_cast(x); -} - void IIM42653::ProcessAccel(const hrt_abstime ×tamp_sample, const FIFO::DATA fifo[], const uint8_t samples) { sensor_accel_fifo_s accel{}; accel.timestamp_sample = timestamp_sample; accel.samples = 0; - // 18-bits of accelerometer data - bool scale_20bit = false; - - // first pass for (int i = 0; i < samples; i++) { uint16_t timestamp_fifo = combine_uint(fifo[i].TimeStamp_h, fifo[i].TimeStamp_l); @@ -653,64 +635,22 @@ void IIM42653::ProcessAccel(const hrt_abstime ×tamp_sample, const FIFO::DAT accel.dt = (float)timestamp_fifo * FIFO_TIMESTAMP_SCALING; } - // 20 bit hires mode - // Sign extension + Accel [19:12] + Accel [11:4] + Accel [3:2] (20 bit extension byte) - // Accel data is 18 bit () - int32_t accel_x = reassemble_20bit(fifo[i].ACCEL_DATA_X1, fifo[i].ACCEL_DATA_X0, - fifo[i].Ext_Accel_X_Gyro_X & 0xF0 >> 4); - int32_t accel_y = reassemble_20bit(fifo[i].ACCEL_DATA_Y1, fifo[i].ACCEL_DATA_Y0, - fifo[i].Ext_Accel_Y_Gyro_Y & 0xF0 >> 4); - int32_t accel_z = reassemble_20bit(fifo[i].ACCEL_DATA_Z1, fifo[i].ACCEL_DATA_Z0, - fifo[i].Ext_Accel_Z_Gyro_Z & 0xF0 >> 4); + // The 16 bit FIFO registers hold data[19:4] of the 20 bit hires sample, which always spans + // the full range (scale set in Configure()). The 20 bit extension nibble is unused so the + // published scale stays constant instead of toggling with batch content. + const int16_t accel_x = combine(fifo[i].ACCEL_DATA_X1, fifo[i].ACCEL_DATA_X0); + const int16_t accel_y = combine(fifo[i].ACCEL_DATA_Y1, fifo[i].ACCEL_DATA_Y0); + const int16_t accel_z = combine(fifo[i].ACCEL_DATA_Z1, fifo[i].ACCEL_DATA_Z0); - // sample invalid if -524288 - if (accel_x != -524288 && accel_y != -524288 && accel_z != -524288) { - // check if any values are going to exceed int16 limits - static constexpr int16_t max_accel = INT16_MAX; - static constexpr int16_t min_accel = INT16_MIN; - - if (accel_x >= max_accel || accel_x <= min_accel) { - scale_20bit = true; - } - - if (accel_y >= max_accel || accel_y <= min_accel) { - scale_20bit = true; - } - - if (accel_z >= max_accel || accel_z <= min_accel) { - scale_20bit = true; - } - - // shift by 2 (2 least significant bits are always 0) - accel.x[accel.samples] = accel_x / 4; - accel.y[accel.samples] = accel_y / 4; - accel.z[accel.samples] = accel_z / 4; + // sample invalid if -32768 (16 bit truncation of the hires invalid marker -524288) + if (accel_x != INT16_MIN && accel_y != INT16_MIN && accel_z != INT16_MIN) { + accel.x[accel.samples] = accel_x; + accel.y[accel.samples] = accel_y; + accel.z[accel.samples] = accel_z; accel.samples++; } } - if (!scale_20bit) { - // if highres enabled accel data is always 4096 LSB/g - _px4_accel.set_scale(CONSTANTS_ONE_G / 4096.f); - - } else { - // 20 bit data scaled to 16 bit (2^4) - for (int i = 0; i < samples; i++) { - // 20 bit hires mode - // Sign extension + Accel [19:12] + Accel [11:4] + Accel [3:2] (20 bit extension byte) - // Accel data is 18 bit () - int16_t accel_x = combine(fifo[i].ACCEL_DATA_X1, fifo[i].ACCEL_DATA_X0); - int16_t accel_y = combine(fifo[i].ACCEL_DATA_Y1, fifo[i].ACCEL_DATA_Y0); - int16_t accel_z = combine(fifo[i].ACCEL_DATA_Z1, fifo[i].ACCEL_DATA_Z0); - - accel.x[i] = accel_x; - accel.y[i] = accel_y; - accel.z[i] = accel_z; - } - - _px4_accel.set_scale(CONSTANTS_ONE_G / 1024.f); - } - // correct frame for publication for (int i = 0; i < accel.samples; i++) { // sensor's frame is +x forward, +y left, +z up @@ -734,10 +674,6 @@ void IIM42653::ProcessGyro(const hrt_abstime ×tamp_sample, const FIFO::DATA gyro.timestamp_sample = timestamp_sample; gyro.samples = 0; - // 20-bits of gyroscope data - bool scale_20bit = false; - - // first pass for (int i = 0; i < samples; i++) { uint16_t timestamp_fifo = combine_uint(fifo[i].TimeStamp_h, fifo[i].TimeStamp_l); @@ -749,48 +685,20 @@ void IIM42653::ProcessGyro(const hrt_abstime ×tamp_sample, const FIFO::DATA gyro.dt = (float)timestamp_fifo * FIFO_TIMESTAMP_SCALING; } - // 20 bit hires mode - // Gyro [19:12] + Gyro [11:4] + Gyro [3:0] (bottom 4 bits of 20 bit extension byte) - int32_t gyro_x = reassemble_20bit(fifo[i].GYRO_DATA_X1, fifo[i].GYRO_DATA_X0, fifo[i].Ext_Accel_X_Gyro_X & 0x0F); - int32_t gyro_y = reassemble_20bit(fifo[i].GYRO_DATA_Y1, fifo[i].GYRO_DATA_Y0, fifo[i].Ext_Accel_Y_Gyro_Y & 0x0F); - int32_t gyro_z = reassemble_20bit(fifo[i].GYRO_DATA_Z1, fifo[i].GYRO_DATA_Z0, fifo[i].Ext_Accel_Z_Gyro_Z & 0x0F); + // The 16 bit FIFO registers hold data[19:4] of the 20 bit hires sample, which always spans + // the full range (scale set in Configure()). The 20 bit extension nibble is unused so the + // published scale stays constant instead of toggling with batch content. + const int16_t gyro_x = combine(fifo[i].GYRO_DATA_X1, fifo[i].GYRO_DATA_X0); + const int16_t gyro_y = combine(fifo[i].GYRO_DATA_Y1, fifo[i].GYRO_DATA_Y0); + const int16_t gyro_z = combine(fifo[i].GYRO_DATA_Z1, fifo[i].GYRO_DATA_Z0); - // check if any values are going to exceed int16 limits - static constexpr int16_t max_gyro = INT16_MAX; - static constexpr int16_t min_gyro = INT16_MIN; - - if (gyro_x >= max_gyro || gyro_x <= min_gyro) { - scale_20bit = true; + // sample invalid if -32768 (16 bit truncation of the hires invalid marker -524288) + if (gyro_x != INT16_MIN && gyro_y != INT16_MIN && gyro_z != INT16_MIN) { + gyro.x[gyro.samples] = gyro_x; + gyro.y[gyro.samples] = gyro_y; + gyro.z[gyro.samples] = gyro_z; + gyro.samples++; } - - if (gyro_y >= max_gyro || gyro_y <= min_gyro) { - scale_20bit = true; - } - - if (gyro_z >= max_gyro || gyro_z <= min_gyro) { - scale_20bit = true; - } - - gyro.x[gyro.samples] = gyro_x / 2; - gyro.y[gyro.samples] = gyro_y / 2; - gyro.z[gyro.samples] = gyro_z / 2; - gyro.samples++; - } - - if (!scale_20bit) { - // published data is the 20 bit value shifted right by 1, so it spans the full range in - // 2^18 counts: 65.536 LSB/dps - _px4_gyro.set_scale(math::radians(4000.f / 262144.f)); - - } else { - // 20 bit data scaled to 16 bit (2^4) - for (int i = 0; i < samples; i++) { - gyro.x[i] = combine(fifo[i].GYRO_DATA_X1, fifo[i].GYRO_DATA_X0); - gyro.y[i] = combine(fifo[i].GYRO_DATA_Y1, fifo[i].GYRO_DATA_Y0); - gyro.z[i] = combine(fifo[i].GYRO_DATA_Z1, fifo[i].GYRO_DATA_Z0); - } - - _px4_gyro.set_scale(math::radians(4000.f / 32768.f)); } // correct frame for publication diff --git a/src/lib/drivers/accelerometer/PX4Accelerometer.cpp b/src/lib/drivers/accelerometer/PX4Accelerometer.cpp index f09120a1457..08e887878b3 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++; } } @@ -167,9 +165,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 687747d4aa9..01f6f9da2e6 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++; } } @@ -166,9 +164,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();