diff --git a/src/modules/navigator/RTLPlanner/GeofenceAvoidancePlannerTest.cpp b/src/modules/navigator/RTLPlanner/GeofenceAvoidancePlannerTest.cpp index ab3bf936257..97c60cb0027 100644 --- a/src/modules/navigator/RTLPlanner/GeofenceAvoidancePlannerTest.cpp +++ b/src/modules/navigator/RTLPlanner/GeofenceAvoidancePlannerTest.cpp @@ -202,6 +202,127 @@ TEST_F(GeofenceAvoidancePlannerTest, PathAroundExclusionZone) EXPECT_NEAR(last_wp(1), vertices[2](1), 1e-3); } +TEST_F(GeofenceAvoidancePlannerTest, RebuildingDifferentFenceRestoresOriginalPath) +{ + FakeGeofence original(kExclusionVertices, 4, NAV_CMD_FENCE_POLYGON_VERTEX_EXCLUSION); + _planner.updateGraphFromGeofence(original, 0.f); + _planner.updateDestination(kExclusionDestination); + ASSERT_EQ(_planner.updateStartAndFillPath(kExclusionStart), 2); + const matrix::Vector2d expected[] = {_planner.waypointAtIndex(0), _planner.waypointAtIndex(1)}; + + // Rebuild with a different graph and projection reference, away from the flight path. + matrix::Vector2d shifted[4]; + + for (int i = 0; i < 4; ++i) { + shifted[i] = kExclusionVertices[i] + matrix::Vector2d{0.01, 0.01}; + } + + FakeGeofence different(shifted, 4, NAV_CMD_FENCE_POLYGON_VERTEX_EXCLUSION); + _planner.updateGraphFromGeofence(different, 0.f); + _planner.updateDestination(kExclusionDestination); + ASSERT_EQ(_planner.status(), GeofenceAvoidancePlanner::Status::Success); + ASSERT_EQ(_planner.updateStartAndFillPath(kExclusionStart), 0); + EXPECT_FALSE(_planner.needsStraightLineFallback()); + + _planner.updateGraphFromGeofence(original, 0.f); + _planner.updateDestination(kExclusionDestination); + ASSERT_EQ(_planner.status(), GeofenceAvoidancePlanner::Status::Success); + ASSERT_EQ(_planner.updateStartAndFillPath(kExclusionStart), 2); + EXPECT_FALSE(_planner.needsStraightLineFallback()); + + for (int i = 0; i < 2; ++i) { + EXPECT_DOUBLE_EQ(_planner.waypointAtIndex(i)(0), expected[i](0)); + EXPECT_DOUBLE_EQ(_planner.waypointAtIndex(i)(1), expected[i](1)); + } +} + +TEST_F(GeofenceAvoidancePlannerTest, RebuildingAfterRejectedFenceRestoresOriginalPath) +{ + FakeGeofence original(kExclusionVertices, 4, NAV_CMD_FENCE_POLYGON_VERTEX_EXCLUSION); + _planner.updateGraphFromGeofence(original, 0.f); + _planner.updateDestination(kExclusionDestination); + ASSERT_EQ(_planner.updateStartAndFillPath(kExclusionStart), 2); + const matrix::Vector2d expected[] = {_planner.waypointAtIndex(0), _planner.waypointAtIndex(1)}; + + const matrix::Vector2d duplicate[] = { + kExclusionVertices[0], kExclusionVertices[1], kExclusionVertices[1], kExclusionVertices[3] + }; + FakeGeofence invalid(duplicate, 4, NAV_CMD_FENCE_POLYGON_VERTEX_EXCLUSION); + _planner.updateGraphFromGeofence(invalid, 0.f); + ASSERT_EQ(_planner.status(), GeofenceAvoidancePlanner::Status::Degenerate); + _planner.updateDestination(kExclusionDestination); + ASSERT_EQ(_planner.updateStartAndFillPath(kExclusionStart), 0); + EXPECT_TRUE(_planner.needsStraightLineFallback()); + + _planner.updateGraphFromGeofence(original, 0.f); + _planner.updateDestination(kExclusionDestination); + ASSERT_EQ(_planner.status(), GeofenceAvoidancePlanner::Status::Success); + ASSERT_EQ(_planner.updateStartAndFillPath(kExclusionStart), 2); + EXPECT_FALSE(_planner.needsStraightLineFallback()); + + for (int i = 0; i < 2; ++i) { + EXPECT_DOUBLE_EQ(_planner.waypointAtIndex(i)(0), expected[i](0)); + EXPECT_DOUBLE_EQ(_planner.waypointAtIndex(i)(1), expected[i](1)); + } +} + +TEST_F(GeofenceAvoidancePlannerTest, PathAroundMaximumVertexPolygon) +{ + const matrix::Vector2d center{47.355, 8.519}; + constexpr double radius_degrees = 0.001; + matrix::Vector2d vertices[DM_KEY_FENCE_POINTS_MAX]; + + for (int i = 0; i < DM_KEY_FENCE_POINTS_MAX; ++i) { + const double angle = 2.0 * M_PI * i / DM_KEY_FENCE_POINTS_MAX; + vertices[i] = center + matrix::Vector2d{radius_degrees * cos(angle), radius_degrees * sin(angle)}; + } + + FakeGeofence fence(vertices, DM_KEY_FENCE_POINTS_MAX, NAV_CMD_FENCE_POLYGON_VERTEX_EXCLUSION); + _planner.updateGraphFromGeofence(fence, 5.f); + ASSERT_EQ(_planner.status(), GeofenceAvoidancePlanner::Status::Success); + + // The direct route between these points crosses the exclusion polygon. + const matrix::Vector2d start = center - matrix::Vector2d{2.0 * radius_degrees, 0.0}; + const matrix::Vector2d destination = center + matrix::Vector2d{2.0 * radius_degrees, 0.0}; + _planner.updateDestination(destination); + ASSERT_EQ(_planner.status(), GeofenceAvoidancePlanner::Status::Success); + EXPECT_GT(_planner.updateStartAndFillPath(start), 0); + EXPECT_FALSE(_planner.needsStraightLineFallback()); +} + +TEST_F(GeofenceAvoidancePlannerTest, OversizedPolygonRejectsPreviousPathAndAllowsRecovery) +{ + FakeGeofence original(kExclusionVertices, 4, NAV_CMD_FENCE_POLYGON_VERTEX_EXCLUSION); + _planner.updateGraphFromGeofence(original, 0.f); + _planner.updateDestination(kExclusionDestination); + ASSERT_EQ(_planner.updateStartAndFillPath(kExclusionStart), 2); + + constexpr int num_vertices = DM_KEY_FENCE_POINTS_MAX + 1; + const matrix::Vector2d center{47.355, 8.519}; + constexpr double radius_degrees = 0.001; + matrix::Vector2d vertices[num_vertices]; + + for (int i = 0; i < num_vertices; ++i) { + const double angle = 2.0 * M_PI * i / num_vertices; + vertices[i] = center + matrix::Vector2d{radius_degrees * cos(angle), radius_degrees * sin(angle)}; + } + + FakeGeofence oversized(vertices, num_vertices, NAV_CMD_FENCE_POLYGON_VERTEX_EXCLUSION); + _planner.updateGraphFromGeofence(oversized, 0.f); + // The graph budget fits; the input exceeds the maximum storable polygon size. + ASSERT_LE(_planner.requiredNodes(), _planner.maxNodes()); + ASSERT_EQ(_planner.status(), GeofenceAvoidancePlanner::Status::BudgetExceeded); + _planner.updateDestination(kExclusionDestination); + EXPECT_EQ(_planner.updateStartAndFillPath(kExclusionStart), 0); + EXPECT_TRUE(_planner.needsStraightLineFallback()); + + _planner.updateGraphFromGeofence(original, 0.f); + _planner.updateDestination(kExclusionDestination); + ASSERT_EQ(_planner.status(), GeofenceAvoidancePlanner::Status::Success); + EXPECT_EQ(_planner.updateStartAndFillPath(kExclusionStart), 2); + EXPECT_FALSE(_planner.needsStraightLineFallback()); +} + TEST_F(GeofenceAvoidancePlannerTest, PathInsideInclusionZone) { using namespace matrix; diff --git a/src/modules/navigator/RTLPlanner/geofence_avoidance_planner.cpp b/src/modules/navigator/RTLPlanner/geofence_avoidance_planner.cpp index 277faee06c1..6be8e773cf8 100644 --- a/src/modules/navigator/RTLPlanner/geofence_avoidance_planner.cpp +++ b/src/modules/navigator/RTLPlanner/geofence_avoidance_planner.cpp @@ -201,16 +201,19 @@ bool GeofenceAvoidancePlanner::updatePolygonsFromGeofence( if (info.fence_type == NAV_CMD_FENCE_POLYGON_VERTEX_INCLUSION || info.fence_type == NAV_CMD_FENCE_POLYGON_VERTEX_EXCLUSION) { - // Could skip this local copy and pass e.g. a reference to geofence into addPolygon, so it could access directly - matrix::Vector2f local_in[info.vertex_count]; + if (info.vertex_count > DM_KEY_FENCE_POINTS_MAX) { + _status = Status::BudgetExceeded; + perf_cancel(_update_polygons_perf); + return false; + } for (int vertex_idx = 0; vertex_idx < info.vertex_count; vertex_idx++) { - local_in[vertex_idx] = get_vertex_local_position(poly_index, vertex_idx, geofence, _reference); + _vertices_in[vertex_idx] = get_vertex_local_position(poly_index, vertex_idx, geofence, _reference); } const bool is_inclusion = (info.fence_type == NAV_CMD_FENCE_POLYGON_VERTEX_INCLUSION); - add_result = _polygons.addPolygon(local_in, info.vertex_count, is_inclusion, margin); + add_result = _polygons.addPolygon(_vertices_in, info.vertex_count, is_inclusion, margin); } else if (info.fence_type == NAV_CMD_FENCE_CIRCLE_INCLUSION || info.fence_type == NAV_CMD_FENCE_CIRCLE_EXCLUSION) { diff --git a/src/modules/navigator/RTLPlanner/geofence_avoidance_planner.h b/src/modules/navigator/RTLPlanner/geofence_avoidance_planner.h index 5e003403afe..ecc60870b7f 100644 --- a/src/modules/navigator/RTLPlanner/geofence_avoidance_planner.h +++ b/src/modules/navigator/RTLPlanner/geofence_avoidance_planner.h @@ -202,6 +202,9 @@ private: perf_counter_t _plan_path_perf{perf_alloc(PC_ELAPSED, "rtl_planner: plan path")}; perf_counter_t _lookup_path_perf{perf_alloc(PC_ELAPSED, "rtl_planner: lookup path")}; + // Scratch storage for the vertices of the polygon being added, off the stack + matrix::Vector2f _vertices_in[DM_KEY_FENCE_POINTS_MAX]; + // Sets _status on failure (matching planPath); returns false to abort the build. bool updatePolygonsFromGeofence(GeofenceInterface &geofence, float margin); void updateEdgeCosts();