feat(lane_route_core): follow preferred lanes, fail fast on blocked goals, allow_unknown (R2.14)

- Start and goal attach to the middle of nearby edges: query-time split points
  (projection and +-1.5 m) on up to 6 directed edges within 3 m, plus up to 4
  preferred edges within 8 m. The compiled graph is unchanged; retries block the
  parent edge.
- Preferred lanes join each other and split nodes; lane ends are protected from
  swing merging, and turns next to a preferred ridge are not penalised. Hard
  rules (forward only, radius, 30 mm gap) are unchanged.
- Goal footprint on a lethal cell returns goal_in_obstacle before any search;
  on unknown it returns goal_in_unknown unless allow_unknown.
- allow_unknown (default false): when true every planner check treats unknown
  as free. The reference graph from robot_costmap_2d still skips unknown.

SEHC replay: lane metres m4 0.3 -> 13.3, m7 1.1 -> 13.8, m8 0.4 -> 26.4.
Random near-lane 190/200 both; NVC 316/316; regress7 3418 (4 losses all solve
when run alone); m15 31.0 m 8/8; independent recheck 0 violations. 146 tests.
This commit is contained in:
2026-11-20 16:30:47 +07:00
parent 07bffab7fa
commit 94475098a2
17 changed files with 695 additions and 64 deletions

View File

