From da968fc54fc99e30d6db1739c5323b9d5ce439e8 Mon Sep 17 00:00:00 2001 From: Andrea Bernasconi Date: Thu, 5 Nov 2026 10:15:55 +0100 Subject: [PATCH] feat(sensors): fly on the IMU median from the start with SENS_IMU_MODE 2 Switching to the median only once the primary IMU has drifted far enough changes the rate and estimator inputs in the middle of a flight, and relies on thresholds that have to be tuned per vehicle. Using the median for the whole flight keeps the inputs continuous and needs no detection at all: a single inconsistent IMU is outvoted from the first sample. SENS_IMU_MODE 2 now sets sensor_selection.imu_median at boot. The inconsistency accumulator, the SENS_IMU_MED_* thresholds and the demotion of the inconsistent IMU are removed, and sensorsPoll() no longer needs the armed state, so the ITCM symbol change for it is reverted. With fewer than three recent IMUs the median falls back to the selected one. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Andrea Bernasconi --- .../scripts/itcm_functions_includes.ld | 2 +- .../scripts/itcm_functions_includes.ld | 2 +- .../scripts/itcm_functions_includes.ld | 2 +- .../scripts/itcm_functions_includes.ld | 2 +- .../scripts/itcm_functions_includes.ld | 2 +- src/modules/sensors/sensor_params.yaml | 33 +------ src/modules/sensors/sensors.cpp | 2 +- .../sensors/test/VotedSensorsUpdateTest.cpp | 73 +++++++------- src/modules/sensors/voted_sensors_update.cpp | 97 ++----------------- src/modules/sensors/voted_sensors_update.h | 26 +---- 10 files changed, 56 insertions(+), 185 deletions(-) diff --git a/boards/agam/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld b/boards/agam/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld index 0ff723e662a..c27d1b61d3d 100644 --- a/boards/agam/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld +++ b/boards/agam/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld @@ -93,7 +93,7 @@ *(.text._ZN3Ekf18controlFusionModesERKN9estimator9imuSampleE) *(.text._ZN4cdev4CDev11poll_notifyEm) *(.text.file_vioctl) -*(.text._ZN7sensors18VotedSensorsUpdate11sensorsPollER17sensor_combined_sb) +*(.text._ZN7sensors18VotedSensorsUpdate11sensorsPollER17sensor_combined_s) *(.text.nxsig_nanosleep) *(.text.imxrt_lpspi1select) *(.text.sem_wait) diff --git a/boards/ark/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld b/boards/ark/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld index 635c6cac03f..e91e9cd1c65 100644 --- a/boards/ark/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld +++ b/boards/ark/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld @@ -88,7 +88,7 @@ *(.text._ZN3Ekf18controlFusionModesERKN9estimator9imuSampleE) *(.text._ZN4cdev4CDev11poll_notifyEm) *(.text.file_vioctl) -*(.text._ZN7sensors18VotedSensorsUpdate11sensorsPollER17sensor_combined_sb) +*(.text._ZN7sensors18VotedSensorsUpdate11sensorsPollER17sensor_combined_s) *(.text.nxsig_nanosleep) *(.text.imxrt_lpspi1select) *(.text.sem_wait) diff --git a/boards/nxp/mr-tropic/nuttx-config/scripts/itcm_functions_includes.ld b/boards/nxp/mr-tropic/nuttx-config/scripts/itcm_functions_includes.ld index c751d127af4..6f9141d6a96 100644 --- a/boards/nxp/mr-tropic/nuttx-config/scripts/itcm_functions_includes.ld +++ b/boards/nxp/mr-tropic/nuttx-config/scripts/itcm_functions_includes.ld @@ -93,7 +93,7 @@ *(.text._ZN3Ekf18controlFusionModesERKN9estimator9imuSampleE) *(.text._ZN4cdev4CDev11poll_notifyEm) *(.text.file_vioctl) -*(.text._ZN7sensors18VotedSensorsUpdate11sensorsPollER17sensor_combined_sb) +*(.text._ZN7sensors18VotedSensorsUpdate11sensorsPollER17sensor_combined_s) *(.text.nxsig_nanosleep) *(.text.sem_wait) *(.text.perf_count_interval.part.0) diff --git a/boards/nxp/tropic-community/nuttx-config/scripts/itcm_functions_includes.ld b/boards/nxp/tropic-community/nuttx-config/scripts/itcm_functions_includes.ld index a7f33f326a9..4aae975a259 100644 --- a/boards/nxp/tropic-community/nuttx-config/scripts/itcm_functions_includes.ld +++ b/boards/nxp/tropic-community/nuttx-config/scripts/itcm_functions_includes.ld @@ -93,7 +93,7 @@ *(.text._ZN3Ekf18controlFusionModesERKN9estimator9imuSampleE) *(.text._ZN4cdev4CDev11poll_notifyEm) *(.text.file_vioctl) -*(.text._ZN7sensors18VotedSensorsUpdate11sensorsPollER17sensor_combined_sb) +*(.text._ZN7sensors18VotedSensorsUpdate11sensorsPollER17sensor_combined_s) *(.text.nxsig_nanosleep) *(.text.sem_wait) *(.text.perf_count_interval.part.0) diff --git a/boards/px4/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld b/boards/px4/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld index 9d0d317faca..cba31d50340 100644 --- a/boards/px4/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld +++ b/boards/px4/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld @@ -93,7 +93,7 @@ *(.text._ZN3Ekf18controlFusionModesERKN9estimator9imuSampleE) *(.text._ZN4cdev4CDev11poll_notifyEm) *(.text.file_vioctl) -*(.text._ZN7sensors18VotedSensorsUpdate11sensorsPollER17sensor_combined_sb) +*(.text._ZN7sensors18VotedSensorsUpdate11sensorsPollER17sensor_combined_s) *(.text.nxsig_nanosleep) *(.text.imxrt_lpspi1select) *(.text.sem_wait) diff --git a/src/modules/sensors/sensor_params.yaml b/src/modules/sensors/sensor_params.yaml index 178d3ac2dc1..8be1333b2c0 100644 --- a/src/modules/sensors/sensor_params.yaml +++ b/src/modules/sensors/sensor_params.yaml @@ -192,37 +192,6 @@ parameters: values: 0: Disabled 1: Publish primary IMU selection - 2: Publish primary IMU selection, median of all IMUs when the primary is inconsistent + 2: Publish primary IMU selection, fly on the median of all IMUs default: 1 reboot_required: true - SENS_IMU_MED_RAT: - description: - short: IMU median angular rate threshold - long: Gyro differences to the mean of all gyros larger than this accumulate angular error. - type: float - default: 7.0 - unit: deg/s - SENS_IMU_MED_ANG: - description: - short: IMU median angular threshold - long: The primary gyro accumulating more angular error than this switches to the - IMU median (SENS_IMU_MODE 2). - type: float - default: 15.0 - unit: deg - SENS_IMU_MED_ACC: - description: - short: IMU median acceleration threshold - long: Accelerometer differences to the mean of all accelerometers larger than this - accumulate velocity error. - type: float - default: 1.0 - unit: m/s^2 - SENS_IMU_MED_VEL: - description: - short: IMU median velocity threshold - long: The primary accelerometer accumulating more velocity error than this switches - to the IMU median (SENS_IMU_MODE 2). - type: float - default: 2.0 - unit: m/s diff --git a/src/modules/sensors/sensors.cpp b/src/modules/sensors/sensors.cpp index a890ad0f2d6..7292031b803 100644 --- a/src/modules/sensors/sensors.cpp +++ b/src/modules/sensors/sensors.cpp @@ -570,7 +570,7 @@ void Sensors::Run() } } - _voted_sensors_update.sensorsPoll(_sensor_combined, _armed); + _voted_sensors_update.sensorsPoll(_sensor_combined); if (_sensor_combined.timestamp != _sensor_combined_prev_timestamp) { diff --git a/src/modules/sensors/test/VotedSensorsUpdateTest.cpp b/src/modules/sensors/test/VotedSensorsUpdateTest.cpp index 8b09eb95fec..d32cfaee880 100644 --- a/src/modules/sensors/test/VotedSensorsUpdateTest.cpp +++ b/src/modules/sensors/test/VotedSensorsUpdateTest.cpp @@ -204,14 +204,14 @@ public: // run the voter at the rate the sensors module polls it, feeding it the given samples template - static sensor_combined_s pollFor(VotedSensorsUpdate &voter, hrt_abstime duration, Feed feed, bool armed = false) + static sensor_combined_s pollFor(VotedSensorsUpdate &voter, hrt_abstime duration, Feed feed) { const hrt_abstime start = hrt_absolute_time(); sensor_combined_s raw{}; do { feed(); - voter.sensorsPoll(raw, armed); + voter.sensorsPoll(raw); px4_usleep(10_ms); } while (hrt_absolute_time() < start + duration); @@ -631,7 +631,7 @@ TEST_F(VotedSensorsUpdateTest, ReEnablingAnImuWhileArmedWaitsForDisarm) EXPECT_EQ(status.accel_device_ids[0], kAccelIds[0]); } -TEST_F(VotedSensorsUpdateTest, AnInconsistentPrimaryImuSwitchesToTheMedianUntilDisarm) +TEST_F(VotedSensorsUpdateTest, ImuMode2FliesOnTheMedianFromTheStart) { setParam("SENS_IMU_MODE", 2); setPriority(0, 75, 75); @@ -641,48 +641,51 @@ TEST_F(VotedSensorsUpdateTest, AnInconsistentPrimaryImuSwitchesToTheMedianUntilD ImuSubscriptionOwner owner; VotedSensorsUpdate voter(false, owner.subs); - float gyro_x_imu0 = 0.f; + // the primary gyro reads 1 rad/s off from the start, the two healthy gyros differ a little + // so that only the median gives 0.05 auto feed = [&]() { - // the two healthy gyros differ a little, so that only the median gives 0.05 - publishImu(0, 9.8f, gyro_x_imu0); + publishImu(0, 9.8f, 1.f); publishImu(1, 9.8f); publishImu(3, 9.8f, 0.05f); }; start(voter, feed); - pollFor(voter, 300_ms, feed, true); + const sensor_combined_s raw = pollFor(voter, 300_ms, feed); sensor_selection_s selection{}; - ASSERT_TRUE(readSelection(selection)); - ASSERT_EQ(selection.gyro_device_id, kGyroIds[0]); - ASSERT_FALSE(selection.imu_median) << "consistent IMUs fly on the primary"; - - // the primary gyro drifts 1 rad/s away from the other two in flight - gyro_x_imu0 = 1.f; - const sensor_combined_s raw = pollFor(voter, 1_s, feed, true); - ASSERT_TRUE(readSelection(selection)); EXPECT_TRUE(selection.imu_median); - EXPECT_EQ(selection.gyro_device_id, kGyroIds[1]) << "consumers of the selected device move to a healthy IMU"; - EXPECT_EQ(selection.accel_device_id, kAccelIds[1]); - EXPECT_NEAR(raw.gyro_rad[0], 0.05f, 0.01f) << "the median, not the selected IMU (0) nor the mean (0.35)"; - EXPECT_EQ(Peer::gyroFailoverCount(voter), 0u) << "switching to the median is not a sensor failure"; - - // the primary agrees again, but the median stays until landing - gyro_x_imu0 = 0.f; - pollFor(voter, 300_ms, feed, true); - ASSERT_TRUE(readSelection(selection)); - EXPECT_TRUE(selection.imu_median); - - pollFor(voter, 100_ms, feed); - ASSERT_TRUE(readSelection(selection)); - EXPECT_FALSE(selection.imu_median) << "disarming ends the median"; - EXPECT_EQ(selection.gyro_device_id, kGyroIds[0]) << "and the next flight starts on the configured primary"; - EXPECT_EQ(Peer::gyroPriority(voter, 0), 75); - EXPECT_EQ(Peer::accelPriority(voter, 0), 75); + EXPECT_EQ(selection.gyro_device_id, kGyroIds[0]) << "the median does not change the selected IMU"; + EXPECT_EQ(selection.accel_device_id, kAccelIds[0]); + EXPECT_NEAR(raw.gyro_rad[0], 0.05f, 0.01f) << "the median, not the selected IMU (1) nor the mean (0.35)"; + EXPECT_EQ(Peer::gyroFailoverCount(voter), 0u); } -TEST_F(VotedSensorsUpdateTest, TwoImusCannotTellWhichOneIsWrongAndKeepThePrimary) +TEST_F(VotedSensorsUpdateTest, ImuMode1FliesOnThePrimary) +{ + setPriority(0, 75, 75); + setPriority(1, 60, 60); + setPriority(3, 50, 50); + + ImuSubscriptionOwner owner; + VotedSensorsUpdate voter(false, owner.subs); + + auto feed = [&]() { + publishImu(0, 9.8f, 1.f); + publishImu(1, 9.8f); + publishImu(3, 9.8f, 0.05f); + }; + + start(voter, feed); + const sensor_combined_s raw = pollFor(voter, 300_ms, feed); + + sensor_selection_s selection{}; + ASSERT_TRUE(readSelection(selection)); + EXPECT_FALSE(selection.imu_median); + EXPECT_NEAR(raw.gyro_rad[0], 1.f, 0.01f); +} + +TEST_F(VotedSensorsUpdateTest, WithTwoImusTheMedianKeepsThePrimary) { setParam("SENS_IMU_MODE", 2); setPriority(0, 75, 75); @@ -697,10 +700,10 @@ TEST_F(VotedSensorsUpdateTest, TwoImusCannotTellWhichOneIsWrongAndKeepThePrimary }; start(voter, feed); - pollFor(voter, 1_s, feed, true); + const sensor_combined_s raw = pollFor(voter, 300_ms, feed); sensor_selection_s selection{}; ASSERT_TRUE(readSelection(selection)); - EXPECT_FALSE(selection.imu_median); EXPECT_EQ(selection.gyro_device_id, kGyroIds[0]); + EXPECT_NEAR(raw.gyro_rad[0], 1.f, 0.01f) << "two IMUs have no median, the primary is used"; } diff --git a/src/modules/sensors/voted_sensors_update.cpp b/src/modules/sensors/voted_sensors_update.cpp index 6c0fe6676dc..4e407cbdb21 100644 --- a/src/modules/sensors/voted_sensors_update.cpp +++ b/src/modules/sensors/voted_sensors_update.cpp @@ -80,6 +80,14 @@ void VotedSensorsUpdate::parametersUpdate(bool armed) // the voter's own parameters are not read in flight, the priorities below are if (!armed) { updateParams(); + + // SENS_IMU_MODE 2 flies on the IMU median from the start, it never switches in flight + const bool imu_median = (_param_sens_imu_mode.get() == 2); + + if (_selection.imu_median != imu_median) { + _selection.imu_median = imu_median; + _selection_changed = true; + } } // run through all IMUs @@ -453,7 +461,7 @@ void VotedSensorsUpdate::printStatus() _accel.voter.print(); } -void VotedSensorsUpdate::sensorsPoll(sensor_combined_s &raw, bool armed) +void VotedSensorsUpdate::sensorsPoll(sensor_combined_s &raw) { imuPoll(raw); @@ -485,10 +493,6 @@ void VotedSensorsUpdate::sensorsPoll(sensor_combined_s &raw, bool armed) } } - if (_param_sens_imu_mode.get() == 2) { - updateImuMedian(status, armed); - } - status.timestamp = hrt_absolute_time(); _sensors_status_imu_pub.publish(status); @@ -498,89 +502,6 @@ void VotedSensorsUpdate::sensorsPoll(sensor_combined_s &raw, bool armed) } } -int VotedSensorsUpdate::accumulateInconsistency(const float inconsistency[], const uint32_t device_ids[], - float threshold, float limit, float dt, float accumulated[]) -{ - // accumulate how far each sensor stays beyond the threshold, so a steady drift is flagged and a spike is not - int count = 0; - int worst = 0; - - for (int i = 0; i < MAX_SENSOR_COUNT; i++) { - if (device_ids[i] != 0) { - count++; - accumulated[i] = fmaxf(accumulated[i] + (inconsistency[i] - threshold) * dt, 0.f); - - } else { - accumulated[i] = 0.f; - } - - if (accumulated[i] > accumulated[worst]) { - worst = i; - } - } - - // with fewer than three sensors the inconsistent one cannot be told apart - return ((count >= 3) && (accumulated[worst] > limit)) ? worst : -1; -} - -void VotedSensorsUpdate::updateImuMedian(const sensors_status_imu_s &status, bool armed) -{ - const hrt_abstime now = hrt_absolute_time(); - const float dt = math::constrain((now - _median_check_last) * 1e-6f, 0.f, 0.02f); - _median_check_last = now; - - if (!armed) { - if (_selection.imu_median) { - // the next flight starts on the IMUs as configured, without re-enabling one disabled in flight - for (const uint8_t index : _median_demoted) { - if (_accel.priority[index] > 0) { _accel.priority[index] = _accel.priority_configured[index]; } - - if (_gyro.priority[index] > 0) { _gyro.priority[index] = _gyro.priority_configured[index]; } - } - - _selection.imu_median = false; - _selection_changed = true; - } - - memset(_accel_median_error, 0, sizeof(_accel_median_error)); - memset(_gyro_median_error, 0, sizeof(_gyro_median_error)); - return; - } - - const int accel_inconsistent = accumulateInconsistency(status.accel_inconsistency_m_s_s, status.accel_device_ids, - _param_sens_imu_med_acc.get(), _param_sens_imu_med_vel.get(), dt, _accel_median_error); - const int gyro_inconsistent = accumulateInconsistency(status.gyro_inconsistency_rad_s, status.gyro_device_ids, - math::radians(_param_sens_imu_med_rat.get()), math::radians(_param_sens_imu_med_ang.get()), dt, _gyro_median_error); - - const bool accel_primary = (accel_inconsistent >= 0) && (accel_inconsistent == _accel.last_best_vote); - const bool gyro_primary = (gyro_inconsistent >= 0) && (gyro_inconsistent == _gyro.last_best_vote); - - if (!_selection.imu_median && (accel_primary || gyro_primary)) { - // also select other IMUs for everything that reads the selected IMU itself - _median_demoted[0] = accel_primary ? accel_inconsistent : gyro_inconsistent; - _median_demoted[1] = gyro_primary ? gyro_inconsistent : accel_inconsistent; - - for (const uint8_t index : _median_demoted) { - _accel.priority[index] = 1; - _gyro.priority[index] = 1; - } - - _selection.imu_median = true; - _selection_changed = true; - - /* EVENT - * @description - * The primary IMU disagrees with the other IMUs. Rate control and estimation use the median of all IMUs until disarm. - * - * - * This check can be configured via SENS_IMU_MODE parameter. - * - */ - events::send(events::ID("sensor_imu_median"), events::Log::Critical, - "IMU {1} inconsistent, using the median of all IMUs", _median_demoted[1]); - } -} - void VotedSensorsUpdate::setRelativeTimestamps(sensor_combined_s &raw) { if (_last_accel_timestamp[_accel.last_best_vote]) { diff --git a/src/modules/sensors/voted_sensors_update.h b/src/modules/sensors/voted_sensors_update.h index 6d0e04fe197..dd3d8b4a89c 100644 --- a/src/modules/sensors/voted_sensors_update.h +++ b/src/modules/sensors/voted_sensors_update.h @@ -95,9 +95,8 @@ public: /** * read new sensor data - * @param armed the IMU median is only switched to in flight and ends on disarm */ - void sensorsPoll(sensor_combined_s &raw, bool armed = false); + void sensorsPoll(sensor_combined_s &raw); /** * set the relative timestamps of each sensor timestamp, based on the last sensorsPoll, @@ -164,18 +163,6 @@ private: */ void calcGyroInconsistency(); - /** - * Accumulate the inconsistency of each sensor of one type beyond threshold - * @return index of the sensor that accumulated the most and more than limit out of at least three, -1 otherwise - */ - static int accumulateInconsistency(const float inconsistency[], const uint32_t device_ids[], float threshold, - float limit, float dt, float accumulated[]); - - /** - * Switch the IMU outputs to the median of all IMUs once the primary is the inconsistent one (SENS_IMU_MODE 2) - */ - void updateImuMedian(const sensors_status_imu_s &status, bool armed); - SensorData _accel{ORB_ID::sensor_accel}; SensorData _gyro{ORB_ID::sensor_gyro}; @@ -201,11 +188,6 @@ private: matrix::Vector3f _accel_diff[MAX_SENSOR_COUNT] {}; /**< filtered accel differences between IMU units (m/s/s) */ matrix::Vector3f _gyro_diff[MAX_SENSOR_COUNT] {}; /**< filtered gyro differences between IMU uinits (rad/s) */ - float _accel_median_error[MAX_SENSOR_COUNT] {}; /**< accel inconsistency beyond SENS_IMU_MED_ACC accumulated in flight (m/s) */ - float _gyro_median_error[MAX_SENSOR_COUNT] {}; /**< gyro inconsistency beyond SENS_IMU_MED_RAT accumulated in flight (rad) */ - uint8_t _median_demoted[2] {}; /**< IMUs demoted for the median, restored on disarm */ - hrt_abstime _median_check_last{0}; - uint32_t _accel_device_id[MAX_SENSOR_COUNT] {}; /**< accel driver device id for each uorb instance */ uint32_t _gyro_device_id[MAX_SENSOR_COUNT] {}; /**< gyro driver device id for each uorb instance */ @@ -216,11 +198,7 @@ private: bool _parameter_update{false}; DEFINE_PARAMETERS( - (ParamInt) _param_sens_imu_mode, - (ParamFloat) _param_sens_imu_med_rat, - (ParamFloat) _param_sens_imu_med_ang, - (ParamFloat) _param_sens_imu_med_acc, - (ParamFloat) _param_sens_imu_med_vel + (ParamInt) _param_sens_imu_mode ) };