From 8f32b783d1111a7a2fcc08ac31f0c6cade9c6b28 Mon Sep 17 00:00:00 2001 From: jonas Date: Wed, 18 Nov 2026 22:30:42 +0100 Subject: [PATCH] refactor(navigator): keep the pre-geofence out of the geofence point check checkPointAgainstAllGeofences() is the geofence check again, as on main, so geofence_allows_position() no longer needs its own copy of it. The mission check adds the pre-geofence limits from Home for every position. Its paths already keep the margin, also for the first position, whose path has no length. The pre-geofence triggers are set in one place after the geofence ones. Disabling the pre-geofence clears them with the next position, as changing GF_ACTION does for the geofence, so the separate release block goes. The orbit and figure-eight refusals send no event, like their geofence twins. Assisted-by: Claude:claude-fable-5-2 --- src/modules/navigator/geofence.cpp | 12 +--- src/modules/navigator/geofence.h | 2 +- .../navigator/mission_feasibility_checker.cpp | 8 ++- src/modules/navigator/navigator.h | 2 +- src/modules/navigator/navigator_main.cpp | 63 ++++++------------- .../test_mission_feasibility_geofence.cpp | 4 +- .../test/test_pre_geofence_breach.cpp | 19 +----- 7 files changed, 32 insertions(+), 78 deletions(-) diff --git a/src/modules/navigator/geofence.cpp b/src/modules/navigator/geofence.cpp index 3226fe164b2..fba0eec21dc 100644 --- a/src/modules/navigator/geofence.cpp +++ b/src/modules/navigator/geofence.cpp @@ -683,9 +683,7 @@ bool Geofence::checkCurrentPositionRequirementsForGeofence(const PolygonInfo &po bool Geofence::checkPointAgainstAllGeofences(double lat, double lon, float altitude) { const bool inside_fence = isCloserThanMaxDistToHome(lat, lon, altitude) && isBelowMaxAltitude(altitude) - && isInsidePolygonOrCircle(lat, lon, altitude) - && isCloserThanPreMaxDistToHome(lat, lon) && isBelowPreMaxAltitude(altitude) - && isInsidePreGeofence(lat, lon); + && isInsidePolygonOrCircle(lat, lon, altitude); return inside_fence; } @@ -894,14 +892,8 @@ bool Geofence::isWithinHomeDistance(float max_distance, double lat, double lon) if (max_distance > FLT_EPSILON && _navigator->home_global_position_valid()) { - float dist_xy = -1.0f; - float dist_z = -1.0f; const home_position_s &home = *_navigator->get_home_position(); - - // Only the horizontal distance is used. - get_distance_to_point_global_wgs84(lat, lon, home.alt, home.lat, home.lon, home.alt, &dist_xy, &dist_z); - - inside_fence = dist_xy < max_distance; + inside_fence = get_distance_to_next_waypoint(lat, lon, home.lat, home.lon) < max_distance; } return inside_fence; diff --git a/src/modules/navigator/geofence.h b/src/modules/navigator/geofence.h index 55c4b6c6bce..c3fa29fcb31 100644 --- a/src/modules/navigator/geofence.h +++ b/src/modules/navigator/geofence.h @@ -91,7 +91,7 @@ public: /** * Check if a 3D point passes the Geofence test. - * Checks max distance, max altitude, inside polygon or circle, and the pre-geofence limits. + * Checks max distance, max altitude, inside polygon or circle. * In addition to checkPolygons(), this takes all additional parameters into account. * * @return false for a geofence violation diff --git a/src/modules/navigator/mission_feasibility_checker.cpp b/src/modules/navigator/mission_feasibility_checker.cpp index 64de7a0ec0f..6ff0a231731 100644 --- a/src/modules/navigator/mission_feasibility_checker.cpp +++ b/src/modules/navigator/mission_feasibility_checker.cpp @@ -208,9 +208,7 @@ MissionFeasibilityChecker::checkMissionAgainstGeofence(const mission_s &mission, // Home-distance and altitude limits are separate from the horizontal fence shapes. point_valid = geofence.isCloserThanMaxDistToHome(missionitem.lat, missionitem.lon, missionitem.altitude) && geofence.isBelowMaxAltitude(missionitem.altitude) - && geofence.isWithinAltitudeBand(missionitem.altitude) - && geofence.isCloserThanPreMaxDistToHome(missionitem.lat, missionitem.lon) - && geofence.isBelowPreMaxAltitude(missionitem.altitude); + && geofence.isWithinAltitudeBand(missionitem.altitude); } } @@ -220,6 +218,10 @@ MissionFeasibilityChecker::checkMissionAgainstGeofence(const mission_s &mission, missionitem.altitude); #endif // CONFIG_NAVIGATOR_GEOFENCE_PATH_CHECKS + // The pre-geofence limits from Home apply to every position. The paths keep its margin to the plan. + point_valid = point_valid && geofence.isCloserThanPreMaxDistToHome(missionitem.lat, missionitem.lon) + && geofence.isBelowPreMaxAltitude(missionitem.altitude); + if (!point_valid) { failure = Failure::Waypoint; failed_item = i; diff --git a/src/modules/navigator/navigator.h b/src/modules/navigator/navigator.h index 760381796d4..21b99aef557 100644 --- a/src/modules/navigator/navigator.h +++ b/src/modules/navigator/navigator.h @@ -508,7 +508,7 @@ private: bool geofence_allows_position(const vehicle_global_position_s &pos); /** True if the pre-geofence permits the commanded target. */ - bool pre_geofence_allows_target(const vehicle_global_position_s &target); + bool pre_geofence_allows_position(const vehicle_global_position_s &target); DEFINE_PARAMETERS( (ParamFloat) _param_nav_loiter_rad, /**< loiter radius for fixedwing */ diff --git a/src/modules/navigator/navigator_main.cpp b/src/modules/navigator/navigator_main.cpp index 28eb4a6e6f1..29d4c9bc2d1 100644 --- a/src/modules/navigator/navigator_main.cpp +++ b/src/modules/navigator/navigator_main.cpp @@ -417,7 +417,7 @@ void Navigator::run() const bool fence_allows_position = geofence_allows_position(position_setpoint); - if (fence_allows_position && pre_geofence_allows_target(position_setpoint)) { + if (fence_allows_position && pre_geofence_allows_position(position_setpoint)) { position_setpoint_triplet_s *rep = get_reposition_triplet(); position_setpoint_triplet_s *curr = get_position_setpoint_triplet(); @@ -585,7 +585,7 @@ void Navigator::run() const bool fence_allows_position = geofence_allows_position(position_setpoint); - if (fence_allows_position && pre_geofence_allows_target(position_setpoint)) { + if (fence_allows_position && pre_geofence_allows_position(position_setpoint)) { _course.set_altitude(new_alt); } else if (!fence_allows_position) { @@ -618,7 +618,7 @@ void Navigator::run() const bool fence_allows_position = geofence_allows_position(position_setpoint); - if (fence_allows_position && pre_geofence_allows_target(position_setpoint)) { + if (fence_allows_position && pre_geofence_allows_position(position_setpoint)) { position_setpoint_triplet_s *rep = get_reposition_triplet(); position_setpoint_triplet_s *curr = get_position_setpoint_triplet(); @@ -720,7 +720,7 @@ void Navigator::run() const bool fence_allows_position = geofence_allows_position(position_setpoint); - if (fence_allows_position && pre_geofence_allows_target(position_setpoint)) { + if (fence_allows_position && pre_geofence_allows_position(position_setpoint)) { position_setpoint_triplet_s *rep = get_reposition_triplet(); rep->current.type = position_setpoint_s::SETPOINT_TYPE_LOITER; rep->current.loiter_radius = get_default_loiter_rad(); @@ -755,9 +755,7 @@ void Navigator::run() mavlink_log_critical(&_mavlink_log_pub, "Orbit is outside geofence"); } else { - mavlink_log_critical(&_mavlink_log_pub, "Orbit is outside the pre-geofence\t"); - events::send(events::ID("navigator_orbit_outside_pre_geofence"), {events::Log::Error, events::LogInternal::Info}, - "Orbit is outside the pre-geofence"); + mavlink_log_critical(&_mavlink_log_pub, "Orbit is outside the pre-geofence"); } } else if (cmd.command == vehicle_command_s::VEHICLE_CMD_DO_FIGUREEIGHT && @@ -775,7 +773,7 @@ void Navigator::run() const bool fence_allows_position = geofence_allows_position(position_setpoint); - if (fence_allows_position && pre_geofence_allows_target(position_setpoint)) { + if (fence_allows_position && pre_geofence_allows_position(position_setpoint)) { position_setpoint_triplet_s *rep = get_reposition_triplet(); rep->current.type = position_setpoint_s::SETPOINT_TYPE_LOITER; rep->current.loiter_minor_radius = fabsf(get_default_loiter_rad()); @@ -814,9 +812,7 @@ void Navigator::run() mavlink_log_critical(&_mavlink_log_pub, "Figure 8 is outside geofence"); } else { - mavlink_log_critical(&_mavlink_log_pub, "Figure 8 is outside the pre-geofence\t"); - events::send(events::ID("navigator_figure_eight_outside_pre_geofence"), {events::Log::Error, events::LogInternal::Info}, - "Figure 8 is outside the pre-geofence"); + mavlink_log_critical(&_mavlink_log_pub, "Figure 8 is outside the pre-geofence"); } #endif // CONFIG_FIGURE_OF_EIGHT @@ -1270,22 +1266,6 @@ void Navigator::geofence_breach_check() } if (hrt_elapsed_time(&_last_geofence_check) > GEOFENCE_CHECK_INTERVAL_US) { - const uint8_t pre_geofence_action = _geofence.getPreGeofenceAction(); - - // Disabling the pre-geofence releases its breach, also without a position or while holding after it. - if (pre_geofence_action == geofence_result_s::GF_ACTION_NONE - && (_geofence_result.pre_geofence_max_dist_triggered || _geofence_result.pre_geofence_max_alt_triggered - || _geofence_result.pre_geofence_custom_fence_triggered - || _geofence_result.pre_geofence_action != geofence_result_s::GF_ACTION_NONE)) { - _geofence_result.pre_geofence_max_dist_triggered = false; - _geofence_result.pre_geofence_max_alt_triggered = false; - _geofence_result.pre_geofence_custom_fence_triggered = false; - _geofence_result.pre_geofence_action = geofence_result_s::GF_ACTION_NONE; - _pre_geofence_reposition_sent = false; - _geofence_result.timestamp = hrt_absolute_time(); - _geofence_result_pub.publish(_geofence_result); - } - double current_latitude = _global_pos.lat; double current_longitude = _global_pos.lon; float current_altitude = _global_pos.alt; @@ -1309,10 +1289,6 @@ void Navigator::geofence_breach_check() return; } - const bool pre_max_dist_breached = !_geofence.isCloserThanPreMaxDistToHome(current_latitude, current_longitude); - const bool pre_max_alt_breached = !_geofence.isBelowPreMaxAltitude(current_altitude); - const bool pre_custom_fence_breached = !_geofence.isInsidePreGeofence(current_latitude, current_longitude); - if (_time_loitering_after_gf_breach > 0) { // while loitering after a breach, only allow new triggers to be set, never cleared. // Prevents the reposition center from walking outward when the vehicle hovers at the fence boundary. @@ -1321,9 +1297,6 @@ void Navigator::geofence_breach_check() _geofence_result.geofence_max_alt_triggered |= !_geofence.isBelowMaxAltitude(current_altitude); _geofence_result.geofence_custom_fence_triggered |= !_geofence.isInsidePolygonOrCircle(current_latitude, current_longitude, current_altitude); - _geofence_result.pre_geofence_max_dist_triggered |= pre_max_dist_breached; - _geofence_result.pre_geofence_max_alt_triggered |= pre_max_alt_breached; - _geofence_result.pre_geofence_custom_fence_triggered |= pre_custom_fence_breached; } else { _geofence_result.geofence_max_dist_triggered = !_geofence.isCloserThanMaxDistToHome(current_latitude, @@ -1331,11 +1304,19 @@ void Navigator::geofence_breach_check() _geofence_result.geofence_max_alt_triggered = !_geofence.isBelowMaxAltitude(current_altitude); _geofence_result.geofence_custom_fence_triggered = !_geofence.isInsidePolygonOrCircle(current_latitude, current_longitude, current_altitude); - _geofence_result.pre_geofence_max_dist_triggered = pre_max_dist_breached; - _geofence_result.pre_geofence_max_alt_triggered = pre_max_alt_breached; - _geofence_result.pre_geofence_custom_fence_triggered = pre_custom_fence_breached; } + // The pre-geofence triggers are kept in the same way while loitering, unless the pre-geofence is disabled. + const uint8_t pre_geofence_action = _geofence.getPreGeofenceAction(); + const bool keep_triggers = _time_loitering_after_gf_breach > 0 + && pre_geofence_action != geofence_result_s::GF_ACTION_NONE; + _geofence_result.pre_geofence_max_dist_triggered = !_geofence.isCloserThanPreMaxDistToHome(current_latitude, + current_longitude) || (keep_triggers && _geofence_result.pre_geofence_max_dist_triggered); + _geofence_result.pre_geofence_max_alt_triggered = !_geofence.isBelowPreMaxAltitude(current_altitude) + || (keep_triggers && _geofence_result.pre_geofence_max_alt_triggered); + _geofence_result.pre_geofence_custom_fence_triggered = !_geofence.isInsidePreGeofence(current_latitude, + current_longitude) || (keep_triggers && _geofence_result.pre_geofence_custom_fence_triggered); + const auto now = hrt_absolute_time(); _last_geofence_check = now; @@ -1942,18 +1923,14 @@ bool Navigator::geofence_allows_position(const vehicle_global_position_s &pos) (_geofence.getGeofenceAction() != geofence_result_s::GF_ACTION_WARN)) { if (PX4_ISFINITE(pos.lat) && PX4_ISFINITE(pos.lon) && PX4_ISFINITE(pos.alt)) { - // Not checkPointAgainstAllGeofences(): it includes the pre-geofence, which GF_ACTION does not - // control. pre_geofence_allows_target() checks it under its own action. - return _geofence.isCloserThanMaxDistToHome(pos.lat, pos.lon, pos.alt) - && _geofence.isBelowMaxAltitude(pos.alt) - && _geofence.isInsidePolygonOrCircle(pos.lat, pos.lon, pos.alt); + return _geofence.checkPointAgainstAllGeofences(pos.lat, pos.lon, pos.alt); } } return true; } -bool Navigator::pre_geofence_allows_target(const vehicle_global_position_s &target) +bool Navigator::pre_geofence_allows_position(const vehicle_global_position_s &target) { const uint8_t action = _geofence.getPreGeofenceAction(); diff --git a/src/modules/navigator/test/test_mission_feasibility_geofence.cpp b/src/modules/navigator/test/test_mission_feasibility_geofence.cpp index 3b80cd3b587..7e7d2956c34 100644 --- a/src/modules/navigator/test/test_mission_feasibility_geofence.cpp +++ b/src/modules/navigator/test/test_mission_feasibility_geofence.cpp @@ -798,9 +798,9 @@ TEST_F(MissionGeofenceLoiterTest, PreGeofenceMarginAppliesToPositionsAndPaths) EXPECT_FALSE(missionFeasible(items)); expectPathViolation(2); - // A first position within the margin fails the point check. + // A first position within the margin fails its path without length. EXPECT_FALSE(missionFeasible({waypoint({0.f, 950.f})})); - EXPECT_TRUE(waypointEvent(events::ID("navigator_mis_geofence_violation"), 1)); + expectPathViolation(1); } TEST_F(MissionGeofenceLoiterTest, PreGeofenceMarginAppliesToLoiterCircles) diff --git a/src/modules/navigator/test/test_pre_geofence_breach.cpp b/src/modules/navigator/test/test_pre_geofence_breach.cpp index 134aa142b0c..46f0e54419e 100644 --- a/src/modules/navigator/test/test_pre_geofence_breach.cpp +++ b/src/modules/navigator/test/test_pre_geofence_breach.cpp @@ -50,7 +50,7 @@ class MissionBaseTestNavigator public: static bool targetAllowed(Navigator &navigator, const vehicle_global_position_s &target) { - return navigator.geofence_allows_position(target) && navigator.pre_geofence_allows_target(target); + return navigator.geofence_allows_position(target) && navigator.pre_geofence_allows_position(target); } // Run the breach check now instead of waiting for its 5 Hz interval. @@ -569,23 +569,6 @@ TEST_P(PreGeofenceSourceTest, DisablingReleasesTheBreachWhileHolding) EXPECT_TRUE(repositionedAt(between())); } -TEST_P(PreGeofenceSourceTest, DisablingReleasesTheBreachWithoutAPosition) -{ - enablePreGeofence(); - ASSERT_TRUE(sourceTriggered(checkAt(between()))); - setNavState(vehicle_status_s::NAVIGATION_STATE_AUTO_LOITER); - - disablePreGeofence(); - _navigator.get_global_position()->timestamp = 0; - MissionBaseTestNavigator::breachCheck(_navigator); - - geofence_result_s result{}; - ASSERT_TRUE(_result_sub.update(&result)); - EXPECT_FALSE(preGeofenceBreached(result)); - EXPECT_EQ(result.pre_geofence_action, geofence_result_s::GF_ACTION_NONE); - EXPECT_TRUE(repositionedAt(between())); -} - // A pre-geofence that is enabled again must not inherit the breach that was released, with its new action. TEST_P(PreGeofenceSourceTest, EnablingAgainDoesNotRestoreAReleasedBreach) {