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 <andrea.bernasconi@auterion.com>

* 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 <andrea.bernasconi@auterion.com>

* 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 <andrea.bernasconi@auterion.com>

---------

Signed-off-by: Andrea Bernasconi <andrea.bernasconi@auterion.com>
This commit is contained in:
Andrea Bernasconi authored and GitHub committed 2026-09-29 12:22:31 +02:00
1 parent 19ed79efa1
commit 0d104226d3
10 files changed
+109 -15

No files matched your search

+1
View File
@@ -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)
@@ -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.
+28
View File
@@ -0,0 +1,28 @@
# Gain compression
<Badge type="tip" text="PX4 v2.0" />
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)
+1
View File
@@ -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)
+1 -1
View File
@@ -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
+22 -8
View File
@@ -36,39 +36,44 @@
using matrix::Vector3f;
using namespace time_literals;
GainCompression3d::GainCompression3d(ModuleParams *parent) : ModuleParams(parent)
template<px4::params ParamEnable, px4::params ParamGainMin>
GainCompression3dT<ParamEnable, ParamGainMin>::GainCompression3dT(ModuleParams *parent) : ModuleParams(parent)
{
updateParams();
_gain_compression_pub.advertise();
}
void GainCompression3d::reset()
template<px4::params ParamEnable, px4::params ParamGainMin>
void GainCompression3dT<ParamEnable, ParamGainMin>::reset()
{
for (unsigned i = 0; i < 3; i++) {
_compression_gains[i].reset();
}
_gains.setOne();
}
void GainCompression3d::updateParams()
template<px4::params ParamEnable, px4::params ParamGainMin>
void GainCompression3dT<ParamEnable, ParamGainMin>::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<px4::params ParamEnable, px4::params ParamGainMin>
void GainCompression3dT<ParamEnable, ParamGainMin>::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<px4::params::FW_GC_EN, px4::params::FW_GC_GAIN_MIN>;
template class GainCompression3dT<px4::params::MC_GC_EN, px4::params::MC_GC_GAIN_MIN>;
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);
+22 -6
View File
@@ -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<float> _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<px4::params ParamEnable, px4::params ParamGainMin>
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<px4::params::FW_GC_EN>) _param_fw_gc_en,
(ParamFloat<px4::params::FW_GC_GAIN_MIN>) _param_fw_gc_gain_min
(ParamBool<ParamEnable>) _param_gc_en,
(ParamFloat<ParamGainMin>) _param_gc_gain_min
)
};
using GainCompression3d = GainCompression3dT<px4::params::FW_GC_EN, px4::params::FW_GC_GAIN_MIN>;
using GainCompression3dMc = GainCompression3dT<px4::params::MC_GC_EN, px4::params::MC_GC_GAIN_MIN>;
@@ -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
@@ -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<uint64_t>(dt * 1e6f));
@@ -33,6 +33,7 @@
#pragma once
#include <lib/rate_control/gain_compression.hpp>
#include <lib/rate_control/rate_control.hpp>
#include <lib/mathlib/math/filter/AlphaFilter.hpp>
#include <lib/matrix/matrix/math.hpp>
@@ -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)};