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)};