perf(navigator): speed up the contingency geofence check (#30005)

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: jonas <jonspero@me.com>
This commit is contained in:
Jonas Perolini authored and GitHub committed 2026-11-25 10:33:13 +01:00
1 parent 5e9be2a6d5
commit b5cf23cc9f
2 files changed
+41 -12

No files matched your search

+32 -7
View File
@@ -156,14 +156,17 @@ static bool pathTouchesEdge(const Geofence::PathCheck &path, unsigned pieces, co
const matrix::Vector2d shifted_end{edge_end(0), edge_end(1) - shift};
const double margin = static_cast<double>(path.margin);
if (pieces == 1) {
return pieceTouchesEdge(path.start, path_end, shifted_start, shifted_end, margin);
}
// The whole arc's bow is at most pieces^2 * kMaxPieceBow. Expand the latitude bounds by that
// distance and the margin to skip distant edges cheaply. Longitude stays between the unwrapped
// endpoints because pathPieces() excludes arcs that could reach a pole; only the margin widens it.
const double margin_lat = math::degrees((pieces * pieces * kMaxPieceBow + margin) / CONSTANTS_RADIUS_OF_EARTH);
// Latitude first: it skips most edges without computing the cosine below.
if (math::max(path.start(0), path_end(0)) + margin_lat < math::min(shifted_start(0), shifted_end(0))
|| math::min(path.start(0), path_end(0)) - margin_lat > math::max(shifted_start(0), shifted_end(0))) {
return false;
}
double margin_lon = 0.0;
if (margin > 0.0) {
@@ -172,13 +175,16 @@ static bool pathTouchesEdge(const Geofence::PathCheck &path, unsigned pieces, co
margin_lon = max_lat < 90.0 ? math::degrees(margin / (CONSTANTS_RADIUS_OF_EARTH * cos(math::radians(max_lat)))) : 360.0;
}
if (math::max(path.start(0), path_end(0)) + margin_lat < math::min(shifted_start(0), shifted_end(0))
|| math::min(path.start(0), path_end(0)) - margin_lat > math::max(shifted_start(0), shifted_end(0))
|| math::max(path.start(1), path_end(1)) + margin_lon < math::min(shifted_start(1), shifted_end(1))
if (math::max(path.start(1), path_end(1)) + margin_lon < math::min(shifted_start(1), shifted_end(1))
|| math::min(path.start(1), path_end(1)) - margin_lon > math::max(shifted_start(1), shifted_end(1))) {
return false;
}
// After the bounds, so single pieces skip distant edges too, e.g. the contingency geofence point check.
if (pieces == 1) {
return pieceTouchesEdge(path.start, path_end, shifted_start, shifted_end, margin);
}
// Split along the great circle, approximating the local straight line near the EKF origin.
const matrix::Vector3d start_unit = unitVector(path.start);
const matrix::Vector3d end_unit = unitVector(path_end);
@@ -250,6 +256,9 @@ Geofence::Geofence(Navigator *navigator) :
Geofence::~Geofence()
{
#if defined(CONFIG_NAVIGATOR_GEOFENCE_PATH_CHECKS)
perf_free(_cont_check_perf);
#endif // CONFIG_NAVIGATOR_GEOFENCE_PATH_CHECKS
_clearFence();
}
@@ -965,6 +974,22 @@ bool Geofence::isCircleInside(double lat, double lon, float radius)
return checkPaths(&path, 1, &clear) && clear;
}
#if defined(CONFIG_NAVIGATOR_GEOFENCE_PATH_CHECKS)
bool Geofence::isInsideContGeofence(double lat, double lon)
{
const float margin = getContGeofencePlanMargin();
if (margin <= FLT_EPSILON) {
return true;
}
perf_begin(_cont_check_perf);
const bool inside = isCircleInside(lat, lon, margin);
perf_end(_cont_check_perf);
return inside;
}
#endif // CONFIG_NAVIGATOR_GEOFENCE_PATH_CHECKS
bool Geofence::checkPointAgainstPolygonCircle(const PolygonInfo &polygon, double lat, double lon, float altitude)
{
bool checksPass = true;
+9 -5
View File
@@ -44,6 +44,7 @@
#include <dataman_client/DatamanClient.hpp>
#include <lib/mathlib/mathlib.h>
#include <lib/perf/perf_counter.h>
#include <px4_platform_common/module_params.h>
#include <drivers/drv_hrt.h>
#include <lib/geo/geo.h>
@@ -193,11 +194,11 @@ public:
float getContGeofencePlanMargin() const { return getContGeofenceMargin(); }
/** True if the position keeps GF_CONT_H_MARGIN from the polygons and circles, or the margin is disabled. */
bool isInsideContGeofence(double lat, double lon)
{
const float margin = getContGeofencePlanMargin();
return margin <= FLT_EPSILON || isCircleInside(lat, lon, margin);
}
#if defined(CONFIG_NAVIGATOR_GEOFENCE_PATH_CHECKS)
bool isInsideContGeofence(double lat, double lon);
#else
bool isInsideContGeofence(double lat, double lon) { return true; }
#endif // CONFIG_NAVIGATOR_GEOFENCE_PATH_CHECKS
bool valid();
@@ -278,6 +279,9 @@ private:
DatamanState _error_state{DatamanState::UpdateRequestWait};
DatamanCache _dataman_cache{"geofence_dm_cache_miss", 0};
DatamanClient &_dataman_client = _dataman_cache.client();
#if defined(CONFIG_NAVIGATOR_GEOFENCE_PATH_CHECKS)
perf_counter_t _cont_check_perf {perf_alloc(PC_ELAPSED, "navigator: contingency geofence")};
#endif // CONFIG_NAVIGATOR_GEOFENCE_PATH_CHECKS
float _altitude_min{0.0f};
float _altitude_max{0.0f};