feat(sensors): allow compile time configuration of more than 2 gyro notch filters (#28892)

* refactor(sensors): loop over the static gyro notch filters

* feat(sensors): allow compile time configuration of more than 2 gyro notch  filters

---------

Co-authored-by: Matthias Grob <maetugr@gmail.com>
This commit is contained in:
Anil KircalialiandMatthias Grob authored and GitHub committed 2026-11-04 09:28:20 +01:00
1 parent 79431c0a3c
commit 351c6d2011
3 files changed
+123 -86

No files matched your search

@@ -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(&param_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<NUM_NOTCH_FILTERS> nf_freq_params{"IMU_GYRO_NF", "_FRQ"};
static constexpr FloatParamInstances<NUM_NOTCH_FILTERS> 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)
@@ -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<float> _lp_filter_velocity[3] {};
math::NotchFilter<float> _notch_filter0_velocity[3] {};
math::NotchFilter<float> _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<float> _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<px4::params::IMU_GYRO_DNF_MIN>) _param_imu_gyro_dnf_min,
#endif // !CONSTRAINED_FLASH
(ParamFloat<px4::params::IMU_GYRO_CUTOFF>) _param_imu_gyro_cutoff,
(ParamFloat<px4::params::IMU_GYRO_NF0_FRQ>) _param_imu_gyro_nf0_frq,
(ParamFloat<px4::params::IMU_GYRO_NF0_BW>) _param_imu_gyro_nf0_bw,
(ParamFloat<px4::params::IMU_GYRO_NF1_FRQ>) _param_imu_gyro_nf1_frq,
(ParamFloat<px4::params::IMU_GYRO_NF1_BW>) _param_imu_gyro_nf1_bw,
(ParamInt<px4::params::IMU_GYRO_RATEMAX>) _param_imu_gyro_ratemax,
(ParamFloat<px4::params::IMU_DGYRO_CUTOFF>) _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 <prefix><instance><suffix> (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 <prefix>0<suffix> .. <prefix>N-1<suffix>, looked up at compile time
template<int N>
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
@@ -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