@@ -21,6 +21,7 @@
#include <lane_route_core/costmap_backend.h>
#include "../src/lattice_internal.h"
#include "../src/route_planner_internal.h"
#include "../src/standability.h"
#include "../src/footprint_zones.h"
@@ -683,17 +684,16 @@ TEST_F(PlanRouteTest, GraphFootprintGapCountsUnknownCells)
ASSERT_TRUE(checkFootprintGap(direct, grid, config_.footprint_min_gap_m, config_.half_x_m, config_.half_y_m).valid);
ASSERT_FALSE(checkFootprintGap(direct, grid, config_.footprint_min_gap_m, config_.half_x_m, config_.half_y_m, nullptr,
nullptr, true).valid);
// Graph only: the candidate along the direct line is rejected. Its shortcut raise uses the lethal clearance field, so the
// graph cannot move the line away from unknown cells by itself; it fails instead of returning the 20 mm path.
// The live clearance patch includes unknown cells, so the graph can move away while retaining the gap rule.
const PlanResult graph_only = planWithObstacle(request, live);
EXPECT_FALSE(graph_only.ok);
EXPECT_EQ(graph_only.info.reason, "footprint_gap_below_minimum");
// With the fallback the lattice, which already counted unknown cells, returns a path that keeps the gap.
expectLegal(graph_only, request, config_);
EXPECT_TRUE(checkFootprintGap(graph_only.path, grid, config_.footprint_min_gap_m, config_.half_x_m, config_.half_y_m, nullptr,
nullptr, true).valid);
// Enabling the fallback preserves the same gap rule.
config_.lattice_enabled = true;
config_.lattice_preferred_gap_m = 0.0;
const PlanResult result = planWithObstacle(request, live);
expectLegal(result, request, config_);
EXPECT_EQ(result.info.tier_used, "lattice");
EXPECT_TRUE(checkFootprintGap(result.path, grid, config_.footprint_min_gap_m, config_.half_x_m, config_.half_y_m, nullptr,
nullptr, true).valid);
}
@@ -1333,6 +1333,296 @@ TEST(Lattice, FreeGoalAcceptsCurvedArrivalPastCorner)
EXPECT_NEAR(wrapAngle(result.path.back().yaw - kPi / 2.0), 0.0, 1e-5);
}
TEST(Lattice, BlockedGoalFailsBeforeSearching)
{
LatticeTestGrid map(160, 160);
PlannerConfig config = latticeConfig();
RouteRequest request = latticeRequest(pose(1.0, 3.0, 0.0), pose(5.0, 3.0, 0.0));
const std::size_t cell = 60 * map.width + 105;
for (const auto& obstacle : {std::make_pair(kLethalCost, "goal_in_obstacle"),
std::make_pair(kNoInformation, "goal_in_unknown")})
{
map.cost[cell] = obstacle.first;
const PlanResult result = map.plan(request, config);
EXPECT_FALSE(result.ok);
EXPECT_EQ(result.info.reason, obstacle.second);
EXPECT_EQ(result.info.lattice_states_expanded, 0u);
EXPECT_LT(result.info.query_ms, 100.0);
request.goal.has_yaw = false;
EXPECT_NE(map.plan(request, config).info.reason, obstacle.second);
request.goal.has_yaw = true;
map.cost[cell] = kFreeSpace;
}
}
TEST(Lattice, AllowUnknownControlsGoalAndTraversal)
{
LatticeTestGrid map(160, 120);
for (int y = 0; y < map.height; ++y)
for (int x = 55; x < map.width; ++x)
map.cost[static_cast<std::size_t>(y) * map.width + x] = kNoInformation;
PlannerConfig config = latticeConfig();
RouteRequest request = latticeRequest(pose(1.0, 3.0, 0.0), pose(6.0, 3.0, 0.0));
const PlanResult rejected = map.plan(request, config);
EXPECT_EQ(rejected.info.reason, "goal_in_unknown");
EXPECT_EQ(rejected.info.lattice_states_expanded, 0u);
config.allow_unknown = true;
const PlanResult accepted = map.plan(request, config);
ASSERT_TRUE(accepted.ok) << accepted.info.reason;
EXPECT_GT(accepted.path.back().x, 5.9);
}
TEST(UnknownGrid, LiveChangesAndClearanceFollowPolicy)
{
LatticeTestGrid map(40, 40);
std::vector<uint8_t> base = map.cost;
map.cost[20 * map.width + 20] = kNoInformation;
GridRef grid = map.view();
EXPECT_FALSE(changedBoxes(base.data(), grid).empty());
std::vector<uint16_t> patched;
patchClearance(base.data(), map.clearance.data(), grid, 0.2, &patched);
EXPECT_EQ(patched[20 * map.width + 20], 0);
grid.allow_unknown = true;
EXPECT_TRUE(changedBoxes(base.data(), grid).empty());
patchClearance(base.data(), map.clearance.data(), grid, 0.2, &patched);
EXPECT_EQ(patched[20 * map.width + 20], map.clearance[20 * map.width + 20]);
base[20 * map.width + 20] = kNoInformation;
map.cost[20 * map.width + 20] = kLethalCost;
patchClearance(base.data(), map.clearance.data(), grid, 0.2, &patched);
EXPECT_EQ(patched[20 * map.width + 20], 0);
}
TEST(UnknownGrid, RemovedLethalNoLongerRestrictsClearance)
{
LatticeTestGrid map(30, 30);
std::vector<uint8_t> base = map.cost;
base[10 * map.width + 10] = kLethalCost;
base[10 * map.width + 20] = kLethalCost;
map.cost = base;
map.cost[10 * map.width + 10] = kNoInformation;
std::fill(map.clearance.begin(), map.clearance.end(), 0);
GridRef grid = map.view();
grid.allow_unknown = true;
std::vector<uint16_t> patched;
patchClearance(base.data(), map.clearance.data(), grid, 1.5, &patched);
for (int y = 0; y < map.height; ++y)
for (int x = 0; x < map.width; ++x)
EXPECT_EQ(patched[static_cast<std::size_t>(y) * map.width + x],
static_cast<uint16_t>(std::floor(std::hypot(x - 20, y - 10) * kResolution * 1000.0)));
map.cost[10 * map.width + 20] = kFreeSpace;
patchClearance(base.data(), map.clearance.data(), grid, 1.5, &patched);
EXPECT_TRUE(std::all_of(patched.begin(), patched.end(), [](uint16_t value) { return value == 65535; }));
}
TEST(QueryEdgeSplits, ConnectsBothEndpointsToTheMiddleOfPreferredEdge)
{
LatticeTestGrid map(400, 140);
for (int y = 57; y <= 63; ++y)
for (int x = 10; x < 390; ++x)
map.cost[static_cast<std::size_t>(y) * map.width + x] = kPreferredSpace;
const GridRef grid = map.view();
auto graph = std::make_shared<CompiledGraph>();
graph->deviation_deg = 90.0;
graph->nodes = {{1.0, 3.025}, {19.0, 3.025}};
graph->edges = {{0, 1, {graph->nodes[0], graph->nodes[1]}, EdgeKind::kPreferredRidge, 18.0}};
graph->outgoing = {{0}, {}};
graph->sample_offsets = {0, 1};
graph->sample_cells = {60 * map.width + 200};
graph->sample_weights = {18.0};
PlannerConfig config = latticeConfig();
config.lattice_enabled = false;
config.detour_upgrade_enabled = false;
config.min_turn_radius_m = 0.4;
config.corner_radii_m = {0.4};
config.regular_turn_radius_m = 0.4;
config.deviation_ladder_deg.clear();
RouteRequest request{pose(4.0, 2.425, 0.0), pose(16.0, 2.425, 0.0), {}};
request.start.has_yaw = false;
request.goal.has_yaw = false;
GraphTrace trace;
const PlanResult result = planRoute([&](double) { return graph; }, request, grid, config, {}, &trace);
ASSERT_TRUE(result.ok) << result.info.reason;
double lane_length = 0.0, length = 0.0;
for (std::size_t i = 1; i < result.path.size(); ++i)
{
const auto& a = result.path[i-1];
const auto& b = result.path[i];
const double d = std::hypot(b.x - a.x, b.y - a.y);
length += d;
if (a.y >= 2.85 && a.y <= 3.2) lane_length += d;
}
EXPECT_GT(lane_length, length * 0.6);
ASSERT_FALSE(trace.runs.empty());
EXPECT_GT(trace.runs.front().nodes.size(), graph->nodes.size() + 2);
}
TEST(QueryEdgeSplits, ReachesLongVoronoiEdgeWithoutGoingToItsEnds)
{
LatticeTestGrid map(1400, 160);
for (int y = 30; y < 80; ++y)
for (int x = 635; x < 645; ++x)
map.cost[static_cast<std::size_t>(y) * map.width + x] = kLethalCost;
robot_costmap_2d::ReferenceGridView reference;
reference.cost = map.cost.data();
reference.geometry = geometryOf(map.view());
map.clearance = robot_costmap_2d::computeVoronoiSkeleton(reference, ridgeParams(defaultConfig())).clearance_mm;
const GridRef grid = map.view();
auto graph = std::make_shared<CompiledGraph>();
graph->deviation_deg = 90.0;
graph->nodes = {{1.0, 5.0}, {69.0, 5.0}};
graph->edges = {{0, 1, {graph->nodes[0], graph->nodes[1]}, EdgeKind::kVoronoi, 68.0}};
graph->outgoing = {{0}, {}};
graph->sample_offsets = {0, 1};
graph->sample_cells = {100 * map.width + 700};
graph->sample_weights = {68.0};
PlannerConfig config = defaultConfig();
config.detour_upgrade_enabled = false;
config.min_turn_radius_m = config.regular_turn_radius_m = 0.4;
config.corner_radii_m = {0.4};
config.deviation_ladder_deg.clear();
RouteRequest request{pose(25.0, 3.0, 0.0), pose(42.0, 3.0, 0.0), {}};
request.start.has_yaw = request.goal.has_yaw = false;
const PlanResult result = planRoute([&](double) { return graph; }, request, grid, config, {});
ASSERT_TRUE(result.ok) << result.info.reason;
EXPECT_LT(result.info.path_length_m, 25.0);
}
TEST(QueryEdgeSplits, KeepsNarrowPreferredLaneAfterSmoothing)
{
LatticeTestGrid map(400, 160);
for (int y = 60; y <= 61; ++y)
for (int x = 20; x < 380; ++x)
map.cost[static_cast<std::size_t>(y) * map.width + x] = kPreferredSpace;
const GridRef grid = map.view();
auto graph = std::make_shared<CompiledGraph>();
graph->deviation_deg = 90.0;
graph->nodes = {{1.0, 3.025}, {19.0, 3.025}};
graph->edges = {{0, 1, {graph->nodes[0], graph->nodes[1]}, EdgeKind::kPreferredRidge, 18.0}};
graph->outgoing = {{0}, {}};
graph->sample_offsets = {0, 1};
graph->sample_cells = {60 * map.width + 200};
graph->sample_weights = {18.0};
PlannerConfig config = defaultConfig();
config.detour_upgrade_enabled = false;
config.min_turn_radius_m = config.regular_turn_radius_m = 0.4;
config.corner_radii_m = {0.4};
config.deviation_ladder_deg.clear();
RouteRequest request{pose(4.0, 5.5, 0.0), pose(16.0, 2.90, 0.0), {}};
request.start.has_yaw = request.goal.has_yaw = false;
const PlanResult result = planRoute([&](double) { return graph; }, request, grid, config, {});
ASSERT_TRUE(result.ok) << result.info.reason;
double preferred = 0.0;
for (std::size_t i = 1; i < result.path.size(); ++i)
{
const auto& a = result.path[i-1];
const auto& b = result.path[i];
if ((a.y + b.y) * 0.5 >= 3.0 && (a.y + b.y) * 0.5 < 3.1)
preferred += std::hypot(b.x - a.x, b.y - a.y);
}
EXPECT_GT(preferred, 6.0);
}
TEST(QueryEdgeSplits, ReachesPreferredLaneFromGoalBeyondThreeMetres)
{
LatticeTestGrid map(400, 200);
for (int y = 60; y <= 63; ++y)
for (int x = 20; x < 380; ++x)
map.cost[static_cast<std::size_t>(y) * map.width + x] = kPreferredSpace;
const GridRef grid = map.view();
auto graph = std::make_shared<CompiledGraph>();
graph->deviation_deg = 90.0;
graph->nodes = {{1.0, 3.025}, {19.0, 3.025}};
graph->edges = {{0, 1, {graph->nodes[0], graph->nodes[1]}, EdgeKind::kPreferredRidge, 18.0}};
graph->outgoing = {{0}, {}};
graph->sample_offsets = {0, 1};
graph->sample_cells = {60 * map.width + 200};
graph->sample_weights = {18.0};
PlannerConfig config = defaultConfig();
config.detour_upgrade_enabled = false;
config.min_turn_radius_m = config.regular_turn_radius_m = 0.4;
config.corner_radii_m = {0.4};
config.deviation_ladder_deg.clear();
RouteRequest request{pose(4.0, 2.5, 0.0), pose(16.0, 7.0, 0.0), {}};
request.start.has_yaw = request.goal.has_yaw = false;
const PlanResult result = planRoute([&](double) { return graph; }, request, grid, config, {});
ASSERT_TRUE(result.ok) << result.info.reason;
double preferred = 0.0;
for (std::size_t i = 1; i < result.path.size(); ++i)
{
const auto& a = result.path[i-1];
const auto& b = result.path[i];
if ((a.y + b.y) * 0.5 >= 3.0 && (a.y + b.y) * 0.5 < 3.2)
preferred += std::hypot(b.x - a.x, b.y - a.y);
}
EXPECT_GT(preferred, 5.0);
}
TEST(QueryEdgeSplits, ConnectsNearbyPreferredLaneEnds)
{
LatticeTestGrid map(200, 120);
map.cost[60 * map.width + 20] = kPreferredSpace;
const GridRef grid = map.view();
RidgeBackend backend;
backend.region_centre_edges = [](const std::vector<uint8_t>&, const GridRef&, std::vector<Point2>* nodes,
std::vector<CoarseEdge>* edges, const std::atomic<bool>*) {
*nodes = {{1.0, 3.0}, {3.0, 3.0}, {4.0, 3.0}, {7.0, 3.0}};
*edges = {{0, 1, {(*nodes)[0], (*nodes)[1]}}, {2, 3, {(*nodes)[2], (*nodes)[3]}}};
};
PlannerConfig config = defaultConfig();
const CompiledGraph graph = compileGraph({}, grid, grid.cost, 90.0, config, backend);
EXPECT_TRUE(std::any_of(graph.edges.begin(), graph.edges.end(), [](const GraphEdge& edge) {
return edge.kind == EdgeKind::kPreferredAttach && edge.from == 1 && edge.to == 2;
}));
}
TEST(QueryEdgeSplits, ConnectsPreferredEndToAnotherLaneInterior)
{
LatticeTestGrid map(200, 120);
map.cost[60 * map.width + 20] = kPreferredSpace;
const GridRef grid = map.view();
RidgeBackend backend;
backend.region_centre_edges = [](const std::vector<uint8_t>&, const GridRef&, std::vector<Point2>* nodes,
std::vector<CoarseEdge>* edges, const std::atomic<bool>*) {
*nodes = {{1.0, 3.0}, {8.0, 3.0}, {4.0, 4.0}, {4.0, 5.0}};
*edges = {{0, 1, {(*nodes)[0], (*nodes)[1]}}, {2, 3, {(*nodes)[2], (*nodes)[3]}}};
};
PlannerConfig config = defaultConfig();
const CompiledGraph graph = compileGraph({}, grid, grid.cost, 90.0, config, backend);
bool attached = false, toward_left = false, toward_right = false;
for (const GraphEdge& edge : graph.edges)
{
const Point2& target = graph.nodes[static_cast<std::size_t>(edge.to)];
if (edge.kind == EdgeKind::kPreferredAttach && edge.from == 2 && distanceOf(target, {4.0, 3.0}) < 1e-6) attached = true;
const Point2& source = graph.nodes[static_cast<std::size_t>(edge.from)];
if (edge.kind == EdgeKind::kPreferredRidge && distanceOf(source, {4.0, 3.0}) < 1e-6)
{
toward_left |= edge.to == 0;
toward_right |= edge.to == 1;
}
}
EXPECT_TRUE(attached);
EXPECT_TRUE(toward_left);
EXPECT_TRUE(toward_right);
}
TEST(QueryEdgeSplits, PreferredEntryTurnDoesNotEraseCostSaving)
{
LatticeTestGrid map(250, 120);
const GridRef grid = map.view();
CompiledGraph graph;
graph.nodes = {{1.0, 2.0}, {2.0, 3.0}, {10.0, 3.0}, {11.0, 2.0}};
graph.edges = {{0, 3, {graph.nodes[0], graph.nodes[3]}, EdgeKind::kDirect, 10.0},
{0, 1, {graph.nodes[0], graph.nodes[1]}, EdgeKind::kAttach, 1.7},
{1, 2, {graph.nodes[1], graph.nodes[2]}, EdgeKind::kPreferredRidge, 15.0},
{2, 3, {graph.nodes[2], graph.nodes[3]}, EdgeKind::kAttach, 1.7}};
graph.outgoing = {{0, 1}, {2}, {3}, {}};
detail::QueryGraph query(graph, grid, 0.0);
std::vector<int32_t> route;
ASSERT_TRUE(detail::searchRoute(query, 0, 3, std::vector<uint8_t>(4, 0), {}, 20.0, 0.4,
{1.0, 1.0, 0.4, 1.0}, &route, nullptr));
EXPECT_EQ(route, (std::vector<int32_t>{1, 2, 3}));
}
TEST(Lattice, FixedGoalUsesCurvedArrivalThenCheckedSpin)
{
LatticeTestGrid map(120, 120);