From 908f99ad32f3bd2ab34994cdc0f2350e9d4bf948 Mon Sep 17 00:00:00 2001 From: Jonas Perolini Date: Tue, 10 Nov 2026 10:58:16 +0100 Subject: [PATCH] refactor(navigator): trim the Return destination selection stack (#29669) setRtlTypeAndDestination() kept room in its frame for a battery_status_s, used only by the RTL_TYPE 6 battery loop, and for a copy of the global position, also while it looked up rally points and VTOL landing approaches. getVtolLandApproachesNearLocation() returned a different object on its early exit, so GCC did not apply NRVO and its 208 B land_approaches_s result was on the stack twice. Read the batteries in a separate function, take the global position by reference and return one named result. On fmu-v6c the two frames drop from 336 B to 176 B and from 272 B to 80 B. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: jonas --- .../mission_route_land_approaches.cpp | 20 ++++----- src/modules/navigator/rtl.cpp | 45 +++++++++++-------- src/modules/navigator/rtl.h | 5 +++ 3 files changed, 42 insertions(+), 28 deletions(-) diff --git a/src/modules/navigator/mission_route_land_approaches.cpp b/src/modules/navigator/mission_route_land_approaches.cpp index 2ee3f5e382c..05a701ae6a7 100644 --- a/src/modules/navigator/mission_route_land_approaches.cpp +++ b/src/modules/navigator/mission_route_land_approaches.cpp @@ -159,34 +159,34 @@ loiter_point_s chooseBestLandingApproach(const land_approaches_s &approaches, co return best_index >= 0 ? approaches.approaches[best_index] : loiter_point_s{}; } +// One named result on every path lets GCC build it in the caller's storage (NRVO), +// otherwise the struct is on the stack twice. land_approaches_s getVtolLandApproachesNearLocation(const Provider &provider, const PositionYawSetpoint &rtl_position, float home_altitude_amsl) { + land_approaches_s vtol_land_approaches{}; int safe_point_index = -1; Position safe_point_position{}; - if (!findAssociatedSafePointIndex(provider, rtl_position, home_altitude_amsl, safe_point_index, safe_point_position)) { - return {}; + if (findAssociatedSafePointIndex(provider, rtl_position, home_altitude_amsl, safe_point_index, safe_point_position)) { + vtol_land_approaches.land_location_lat_lon = matrix::Vector2d(safe_point_position.lat, safe_point_position.lon); + scanVtolLandApproachBlock(provider, safe_point_index, home_altitude_amsl, &vtol_land_approaches); } - land_approaches_s vtol_land_approaches{}; - vtol_land_approaches.land_location_lat_lon = matrix::Vector2d(safe_point_position.lat, safe_point_position.lon); - scanVtolLandApproachBlock(provider, safe_point_index, home_altitude_amsl, &vtol_land_approaches); return vtol_land_approaches; } land_approaches_s getVtolLandApproachesAtSafePointIndex(const Provider &provider, int safe_point_index, float home_altitude_amsl) { + land_approaches_s vtol_land_approaches{}; Position safe_point_position{}; - if (!loadSafePointPosition(provider, safe_point_index, home_altitude_amsl, safe_point_position)) { - return {}; + if (loadSafePointPosition(provider, safe_point_index, home_altitude_amsl, safe_point_position)) { + vtol_land_approaches.land_location_lat_lon = matrix::Vector2d(safe_point_position.lat, safe_point_position.lon); + scanVtolLandApproachBlock(provider, safe_point_index, home_altitude_amsl, &vtol_land_approaches); } - land_approaches_s vtol_land_approaches{}; - vtol_land_approaches.land_location_lat_lon = matrix::Vector2d(safe_point_position.lat, safe_point_position.lon); - scanVtolLandApproachBlock(provider, safe_point_index, home_altitude_amsl, &vtol_land_approaches); return vtol_land_approaches; } diff --git a/src/modules/navigator/rtl.cpp b/src/modules/navigator/rtl.cpp index 330cacd2fcc..691ba3d7624 100644 --- a/src/modules/navigator/rtl.cpp +++ b/src/modules/navigator/rtl.cpp @@ -277,23 +277,7 @@ void RTL::setRtlTypeAndDestination() const rtl_time_estimate_s time_to_home = _rtl_direct.calc_rtl_time_estimate(); - // Take the worst (smallest) remaining time across all connected batteries, matching the - // battery_low_remaining_time failsafe check in BatteryChecks::rtlEstimateCheck() - float worst_battery_time_s{NAN}; - - for (auto &battery_sub : _battery_status_subs) { - battery_status_s battery; - - if (!battery_sub.copy(&battery)) { - continue; - } - - if (battery.connected - && PX4_ISFINITE(battery.time_remaining_s) - && (!PX4_ISFINITE(worst_battery_time_s) || (battery.time_remaining_s < worst_battery_time_s))) { - worst_battery_time_s = battery.time_remaining_s; - } - } + const float worst_battery_time_s = worstBatteryTimeRemaining(); const bool home_within_reach = time_to_home.valid && PX4_ISFINITE(worst_battery_time_s) @@ -399,7 +383,7 @@ void RTL::setRtlTypeAndDestination() planner.updateDestination(planner_destination); } - const vehicle_global_position_s gpos = _global_pos_sub.get(); + const vehicle_global_position_s &gpos = _global_pos_sub.get(); const bool gpos_recent = gpos.timestamp > 0 && hrt_elapsed_time(&gpos.timestamp) < 10_s; @@ -421,6 +405,31 @@ void RTL::setRtlTypeAndDestination() _rtl_status_pub.publish(rtl_status); } +// Keep out of setRtlTypeAndDestination(): inlined, the battery_status_s copy would stay +// on its stack while it selects the destination. +float RTL::worstBatteryTimeRemaining() +{ + // Take the worst (smallest) remaining time across all connected batteries, matching the + // battery_low_remaining_time failsafe check in BatteryChecks::rtlEstimateCheck() + float worst_battery_time_s{NAN}; + + for (auto &battery_sub : _battery_status_subs) { + battery_status_s battery; + + if (!battery_sub.copy(&battery)) { + continue; + } + + if (battery.connected + && PX4_ISFINITE(battery.time_remaining_s) + && (!PX4_ISFINITE(worst_battery_time_s) || (battery.time_remaining_s < worst_battery_time_s))) { + worst_battery_time_s = battery.time_remaining_s; + } + } + + return worst_battery_time_s; +} + PositionYawSetpoint RTL::findClosestSafePoint(float min_dist, uint8_t &safe_point_index) { #if defined(CONFIG_MODULES_VTOL_ATT_CONTROL) && CONFIG_MODULES_VTOL_ATT_CONTROL diff --git a/src/modules/navigator/rtl.h b/src/modules/navigator/rtl.h index 2d8cab77cee..c776211b488 100644 --- a/src/modules/navigator/rtl.h +++ b/src/modules/navigator/rtl.h @@ -136,6 +136,11 @@ private: */ PositionYawSetpoint findClosestSafePoint(float min_dist, uint8_t &safe_point_index); + /** + * @brief Smallest remaining time [s] of the connected batteries, NAN if none reports one. + */ + float worstBatteryTimeRemaining(); + /** * @brief Set the position of the land start marker in the planned mission as destination. *