From 4295a7f30a19157a293b4dbdc1d1bfaed900eb2d Mon Sep 17 00:00:00 2001 From: Andrea Bernasconi Date: Wed, 25 Nov 2026 13:54:13 +0100 Subject: [PATCH] feat(failsafe): continue modes without sticks during link loss delay On RC or GCS loss, modes not flown with sticks (e.g. Mission) keep flying during COM_FAIL_ACT_T instead of Hold, and continue if the link is regained in time. Manual modes keep the Hold without automatic switching when regained. Co-authored-by: Matthias Grob Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Andrea Bernasconi --- src/modules/commander/commander_params.yaml | 4 +- src/modules/commander/failsafe/failsafe.cpp | 2 + .../commander/failsafe/failsafe_test.cpp | 171 ++++++++++++++++++ src/modules/commander/failsafe/framework.cpp | 32 +++- src/modules/commander/failsafe/framework.h | 1 + 5 files changed, 203 insertions(+), 7 deletions(-) diff --git a/src/modules/commander/commander_params.yaml b/src/modules/commander/commander_params.yaml index 7f1a1c6a07e..86018a59c62 100644 --- a/src/modules/commander/commander_params.yaml +++ b/src/modules/commander/commander_params.yaml @@ -200,7 +200,7 @@ parameters: short: Delay between failsafe condition triggered and failsafe reaction long: |- Before entering failsafe (Return, Land, Hold), wait COM_FAIL_ACT_T seconds in Hold mode - for the user to realize. + for the user to realize (link loss: see NAV_RCL_ACT). During that time the user can switch modes, but cannot take over control via the manual control override feature (see MAN_OVERRIDE_SPD). Afterwards the configured failsafe action is triggered and the user may use manual control override. @@ -442,6 +442,7 @@ parameters: The GCS connection loss failsafe will only be entered after a timeout, set by COM_DL_LOSS_T in seconds. Once the timeout occurs the selected action will be executed. + Autonomous modes continue during COM_FAIL_ACT_T and recover if regained. type: enum values: 0: Disabled @@ -457,6 +458,7 @@ parameters: long: |- The manual control loss failsafe will only be entered after a timeout, set by COM_RC_LOSS_T in seconds. + Autonomous modes continue during COM_FAIL_ACT_T and recover if regained. "Hold mode (no failsafe)" does not trigger the failsafe: instead, if manual control is lost while actively flying a manual mode, the vehicle switches to Hold as a regular diff --git a/src/modules/commander/failsafe/failsafe.cpp b/src/modules/commander/failsafe/failsafe.cpp index 596d1715036..d2e6f087b2c 100644 --- a/src/modules/commander/failsafe/failsafe.cpp +++ b/src/modules/commander/failsafe/failsafe.cpp @@ -70,11 +70,13 @@ FailsafeBase::ActionOptions Failsafe::fromNavDllOrRclActParam(int param_value) case gcs_connection_loss_failsafe_mode::Return_mode: options.action = Action::RTL; options.clear_condition = ClearCondition::OnModeChangeOrDisarm; + options.continue_during_delay = true; // a short loss has no effect in modes not flown with sticks break; case gcs_connection_loss_failsafe_mode::Land_mode: options.action = Action::Land; options.clear_condition = ClearCondition::OnModeChangeOrDisarm; + options.continue_during_delay = true; // a short loss has no effect in modes not flown with sticks break; case gcs_connection_loss_failsafe_mode::Terminate: diff --git a/src/modules/commander/failsafe/failsafe_test.cpp b/src/modules/commander/failsafe/failsafe_test.cpp index 496ba74e3c5..bf2d035e7fa 100644 --- a/src/modules/commander/failsafe/failsafe_test.cpp +++ b/src/modules/commander/failsafe/failsafe_test.cpp @@ -1502,3 +1502,174 @@ TEST_F(GeofenceFailsafeTest, ContGeofenceHoldIsLeftByModeSwitch) _flags.gf_cont_breach = true; EXPECT_EQ(update(failsafe), Action::Hold); } + +class RcLossFailsafeTest : public FailsafeTest +{ +public: + void SetUp() override + { + FailsafeTest::SetUp(); + int32_t nav_rcl_act = 2; // Return + param_set(param_handle(px4::params::NAV_RCL_ACT), &nav_rcl_act); + int32_t com_rcl_except = 0; + param_set(param_handle(px4::params::COM_RCL_EXCEPT), &com_rcl_except); + mode_util::getModeRequirements(vehicle_status_s::VEHICLE_TYPE_ROTARY_WING, failsafe_flags); + state.armed = true; + state.vehicle_type = vehicle_status_s::VEHICLE_TYPE_ROTARY_WING; + } + + uint8_t update(Failsafe &failsafe) { return failsafe.update(time, state, false, false, failsafe_flags); } + + failsafe_flags_s failsafe_flags{}; + FailsafeBase::State state{}; + hrt_abstime time{5_s}; +}; + +TEST_F(RcLossFailsafeTest, MissionContinuesDuringDelay) +{ + Failsafe failsafe(nullptr); + state.user_intended_mode = vehicle_status_s::NAVIGATION_STATE_AUTO_MISSION; + update(failsafe); + ASSERT_EQ(failsafe.selectedAction(), FailsafeBase::Action::None); + + // RC lost -> keep flying the mission during the delay + time += 10_ms; + failsafe_flags.manual_control_signal_lost = true; + EXPECT_EQ(update(failsafe), vehicle_status_s::NAVIGATION_STATE_AUTO_MISSION); + EXPECT_EQ(failsafe.selectedAction(), FailsafeBase::Action::Warn); + time += 4_s; + EXPECT_EQ(update(failsafe), vehicle_status_s::NAVIGATION_STATE_AUTO_MISSION); + EXPECT_EQ(failsafe.selectedAction(), FailsafeBase::Action::Warn); + + // Delay over -> RTL + time += 1100_ms; + update(failsafe); + EXPECT_EQ(failsafe.selectedAction(), FailsafeBase::Action::RTL); + + // Regaining RC after the delay does not abort the RTL + time += 10_ms; + failsafe_flags.manual_control_signal_lost = false; + update(failsafe); + EXPECT_EQ(failsafe.selectedAction(), FailsafeBase::Action::RTL); +} + +TEST_F(RcLossFailsafeTest, MissionRecoversDuringDelay) +{ + Failsafe failsafe(nullptr); + state.user_intended_mode = vehicle_status_s::NAVIGATION_STATE_AUTO_MISSION; + update(failsafe); + + time += 10_ms; + failsafe_flags.manual_control_signal_lost = true; + update(failsafe); + ASSERT_EQ(failsafe.selectedAction(), FailsafeBase::Action::Warn); + + // Regained during the delay -> no action, mission continues + time += 2_s; + failsafe_flags.manual_control_signal_lost = false; + EXPECT_EQ(update(failsafe), vehicle_status_s::NAVIGATION_STATE_AUTO_MISSION); + EXPECT_EQ(failsafe.selectedAction(), FailsafeBase::Action::None); + + // Much later, staying lost -> RTL after the delay + time += 30_s; + failsafe_flags.manual_control_signal_lost = true; + update(failsafe); + EXPECT_EQ(failsafe.selectedAction(), FailsafeBase::Action::Warn); + time += 5100_ms; + update(failsafe); + EXPECT_EQ(failsafe.selectedAction(), FailsafeBase::Action::RTL); +} + +TEST_F(RcLossFailsafeTest, ManualModeHoldsDuringDelayWithoutRecovery) +{ + Failsafe failsafe(nullptr); + state.user_intended_mode = vehicle_status_s::NAVIGATION_STATE_POSCTL; + update(failsafe); + + // RC lost in Position mode -> Hold to give the pilot time to react + time += 10_ms; + failsafe_flags.manual_control_signal_lost = true; + update(failsafe); + EXPECT_EQ(failsafe.selectedAction(), FailsafeBase::Action::Hold); + + // Regained during the delay -> stay in Hold, the pilot has to notice the failsafe + time += 2_s; + failsafe_flags.manual_control_signal_lost = false; + EXPECT_EQ(update(failsafe), vehicle_status_s::NAVIGATION_STATE_POSCTL); + EXPECT_EQ(failsafe.selectedAction(), FailsafeBase::Action::Hold); + + // Delay over -> RTL as before + time += 3100_ms; + update(failsafe); + EXPECT_EQ(failsafe.selectedAction(), FailsafeBase::Action::RTL); +} + +TEST_F(RcLossFailsafeTest, HoldActionHoldsImmediately) +{ + int32_t nav_rcl_act = 1; // Hold + param_set(param_handle(px4::params::NAV_RCL_ACT), &nav_rcl_act); + + // No delay for Hold, also not in an autonomous mode + Failsafe failsafe(nullptr); + state.user_intended_mode = vehicle_status_s::NAVIGATION_STATE_AUTO_MISSION; + update(failsafe); + + time += 10_ms; + failsafe_flags.manual_control_signal_lost = true; + update(failsafe); + EXPECT_EQ(failsafe.selectedAction(), FailsafeBase::Action::Hold); + + // Regained -> stay in Hold + time += 1_s; + failsafe_flags.manual_control_signal_lost = false; + update(failsafe); + EXPECT_EQ(failsafe.selectedAction(), FailsafeBase::Action::Hold); +} + +TEST_F(RcLossFailsafeTest, MissionHoldsIfOtherFailsafeNeedsHold) +{ + int32_t com_gnssloss_act = 1; // Return, which holds during the delay + param_set(param_handle(px4::params::COM_GNSSLOSS_ACT), &com_gnssloss_act); + + Failsafe failsafe(nullptr); + state.user_intended_mode = vehicle_status_s::NAVIGATION_STATE_AUTO_MISSION; + update(failsafe); + + time += 10_ms; + failsafe_flags.manual_control_signal_lost = true; + failsafe_flags.gnss_lost = true; + update(failsafe); + EXPECT_EQ(failsafe.selectedAction(), FailsafeBase::Action::Hold); +} + +TEST_F(RcLossFailsafeTest, MissionIntermittentLossEndsInRtl) +{ + Failsafe failsafe(nullptr); + state.user_intended_mode = vehicle_status_s::NAVIGATION_STATE_AUTO_MISSION; + update(failsafe); + + // RC toggling 1s lost / 1s regained: each loss recovers within the delay, + // but the delay shrinks faster than it grows back, so RTL is triggered eventually + bool rtl_triggered = false; + + for (int cycle = 0; cycle < 20 && !rtl_triggered; ++cycle) { + failsafe_flags.manual_control_signal_lost = true; + + for (int i = 0; i < 10; ++i) { + time += 100_ms; + update(failsafe); + rtl_triggered |= failsafe.selectedAction() == FailsafeBase::Action::RTL; + } + + failsafe_flags.manual_control_signal_lost = false; + + for (int i = 0; i < 10; ++i) { + time += 100_ms; + update(failsafe); + rtl_triggered |= failsafe.selectedAction() == FailsafeBase::Action::RTL; + } + } + + EXPECT_TRUE(rtl_triggered); + EXPECT_EQ(failsafe.selectedAction(), FailsafeBase::Action::RTL); +} diff --git a/src/modules/commander/failsafe/framework.cpp b/src/modules/commander/failsafe/framework.cpp index a0661c4b85e..9ffab210220 100644 --- a/src/modules/commander/failsafe/framework.cpp +++ b/src/modules/commander/failsafe/framework.cpp @@ -193,7 +193,7 @@ void FailsafeBase::notifyUser(uint8_t user_intended_mode, Action action, Action static_assert((int)failsafe_action_t::_max + 1 == (int)Action::Count, "Enum needs to be extended"); failsafe_cause_t failsafe_cause = (failsafe_cause_t)cause; - if (action == Action::Hold && delayed_action != Action::None) { + if (delayed_action != Action::None) { failsafe_action_t failsafe_action = (failsafe_action_t)delayed_action; if (cause == Cause::Generic) { @@ -216,7 +216,12 @@ void FailsafeBase::notifyUser(uint8_t user_intended_mode, Action action, Action (uint16_t) delay_s, failsafe_cause); } - mavlink_log_critical(&_mavlink_log_pub, "Failsafe activated: entering Hold for %i seconds\t", delay_s); + if (action == Action::Hold) { + mavlink_log_critical(&_mavlink_log_pub, "Failsafe activated: entering Hold for %i seconds\t", delay_s); + + } else { + mavlink_log_critical(&_mavlink_log_pub, "Failsafe activated: continuing for %i seconds\t", delay_s); + } } else { // no delay failsafe_action_t failsafe_action = (failsafe_action_t)action; @@ -404,10 +409,11 @@ void FailsafeBase::removeAction(ActionOptions &action) const // clear_condition to avoid triggering a failsafe after deferring is disabled. const bool remove_while_deferring = _defer_failsafes && action.can_be_deferred; - // The action did not start yet (still in the delayed Hold), so recovering is allowed. + // The action did not start yet, so recovering is allowed. Warn during the delay means the mode + // kept flying (continue_during_delay). A pilot flying manually is in Hold and has to notice it. // _current_delay is shared, so also check that no action beyond Hold is already running. - const bool remove_during_delay = action.clear_during_delay && _current_delay > 0 - && _selected_action <= Action::Hold; + const bool remove_during_delay = _current_delay > 0 && _selected_action <= Action::Hold + && (action.clear_during_delay || _selected_action == Action::Warn); if (action.clear_condition == ClearCondition::WhenConditionClears || remove_while_deferring || remove_during_delay) { // Remove action @@ -467,6 +473,7 @@ void FailsafeBase::getSelectedAction(const State &state, const failsafe_flags_s Action &selected_action = returned_state.action; UserTakeoverAllowed allow_user_takeover = UserTakeoverAllowed::Always; bool allow_failsafe_to_be_deferred{true}; + bool continue_during_delay{true}; // Select the worst action based on the current active actions for (int action_idx = 0; action_idx < max_num_actions; ++action_idx) { @@ -490,6 +497,10 @@ void FailsafeBase::getSelectedAction(const State &state, const failsafe_flags_s if (!cur_action.can_be_deferred) { allow_failsafe_to_be_deferred = false; } + + if (cur_action.action > Action::Warn && !cur_action.continue_during_delay) { + continue_during_delay = false; + } } } @@ -509,8 +520,17 @@ void FailsafeBase::getSelectedAction(const State &state, const failsafe_flags_s if (_current_delay > 0 && !_user_takeover_active && allow_user_takeover <= UserTakeoverAllowed::AlwaysModeSwitchOnly && action_can_be_delayed) { returned_state.delayed_action = selected_action; - selected_action = Action::Hold; allow_user_takeover = UserTakeoverAllowed::AlwaysModeSwitchOnly; + + const bool flying_with_sticks = status_flags.mode_req_manual_control & (1u << state.user_intended_mode); + + if (continue_during_delay && !flying_with_sticks) { + // The Hold gives a pilot flying with sticks time to react, other modes keep flying their plan instead + selected_action = Action::Warn; + + } else { + selected_action = Action::Hold; + } } // User takeover interrupting a failsafe is triggered by a change of the user-intended mode diff --git a/src/modules/commander/failsafe/framework.h b/src/modules/commander/failsafe/framework.h index 23be7df1637..431c670c350 100644 --- a/src/modules/commander/failsafe/framework.h +++ b/src/modules/commander/failsafe/framework.h @@ -196,6 +196,7 @@ protected: UserTakeoverAllowed allow_user_takeover{UserTakeoverAllowed::Auto}; bool can_be_deferred{true}; bool clear_during_delay{false}; ///< also clear if the condition clears during the Hold delay (COM_FAIL_ACT_T) + bool continue_during_delay{false}; ///< in a mode not requiring sticks, keep flying it instead of Hold during the delay, and clear if the condition clears in that time bool state_failure{false}; ///< used when the clear_condition isn't set to clear immediately bool activated{false}; ///< true if checkFailsafe was called during current update