diff --git a/src/modules/navigator/rtl.cpp b/src/modules/navigator/rtl.cpp index d93d699936b..2d2a5c512e6 100644 --- a/src/modules/navigator/rtl.cpp +++ b/src/modules/navigator/rtl.cpp @@ -97,7 +97,7 @@ void RTL::on_inactive() _destination_check_time = now; const ReturnSelection selection = selectReturn(); applyReturn(selection); - publishRemainingTimeEstimate(); + publishRemainingTimeEstimate(selection.fallback_safe_time_estimate); } } @@ -113,7 +113,7 @@ void RTL::on_inactivation() _rtl_direct.run(false); } -void RTL::publishRemainingTimeEstimate() +void RTL::publishRemainingTimeEstimate(float fallback_safe_time_estimate) { const bool global_position_recently_updated = _global_pos_sub.get().timestamp > 0 && hrt_elapsed_time(&_global_pos_sub.get().timestamp) < 10_s; @@ -133,8 +133,8 @@ void RTL::publishRemainingTimeEstimate() // RTL_TYPE 6 while inactive: the lower estimate of Home and its rally point, for the distance-aware return failsafe estimated_time.min_safe_time_estimate = estimated_time.safe_time_estimate; - if (estimated_time.valid && !isActive() && PX4_ISFINITE(_alternative_safe_time_estimate)) { - estimated_time.min_safe_time_estimate = min(estimated_time.safe_time_estimate, _alternative_safe_time_estimate); + if (estimated_time.valid && PX4_ISFINITE(fallback_safe_time_estimate)) { + estimated_time.min_safe_time_estimate = min(estimated_time.safe_time_estimate, fallback_safe_time_estimate); } estimated_time.timestamp = hrt_absolute_time(); @@ -192,7 +192,7 @@ void RTL::on_active() if ((now - _destination_check_time) > RTL_REPLAN_INTERVAL) { _destination_check_time = now; - publishRemainingTimeEstimate(); + publishRemainingTimeEstimate(NAN); } } @@ -320,14 +320,12 @@ void RTL::selectHomeIfReachable(ReturnSelection &selection) } // If no rally points are closer (or none are defined), fall back to home (already set as the destination) - } + } else if (has_rally_point) { + // Only a quicker rally point can extend the available return time. + const rtl_time_estimate_s fallback = estimateDirectReturn(rally_point); - if (has_rally_point) { - // Estimate the destination not selected, for min_safe_time_estimate - const rtl_time_estimate_s alternative = home_within_reach ? estimateDirectReturn(rally_point) : time_to_home; - - if (alternative.valid) { - selection.alternative_safe_time_estimate = alternative.safe_time_estimate; + if (fallback.valid && fallback.safe_time_estimate < time_to_home.safe_time_estimate) { + selection.fallback_safe_time_estimate = fallback.safe_time_estimate; } } } @@ -400,7 +398,6 @@ void RTL::applyReturn(const ReturnSelection &selection) selection.destination_type == DestinationType::DESTINATION_TYPE_CURRENT_POSITION); _rtl_type = new_rtl_type; - _alternative_safe_time_estimate = selection.alternative_safe_time_estimate; #if CONFIG_NAVIGATOR_GEOFENCE_AVOIDANCE diff --git a/src/modules/navigator/rtl.h b/src/modules/navigator/rtl.h index 6856b0b1fc8..62dff013f97 100644 --- a/src/modules/navigator/rtl.h +++ b/src/modules/navigator/rtl.h @@ -129,7 +129,7 @@ private: uint8_t safe_point_index{RTL_STATUS_NO_SAFE_POINT}; bool home_has_land_approach{false}; ///< Home has a VTOL land approach, for the types that look bool rally_point_has_land_approach{false}; ///< A rally point searched for this selection has one - float alternative_safe_time_estimate{NAN}; ///< [s] RTL_TYPE 6: estimate to the destination not selected + float fallback_safe_time_estimate{NAN}; ///< [s] Safe return time to an available fallback, or NAN }; /** Selects the destination of the current RTL_TYPE, then applies it. */ @@ -162,7 +162,7 @@ private: * * While inactive, RTL_TYPE 6 also reports the lower estimate of Home and its rally point. */ - void publishRemainingTimeEstimate(); + void publishRemainingTimeEstimate(float fallback_safe_time_estimate); /** * @brief Find the closest rally point within min_dist @@ -227,8 +227,6 @@ private: RtlDirect _rtl_direct; - float _alternative_safe_time_estimate{NAN}; ///< [s] from the latest selection - bool _enforce_rtl_alt{false}; DEFINE_PARAMETERS( diff --git a/src/modules/navigator/test/test_RTL.cpp b/src/modules/navigator/test/test_RTL.cpp index ba9b3bd8c6f..878dd10cb36 100644 --- a/src/modules/navigator/test/test_RTL.cpp +++ b/src/modules/navigator/test/test_RTL.cpp @@ -255,7 +255,7 @@ public: bool directActiveForTest() { return _rtl_direct.isActive(); } // an inactive evaluation of the given RTL_TYPE with the latest vehicle state - void evaluateReturnTypeForTest(int32_t rtl_type) + float evaluateReturnTypeForTest(int32_t rtl_type) { _param_rtl_type.set(rtl_type); _global_pos_sub.update(); @@ -263,7 +263,9 @@ public: _mission_sub.update(); _home_pos_sub.update(); _wind_sub.update(); - setRtlTypeAndDestination(); + const ReturnSelection selection = selectReturn(); + applyReturn(selection); + return selection.fallback_safe_time_estimate; } void activateReturnTypeForTest(int32_t rtl_type) @@ -276,7 +278,7 @@ public: rtl_time_estimate_s directEstimateForTest() { return _rtl_direct.calc_rtl_time_estimate(); } // RTL publishes its estimate every few seconds, after it selected the destination - void publishTimeEstimateForTest() { publishRemainingTimeEstimate(); } + void publishTimeEstimateForTest(float fallback) { publishRemainingTimeEstimate(fallback); } // the estimate RTL publishes while the return type is mission-based rtl_time_estimate_s missionEstimateForTest() { return _rtl_mission_type_handle->calc_rtl_time_estimate(); } @@ -3559,9 +3561,9 @@ protected: } // the estimate RTL publishes after an evaluation - rtl_time_estimate_s publishedEstimate() + rtl_time_estimate_s publishedEstimate(float fallback) { - _rtl.publishTimeEstimateForTest(); + _rtl.publishTimeEstimateForTest(fallback); EXPECT_TRUE(_estimate_sub.update()); EXPECT_TRUE(_estimate_sub.get().valid); return _estimate_sub.get(); @@ -3585,15 +3587,15 @@ TEST_F(RTLLowestEstimateTest, Type6LowestEstimateDoesNotDependOnTheBattery) { // WHEN: the battery reaches Home publishBatteryTimeRemaining(3600.f); - _rtl.evaluateReturnTypeForTest(6); + const float home_fallback = _rtl.evaluateReturnTypeForTest(6); ASSERT_FALSE(selectsRallyPoint()); - const rtl_time_estimate_s to_home = publishedEstimate(); + const rtl_time_estimate_s to_home = publishedEstimate(home_fallback); // WHEN: the battery does not reach Home publishBatteryTimeRemaining(1.f); - _rtl.evaluateReturnTypeForTest(6); + const float rally_fallback = _rtl.evaluateReturnTypeForTest(6); ASSERT_TRUE(selectsRallyPoint()); - const rtl_time_estimate_s to_rally_point = publishedEstimate(); + const rtl_time_estimate_s to_rally_point = publishedEstimate(rally_fallback); // THEN: the selected estimate drops from Home's to the rally point's, the lowest one stays the rally point's EXPECT_GT(to_home.safe_time_estimate, to_rally_point.safe_time_estimate + 1.f); @@ -3601,20 +3603,40 @@ TEST_F(RTLLowestEstimateTest, Type6LowestEstimateDoesNotDependOnTheBattery) EXPECT_FLOAT_EQ(to_rally_point.min_safe_time_estimate, to_rally_point.safe_time_estimate); } +TEST_F(RTLLowestEstimateTest, CloserButSlowerRallyDoesNotProvideFallbackTime) +{ + const PositionYawSetpoint rally = makePositionYawSetpointFromOffset(kBaseLat, kBaseLon, 100.f, 0.f, kAlt + 500.f); + loadSafePointsIntoRouteCache({makeSafePointItem(rally.lat, rally.lon, rally.alt, NAV_FRAME_GLOBAL)}); + + publishBatteryTimeRemaining(3600.f); + const float fallback = _rtl.evaluateReturnTypeForTest(6); + ASSERT_FALSE(selectsRallyPoint()); + EXPECT_TRUE(std::isnan(fallback)); + const rtl_time_estimate_s home = publishedEstimate(fallback); + EXPECT_FLOAT_EQ(home.min_safe_time_estimate, home.safe_time_estimate); + + publishBatteryTimeRemaining(1.f); + const float selected_rally_fallback = _rtl.evaluateReturnTypeForTest(6); + ASSERT_TRUE(selectsRallyPoint()); + const rtl_time_estimate_s selected_rally = publishedEstimate(selected_rally_fallback); + EXPECT_GT(selected_rally.safe_time_estimate, home.safe_time_estimate); + EXPECT_FLOAT_EQ(selected_rally.min_safe_time_estimate, selected_rally.safe_time_estimate); +} + // WHY: only RTL_TYPE 6 chooses between two destinations by the battery. // WHAT: without a second destination, the lowest estimate is the one to the selected destination. TEST_F(RTLLowestEstimateTest, LowestEstimateIsTheSelectedOneWithoutAnotherDestination) { // GIVEN: RTL_TYPE 6 going Home with the rally point as the other destination publishBatteryTimeRemaining(3600.f); - _rtl.evaluateReturnTypeForTest(6); - const rtl_time_estimate_s with_rally_point = publishedEstimate(); + const float home_fallback = _rtl.evaluateReturnTypeForTest(6); + const rtl_time_estimate_s with_rally_point = publishedEstimate(home_fallback); ASSERT_LT(with_rally_point.min_safe_time_estimate, with_rally_point.safe_time_estimate); // WHEN: RTL_TYPE 0 takes the closest destination, the rally point - _rtl.evaluateReturnTypeForTest(0); + const float closest_fallback = _rtl.evaluateReturnTypeForTest(0); ASSERT_TRUE(selectsRallyPoint()); - const rtl_time_estimate_s closest = publishedEstimate(); + const rtl_time_estimate_s closest = publishedEstimate(closest_fallback); // THEN: its estimate is also the lowest one EXPECT_FLOAT_EQ(closest.min_safe_time_estimate, closest.safe_time_estimate); @@ -3623,9 +3645,9 @@ TEST_F(RTLLowestEstimateTest, LowestEstimateIsTheSelectedOneWithoutAnotherDestin // WHEN: RTL_TYPE 6 has no rally point closer than Home const PositionYawSetpoint far_rally_point = makePositionYawSetpointFromOffset(kBaseLat, kBaseLon, 400.f, 0.f, kAlt); loadSafePointsIntoRouteCache({makeSafePointItem(far_rally_point.lat, far_rally_point.lon, kAlt, NAV_FRAME_GLOBAL)}); - _rtl.evaluateReturnTypeForTest(6); + const float home_only_fallback = _rtl.evaluateReturnTypeForTest(6); ASSERT_FALSE(selectsRallyPoint()); - const rtl_time_estimate_s home_only = publishedEstimate(); + const rtl_time_estimate_s home_only = publishedEstimate(home_only_fallback); // THEN: Home's estimate is also the lowest one EXPECT_FLOAT_EQ(home_only.min_safe_time_estimate, home_only.safe_time_estimate); @@ -3638,14 +3660,14 @@ TEST_F(RTLLowestEstimateTest, Type6LowestEstimateFollowsTheActiveReturn) { // GIVEN: the battery reaches Home, and the rally point's estimate is the lowest one publishBatteryTimeRemaining(3600.f); - _rtl.evaluateReturnTypeForTest(6); - const rtl_time_estimate_s inactive = publishedEstimate(); + const float home_fallback = _rtl.evaluateReturnTypeForTest(6); + const rtl_time_estimate_s inactive = publishedEstimate(home_fallback); ASSERT_LT(inactive.min_safe_time_estimate, inactive.safe_time_estimate); // WHEN: Return activates _rtl.activateReturnTypeForTest(6); ASSERT_FALSE(selectsRallyPoint()); - const rtl_time_estimate_s active = publishedEstimate(); + const rtl_time_estimate_s active = publishedEstimate(NAN); // THEN: the lowest estimate is the one to Home EXPECT_FLOAT_EQ(active.min_safe_time_estimate, active.safe_time_estimate);