diff --git a/docs/en/flight_modes/offboard.md b/docs/en/flight_modes/offboard.md index 6788fb9d9eb..ecf0941fb17 100644 --- a/docs/en/flight_modes/offboard.md +++ b/docs/en/flight_modes/offboard.md @@ -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 diff --git a/src/modules/mavlink/mavlink_receiver.cpp b/src/modules/mavlink/mavlink_receiver.cpp index 9444f3ea41d..4b9f165c03b 100644 --- a/src/modules/mavlink/mavlink_receiver.cpp +++ b/src/modules/mavlink/mavlink_receiver.cpp @@ -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 diff --git a/src/modules/mc_att_control/AttitudeControl/AttitudeControl.cpp b/src/modules/mc_att_control/AttitudeControl/AttitudeControl.cpp index f9a333c9b19..08a3f5b838f 100644 --- a/src/modules/mc_att_control/AttitudeControl/AttitudeControl.cpp +++ b/src/modules/mc_att_control/AttitudeControl/AttitudeControl.cpp @@ -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) diff --git a/src/modules/mc_att_control/AttitudeControl/AttitudeControl.hpp b/src/modules/mc_att_control/AttitudeControl/AttitudeControl.hpp index 4124719435d..380ad83ef0b 100644 --- a/src/modules/mc_att_control/AttitudeControl/AttitudeControl.hpp +++ b/src/modules/mc_att_control/AttitudeControl/AttitudeControl.hpp @@ -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 diff --git a/src/modules/mc_att_control/AttitudeControl/AttitudeControlTest.cpp b/src/modules/mc_att_control/AttitudeControl/AttitudeControlTest.cpp index 2bf0dc914af..60f1f31b3b5 100644 --- a/src/modules/mc_att_control/AttitudeControl/AttitudeControlTest.cpp +++ b/src/modules/mc_att_control/AttitudeControl/AttitudeControlTest.cpp @@ -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); +} diff --git a/src/modules/mc_att_control/mc_att_control_main.cpp b/src/modules/mc_att_control/mc_att_control_main.cpp index 2c348799dce..6f9701e27df 100644 --- a/src/modules/mc_att_control/mc_att_control_main.cpp +++ b/src/modules/mc_att_control/mc_att_control_main.cpp @@ -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; + } } }