From 0d104226d362cf6976a9f5684232e18598ce2f2e Mon Sep 17 00:00:00 2001 From: Andrea Bernasconi Date: Tue, 29 Sep 2026 12:22:31 +0200 Subject: [PATCH] feat(mc_rate_control): add rate loop gain compression for MC (#28719) * feat(mc_rate_control): add rate loop gain compression Extend the gain compression already available on fixed-wing to the multicopter rate controller, so that an oscillation (limit cycle) on the torque setpoint dynamically reduces the loop gain instead of requiring a manual retune. The gain recovers to 1.0 once the oscillation stops. GainCompression3d is templated on the enable and minimum gain parameter IDs so both airframe types share one implementation while keeping their own MC_GC_* / FW_GC_* parameters. Behaviour is opt-in: MC_GC_EN defaults to disabled. The dt clamp in the spectral damper is widened from 1 ms to 0.125 ms because multicopter rate loops run well above the 1 kHz that the previous floor assumed, which would otherwise mis-scale the filter coefficients. Compression is reset while landed so that ground contact vibration does not compress the gains before takeoff. Assisted-by: Claude:claude-opus-5 Signed-off-by: Andrea Bernasconi * fix(rate_control): fully restart gain compression on reset reset() only restored the scalar compression gains, leaving the spectral damper history and the cached gain vector untouched. A reset therefore did not really restart at gain 1: the next cycle still multiplied the torque setpoint by the previously compressed vector, and the stale HPF/LPF state could immediately recreate compression. Clear the detector history and the cached gains so that disarming, a non-rotary-wing transition or ground contact genuinely restart from an uncompressed loop. Seed the high-pass filter from the first sample after a reset instead of zeroing the input history. Since the multicopter controller resets on every cycle while landed, a zeroed history would make the first airborne sample look like a step edge and inject a spurious spike into the spectral damper right at takeoff. Assisted-by: Claude:claude-opus-5 Signed-off-by: Andrea Bernasconi * docs(mc_rate_control): document multicopter gain compression Add a feature page for gain compression on multicopters, mirroring the fixed-wing one but covering the multicopter specifics: it is opt-in (MC_GC_EN defaults to disabled), and compression is reset while disarmed or landed so ground contact cannot compress the gains before takeoff. The fixed-wing block diagram is linked rather than duplicated, since it contains an airspeed scaling stage that does not exist in the multicopter rate loop. Also note in the multicopter PID tuning guide that compression should be off during manual tuning, as it damps out the very oscillation the tuning process looks for. Assisted-by: Claude:claude-opus-5 Signed-off-by: Andrea Bernasconi --------- Signed-off-by: Andrea Bernasconi --- docs/en/SUMMARY.md | 1 + .../config_mc/pid_tuning_guide_multicopter.md | 1 + docs/en/features_mc/gain_compression.md | 28 +++++++++++++++++ docs/en/features_mc/index.md | 1 + docs/en/releases/main.md | 2 +- src/lib/rate_control/gain_compression.cpp | 30 ++++++++++++++----- src/lib/rate_control/gain_compression.hpp | 28 +++++++++++++---- .../rate_control/gain_compression_params.yaml | 21 +++++++++++++ .../MulticopterRateControl.cpp | 10 +++++++ .../MulticopterRateControl.hpp | 2 ++ 10 files changed, 109 insertions(+), 15 deletions(-) create mode 100644 docs/en/features_mc/gain_compression.md diff --git a/docs/en/SUMMARY.md b/docs/en/SUMMARY.md index 090b3b81d77..5f8e19599f1 100644 --- a/docs/en/SUMMARY.md +++ b/docs/en/SUMMARY.md @@ -29,6 +29,7 @@ - [Vision Target Estimator Deep Dive](advanced_features/vision_target_estimator_advanced.md) - [Terrain Following/Holding](flying/terrain_following_holding.md) - [Throw Launch](flight_modes_mc/throw_launch.md) + - [Gain compression](features_mc/gain_compression.md) - [Assembly](assembly/assembly_mc.md) - [Configuration/Tuning](config_mc/index.md) - [Auto-tune](config/autotune_mc.md) diff --git a/docs/en/config_mc/pid_tuning_guide_multicopter.md b/docs/en/config_mc/pid_tuning_guide_multicopter.md index 652a9835f59..633145f4fc6 100644 --- a/docs/en/config_mc/pid_tuning_guide_multicopter.md +++ b/docs/en/config_mc/pid_tuning_guide_multicopter.md @@ -25,6 +25,7 @@ Here are some general points to follow when tuning: Slowly increase the throttle and check for oscillations. - Tune the vehicle around the hovering thrust point, and use the [thrust curve parameter](#thrust-curve) to account for thrust non-linearities or high-thrust oscillations. - Optionally enable the high-rate logging profile with the [SDLOG_PROFILE](../advanced_config/parameter_reference.md#SDLOG_PROFILE) parameter so you can use the log to evaluate the rate and attitude tracking performance (the option can be disabled afterwards). +- Disable automatic [gain compression](../features_mc/gain_compression.md) ([MC_GC_EN](../advanced_config/parameter_reference.md#MC_GC_EN)) if you enabled it, to avoid over-tuning (it is disabled by default). :::warning Always disable [MC_AIRMODE](../advanced_config/parameter_reference.md#MC_AIRMODE) when tuning a vehicle. diff --git a/docs/en/features_mc/gain_compression.md b/docs/en/features_mc/gain_compression.md new file mode 100644 index 00000000000..230e76e795c --- /dev/null +++ b/docs/en/features_mc/gain_compression.md @@ -0,0 +1,28 @@ +# Gain compression + + + +Automatic gain compression reduces the gains of the angular-rate PID whenever oscillations are detected. +It monitors the angular-rate controller output (the torque setpoint) through a band-pass filter to identify these oscillations, and scales the output down until they stop. +The gain recovers to 1.0 once the oscillation is gone. + +This approach is a safe adaptive mechanism: the PID gains remain unchanged when no oscillations are present, they are never increased beyond their nominal values, and they are bounded by a minimum limit ([MC_GC_GAIN_MIN](../advanced_config/parameter_reference.md#MC_GC_GAIN_MIN)). + +Gain compression can help prevent actuator damage, and even loss of the vehicle, when the rate loop becomes oscillatory in flight: for example after a payload change that shifts the CG or inertia, a partial propeller failure, or gains that were tuned for a different configuration. +It removes the need to retune manually for the oscillation to stop. + +The algorithm is the same one used on fixed-wing vehicles, where you can also find a [block diagram](../features_fw/gain_compression.md) of the adaptive law (the multicopter rate loop has no airspeed scaling stage). + +## Usage + +Gain compression is disabled by default and is enabled with [MC_GC_EN](../advanced_config/parameter_reference.md#MC_GC_EN). + +It should be disabled during multicopter [manual tuning](../config_mc/pid_tuning_guide_multicopter.md) to avoid over-tuning: with compression active, an oscillation caused by gains that are too high is damped out, which hides the very symptom the tuning process relies on. +It does not need to be disabled when [autotuning](../config/autotune_mc.md). + +Compression is automatically reset (gain back to 1.0) while the vehicle is disarmed or landed, so that vibration from ground contact cannot compress the gains before takeoff. + +## Parameters + +- [MC_GC_EN](../advanced_config/parameter_reference.md#MC_GC_EN) +- [MC_GC_GAIN_MIN](../advanced_config/parameter_reference.md#MC_GC_GAIN_MIN) diff --git a/docs/en/features_mc/index.md b/docs/en/features_mc/index.md index 44e8a339ff9..0ea80a215fe 100644 --- a/docs/en/features_mc/index.md +++ b/docs/en/features_mc/index.md @@ -8,3 +8,4 @@ This section lists features that are specific to (or customised for) multicopter - [Terrain Following/Holding](../flying/terrain_following_holding.md) - [Throw Launch](../flight_modes_mc/throw_launch.md) - [Motor Failure Recovery](../config/motor_failure_recovery.md) +- [Gain Compression](../features_mc/gain_compression.md) diff --git a/docs/en/releases/main.md b/docs/en/releases/main.md index 8f9772feeda..f05f78aeae3 100644 --- a/docs/en/releases/main.md +++ b/docs/en/releases/main.md @@ -52,7 +52,7 @@ Please continue reading for [upgrade instructions](#upgrade-guide). ### Control -- TBD +- [Gain compression](../features_mc/gain_compression.md) is now available on multicopters ([MC_GC_EN](../advanced_config/parameter_reference.md#MC_GC_EN)). When an oscillation is detected on the torque setpoint the rate loop gain is dynamically reduced instead of requiring a manual retune, and recovers to 1.0 once the oscillation stops. The lower bound is set with [MC_GC_GAIN_MIN](../advanced_config/parameter_reference.md#MC_GC_GAIN_MIN) (default `0.3`). Disabled by default. ### Safety diff --git a/src/lib/rate_control/gain_compression.cpp b/src/lib/rate_control/gain_compression.cpp index 40a60c19ba7..19d1cf5a781 100644 --- a/src/lib/rate_control/gain_compression.cpp +++ b/src/lib/rate_control/gain_compression.cpp @@ -36,39 +36,44 @@ using matrix::Vector3f; using namespace time_literals; -GainCompression3d::GainCompression3d(ModuleParams *parent) : ModuleParams(parent) +template +GainCompression3dT::GainCompression3dT(ModuleParams *parent) : ModuleParams(parent) { updateParams(); _gain_compression_pub.advertise(); } -void GainCompression3d::reset() +template +void GainCompression3dT::reset() { for (unsigned i = 0; i < 3; i++) { _compression_gains[i].reset(); } + + _gains.setOne(); } -void GainCompression3d::updateParams() +template +void GainCompression3dT::updateParams() { ModuleParams::updateParams(); for (unsigned i = 0; i < 3; i++) { - _compression_gains[i].setCompressionGainMin(_param_fw_gc_gain_min.get()); + _compression_gains[i].setCompressionGainMin(_param_gc_gain_min.get()); } } -void GainCompression3d::update(const Vector3f &input, const float dt) +template +void GainCompression3dT::update(const Vector3f &input, const float dt) { - if (!_param_fw_gc_en.get()) { + if (!_param_gc_en.get()) { reset(); - _gains.setOne(); return; } Vector3f hpf; Vector3f lpf; - const float sample_freq = 1.f / math::constrain(dt, 1e-3f, 100e-3f); + const float sample_freq = 1.f / math::constrain(dt, 0.125e-3f, 100e-3f); // supports rate loops up to 8kHz for (unsigned i = 0; i < 3; i++) { _compression_gains[i].setLpfCutoffFrequency(sample_freq, _kLpfCutoffFrequency); @@ -94,12 +99,21 @@ void GainCompression3d::update(const Vector3f &input, const float dt) } } +template class GainCompression3dT; +template class GainCompression3dT; + float GainCompression::update(const float input, const float dt) { if (!PX4_ISFINITE(input)) { return _compression_gain; } + if (!_input_initialized) { + // seed the high-pass filter to avoid detecting a step on the first sample after a reset + _input_prev = input; + _input_initialized = true; + } + _hpf = _alpha_hpf * _hpf + _alpha_hpf * (input - _input_prev); _lpf.update(_hpf * _hpf); diff --git a/src/lib/rate_control/gain_compression.hpp b/src/lib/rate_control/gain_compression.hpp index 20611e9ac96..478fa9a1f39 100644 --- a/src/lib/rate_control/gain_compression.hpp +++ b/src/lib/rate_control/gain_compression.hpp @@ -57,7 +57,13 @@ class GainCompression { public: - void reset() { _compression_gain = 1.f; } + void reset() + { + _compression_gain = 1.f; + _hpf = 0.f; + _lpf.reset(0.f); + _input_initialized = false; + } float update(float input, float dt); void setLpfCutoffFrequency(float sample_freq, float cutoff) @@ -78,6 +84,7 @@ private: float _hpf{0.f}; float _input_prev{0.f}; + bool _input_initialized{false}; AlphaFilter _lpf; @@ -86,11 +93,17 @@ private: }; -class GainCompression3d : public ModuleParams +/* + * Templated on the enable and minimum gain parameter IDs so that the same + * implementation can be used by the fixed-wing and multicopter rate controllers + * with their own set of parameters. + */ +template +class GainCompression3dT : public ModuleParams { public: - GainCompression3d(ModuleParams *parent); - ~GainCompression3d() = default; + GainCompression3dT(ModuleParams *parent); + ~GainCompression3dT() = default; void reset(); void update(const matrix::Vector3f &input, float dt); @@ -113,7 +126,10 @@ private: static constexpr float _kHpfCutoffFrequency{2.f * _kLpfCutoffFrequency}; // 1 Octave above LPF cutoff, as recommended by the reference paper DEFINE_PARAMETERS( - (ParamBool) _param_fw_gc_en, - (ParamFloat) _param_fw_gc_gain_min + (ParamBool) _param_gc_en, + (ParamFloat) _param_gc_gain_min ) }; + +using GainCompression3d = GainCompression3dT; +using GainCompression3dMc = GainCompression3dT; diff --git a/src/lib/rate_control/gain_compression_params.yaml b/src/lib/rate_control/gain_compression_params.yaml index 4a2339a60aa..0661a54765e 100644 --- a/src/lib/rate_control/gain_compression_params.yaml +++ b/src/lib/rate_control/gain_compression_params.yaml @@ -17,3 +17,24 @@ parameters: max: 1.0 increment: 0.01 decimal: 2 +- group: Multicopter Rate Control + definitions: + MC_GC_EN: + description: + short: Enable rate gain compression + long: | + Dynamically reduces the rate controller output gain when an oscillation + (limit cycle) is detected on the torque setpoint, e.g. caused by too high gains. + The gain recovers to 1.0 once the oscillation stops. + type: boolean + default: 0 + MC_GC_GAIN_MIN: + description: + short: Compression gain lower limit + long: The range of the compression gain is between this parameter and 1.0 + type: float + default: 0.3 + min: 0.0 + max: 1.0 + increment: 0.01 + decimal: 2 diff --git a/src/modules/mc_rate_control/MulticopterRateControl.cpp b/src/modules/mc_rate_control/MulticopterRateControl.cpp index 4b1f6a9c183..12ca513107d 100644 --- a/src/modules/mc_rate_control/MulticopterRateControl.cpp +++ b/src/modules/mc_rate_control/MulticopterRateControl.cpp @@ -191,6 +191,12 @@ MulticopterRateControl::Run() // reset integral if disarmed if (!_vehicle_control_mode.flag_armed || _vehicle_status.vehicle_type != vehicle_status_s::VEHICLE_TYPE_ROTARY_WING) { _rate_control.resetIntegral(); + _gain_compression.reset(); + } + + // reset gain compression on the ground to prevent ground contact from compressing the gains + if (_landed) { + _gain_compression.reset(); } // update saturation status from control allocation feedback @@ -219,6 +225,10 @@ MulticopterRateControl::Run() Vector3f torque_setpoint = _rate_control.update(rates, _rates_setpoint, angular_accel, dt, _maybe_landed || _landed); + // reduce the loop gain when an oscillation is detected on the controller output + torque_setpoint = _gain_compression.getGains().emult(torque_setpoint); + _gain_compression.update(torque_setpoint, dt); + // apply low-pass filtering on yaw axis to reduce high frequency torque caused by rotor acceleration torque_setpoint(2) = _output_lpf_yaw.update(torque_setpoint(2), static_cast(dt * 1e6f)); diff --git a/src/modules/mc_rate_control/MulticopterRateControl.hpp b/src/modules/mc_rate_control/MulticopterRateControl.hpp index 44c6c13bc43..892419f7fdc 100644 --- a/src/modules/mc_rate_control/MulticopterRateControl.hpp +++ b/src/modules/mc_rate_control/MulticopterRateControl.hpp @@ -33,6 +33,7 @@ #pragma once +#include #include #include #include @@ -93,6 +94,7 @@ private: void updateActuatorControlsStatus(const vehicle_torque_setpoint_s &vehicle_torque_setpoint, float dt); RateControl _rate_control; ///< class for rate control calculations + GainCompression3dMc _gain_compression{this}; ///< reduces the loop gain when an oscillation is detected uORB::Subscription _battery_status_sub{ORB_ID(battery_status)}; uORB::Subscription _control_allocator_status_sub{ORB_ID(control_allocator_status)};