diff --git a/src/modules/sensors/vehicle_angular_velocity/VehicleAngularVelocity.cpp b/src/modules/sensors/vehicle_angular_velocity/VehicleAngularVelocity.cpp index 5cd9d6edcbd..aea1b3d5c60 100644 --- a/src/modules/sensors/vehicle_angular_velocity/VehicleAngularVelocity.cpp +++ b/src/modules/sensors/vehicle_angular_velocity/VehicleAngularVelocity.cpp @@ -172,15 +172,12 @@ void VehicleAngularVelocity::ResetFilters(const hrt_abstime &time_now_us) _lp_filter_velocity[axis].set_cutoff_frequency(_filter_sample_rate_hz, _param_imu_gyro_cutoff.get()); _lp_filter_velocity[axis].reset(angular_velocity_uncalibrated(axis)); - // angular velocity notch 0 - _notch_filter0_velocity[axis].setParameters(_filter_sample_rate_hz, _param_imu_gyro_nf0_frq.get(), - _param_imu_gyro_nf0_bw.get()); - _notch_filter0_velocity[axis].reset(); - - // angular velocity notch 1 - _notch_filter1_velocity[axis].setParameters(_filter_sample_rate_hz, _param_imu_gyro_nf1_frq.get(), - _param_imu_gyro_nf1_bw.get()); - _notch_filter1_velocity[axis].reset(); + // angular velocity notches + for (int i = 0; i < NUM_NOTCH_FILTERS; i++) { + _notch_filter_velocity[i][axis].setParameters(_filter_sample_rate_hz, _notch_filter_freq_param[i], + _notch_filter_bw_param[i]); + _notch_filter_velocity[i][axis].reset(); + } // angular acceleration low pass if ((_param_imu_dgyro_cutoff.get() > 0.f) @@ -376,14 +373,8 @@ void VehicleAngularVelocity::ParametersUpdate(bool force) parameter_update_s param_update; _parameter_update_sub.copy(¶m_update); - const bool nf0_enabled_prev = (_param_imu_gyro_nf0_frq.get() > 0.f) && (_param_imu_gyro_nf0_bw.get() > 0.f); - const bool nf1_enabled_prev = (_param_imu_gyro_nf1_frq.get() > 0.f) && (_param_imu_gyro_nf1_bw.get() > 0.f); - updateParams(); - const bool nf0_enabled = (_param_imu_gyro_nf0_frq.get() > 0.f) && (_param_imu_gyro_nf0_bw.get() > 0.f); - const bool nf1_enabled = (_param_imu_gyro_nf1_frq.get() > 0.f) && (_param_imu_gyro_nf1_bw.get() > 0.f); - _calibration.ParametersUpdate(); // IMU_GYRO_RATEMAX @@ -411,25 +402,35 @@ void VehicleAngularVelocity::ParametersUpdate(bool force) } } - // gyro notch filter 0 frequency or bandwidth changed - for (auto &nf : _notch_filter0_velocity) { - const bool nf_freq_changed = (fabsf(nf.getNotchFreq() - _param_imu_gyro_nf0_frq.get()) > 0.01f); - const bool nf_bw_changed = (fabsf(nf.getBandwidth() - _param_imu_gyro_nf0_bw.get()) > 0.01f); + // gyro notch filter frequency or bandwidth changed + static constexpr FloatParamInstances nf_freq_params{"IMU_GYRO_NF", "_FRQ"}; + static constexpr FloatParamInstances nf_bw_params{"IMU_GYRO_NF", "_BW"}; - if ((nf0_enabled_prev != nf0_enabled) || (nf0_enabled && (nf_freq_changed || nf_bw_changed))) { - _reset_filters = true; - break; - } - } + static_assert(nf_freq_params.allFound() && nf_bw_params.allFound(), + "NUM_NOTCH_FILTERS larger than num_notch_filters in imu_gyro_parameters.yaml"); + static_assert(findFloatParamInstance("IMU_GYRO_NF", NUM_NOTCH_FILTERS, "_FRQ") == PARAM_INVALID, + "NUM_NOTCH_FILTERS smaller than num_notch_filters in imu_gyro_parameters.yaml"); - // gyro notch filter 1 frequency or bandwidth changed - for (auto &nf : _notch_filter1_velocity) { - const bool nf_freq_changed = (fabsf(nf.getNotchFreq() - _param_imu_gyro_nf1_frq.get()) > 0.01f); - const bool nf_bw_changed = (fabsf(nf.getBandwidth() - _param_imu_gyro_nf1_bw.get()) > 0.01f); + for (int i = 0; i < NUM_NOTCH_FILTERS; i++) { + float nf_freq = 0.f; + float nf_bw = 0.f; + param_get_mark_used(nf_freq_params.handle[i], &nf_freq); + param_get_mark_used(nf_bw_params.handle[i], &nf_bw); - if ((nf1_enabled_prev != nf1_enabled) || (nf1_enabled && (nf_freq_changed || nf_bw_changed))) { - _reset_filters = true; - break; + const bool nf_enabled_prev = (_notch_filter_freq_param[i] > 0.f) && (_notch_filter_bw_param[i] > 0.f); + const bool nf_enabled = (nf_freq > 0.f) && (nf_bw > 0.f); + + _notch_filter_freq_param[i] = nf_freq; + _notch_filter_bw_param[i] = nf_bw; + + for (auto &nf : _notch_filter_velocity[i]) { + const bool nf_freq_changed = (fabsf(nf.getNotchFreq() - nf_freq) > 0.01f); + const bool nf_bw_changed = (fabsf(nf.getBandwidth() - nf_bw) > 0.01f); + + if ((nf_enabled_prev != nf_enabled) || (nf_enabled && (nf_freq_changed || nf_bw_changed))) { + _reset_filters = true; + break; + } } } @@ -757,14 +758,11 @@ float VehicleAngularVelocity::FilterAngularVelocity(int axis, float data[], int #endif // !CONSTRAINED_FLASH - // Apply general notch filter 0 (IMU_GYRO_NF0_FRQ) - if (_notch_filter0_velocity[axis].getNotchFreq() > 0.f) { - _notch_filter0_velocity[axis].applyArray(data, N); - } - - // Apply general notch filter 1 (IMU_GYRO_NF1_FRQ) - if (_notch_filter1_velocity[axis].getNotchFreq() > 0.f) { - _notch_filter1_velocity[axis].applyArray(data, N); + // Apply general notch filters (IMU_GYRO_NF${i}_FRQ) + for (auto &nf : _notch_filter_velocity) { + if (nf[axis].getNotchFreq() > 0.f) { + nf[axis].applyArray(data, N); + } } // Apply general low-pass filter (IMU_GYRO_CUTOFF) diff --git a/src/modules/sensors/vehicle_angular_velocity/VehicleAngularVelocity.hpp b/src/modules/sensors/vehicle_angular_velocity/VehicleAngularVelocity.hpp index 8aae8a3c03e..a887b61868c 100644 --- a/src/modules/sensors/vehicle_angular_velocity/VehicleAngularVelocity.hpp +++ b/src/modules/sensors/vehicle_angular_velocity/VehicleAngularVelocity.hpp @@ -1,6 +1,6 @@ /**************************************************************************** * - * Copyright (c) 2019-2022 PX4 Development Team. All rights reserved. + * Copyright (c) 2019-2026 PX4 Development Team. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -130,8 +130,13 @@ private: // angular velocity filters math::LowPassFilter2p _lp_filter_velocity[3] {}; - math::NotchFilter _notch_filter0_velocity[3] {}; - math::NotchFilter _notch_filter1_velocity[3] {}; + + // static notch filters (IMU_GYRO_NF${i}_*), has to match num_notch_filters in imu_gyro_parameters.yaml + static constexpr int NUM_NOTCH_FILTERS = 2; + + math::NotchFilter _notch_filter_velocity[NUM_NOTCH_FILTERS][3] {}; + float _notch_filter_freq_param[NUM_NOTCH_FILTERS] {}; + float _notch_filter_bw_param[NUM_NOTCH_FILTERS] {}; #if !defined(CONSTRAINED_FLASH) @@ -189,13 +194,74 @@ private: (ParamFloat) _param_imu_gyro_dnf_min, #endif // !CONSTRAINED_FLASH (ParamFloat) _param_imu_gyro_cutoff, - (ParamFloat) _param_imu_gyro_nf0_frq, - (ParamFloat) _param_imu_gyro_nf0_bw, - (ParamFloat) _param_imu_gyro_nf1_frq, - (ParamFloat) _param_imu_gyro_nf1_bw, (ParamInt) _param_imu_gyro_ratemax, (ParamFloat) _param_imu_dgyro_cutoff ) }; +/* + * Compile time parameter lookup helpers + * + * Instanced parameters like IMU_GYRO_NF${i}_FRQ are generated from imu_gyro_parameters.yaml and can't be listed in + * DEFINE_PARAMETERS() by index. These helpers search the generated px4::parameters list by name at compile time to get + * the parameter handles and allow static_asserts that the number of instances in the yaml matches NUM_NOTCH_FILTERS. + * Nothing of this is evaluated at runtime. + */ + +// returns the remainder of str after prefix, nullptr if str doesn't start with prefix +constexpr const char *skipPrefix(const char *str, const char *prefix) +{ + while (*prefix != '\0') { + if (*str++ != *prefix++) { + return nullptr; + } + } + + return str; +} + +// compile time lookup of the float parameter (single digit instance), PARAM_INVALID if it doesn't exist +constexpr param_t findFloatParamInstance(const char *prefix, int instance, const char *suffix) +{ + constexpr param_t param_count = sizeof(px4::parameters) / sizeof(px4::parameters[0]); + + for (param_t handle = 0; handle < param_count; handle++) { + const char *rest = skipPrefix(px4::parameters[handle].name, prefix); + + if ((instance >= 0) && (instance <= 9) && rest && (*rest == '0' + instance)) { + rest = skipPrefix(rest + 1, suffix); + + if (rest && (*rest == '\0') && (px4::parameters_type[handle] == PARAM_TYPE_FLOAT)) { + return handle; + } + } + } + + return PARAM_INVALID; +} + +// handles of the float parameters 0 .. N-1, looked up at compile time +template +struct FloatParamInstances { + constexpr FloatParamInstances(const char *prefix, const char *suffix) + { + for (int i = 0; i < N; i++) { + handle[i] = findFloatParamInstance(prefix, i, suffix); + } + } + + constexpr bool allFound() const + { + for (int i = 0; i < N; i++) { + if (handle[i] == PARAM_INVALID) { + return false; + } + } + + return true; + } + + param_t handle[N] {}; +}; + } // namespace sensors diff --git a/src/modules/sensors/vehicle_angular_velocity/imu_gyro_parameters.yaml b/src/modules/sensors/vehicle_angular_velocity/imu_gyro_parameters.yaml index 9b07202eb72..633585425cd 100644 --- a/src/modules/sensors/vehicle_angular_velocity/imu_gyro_parameters.yaml +++ b/src/modules/sensors/vehicle_angular_velocity/imu_gyro_parameters.yaml @@ -1,16 +1,19 @@ +# number of static gyro notch filters, has to match NUM_NOTCH_FILTERS in VehicleAngularVelocity.hpp +__num_notch_filters: &num_notch_filters 2 + module_name: vehicle_angular_velocity parameters: - group: Sensors definitions: - IMU_GYRO_NF0_FRQ: + IMU_GYRO_NF${i}_FRQ: description: - short: Notch filter frequency for gyro + short: Notch filter ${i} frequency for gyro long: |- The center frequency for the 2nd order notch filter on the primary gyro. This filter can be enabled to avoid feedback amplification of structural resonances at a specific frequency. This only affects the signal sent to the controllers, not the estimators. Applies to both angular velocity and angular acceleration sent to the controllers. - See "IMU_GYRO_NF0_BW" to set the bandwidth of the filter. + See "IMU_GYRO_NF${i}_BW" to set the bandwidth of the filter. A value of 0 disables the filter. type: float @@ -21,46 +24,14 @@ parameters: increment: 0.1 decimal: 1 reboot_required: false - IMU_GYRO_NF0_BW: + num_instances: *num_notch_filters + instance_start: 0 + IMU_GYRO_NF${i}_BW: description: - short: Notch filter bandwidth for gyro + short: Notch filter ${i} bandwidth for gyro long: |- The frequency width of the stop band for the 2nd order notch filter on the primary gyro. - See "IMU_GYRO_NF0_FRQ" to activate the filter and to set the notch frequency. - Applies to both angular velocity and angular acceleration sent to the controllers. - type: float - default: 20.0 - min: 0 - max: 100 - unit: Hz - increment: 0.1 - decimal: 1 - reboot_required: false - IMU_GYRO_NF1_FRQ: - description: - short: Notch filter 2 frequency for gyro - long: |- - The center frequency for the 2nd order notch filter on the primary gyro. - This filter can be enabled to avoid feedback amplification of structural resonances at a specific frequency. - This only affects the signal sent to the controllers, not the estimators. - Applies to both angular velocity and angular acceleration sent to the controllers. - See "IMU_GYRO_NF1_BW" to set the bandwidth of the filter. - - A value of 0 disables the filter. - type: float - default: 0.0 - min: 0 - max: 1000 - unit: Hz - increment: 0.1 - decimal: 1 - reboot_required: false - IMU_GYRO_NF1_BW: - description: - short: Notch filter 1 bandwidth for gyro - long: |- - The frequency width of the stop band for the 2nd order notch filter on the primary gyro. - See "IMU_GYRO_NF1_FRQ" to activate the filter and to set the notch frequency. + See "IMU_GYRO_NF${i}_FRQ" to activate the filter and to set the notch frequency. Applies to both angular velocity and angular acceleration sent to the controllers. type: float default: 20.0 @@ -70,6 +41,8 @@ parameters: increment: 0.1 decimal: 1 reboot_required: false + num_instances: *num_notch_filters + instance_start: 0 IMU_GYRO_CUTOFF: description: short: Low pass filter cutoff frequency for gyro