mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-11-25 16:51:16 +08:00
fix(mc_att_control): reject invalid attitude setpoints (#29008)
* fix(mc_att_control): ignore invalid attitude setpoints in the reference model
Since the reference model (2a0e048171, PR #27255) the controller tracks _q_ref,
which is integrated from its previous value and only snapped to the setpoint on
the first call. A single attitude setpoint with a zero or non-finite quaternion
(normalizing a zero quaternion gives NaN) therefore makes _q_ref NaN for the rest
of the boot: every later setpoint is ignored, the rate setpoint stays NaN and the
vehicle has no attitude control, even after a mode switch. Before the reference
model the controller used q_d directly, so a bad sample only lasted one cycle.
One MAVLink SET_ATTITUDE_TARGET with q = [0, 0, 0, 0] in Offboard reproduces it
in SIH: vehicle_rates_setpoint stays NaN and the vehicle no longer responds to
the sticks after switching to Position mode.
Ignore setpoints and heading-reset deltas that are zero or non-finite, keeping
the last valid reference, and snap the reference to the next valid setpoint if
it ever becomes non-finite.
Signed-off-by: Isak Åslund <isak.aslund@airolit.com>
* fix(mc_att_control): drop the whole attitude setpoint when its quaternion is invalid
When the controller rejects an invalid quaternion, the thrust and timestamp
from the same message were still applied. The vehicle then flew the last
valid attitude with thrust from a message that cannot be trusted.
setAttitudeSetpoint() now returns whether the setpoint was accepted. The
thrust and the setpoint timestamp are only updated when it was. Keeping the
timestamp also gives the next valid setpoint the true dt since the last
accepted one.
Signed-off-by: Isak Åslund <isak.aslund@airolit.com>
* fix(mc_att_control): reject overflowing quaternions and drop invalid SET_ATTITUDE_TARGET
Review follow-ups:
- Validate the setpoint through its norm. A quaternion with a finite component
above ~1.8e19 (e.g. q = (1e20, 0, 0, 0)) passed isAllFinite(), its norm
overflowed to inf and normalizing it gave a zero quaternion. As the first
setpoint this made the reference NaN for the next setpoint. One finite norm
check rejects NaN, inf, zero and overflow.
- Drop the ref_valid / dt finiteness recovery branch: with every input of the
reference model validated it cannot be reached, and it costs flash.
- Drop the adaptAttitudeSetpoint() guard: no estimator publishes an invalid
delta with a changed reset counter, and it only protected the reference,
not the yaw setpoint and StickYaw that use the same delta.
- MAVLink SET_ATTITUDE_TARGET: drop a message with an unusable q before
publishing offboard_control_mode. Otherwise a companion streaming only
invalid quaternions keeps the offboard heartbeat alive and the vehicle holds
the last valid tilt and thrust indefinitely without triggering
COM_OF_LOSS_T.
Signed-off-by: Isak Åslund <isak.aslund@airolit.com>
* docs(offboard): note that SET_ATTITUDE_TARGET with an invalid q is dropped
Since the previous commit, such a message no longer counts as the offboard
signal, which is a behavior change MAVLink users can see. Also name the 1e-3
norm threshold in the setAttitudeSetpoint() doc, and point the MAVLink and
controller checks at each other: if the MAVLink threshold were lowered below
the controller's, messages the controller drops would keep offboard alive.
Signed-off-by: Isak Åslund <isak.aslund@airolit.com>
---------
Signed-off-by: Isak Åslund <isak.aslund@airolit.com>
This commit is contained in:
1 parent
86b71ffb7f
commit
7902349f7a
6 files changed
+115
-9
No files matched your search
@@ -310,6 +310,7 @@ The following MAVLink messages and their particular fields and field values are
|
||||
- The following input combinations are supported:
|
||||
- Attitude/orientation (`SET_ATTITUDE_TARGET.q`) with thrust setpoint (`SET_ATTITUDE_TARGET.thrust`).
|
||||
- Body rate (`SET_ATTITUDE_TARGET` `.body_roll_rate` ,`.body_pitch_rate`, `.body_yaw_rate`) with thrust setpoint (`SET_ATTITUDE_TARGET.thrust`).
|
||||
- A message with attitude enabled and a zero or non-finite `q` (norm below 0.001) is ignored as a whole and does not count as the offboard signal.
|
||||
|
||||
### Fixed-wing
|
||||
|
||||
@@ -362,6 +363,7 @@ The following MAVLink messages and their particular fields and field values are
|
||||
- The following input combinations are supported:
|
||||
- Attitude/orientation (`SET_ATTITUDE_TARGET.q`) with thrust setpoint (`SET_ATTITUDE_TARGET.thrust`).
|
||||
- Body rate (`SET_ATTITUDE_TARGET` `.body_roll_rate` ,`.body_pitch_rate`, `.body_yaw_rate`) with thrust setpoint (`SET_ATTITUDE_TARGET.thrust`).
|
||||
- A message with attitude enabled and a zero or non-finite `q` (norm below 0.001) is ignored as a whole and does not count as the offboard signal.
|
||||
|
||||
### Rover
|
||||
|
||||
|
||||
@@ -1861,6 +1861,16 @@ MavlinkReceiver::handle_message_set_attitude_target(mavlink_message_t *msg)
|
||||
const bool thrust = !(type_mask & ATTITUDE_TARGET_TYPEMASK_THROTTLE_IGNORE);
|
||||
const bool has_thrust = thrust || thrust_body;
|
||||
|
||||
const matrix::Quatf q{attitude_target.q};
|
||||
const float q_norm = q.norm();
|
||||
|
||||
if (attitude && (!PX4_ISFINITE(q_norm) || (q_norm < 1e-3f))) {
|
||||
// Drop the whole message, including the offboard heartbeat, so the offboard loss failsafe
|
||||
// still triggers if a companion only sends unusable attitudes.
|
||||
// Keep in sync with the check in AttitudeControl::setAttitudeSetpoint().
|
||||
return;
|
||||
}
|
||||
|
||||
vehicle_status_s vehicle_status{};
|
||||
_vehicle_status_sub.copy(&vehicle_status);
|
||||
|
||||
@@ -1875,7 +1885,6 @@ MavlinkReceiver::handle_message_set_attitude_target(mavlink_message_t *msg)
|
||||
if (attitude && has_thrust) {
|
||||
vehicle_attitude_setpoint_s attitude_setpoint{};
|
||||
|
||||
const matrix::Quatf q{attitude_target.q};
|
||||
q.copyTo(attitude_setpoint.q_d);
|
||||
|
||||
// TODO: review use case
|
||||
|
||||
@@ -62,10 +62,20 @@ void AttitudeControl::setRefModelFrequency(float omega_n)
|
||||
_kq = _omega_n * _omega_n;
|
||||
}
|
||||
|
||||
void AttitudeControl::setAttitudeSetpoint(const Quatf &qd, const float yawspeed_setpoint, const float dt)
|
||||
bool AttitudeControl::setAttitudeSetpoint(const Quatf &qd, const float yawspeed_setpoint, const float dt)
|
||||
{
|
||||
Quatf qd_normalized = qd;
|
||||
qd_normalized.normalize();
|
||||
// The reference model integrates from its previous state, so a single invalid setpoint would make
|
||||
// it NaN until reboot. Keep tracking the last valid reference instead.
|
||||
// A zero, non-finite or overflowing quaternion has no attitude: normalizing it gives NaN or zero.
|
||||
// Keep in sync with handle_message_set_attitude_target() in mavlink_receiver.cpp, which drops
|
||||
// these messages so they do not keep offboard alive.
|
||||
const float qd_norm = qd.norm();
|
||||
|
||||
if (!PX4_ISFINITE(qd_norm) || (qd_norm < 1e-3f)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const Quatf qd_normalized = qd / qd_norm;
|
||||
|
||||
if (_ref_initialized && dt > 0.f) {
|
||||
propagateReferenceModel(qd_normalized, yawspeed_setpoint, dt);
|
||||
@@ -77,6 +87,8 @@ void AttitudeControl::setAttitudeSetpoint(const Quatf &qd, const float yawspeed_
|
||||
_omega_command.zero();
|
||||
_ref_initialized = true;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void AttitudeControl::propagateReferenceModel(const Quatf &qd, const float yawspeed_setpoint, const float dt)
|
||||
|
||||
@@ -84,8 +84,9 @@ public:
|
||||
* @param qd desired vehicle attitude setpoint
|
||||
* @param yawspeed_setpoint [rad/s] yaw feed forward angular rate in world frame
|
||||
* @param dt [s] time since previous setpoint
|
||||
* @return false if the norm of qd is below 1e-3 or not finite, in which case it is ignored
|
||||
*/
|
||||
void setAttitudeSetpoint(const matrix::Quatf &qd, const float yawspeed_setpoint, const float dt = -1.f);
|
||||
bool setAttitudeSetpoint(const matrix::Quatf &qd, const float yawspeed_setpoint, const float dt = -1.f);
|
||||
|
||||
/**
|
||||
* Adjust last known attitude setpoint by a delta rotation
|
||||
|
||||
@@ -433,3 +433,82 @@ TEST_F(AttitudeControlFeedforwardTest, FractionalGainScalesAnticipation)
|
||||
EXPECT_NEAR(rate_setpoint(1), 0.f, 1e-3f);
|
||||
EXPECT_NEAR(rate_setpoint(2), 0.f, 1e-3f);
|
||||
}
|
||||
|
||||
TEST_F(AttitudeControlFeedforwardTest, InvalidSetpointKeepsLastReference)
|
||||
{
|
||||
// GIVEN: a settled reference on a tilted setpoint, vehicle level
|
||||
const Quatf q_d(AxisAnglef(Vector3f(0.1f, 0.f, 0.f)));
|
||||
|
||||
for (int i = 0; i < kSettleSteps; i++) {
|
||||
_attitude_control.setAttitudeSetpoint(q_d, 0.f, (i == 0) ? -1.f : kDt);
|
||||
}
|
||||
|
||||
const Vector3f rate_setpoint_valid = _attitude_control.update(Quatf());
|
||||
ASSERT_TRUE(rate_setpoint_valid.isAllFinite());
|
||||
ASSERT_GT(rate_setpoint_valid.norm(), 0.1f);
|
||||
|
||||
// WHEN: one zero quaternion setpoint and one NaN quaternion setpoint arrive
|
||||
const bool accepted_zero = _attitude_control.setAttitudeSetpoint(Quatf(0.f, 0.f, 0.f, 0.f), 0.f, kDt);
|
||||
const Vector3f rate_setpoint_zero = _attitude_control.update(Quatf());
|
||||
const bool accepted_nan = _attitude_control.setAttitudeSetpoint(Quatf(NAN, 0.f, 0.f, 0.f), 0.f, kDt);
|
||||
const Vector3f rate_setpoint_nan = _attitude_control.update(Quatf());
|
||||
// norm() overflows to inf although every component is finite
|
||||
const bool accepted_overflow = _attitude_control.setAttitudeSetpoint(Quatf(1e20f, 0.f, 0.f, 0.f), 0.f, kDt);
|
||||
const Vector3f rate_setpoint_overflow = _attitude_control.update(Quatf());
|
||||
|
||||
// THEN: they are rejected, the controller keeps tracking the last valid reference
|
||||
EXPECT_FALSE(accepted_zero);
|
||||
EXPECT_FALSE(accepted_nan);
|
||||
EXPECT_FALSE(accepted_overflow);
|
||||
EXPECT_TRUE(rate_setpoint_zero.isAllFinite());
|
||||
EXPECT_TRUE(rate_setpoint_nan.isAllFinite());
|
||||
EXPECT_NEAR((rate_setpoint_zero - rate_setpoint_valid).norm(), 0.f, 1e-5f);
|
||||
EXPECT_NEAR((rate_setpoint_nan - rate_setpoint_valid).norm(), 0.f, 1e-5f);
|
||||
EXPECT_NEAR((rate_setpoint_overflow - rate_setpoint_valid).norm(), 0.f, 1e-5f);
|
||||
|
||||
// WHEN: valid setpoints resume with a new attitude
|
||||
const Quatf q_d_new(AxisAnglef(Vector3f(0.f, -0.2f, 0.f)));
|
||||
|
||||
bool accepted_new = true;
|
||||
|
||||
for (int i = 0; i < kSettleSteps; i++) {
|
||||
accepted_new &= _attitude_control.setAttitudeSetpoint(q_d_new, 0.f, kDt);
|
||||
}
|
||||
|
||||
// THEN: they are accepted and the reference converges on the new setpoint
|
||||
EXPECT_TRUE(accepted_new);
|
||||
EXPECT_TRUE(_attitude_control.getReferenceAttitude().isAllFinite());
|
||||
EXPECT_NEAR(_attitude_control.update(q_d_new).norm(), 0.f, 1e-3f);
|
||||
}
|
||||
|
||||
TEST_F(AttitudeControlFeedforwardTest, NonUnitSetpointIsAccepted)
|
||||
{
|
||||
// GIVEN: a valid setpoint that is finite but not unit length
|
||||
const Quatf q_d(AxisAnglef(Vector3f(0.f, 0.2f, 0.f)));
|
||||
|
||||
// WHEN: it is set scaled up and scaled down
|
||||
const bool accepted_large = _attitude_control.setAttitudeSetpoint(q_d * 3.f, 0.f);
|
||||
const bool accepted_small = _attitude_control.setAttitudeSetpoint(q_d * 0.01f, 0.f, kDt);
|
||||
|
||||
// THEN: both are accepted and normalized
|
||||
EXPECT_TRUE(accepted_large);
|
||||
EXPECT_TRUE(accepted_small);
|
||||
EXPECT_NEAR(_attitude_control.getReferenceAttitude().norm(), 1.f, 1e-5f);
|
||||
EXPECT_NEAR(_attitude_control.update(q_d).norm(), 0.f, 1e-3f);
|
||||
}
|
||||
|
||||
TEST_F(AttitudeControlFeedforwardTest, InvalidFirstSetpointIsRejected)
|
||||
{
|
||||
// WHEN: the first setpoint after boot is invalid
|
||||
const bool accepted_zero = _attitude_control.setAttitudeSetpoint(Quatf(0.f, 0.f, 0.f, 0.f), 0.f);
|
||||
const bool accepted_overflow = _attitude_control.setAttitudeSetpoint(Quatf(1e20f, 0.f, 0.f, 0.f), 0.f);
|
||||
|
||||
// THEN: it is rejected and the next valid setpoint initializes the reference
|
||||
EXPECT_FALSE(accepted_zero);
|
||||
EXPECT_FALSE(accepted_overflow);
|
||||
|
||||
const Quatf q_d(AxisAnglef(Vector3f(0.f, 0.2f, 0.f)));
|
||||
EXPECT_TRUE(_attitude_control.setAttitudeSetpoint(q_d, 0.f, kDt));
|
||||
EXPECT_TRUE(_attitude_control.getReferenceAttitude().isAllFinite());
|
||||
EXPECT_NEAR(_attitude_control.update(q_d).norm(), 0.f, 1e-5f);
|
||||
}
|
||||
@@ -333,10 +333,13 @@ MulticopterAttitudeControl::Run()
|
||||
const float setpoint_dt = (_last_attitude_setpoint > 0)
|
||||
? (vehicle_attitude_setpoint.timestamp - _last_attitude_setpoint) * 1e-6f
|
||||
: -1.f;
|
||||
_attitude_control.setAttitudeSetpoint(Quatf(vehicle_attitude_setpoint.q_d),
|
||||
vehicle_attitude_setpoint.yaw_sp_move_rate, setpoint_dt);
|
||||
_thrust_setpoint_body = Vector3f(vehicle_attitude_setpoint.thrust_body);
|
||||
_last_attitude_setpoint = vehicle_attitude_setpoint.timestamp;
|
||||
|
||||
// Drop the whole message if its attitude is invalid: its thrust cannot be trusted either
|
||||
if (_attitude_control.setAttitudeSetpoint(Quatf(vehicle_attitude_setpoint.q_d),
|
||||
vehicle_attitude_setpoint.yaw_sp_move_rate, setpoint_dt)) {
|
||||
_thrust_setpoint_body = Vector3f(vehicle_attitude_setpoint.thrust_body);
|
||||
_last_attitude_setpoint = vehicle_attitude_setpoint.timestamp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user