feat(lane_route): keep a preferred footprint gap on lattice paths (R2.5)

The lattice tier first finds a path with the minimum gap, as before, so
coverage cannot drop. If that path is not legal under
lattice_preferred_gap_m (0.15 m) away from the ends, a bounded upgrade
search (lattice_preferred_time_fraction of the rung slice) tries a wider
path and replaces it on success. Smoothing keeps the margin of the
returned path. PlanInfo, eval JSON and the plugin debug/fallback line
report the gap pass, the minimum gap and the upgrade time. The plugin
loads the preferred-gap and smoother keys.

regress7 (3 s): coverage equal to R2c (hook_150 1718, kit 1709),
graph-only output identical, middle gap >= 0.15 m on 133/153 and
128/190 lattice paths (R2c 88 and 89), Python recheck 0 bad.
This commit is contained in:
2026-11-09 17:42:48 +07:00
parent b0a3954541
commit d27cff23bd
11 changed files with 308 additions and 13 deletions

View File

@@ -242,6 +242,13 @@ TEST(GapCheck, MatchesHandComputedDistances)
// Nothing near.
path = { { 1.0, 1.0, 0.0, 0.0 } };
EXPECT_TRUE(std::isinf(checkFootprintGap(path, grid, 0.03, 0.3, 0.3).min_gap_m));
// Extending the measurement scan does not change the 0.03 m acceptance threshold.
path = { { 2.4, 2.525, 0.0, 0.0 } };
const GapCheck default_scan = checkFootprintGap(path, grid, 0.03, 0.3, 0.3);
const GapCheck extended = checkFootprintGap(path, grid, 0.03, 0.3, 0.3, nullptr, nullptr, false, 0.3);
EXPECT_EQ(default_scan.valid, extended.valid);
EXPECT_TRUE(std::isinf(default_scan.min_gap_m));
EXPECT_NEAR(extended.min_gap_m, 0.3, 1e-9);
// An in-place spin is sampled too: a square turning 45 deg reaches further than it starts.
path = { { 2.69, 2.525, 0.0, 0.0 }, { 2.69, 2.525, kPi / 4.0, 0.0 } };
EXPECT_NEAR(checkFootprintGap(path, grid, 0.03, 0.2, 0.2).min_gap_m, 3.0 - (2.69 + 0.2 * std::sqrt(2.0)), 1e-9);
@@ -983,6 +990,7 @@ PlannerConfig latticeConfig()
{
PlannerConfig c;
c.lattice_enabled = true;
c.lattice_preferred_gap_m = 0.0; // Existing lattice tests exercise the minimum-gap search.
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;
@@ -1459,6 +1467,21 @@ TEST(Lattice, InvalidParametersAreRejected)
EXPECT_FALSE(config.validate().empty());
config.lattice_smooth_max_checks = 0;
EXPECT_TRUE(config.validate().empty());
config.lattice_preferred_gap_m = -0.01;
EXPECT_FALSE(config.validate().empty());
config.lattice_preferred_gap_m = 0.01; // Below footprint_min_gap_m: the preferred pass is off, the value is legal.
EXPECT_TRUE(config.validate().empty());
config.lattice_preferred_gap_m = 0.15;
config.lattice_preferred_time_fraction = 0.0;
EXPECT_FALSE(config.validate().empty());
config.lattice_preferred_time_fraction = 1.01;
EXPECT_FALSE(config.validate().empty());
config.lattice_preferred_time_fraction = 1.0;
EXPECT_TRUE(config.validate().empty());
config.lattice_enabled = false;
config.lattice_preferred_gap_m = -1.0;
config.lattice_preferred_time_fraction = 0.0;
EXPECT_TRUE(config.validate().empty());
}
TEST(Lattice, DirectionChecksBothCellsOnAnExactBoundary)
@@ -1868,3 +1891,144 @@ TEST(LatticeSmoother, PlannerRunsEnabledSmoother)
EXPECT_LE(result.info.lattice_smooth_rejects_verify, result.info.lattice_smooth_candidates);
EXPECT_FALSE(result.info.lattice_smooth_stop.empty());
}
namespace
{
void preferredGapCorridor(LatticeTestGrid* map, int first_free_row, int last_free_row)
{
for (int y = 0; y < map->height; ++y)
if (y < first_free_row || y > last_free_row)
for (int x = 0; x < map->width; ++x)
map->cost[static_cast<std::size_t>(y)*map->width+x] = kLethalCost;
fillClearance(map);
}
} // namespace
TEST(LatticePreferredGap, WideCorridorUsesPreferredAndSmoothingKeepsGap)
{
LatticeTestGrid map(120, 100);
preferredGapCorridor(&map, 40, 59); // Free width 1.0 m: a 0.6 m robot has 0.20 m each side.
PlannerConfig config = latticeConfig();
config.lattice_preferred_gap_m = 0.15;
config.lattice_smooth_enabled = true;
const auto result = map.plan(latticeRequest(pose(1.0, 2.5, 0.0), pose(4.5, 2.5, 0.0)), config);
ASSERT_TRUE(result.ok) << result.info.reason;
EXPECT_EQ(result.info.lattice_gap_pass, "preferred");
EXPECT_TRUE(result.check.valid);
const GapCheck gap = checkFootprintGap(result.path, map.view(), 0.15, config.half_x_m, config.half_y_m, nullptr, nullptr, true);
EXPECT_TRUE(gap.valid);
EXPECT_FALSE(gap.exemption_used);
EXPECT_GE(gap.min_gap_m, 0.15 - 1e-9);
}
TEST(LatticePreferredGap, PreferredAtOrBelowMinimumSkipsUpgrade)
{
LatticeTestGrid map(120, 120);
PlannerConfig config = latticeConfig();
config.footprint_min_gap_m = 0.20;
config.lattice_preferred_gap_m = 0.15;
const auto 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_EQ(result.info.lattice_gap_pass, "minimum");
EXPECT_DOUBLE_EQ(result.info.lattice_upgrade_ms, 0.0);
}
TEST(LatticePreferredGap, NarrowCorridorFallsBackToMinimum)
{
LatticeTestGrid map(120, 100);
preferredGapCorridor(&map, 42, 56); // Free width 0.75 m: 0.075 m gap on each side.
PlannerConfig config = latticeConfig();
config.lattice_preferred_gap_m = 0.15;
config.lattice_preferred_time_fraction = 0.05;
const auto result = map.plan(latticeRequest(pose(1.0, 2.475, 0.0), pose(4.5, 2.475, 0.0)), config);
ASSERT_TRUE(result.ok) << result.info.reason;
EXPECT_EQ(result.info.lattice_gap_pass, "minimum");
EXPECT_EQ(result.info.lattice_reason, "verified");
EXPECT_FALSE(result.info.gap_exemption_used);
EXPECT_GE(result.info.lattice_upgrade_ms, 0.0);
EXPECT_TRUE(result.info.lattice_gap_exact);
EXPECT_GE(result.info.min_gap_m, config.footprint_min_gap_m - 1e-9);
EXPECT_LT(result.info.min_gap_m, config.lattice_preferred_gap_m);
}
TEST(LatticePreferredGap, UpgradeReplacesShortNarrowRouteWithWideDetour)
{
LatticeTestGrid map(360, 240);
// The straight gap between the two walls is 0.70 m wide; there is open room above and below them.
constexpr int x0 = 120, x1 = 239;
constexpr int lower_y0 = 80, lower_y1 = 92, upper_y0 = 107, upper_y1 = 119;
for (int y = 0; y < map.height; ++y)
for (int x = 0; x < map.width; ++x)
{
if (x >= x0 && x <= x1 && ((y >= lower_y0 && y <= lower_y1) || (y >= upper_y0 && y <= upper_y1)))
map.cost[static_cast<std::size_t>(y)*map.width+x] = kLethalCost;
const int dx = std::max({x0-x, 0, x-x1});
const int lower_dy = std::max({lower_y0-y, 0, y-lower_y1});
const int upper_dy = std::max({upper_y0-y, 0, y-upper_y1});
const double cells = std::min(std::hypot(static_cast<double>(dx), static_cast<double>(lower_dy)),
std::hypot(static_cast<double>(dx), static_cast<double>(upper_dy)));
map.clearance[static_cast<std::size_t>(y)*map.width+x] =
static_cast<uint16_t>(std::min(65535.0, std::floor(cells*kResolution*1000.0)));
}
PlannerConfig config = latticeConfig();
config.time_limit_s = 20.0;
config.fallback_time_limit_s = 12.0;
config.lattice_preferred_time_fraction = 0.9;
config.lattice_wall_weight_m = 0.0;
config.lattice_field_clearance_weight_m = 0.0;
const RouteRequest request = latticeRequest(pose(2.0, 5.0, 0.0), pose(16.0, 5.0, 0.0));
const auto minimum = map.plan(request, config);
ASSERT_TRUE(minimum.ok) << minimum.info.reason;
EXPECT_EQ(minimum.info.lattice_gap_pass, "minimum");
EXPECT_DOUBLE_EQ(minimum.info.lattice_upgrade_ms, 0.0);
const GapCheck narrow = checkFootprintGap(minimum.path, map.view(), 0.15, config.half_x_m, config.half_y_m, nullptr, nullptr, true);
ASSERT_LT(narrow.min_gap_m, 0.10);
EXPECT_NEAR(narrow.min_gap_m, 0.05, 1e-6);
config.lattice_preferred_gap_m = 0.15;
const auto upgraded = map.plan(request, config);
ASSERT_TRUE(upgraded.ok) << upgraded.info.reason;
EXPECT_EQ(upgraded.info.lattice_gap_pass, "preferred");
EXPECT_GT(upgraded.info.path_length_m, minimum.info.path_length_m + 0.25);
EXPECT_GT(upgraded.info.lattice_upgrade_ms, 0.0);
const GapCheck wide = checkFootprintGap(upgraded.path, map.view(), 0.15, config.half_x_m, config.half_y_m, nullptr, nullptr, true);
EXPECT_TRUE(wide.valid);
EXPECT_FALSE(wide.exemption_used);
EXPECT_GE(wide.min_gap_m, 0.15 - 1e-6);
}
TEST(LatticePreferredGap, ParkedStartRetainsEndpointFloor)
{
LatticeTestGrid map(120, 100);
for (int y = 0; y < map.height; ++y) map.cost[y * map.width + 100] = kLethalCost;
map.clearance[50 * map.width + 93] = 315;
PlannerConfig config = latticeConfig();
config.clearance_min_m = 0.30;
config.clearance_hard_m = 0.30;
config.lattice_preferred_gap_m = 0.15;
const auto result = map.plan(latticeRequest(pose(4.685, 2.5, kPi), pose(1.0, 2.5, kPi)), config);
ASSERT_TRUE(result.ok) << result.info.reason;
EXPECT_EQ(result.info.lattice_gap_pass, "preferred");
EXPECT_TRUE(result.info.gap_exemption_used);
EXPECT_TRUE(result.info.lattice_gap_exact);
EXPECT_NEAR(result.info.min_gap_m, 0.015, 1e-6);
}
TEST(LatticePreferredGap, ChangingMarginInvalidatesCachedPoses)
{
LatticeTestGrid map(140, 100);
preferredGapCorridor(&map, 42, 58); // 0.125 m gap: a minimum-margin cache entry can be free, preferred cannot.
PlannerConfig config = latticeConfig();
RouteRequest request = latticeRequest(pose(1.0, 2.525, 0.0), pose(6.0, 2.525, 0.0));
GridRef grid = map.view();
PlanInfo info;
detail::LatticeMemory memory(64 * 1024 * 1024);
detail::LatticeContext context{request, grid, config, 90.0, detail::Clock::now()+std::chrono::seconds(10), info, memory};
detail::LatticeCSpace space(context);
EXPECT_TRUE(space.poseFree(3.5, 2.525, 0.0));
EXPECT_GT(info.lattice_tiles_built, 0u);
space.setGapMargin(0.15);
EXPECT_FALSE(space.poseFree(3.5, 2.525, 0.0));
space.setGapMargin(config.footprint_min_gap_m);
EXPECT_TRUE(space.poseFree(3.5, 2.525, 0.0));
}