From 59ece7d571a83463cda0a076ea0bb10883786d9b Mon Sep 17 00:00:00 2001 From: Andrea Bernasconi Date: Mon, 9 Nov 2026 15:40:04 +0100 Subject: [PATCH] feat(sensors): filter every gyro at the FIFO rate with SENS_IMU_MODE 2 Taking the median over sensor_gyro moved the rate controller off the FIFO path, so the gyro filters ran at IMU_GYRO_RATEMAX instead of the raw rate: notches above Nyquist and the FFT notch were lost and vibration aliased into the control band. Instead run a VehicleAngularVelocity instance per IMU, each with the full filter chain on its own FIFO, and publish vehicle_angular_velocity from the instance of the selected gyro as the per-axis median of all filtered outputs. On failover the next instance takes over with warm filters. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Andrea Bernasconi --- msg/versioned/VehicleAngularVelocity.msg | 2 +- src/modules/sensors/sensors.cpp | 40 ++++++- src/modules/sensors/sensors.hpp | 3 +- .../VehicleAngularVelocity.cpp | 102 +++++++++++++----- .../VehicleAngularVelocity.hpp | 23 +++- 5 files changed, 133 insertions(+), 37 deletions(-) diff --git a/msg/versioned/VehicleAngularVelocity.msg b/msg/versioned/VehicleAngularVelocity.msg index 6c91f3c0703..8d0defed697 100644 --- a/msg/versioned/VehicleAngularVelocity.msg +++ b/msg/versioned/VehicleAngularVelocity.msg @@ -7,4 +7,4 @@ float32[3] xyz # Bias corrected angular velocity about the FRD body frame XYZ float32[3] xyz_derivative # angular acceleration about the FRD body frame XYZ-axis in rad/s^2 -# TOPICS vehicle_angular_velocity vehicle_angular_velocity_groundtruth +# TOPICS vehicle_angular_velocity vehicle_angular_velocity_groundtruth vehicle_angular_velocity_imu diff --git a/src/modules/sensors/sensors.cpp b/src/modules/sensors/sensors.cpp index 7292031b803..34e8ca7be16 100644 --- a/src/modules/sensors/sensors.cpp +++ b/src/modules/sensors/sensors.cpp @@ -65,7 +65,12 @@ Sensors::Sensors(bool hil_enabled) : #endif // CONFIG_SENSORS_VEHICLE_AIRSPEED #if defined(CONFIG_SENSORS_VEHICLE_ANGULAR_VELOCITY) - _vehicle_angular_velocity.Start(); + + // SENS_IMU_MODE 2 filters every gyro instead, see InitializeVehicleIMU() + if (_param_sens_imu_mode.get() != 2) { + _vehicle_angular_velocity.Start(); + } + #endif // CONFIG_SENSORS_VEHICLE_ANGULAR_VELOCITY #if defined(CONFIG_SENSORS_VEHICLE_OPTICAL_FLOW) @@ -99,6 +104,11 @@ Sensors::~Sensors() #if defined(CONFIG_SENSORS_VEHICLE_ANGULAR_VELOCITY) _vehicle_angular_velocity.Stop(); + + for (auto &angular_velocity : _vehicle_angular_velocity_imu_list) { + delete angular_velocity; // stops itself + } + #endif // CONFIG_SENSORS_VEHICLE_ANGULAR_VELOCITY #if defined(CONFIG_SENSORS_VEHICLE_AIR_DATA) @@ -408,6 +418,19 @@ void Sensors::InitializeVehicleIMU() if (imu->Start()) { _vehicle_imu_list[i] = imu; +#if defined(CONFIG_SENSORS_VEHICLE_ANGULAR_VELOCITY) + + // SENS_IMU_MODE 2 runs the full gyro filter chain on every IMU and flies on their median + if (_param_sens_imu_mode.get() == 2) { + _vehicle_angular_velocity_imu_list[i] = new VehicleAngularVelocity(i); + + if (_vehicle_angular_velocity_imu_list[i] != nullptr) { + _vehicle_angular_velocity_imu_list[i]->Start(); + } + } + +#endif // CONFIG_SENSORS_VEHICLE_ANGULAR_VELOCITY + } else { delete imu; } @@ -692,8 +715,19 @@ int Sensors::print_status() #endif // CONFIG_SENSORS_VEHICLE_ACCELERATION #if defined(CONFIG_SENSORS_VEHICLE_ANGULAR_VELOCITY) - PX4_INFO_RAW("\n"); - _vehicle_angular_velocity.PrintStatus(); + + if (_param_sens_imu_mode.get() != 2) { + PX4_INFO_RAW("\n"); + _vehicle_angular_velocity.PrintStatus(); + } + + for (auto &angular_velocity : _vehicle_angular_velocity_imu_list) { + if (angular_velocity != nullptr) { + PX4_INFO_RAW("\n"); + angular_velocity->PrintStatus(); + } + } + #endif // CONFIG_SENSORS_VEHICLE_ANGULAR_VELOCITY #if defined(CONFIG_SENSORS_VEHICLE_GPS_POSITION) diff --git a/src/modules/sensors/sensors.hpp b/src/modules/sensors/sensors.hpp index be7fafb385f..0b996d1cbce 100644 --- a/src/modules/sensors/sensors.hpp +++ b/src/modules/sensors/sensors.hpp @@ -199,6 +199,7 @@ private: #if defined(CONFIG_SENSORS_VEHICLE_ANGULAR_VELOCITY) VehicleAngularVelocity _vehicle_angular_velocity; + VehicleAngularVelocity *_vehicle_angular_velocity_imu_list[MAX_SENSOR_COUNT] {}; // SENS_IMU_MODE 2 #endif // CONFIG_SENSORS_VEHICLE_ANGULAR_VELOCITY #if defined(CONFIG_SENSORS_VEHICLE_MAGNETOMETER) @@ -228,6 +229,6 @@ private: #if defined(CONFIG_SENSORS_VEHICLE_MAGNETOMETER) (ParamInt) _param_sys_has_mag, #endif // CONFIG_SENSORS_VEHICLE_MAGNETOMETER - (ParamBool) _param_sens_imu_mode + (ParamInt) _param_sens_imu_mode ) }; diff --git a/src/modules/sensors/vehicle_angular_velocity/VehicleAngularVelocity.cpp b/src/modules/sensors/vehicle_angular_velocity/VehicleAngularVelocity.cpp index 55833b64c7a..e5cc1c25e6c 100644 --- a/src/modules/sensors/vehicle_angular_velocity/VehicleAngularVelocity.cpp +++ b/src/modules/sensors/vehicle_angular_velocity/VehicleAngularVelocity.cpp @@ -42,11 +42,16 @@ using namespace matrix; namespace sensors { -VehicleAngularVelocity::VehicleAngularVelocity() : +VehicleAngularVelocity::VehicleAngularVelocity(int8_t imu_instance) : ModuleParams(nullptr), - ScheduledWorkItem(MODULE_NAME, px4::wq_configurations::rate_ctrl) + ScheduledWorkItem(MODULE_NAME, px4::wq_configurations::rate_ctrl), + _imu_instance(imu_instance) { _vehicle_angular_velocity_pub.advertise(); + + if (_imu_instance >= 0) { + _vehicle_angular_velocity_imu_pub.advertise(); + } } VehicleAngularVelocity::~VehicleAngularVelocity() @@ -106,8 +111,7 @@ bool VehicleAngularVelocity::UpdateSampleRate() uORB::SubscriptionData imu_status{ORB_ID(vehicle_imu_status), i}; if (imu_status.get().gyro_device_id == _selected_sensor_device_id) { - // sensor_gyro carries one sample per publication, sensor_gyro_fifo every raw sample - sample_rate_hz = _fifo_available ? imu_status.get().gyro_raw_rate_hz : imu_status.get().gyro_rate_hz; + sample_rate_hz = imu_status.get().gyro_raw_rate_hz; publish_rate_hz = imu_status.get().gyro_rate_hz; break; } @@ -260,15 +264,23 @@ bool VehicleAngularVelocity::SensorSelectionUpdate(const hrt_abstime &time_now_u device_id = device_id_first_valid_imu; } - const bool imu_median_changed = (sensor_selection.imu_median != _imu_median); - _imu_median = sensor_selection.imu_median; + _sensor_selection_gyro_device_id = sensor_selection.gyro_device_id; - if ((_selected_sensor_device_id != device_id) || imu_median_changed || force) { + if (_imu_instance >= 0) { + // always filter the same gyro, the selection only decides which instance publishes the median + device_id = uORB::SubscriptionData {ORB_ID(sensor_gyro), (uint8_t)_imu_instance} .get().device_id; + + if (device_id == 0) { + return false; + } + } + + if ((_selected_sensor_device_id != device_id) || force) { const bool device_id_valid = (device_id != 0); - // see if the selected sensor publishes sensor_gyro_fifo, the median is taken over sensor_gyro - for (uint8_t i = 0; !_imu_median && (i < MAX_SENSOR_COUNT); i++) { + // see if the selected sensor publishes sensor_gyro_fifo + for (uint8_t i = 0; i < MAX_SENSOR_COUNT; i++) { uORB::SubscriptionData sensor_gyro_fifo_sub{ORB_ID(sensor_gyro_fifo), i}; if (sensor_gyro_fifo_sub.advertised() @@ -380,7 +392,6 @@ void VehicleAngularVelocity::ParametersUpdate(bool force) updateParams(); _calibration.ParametersUpdate(); - _median.ParametersUpdate(); // IMU_GYRO_RATEMAX if (_param_imu_gyro_ratemax.get() <= 0) { @@ -691,7 +702,7 @@ void VehicleAngularVelocity::UpdateDynamicNotchFFT(const hrt_abstime &time_now_u sensor_gyro_fft_s sensor_gyro_fft; if (_sensor_gyro_fft_sub.copy(&sensor_gyro_fft) - && (sensor_gyro_fft.device_id == _selected_sensor_device_id) + && ((sensor_gyro_fft.device_id == _selected_sensor_device_id) || (_imu_instance >= 0)) // FFT of the selected gyro && (time_now_us < sensor_gyro_fft.timestamp + DYNAMIC_NOTCH_FITLER_TIMEOUT) && ((fabsf(sensor_gyro_fft.sensor_sample_rate_hz - _filter_sample_rate_hz) / _filter_sample_rate_hz) < 0.02f)) { @@ -895,14 +906,7 @@ void VehicleAngularVelocity::Run() Vector3f angular_velocity_uncalibrated; Vector3f angular_acceleration_uncalibrated; - Vector3f raw{sensor_data.x, sensor_data.y, sensor_data.z}; - - if (_imu_median) { - // the filters run on raw data of the selected sensor, so feed them the median in that form - raw = _calibration.Uncorrect(_median.Apply(sensor_data, _calibration.Correct(raw))); - } - - float raw_data_array[] {raw(0), raw(1), raw(2)}; + float raw_data_array[] {sensor_data.x, sensor_data.y, sensor_data.z}; for (int axis = 0; axis < 3; axis++) { // copy sensor sample to float array for filtering @@ -941,20 +945,41 @@ bool VehicleAngularVelocity::CalibrateAndPublish(const hrt_abstime ×tamp_sa { if (timestamp_sample >= _last_publish + _publish_interval_min_us) { - // Publish vehicle_angular_velocity vehicle_angular_velocity_s angular_velocity; angular_velocity.timestamp_sample = timestamp_sample; - // Angular velocity: rotate sensor frame to board, scale raw data to SI, apply calibration, and remove in-run estimated bias - _angular_velocity = _calibration.Correct(angular_velocity_uncalibrated) - _bias; - _angular_velocity.copyTo(angular_velocity.xyz); + // Angular velocity: rotate sensor frame to board, scale raw data to SI, apply calibration + Vector3f xyz{_calibration.Correct(angular_velocity_uncalibrated)}; // Angular acceleration: rotate sensor frame to board, scale raw data to SI, apply any additional configured rotation - _angular_acceleration = _calibration.rotation() * angular_acceleration_uncalibrated; - _angular_acceleration.copyTo(angular_velocity.xyz_derivative); + Vector3f xyz_derivative = _calibration.rotation() * angular_acceleration_uncalibrated; - angular_velocity.timestamp = hrt_absolute_time(); - _vehicle_angular_velocity_pub.publish(angular_velocity); + bool publish = true; + + if (_imu_instance >= 0) { + xyz.copyTo(angular_velocity.xyz); + xyz_derivative.copyTo(angular_velocity.xyz_derivative); + angular_velocity.timestamp = hrt_absolute_time(); + _vehicle_angular_velocity_imu_pub.publish(angular_velocity); + + // only the instance of the selected gyro publishes, the median of all IMUs + publish = (_selected_sensor_device_id == _sensor_selection_gyro_device_id); + + if (publish) { + ApplyImuMedian(timestamp_sample, xyz, xyz_derivative); + } + } + + // remove in-run estimated bias + _angular_velocity = xyz - _bias; + _angular_acceleration = xyz_derivative; + + if (publish) { + _angular_velocity.copyTo(angular_velocity.xyz); + _angular_acceleration.copyTo(angular_velocity.xyz_derivative); + angular_velocity.timestamp = hrt_absolute_time(); + _vehicle_angular_velocity_pub.publish(angular_velocity); + } // shift last publish time forward, but don't let it get further behind than the interval _last_publish = math::constrain(_last_publish + _publish_interval_min_us, @@ -966,11 +991,32 @@ bool VehicleAngularVelocity::CalibrateAndPublish(const hrt_abstime ×tamp_sa return false; } +void VehicleAngularVelocity::ApplyImuMedian(const hrt_abstime ×tamp_sample, Vector3f &xyz, Vector3f &xyz_derivative) +{ + // latest output of two other recent IMUs + vehicle_angular_velocity_s imu[2]; + int count = 0; + + for (int i = 0; (i < MAX_SENSOR_COUNT) && (count < 2); i++) { + if ((i != _vehicle_angular_velocity_imu_pub.get_instance()) && _vehicle_angular_velocity_imu_sub[i].copy(&imu[count]) + && (timestamp_sample < imu[count].timestamp_sample + IMU_MEDIAN_SAMPLE_TIMEOUT_US)) { + count++; + } + } + + auto median = [](float a, float b, float c) { return math::max(math::min(a, b), math::min(math::max(a, b), c)); }; + + for (int axis = 0; (count == 2) && (axis < 3); axis++) { + xyz(axis) = median(xyz(axis), imu[0].xyz[axis], imu[1].xyz[axis]); + xyz_derivative(axis) = median(xyz_derivative(axis), imu[0].xyz_derivative[axis], imu[1].xyz_derivative[axis]); + } +} + void VehicleAngularVelocity::PrintStatus() { PX4_INFO_RAW("[vehicle_angular_velocity] selected sensor: %" PRIu32 ", rate: %.1f Hz %s, estimated bias: [%.5f %.5f %.5f]\n", - _calibration.device_id(), (double)_filter_sample_rate_hz, _fifo_available ? "FIFO" : (_imu_median ? "median" : ""), + _calibration.device_id(), (double)_filter_sample_rate_hz, _fifo_available ? "FIFO" : "", (double)_bias(0), (double)_bias(1), (double)_bias(2)); _calibration.PrintStatus(); diff --git a/src/modules/sensors/vehicle_angular_velocity/VehicleAngularVelocity.hpp b/src/modules/sensors/vehicle_angular_velocity/VehicleAngularVelocity.hpp index 3dfdd6fd90b..955b95dcf9c 100644 --- a/src/modules/sensors/vehicle_angular_velocity/VehicleAngularVelocity.hpp +++ b/src/modules/sensors/vehicle_angular_velocity/VehicleAngularVelocity.hpp @@ -46,6 +46,7 @@ #include #include #include +#include #include #include #include @@ -66,7 +67,11 @@ namespace sensors class VehicleAngularVelocity : public ModuleParams, public px4::ScheduledWorkItem { public: - VehicleAngularVelocity(); + /** + * @param imu_instance -1 to filter the gyro in sensor_selection, otherwise the sensor_gyro instance to always filter, + * publishing vehicle_angular_velocity as the median of all IMUs while that gyro is selected + */ + explicit VehicleAngularVelocity(int8_t imu_instance = -1); ~VehicleAngularVelocity() override; void PrintStatus(); @@ -79,6 +84,8 @@ private: bool CalibrateAndPublish(const hrt_abstime ×tamp_sample, const matrix::Vector3f &angular_velocity_uncalibrated, const matrix::Vector3f &angular_acceleration_uncalibrated); + void ApplyImuMedian(const hrt_abstime ×tamp_sample, matrix::Vector3f &xyz, matrix::Vector3f &xyz_derivative); + inline float FilterAngularVelocity(int axis, float data[], int N = 1); inline float FilterAngularAcceleration(int axis, float inverse_dt_s, float data[], int N = 1); @@ -100,6 +107,14 @@ private: static constexpr int MAX_SENSOR_COUNT = 4; uORB::Publication _vehicle_angular_velocity_pub{ORB_ID(vehicle_angular_velocity)}; + uORB::PublicationMulti _vehicle_angular_velocity_imu_pub{ORB_ID(vehicle_angular_velocity_imu)}; + + uORB::Subscription _vehicle_angular_velocity_imu_sub[MAX_SENSOR_COUNT] { + {ORB_ID(vehicle_angular_velocity_imu), 0}, + {ORB_ID(vehicle_angular_velocity_imu), 1}, + {ORB_ID(vehicle_angular_velocity_imu), 2}, + {ORB_ID(vehicle_angular_velocity_imu), 3} + }; uORB::Subscription _estimator_selector_status_sub{ORB_ID(estimator_selector_status)}; uORB::Subscription _estimator_sensor_bias_sub{ORB_ID(estimator_sensor_bias)}; @@ -116,8 +131,6 @@ private: calibration::Gyroscope _calibration{}; - SensorMedian _median{ORB_ID::sensor_gyro}; - matrix::Vector3f _bias{}; matrix::Vector3f _angular_velocity{}; @@ -179,11 +192,13 @@ private: // angular acceleration filter AlphaFilter _lp_filter_acceleration[3] {}; + const int8_t _imu_instance; + uint32_t _selected_sensor_device_id{0}; + uint32_t _sensor_selection_gyro_device_id{0}; bool _reset_filters{true}; bool _fifo_available{false}; - bool _imu_median{false}; bool _update_sample_rate{true}; perf_counter_t _cycle_perf{perf_alloc(PC_ELAPSED, MODULE_NAME": gyro filter")};