fix(mc_att_control): stop the reference heading winding up on a yaw rate command

This keeps the reference heading on the setpoint heading instead of
integrating the commanded yaw rate, which grew the yaw rate setpoint far
past the command whenever the vehicle could not follow it
This commit is contained in:
bresch authored and Matthias Grob committed 2026-09-29 10:36:45 +02:00
1 parent 27d3a62df1
commit d9fa6201f4
2 files changed
+47 -20

No files matched your search

@@ -88,9 +88,9 @@ void AttitudeControl::propagateReferenceModel(const Quatf &qd, const float yawsp
// frame, and form the small-angle error vector from q_ref to q_d.
const Quatf q_ref_inv = qinv(_q_ref);
const Vector3f yaw_axis_body = qzaxis(q_ref_inv); // world yaw axis expressed in q_ref's body frame
const Vector3f omega_command = PX4_ISFINITE(yawspeed_setpoint)
? yaw_axis_body * yawspeed_setpoint
: Vector3f{};
// Commanded (analytical) reference rate, kept separate so update() can feed it forward at unity.
_omega_command = PX4_ISFINITE(yawspeed_setpoint) ? yaw_axis_body * yawspeed_setpoint : Vector3f{};
Quatf q_err = qmul(q_ref_inv, qd);
q_err.canonicalize();
@@ -110,19 +110,16 @@ void AttitudeControl::propagateReferenceModel(const Quatf &qd, const float yawsp
const float gamma = _kq * dt * emt;
const float delta = (1.f - w_dt) * emt;
// While a yaw rate is commanded, StickYaw unlocks the heading and sets the setpoint yaw to the
// measurement, so on that axis q_d carries no reference for the model to filter.
const bool heading_unlocked = PX4_ISFINITE(yawspeed_setpoint) && (fabsf(yawspeed_setpoint) > FLT_EPSILON);
const Vector3f e_heading = heading_unlocked ? e.dot(yaw_axis_body) * yaw_axis_body : Vector3f{};
const Vector3f e_filtered = e - e_heading;
// Propagate the error-driven correction in tangent space (the 2nd-order state). delta_phi is the integral
// of omega over [0, dt]; the correction part collapses to e(0) - e(dt) since e_dot = -correction.
const Vector3f delta_phi = (1.f - a) * e + b * _omega_correction + omega_command * dt;
_omega_correction = gamma * e + delta * _omega_correction;
// Yaw-rate command: the heading setpoint just follows the measured yaw, so feeding the error-driven
// rate forward closes a positive-feedback loop. Keep only the commanded rate (omega_command) on the yaw axis.
if (PX4_ISFINITE(yawspeed_setpoint) && (fabsf(yawspeed_setpoint) > FLT_EPSILON)) {
_omega_correction -= _omega_correction.dot(yaw_axis_body) * yaw_axis_body;
}
// Commanded (analytical) reference rate, kept separate so update() can exempt it from the feedforward limit.
_omega_command = omega_command;
const Vector3f delta_phi = (1.f - a) * e_filtered + b * _omega_correction + e_heading;
_omega_correction = gamma * e_filtered + delta * _omega_correction;
_q_ref = qmul(_q_ref, Quatf(AxisAnglef(delta_phi)));
_q_ref.normalize();
@@ -231,9 +231,7 @@ TEST_F(AttitudeControlFeedforwardTest, HighRateRampStillExact)
TEST_F(AttitudeControlFeedforwardTest, YawRampOnlyAnalyticalFeedforwardContributes)
{
// GIVEN: a yaw ramp with the analytical yawspeed setpoint matching. The reference
// model's damping is biased toward this known rate, so omega_ref settles to
// (0,0,omega) in q_ref's body frame and the FF reads out the body-z component.
// GIVEN: a yaw ramp with the analytical yawspeed setpoint matching
const float omega = 0.5f;
rampSetpoint(Vector3f(0.f, 0.f, omega), omega, kSettleSteps);
@@ -249,9 +247,6 @@ TEST_F(AttitudeControlFeedforwardTest, TiltedYawDoesNotDoubleCount)
// GIVEN: body locked at constant tilt, yawing around world-z at constant rate.
// Truth body angular velocity: ω_body = R_BW · (0, 0, yaw_rate)
// = (-sin(tilt)·yaw_rate, 0, cos(tilt)·yaw_rate)
// The reference model bakes yaw_sp_move_rate into omega_ref via its damping bias,
// so the FF reproduces the body-frame projection of the world-z rotation —
// no separate analytical path, no double-count possible.
const float tilt = 0.5f; // ~28.6° pitch
const float yaw_rate = 0.5f; // ~28.6 dps
const Quatf q_pitch(AxisAnglef(Vector3f(0.f, tilt, 0.f)));
@@ -313,6 +308,41 @@ TEST_F(AttitudeControlFeedforwardTest, UnlockedYawDoesNotFeedBackSlavedHeading)
EXPECT_NEAR(rate_setpoint(1), 0.f, 1e-3f);
}
TEST_F(AttitudeControlFeedforwardTest, UnlockedHeadingDoesNotWindUpWhenVehicleCannotYaw)
{
// GIVEN: a steady yaw-rate command the vehicle cannot follow (yaw held, e.g. still on the ground),
// so the heading setpoint never moves either
const float commanded = 0.2f;
const Quatf q_stuck; // vehicle and setpoint both pinned at identity
for (int i = 0; i < 4 * kSettleSteps; i++) { // 8 s at the test's setpoint rate
_attitude_control.setAttitudeSetpoint(q_stuck, commanded, (i == 0) ? -1.f : kDt);
}
// WHEN: evaluated at the actual (stuck) attitude, so a runaway reference would show up as a P term
const Vector3f rate_setpoint = _attitude_control.update(q_stuck);
// THEN: the output is still just the commanded rate
EXPECT_NEAR(rate_setpoint(2), commanded, 1e-3f);
EXPECT_NEAR(rate_setpoint(0), 0.f, 1e-3f);
EXPECT_NEAR(rate_setpoint(1), 0.f, 1e-3f);
// AND: the reference heading has not drifted off the setpoint
EXPECT_NEAR(Eulerf(_attitude_control.getReferenceAttitude()).psi(), 0.f, 1e-3f);
}
TEST_F(AttitudeControlFeedforwardTest, UnlockedHeadingReferenceTracksSetpoint)
{
// GIVEN: the unlocked-heading regime, setpoint heading ramping at the vehicle's yaw rate
const float heading_ramp_rate = 0.8f;
const float commanded = 0.05f;
const Quatf q_d = rampSetpoint(Vector3f(0.f, 0.f, heading_ramp_rate), commanded, kSettleSteps);
// THEN: the reference heading tracks the setpoint heading, so the yaw P term contributes nothing
EXPECT_NEAR(Eulerf(_attitude_control.getReferenceAttitude()).psi(), Eulerf(q_d).psi(),
2.f * heading_ramp_rate * kDt); // one setpoint sample of catch-up lag
}
TEST_F(AttitudeControlFeedforwardTest, LockedYawRampStillFeedsForward)
{
// GIVEN: a genuine yaw-angle target slewed with no commanded rate (heading-hold / auto yaw).