fix(navigator): unify geofence_allows_position and pre_geofence_allows_position

pre_geofence_allows_position() did not check the part of a target that
the command left unchanged, so that a vehicle outside the pre-geofence
could still change its altitude. geofence_allows_position() has no such
rule.

Drop it. The two checks now differ only in the limits they apply. An
altitude change is refused while the vehicle is horizontally outside the
pre-geofence, as it is outside the geofence. A Go-to back inside is
accepted as before.

Assisted-by: Claude:claude-fable-5-2
Signed-off-by: jonas <jonspero@me.com>
This commit is contained in:
jonas committed 2026-11-19 16:17:35 +01:00
1 parent 91db003439
commit 9c467b774e
3 files changed
+3 -27

No files matched your search

+1 -1
View File
@@ -307,7 +307,7 @@ PX4 also keeps planned flight inside the pre-geofence:
It is also rejected if a leg between positions or a fixed-wing loiter circle comes closer to the plan than the margin.
- The target of a _Go to location_ ([reposition](https://mavlink.io/en/messages/common.html#MAV_CMD_DO_REPOSITION)) or of an altitude change, and the centre of a fixed-wing orbit or figure-eight, is refused if it lies outside the pre-geofence (unless the action is `Warning`).
As for the geofence, only the target is checked, not the path to it or the loiter circle around it.
A vehicle that is outside the pre-geofence can still be paused or change its altitude, because only what the command changes is checked.
An altitude change keeps the vehicle's position, so it is refused while the vehicle is horizontally outside the pre-geofence: send the vehicle back inside first.
- [Return mode](../flight_modes/return.md#geofence_awareness) plans its route around the Geofence Plan with the margin added.
## Position Estimation Failsafes
+2 -9
View File
@@ -1929,15 +1929,8 @@ bool Navigator::pre_geofence_allows_position(const vehicle_global_position_s &po
(_geofence.getPreGeofenceAction() != geofence_result_s::GF_ACTION_WARN)) {
if (PX4_ISFINITE(pos.lat) && PX4_ISFINITE(pos.lon) && PX4_ISFINITE(pos.alt)) {
// Unlike geofence_allows_position(), only what the command changes is checked. Example: a vehicle that
// holds outside the pre-geofence can still change its altitude, because that command keeps its position.
const bool keeps_position = fabs(pos.lat - _global_pos.lat) < DBL_EPSILON
&& fabs(pos.lon - _global_pos.lon) < DBL_EPSILON;
const bool keeps_altitude = fabsf(pos.alt - _global_pos.alt) < FLT_EPSILON;
return (keeps_position || (_geofence.isCloserThanPreMaxDistToHome(pos.lat, pos.lon)
&& _geofence.isInsidePreGeofence(pos.lat, pos.lon)))
&& (keeps_altitude || _geofence.isBelowPreMaxAltitude(pos.alt));
return _geofence.isCloserThanPreMaxDistToHome(pos.lat, pos.lon) && _geofence.isBelowPreMaxAltitude(pos.alt)
&& _geofence.isInsidePreGeofence(pos.lat, pos.lon);
}
}
@@ -267,23 +267,6 @@ TEST_F(PreGeofenceBreachTest, ReturnKeepsTheMargin)
}
#endif // CONFIG_NAVIGATOR_GEOFENCE_AVOIDANCE
// A command without a position or an altitude keeps the vehicle's own, also outside the pre-geofence.
TEST_F(PreGeofenceBreachTest, TargetsKeepTheVehiclePositionAndAltitude)
{
setParam("GF_PRE_MAX_HDIST", 400.f);
setParam("GF_PRE_MAX_VDIST", 80.f);
const Place vehicle{0.f, 420.f, kHomeAltitude + 90.f};
ASSERT_TRUE(preGeofenceBreached(checkAt(vehicle)));
EXPECT_TRUE(targetAllowed(vehicle));
// Back inside horizontally, or down below the altitude limit.
EXPECT_TRUE(targetAllowed({0.f, 300.f, vehicle.altitude}));
EXPECT_TRUE(targetAllowed({vehicle.north, vehicle.east, kAltitude}));
// Further out or up.
EXPECT_FALSE(targetAllowed({0.f, 440.f, vehicle.altitude}));
EXPECT_FALSE(targetAllowed({vehicle.north, vehicle.east, kHomeAltitude + 95.f}));
}
// What triggers the pre-geofence. Each source has its geofence twin further out.
enum class Source {
HomeDistance, // GF_PRE_MAX_HDIST 400 m inside GF_MAX_HOR_DIST 500 m