From 6a6d76cf6a93d113f370afd8b0b6b24e4f2c699e Mon Sep 17 00:00:00 2001 From: jonas Date: Fri, 20 Nov 2026 09:44:57 +0100 Subject: [PATCH] refactor(navigator): ask the geofence if the vehicle keeps the margin The fence checks of the route planner found out whether the vehicle is already inside the contingency margin with a path check of their own: a path that starts and ends at the vehicle, kept in a spare entry of the path batch to stay off the stack. That was hard to follow, and it saved no stack (measured for fmu-v6c). Geofence::isInsideContGeofence() answers the same question by name. The planner calls it now, and the margin of a segment is a constant. The results are the same. Assisted-by: Claude:claude-fable-5-2 Signed-off-by: jonas --- .../navigator/mission_route_geofence.cpp | 52 +++++-------------- .../navigator/mission_route_geofence.h | 1 + 2 files changed, 15 insertions(+), 38 deletions(-) diff --git a/src/modules/navigator/mission_route_geofence.cpp b/src/modules/navigator/mission_route_geofence.cpp index 9975b4dc74c..430907d4398 100644 --- a/src/modules/navigator/mission_route_geofence.cpp +++ b/src/modules/navigator/mission_route_geofence.cpp @@ -79,25 +79,6 @@ bool checkBatch(Geofence &geofence, PathBatch &batch) return true; } -// Start a path at the vehicle with the margin it can keep: GF_CONT_H_MARGIN, or 0 if the vehicle is already closer -// to the fence. Example: the Return that GF_CONT_ACTION starts there is checked against the fence itself. -bool startVehiclePath(Geofence &geofence, const Position &vehicle, Geofence::PathCheck &path) -{ - path = {{vehicle.lat, vehicle.lon}, {vehicle.lat, vehicle.lon}, 0.f, geofence.getContGeofenceMargin()}; - bool keeps_margin = true; - - // A path without length tells whether the vehicle keeps the margin, as for the first waypoint of a mission. - if (path.margin > 0.f && !geofence.checkPathBatch(&path, 1, &keeps_margin)) { - return false; - } - - if (!keeps_margin) { - path.margin = 0.f; - } - - return true; -} - } // namespace // Polygons and circles only: a vehicle above the altitude limits or too far from Home does not recover, so its @@ -107,6 +88,13 @@ bool GeofencePathChecker::recovering(const Position &vehicle) const return !_geofence.isInsideHorizontalFences(vehicle.lat, vehicle.lon, vehicle.alt); } +// The margin a segment from the vehicle keeps: GF_CONT_H_MARGIN, or 0 if the vehicle is already closer to the fence. +// Example: the Return that GF_CONT_ACTION starts inside the margin is checked against the fence itself. +float GeofencePathChecker::vehicleMargin(const Position &vehicle) const +{ + return _geofence.isInsideContGeofence(vehicle.lat, vehicle.lon) ? _geofence.getContGeofenceMargin() : 0.f; +} + bool GeofencePathChecker::pruneCandidates(ProjectionReferenceBatch &batch) const { if (!_geofence.isReadyForPathChecks()) { @@ -129,25 +117,16 @@ bool GeofencePathChecker::pruneCandidates(ProjectionReferenceBatch &batch) const continue; } - // Segments keep GF_CONT_H_MARGIN from the polygons and circles, like the legs of a mission. - float margin = _geofence.getContGeofenceMargin(); + // The reference is the vehicle (no source index) or a safe point. + const bool is_vehicle = reference.source_index < 0; - if (reference.source_index < 0) { + if (is_vehicle) { // A vehicle in recovery keeps all its candidates unchecked: set bits 0 to count - 1. if (recovering(anchor)) { shared_paths.valid_masks[i] = static_cast((1U << reference.candidate_buffer.count) - 1U); continue; } - // Scratch off the stack: a full batch is checked at once, so the next path of the batch is always free. - Geofence::PathCheck &scratch = shared_paths.paths[shared_paths.count]; - - if (!startVehiclePath(_geofence, anchor, scratch)) { - return false; - } - - margin = scratch.margin; - } else if (!_geofence.checkPointAgainstAllGeofences(anchor.lat, anchor.lon, anchor.alt) || !_geofence.isCloserThanContMaxDistToHome(anchor.lat, anchor.lon)) { // A safe point outside the allowed area keeps no candidates: a segment entirely inside an exclusion @@ -155,6 +134,9 @@ bool GeofencePathChecker::pruneCandidates(ProjectionReferenceBatch &batch) const continue; } + // Segments keep GF_CONT_H_MARGIN from the polygons and circles, like the legs of a mission. + const float margin = is_vehicle ? vehicleMargin(anchor) : _geofence.getContGeofenceMargin(); + for (uint8_t j = 0; j < reference.candidate_buffer.count; ++j) { const Position &projection = reference.candidate_buffer.candidates[j].projection; @@ -213,13 +195,7 @@ bool GeofencePathChecker::checkPath(const Position &vehicle, const Position &end return true; } - Geofence::PathCheck path; - - if (!startVehiclePath(_geofence, vehicle, path)) { - return false; - } - - path.end = {end.lat, end.lon}; + const Geofence::PathCheck path{{vehicle.lat, vehicle.lon}, {end.lat, end.lon}, 0.f, vehicleMargin(vehicle)}; return _geofence.checkPathBatch(&path, 1, &clear); } diff --git a/src/modules/navigator/mission_route_geofence.h b/src/modules/navigator/mission_route_geofence.h index 2565f8e714e..633d1cf50b3 100644 --- a/src/modules/navigator/mission_route_geofence.h +++ b/src/modules/navigator/mission_route_geofence.h @@ -64,6 +64,7 @@ public: private: bool recovering(const Position &vehicle) const; + float vehicleMargin(const Position &vehicle) const; Geofence &_geofence; };