feat(lane_route): smooth lattice paths (R2) and expose the lattice tier in the plugin (R3)

- lane_route_core: port of the S3 smoother for lattice paths (direct,
  greedy and bounded graph improvement over witness anchors, then
  clothoid corners when budget remains), shape metrics and smoother
  diagnostics in PlanInfo and lane_route_eval.
- lane_route_planner: loads the lattice keys from YAML, logs a config
  summary at start-up and one debug/fallback line per lattice request.

regress7: coverage unchanged from R1, every lattice path smoothed, 0 bad
in the independent recheck. Local snapshot for history only; not pushed.
This commit is contained in:
2026-11-09 16:11:34 +07:00
parent 24822f5408
commit b0a3954541
13 changed files with 946 additions and 20 deletions

View File

@@ -983,6 +983,7 @@ PlannerConfig latticeConfig()
{
PlannerConfig c;
c.lattice_enabled = true;
c.lattice_smooth_enabled = false; // R1 tests inspect the unsmoothed lattice result.
c.time_limit_s = 10.0;
c.fallback_time_limit_s = 3.0;
return c;
@@ -1453,6 +1454,11 @@ TEST(Lattice, InvalidParametersAreRejected)
EXPECT_FALSE(config.validate().empty());
config.lattice_enabled = false;
EXPECT_TRUE(config.validate().empty());
config = latticeConfig();
config.lattice_smooth_max_checks = -1;
EXPECT_FALSE(config.validate().empty());
config.lattice_smooth_max_checks = 0;
EXPECT_TRUE(config.validate().empty());
}
TEST(Lattice, DirectionChecksBothCellsOnAnExactBoundary)
@@ -1667,3 +1673,198 @@ TEST(Lattice, GapBelowEndpointFloorIsRejected)
EXPECT_FALSE(detail::verifyLatticeCandidate(context, path, &check));
EXPECT_TRUE(check.valid); // Collision, curvature, directions and crossing passed; the gap rejected it.
}
namespace
{
void appendSmoothingTestMotion(Path* path, double curvature, double length)
{
const PathPoint start = path->back();
const int samples = std::max(1, static_cast<int>(std::ceil(length / 0.04)));
for (int i = 1; i <= samples; ++i)
{
const double s = length * i / samples;
const double yaw = start.yaw + curvature * s;
if (std::abs(curvature) < 1e-12)
path->push_back({start.x+s*std::cos(start.yaw), start.y+s*std::sin(start.yaw), yaw, 0.0});
else
path->push_back({start.x+(std::sin(yaw)-std::sin(start.yaw))/curvature,
start.y-(std::cos(yaw)-std::cos(start.yaw))/curvature, yaw, curvature});
}
}
/** Real clearance for a test grid: distance [mm] from each cell centre to the nearest lethal cell centre, as the costmap provides.
* The default fixture value of 10 m everywhere makes the smoother place anchors 8 m apart. */
void fillClearance(LatticeTestGrid* map)
{
const double resolution = kResolution;
for (int y = 0; y < map->height; ++y)
for (int x = 0; x < map->width; ++x)
{
double best = 10.0;
for (int r = 0; r < std::max(map->width, map->height) && r * resolution < best; ++r)
for (int dy = -r; dy <= r; ++dy)
for (int dx = -r; dx <= r; ++dx)
{
if (std::max(std::abs(dx), std::abs(dy)) != r) continue;
const int nx = x + dx, ny = y + dy;
if (nx < 0 || ny < 0 || nx >= map->width || ny >= map->height) continue;
if (map->cost[static_cast<std::size_t>(ny) * map->width + nx] == kLethalCost)
best = std::min(best, std::hypot(dx, dy) * resolution);
}
map->clearance[static_cast<std::size_t>(y) * map->width + x] = static_cast<uint16_t>(std::lround(best * 1000.0));
}
}
Path smoothingZigzag()
{
Path path{{2.0, 2.0, 0.0, 0.0}};
for (int i = 0; i < 4; ++i)
{
appendSmoothingTestMotion(&path, 1.0/1.08, 0.18);
appendSmoothingTestMotion(&path, -1.0/1.08, 0.18);
}
return path;
}
struct SmoothingResult
{
Path path;
PathCheck check;
PlanInfo info;
};
SmoothingResult runSmoothing(const LatticeTestGrid& map, const Path& input, int64_t limit = 450000)
{
PlannerConfig config = latticeConfig();
config.lattice_smooth_enabled = true;
config.lattice_smooth_max_checks = limit;
RouteRequest request = latticeRequest(pose(input.front().x, input.front().y, input.front().yaw),
pose(input.back().x, input.back().y, input.back().yaw));
request.start.has_yaw = false;
request.goal.has_yaw = false;
GridRef grid = map.view();
SmoothingResult result;
result.path = input;
detail::LatticeMemory memory(128u * 1024u * 1024u);
detail::LatticeContext context{request, grid, config, 90.0, detail::Clock::now()+std::chrono::seconds(10), result.info, memory};
detail::LatticeCSpace space(context);
EXPECT_TRUE(detail::verifyLatticeCandidate(context, result.path, &result.check));
context.smooth_checks = &result.info.lattice_smooth_checks;
context.smooth_limit = static_cast<uint64_t>(limit);
detail::smoothLatticePath(context, space, &result.path, &result.check);
return result;
}
} // namespace
TEST(LatticeSmoother, OpenZigzagBecomesStraight)
{
LatticeTestGrid map(160, 160);
const SmoothingResult result = runSmoothing(map, smoothingZigzag());
ASSERT_EQ(result.info.lattice_smoothed, 1);
ASSERT_TRUE(result.check.valid);
EXPECT_GT(result.info.lattice_smooth_connectors_tried, 0u);
EXPECT_GT(result.info.lattice_smooth_connectors_ok, 0u);
EXPECT_GT(result.info.lattice_smooth_candidates, 0u);
EXPECT_EQ(result.info.lattice_smooth_stop, "accepted");
for (std::size_t i = 1; i < result.path.size(); ++i)
EXPECT_NEAR(wrapAngle(result.path[i].yaw-result.path[i-1].yaw), 0.0, 1e-6);
}
TEST(LatticeSmoother, LShapedCorridorKeepsOneCleanTurn)
{
LatticeTestGrid map(160, 160);
for (int y = 0; y < map.height; ++y)
for (int x = 0; x < map.width; ++x)
{
const double wx = (x+0.5)*kResolution, wy = (y+0.5)*kResolution;
const bool horizontal = wx >= 0.8 && wx <= 5.9 && wy >= 0.9 && wy <= 3.0;
const bool vertical = wx >= 3.9 && wx <= 6.1 && wy >= 0.9 && wy <= 6.5;
if (!horizontal && !vertical) map.cost[static_cast<std::size_t>(y)*map.width+x] = kLethalCost;
}
fillClearance(&map);
Path path = smoothingZigzag();
appendSmoothingTestMotion(&path, 0.0, 0.5);
appendSmoothingTestMotion(&path, 1.0/1.08, 1.08*kPi/2.0);
appendSmoothingTestMotion(&path, 0.0, 1.5);
const SmoothingResult result = runSmoothing(map, path);
ASSERT_TRUE(result.check.valid);
ASSERT_GE(result.info.lattice_smoothed, 1);
double min_yaw = 1e9, max_yaw = -1e9;
for (const auto& p : result.path) { min_yaw = std::min(min_yaw, p.yaw); max_yaw = std::max(max_yaw, p.yaw); }
EXPECT_GE(min_yaw, -0.05);
EXPECT_LE(max_yaw, kPi/2.0+0.05);
}
TEST(LatticeSmoother, NoBudgetKeepsVerifiedRawPath)
{
LatticeTestGrid map(160, 160);
const Path raw = smoothingZigzag();
const SmoothingResult result = runSmoothing(map, raw, 0);
ASSERT_TRUE(result.check.valid);
EXPECT_EQ(result.info.lattice_smoothed, 0);
EXPECT_EQ(result.info.lattice_smooth_stop, "budget");
ASSERT_EQ(result.path.size(), raw.size());
for (std::size_t i = 0; i < raw.size(); ++i)
{
EXPECT_DOUBLE_EQ(result.path[i].x, raw[i].x);
EXPECT_DOUBLE_EQ(result.path[i].y, raw[i].y);
EXPECT_DOUBLE_EQ(result.path[i].yaw, raw[i].yaw);
EXPECT_DOUBLE_EQ(result.path[i].curvature, raw[i].curvature);
}
}
TEST(LatticeSmoother, ExhaustionReturnsVerifiedPath)
{
LatticeTestGrid map(160, 160);
const SmoothingResult result = runSmoothing(map, smoothingZigzag(), 5);
EXPECT_TRUE(result.check.valid);
EXPECT_LE(result.info.lattice_smooth_checks, 5u);
}
TEST(LatticeSmoother, DeterministicForSameInput)
{
LatticeTestGrid map(160, 160);
const Path raw = smoothingZigzag();
const SmoothingResult a = runSmoothing(map, raw), b = runSmoothing(map, raw);
ASSERT_EQ(a.path.size(), b.path.size());
for (std::size_t i = 0; i < a.path.size(); ++i)
{
EXPECT_DOUBLE_EQ(a.path[i].x, b.path[i].x);
EXPECT_DOUBLE_EQ(a.path[i].y, b.path[i].y);
EXPECT_DOUBLE_EQ(a.path[i].yaw, b.path[i].yaw);
EXPECT_DOUBLE_EQ(a.path[i].curvature, b.path[i].curvature);
}
}
TEST(LatticeSmoother, EqualScoreStraightCandidateIsAccepted)
{
LatticeTestGrid map(160, 160);
Path raw{{2.0, 2.0, 0.0, 0.0}};
appendSmoothingTestMotion(&raw, 0.0, 2.0);
const SmoothingResult result = runSmoothing(map, raw);
ASSERT_TRUE(result.check.valid);
EXPECT_EQ(result.info.lattice_smoothed, 1);
EXPECT_EQ(result.info.lattice_smooth_stop, "accepted");
EXPECT_GT(result.info.lattice_smooth_candidates, 0u);
EXPECT_NEAR(result.path.back().x, raw.back().x, 1e-6);
EXPECT_NEAR(result.path.back().y, raw.back().y, 1e-6);
for (const auto& point : result.path) EXPECT_NEAR(point.y, raw.front().y, 1e-6);
}
TEST(LatticeSmoother, PlannerRunsEnabledSmoother)
{
LatticeTestGrid map(120, 120);
PlannerConfig config = latticeConfig();
config.lattice_smooth_enabled = true;
const PlanResult result = map.plan(latticeRequest(pose(1.0, 3.0, 0.0), pose(4.0, 3.0, 0.0)), config);
ASSERT_TRUE(result.ok) << result.info.reason;
EXPECT_TRUE(result.check.valid);
EXPECT_EQ(result.info.tier_used, "lattice");
EXPECT_GE(result.info.lattice_smooth_ms, 0.0);
EXPECT_LE(result.info.lattice_smooth_checks, static_cast<uint64_t>(config.lattice_smooth_max_checks));
EXPECT_LE(result.info.lattice_smooth_connectors_ok, result.info.lattice_smooth_connectors_tried);
EXPECT_LE(result.info.lattice_smooth_rejects_verify, result.info.lattice_smooth_candidates);
EXPECT_FALSE(result.info.lattice_smooth_stop.empty());
}