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

@@ -108,6 +108,9 @@ std::string PlannerConfig::validate() const
error << "live ridge limits must be >= 0";
else if (lattice_enabled && (lattice_heading_bins < 8 || lattice_heading_bins > 1024 || lattice_heading_bins % 2 != 0))
error << "lattice_heading_bins must be even and in [8, 1024]";
else if (lattice_enabled && (!not_negative(lattice_preferred_gap_m) ||
!positive(lattice_preferred_time_fraction) || lattice_preferred_time_fraction > 1.0))
error << "lattice_preferred_gap_m must be >= 0; lattice_preferred_time_fraction must be in (0, 1]";
else if (lattice_enabled && (!positive(lattice_closed_resolution_m) || !std::isfinite(lattice_heuristic_weight) || lattice_heuristic_weight < 1.0 ||
!not_negative(lattice_bucket_width_m) ||
!not_negative(lattice_field_clearance_weight_m) || !not_negative(lattice_wall_weight_m) ||
@@ -572,6 +575,13 @@ GapCheck checkFootprintGap(const Path& path, const GridRef& grid, double minimum
GapCheck checkFootprintGap(const Path& path, const GridRef& grid, double minimum_m, double half_x, double half_y,
const std::atomic<bool>* cancel, const std::chrono::steady_clock::time_point* deadline,
bool count_unknown)
{
return checkFootprintGap(path, grid, minimum_m, half_x, half_y, cancel, deadline, count_unknown, minimum_m);
}
GapCheck checkFootprintGap(const Path& path, const GridRef& grid, double minimum_m, double half_x, double half_y,
const std::atomic<bool>* cancel, const std::chrono::steady_clock::time_point* deadline,
bool count_unknown, double search_margin_m)
{
std::vector<PathPoint> poses;
if (path.empty())
@@ -594,7 +604,7 @@ GapCheck checkFootprintGap(const Path& path, const GridRef& grid, double minimum
const double resolution = grid.resolution;
const double half_cell = resolution / 2.0;
const double radius = std::hypot(half_x, half_y) + std::sqrt(2.0) * half_cell + minimum_m + 1e-9;
const double radius = std::hypot(half_x, half_y) + std::sqrt(2.0) * half_cell + std::max(minimum_m, search_margin_m) + 1e-9;
const double squared_radius = radius * radius;
std::vector<double> gaps(poses.size(), kInfinity);
for (std::size_t i = 0; i < poses.size(); ++i)

View File

@@ -45,12 +45,20 @@ bool hitsCell(double x0, double y0, double x1, double y1, int32_t x, int32_t y,
} // namespace
LatticeCSpace::LatticeCSpace(LatticeContext& context)
: context_(context), tiles_(&context.memory), blocked_prefix_(&context.memory)
: context_(context), tiles_(&context.memory), blocked_prefix_(&context.memory), gap_margin_(context.config.footprint_min_gap_m)
{
start_floor_ = endFloor(context.request.start);
goal_floor_ = endFloor(context.request.goal);
}
void LatticeCSpace::setGapMargin(double margin)
{
if (margin == gap_margin_) return;
// Tile values prove clearance at a specific margin. The obstacle prefix and endpoint floors do not depend on it.
tiles_.clear();
gap_margin_ = margin;
}
double LatticeCSpace::endFloor(const Pose2& pose) const
{
// The final lattice check measures the gap to lethal and unknown cells and lets the path stay down to the end pose gap minus 1 mm
@@ -124,7 +132,7 @@ bool LatticeCSpace::poseFree(double x, double y, double yaw, double sweep_margin
const bool near_goal = std::hypot(x - goal.x, y - goal.y) + centre_sweep <= relax;
// Away from the ends: full gap plus the sweep bound, conservative. Near an end: the floor of that end without the sweep bound,
// because the final check is the authority there and adding the sweep could lock in a robot parked closer than gap plus sweep.
double margin = c.footprint_min_gap_m + std::max(0.0, sweep_margin);
double margin = gap_margin_ + std::max(0.0, sweep_margin);
if (near_start || near_goal)
margin = std::min(near_start ? start_floor_ : c.footprint_min_gap_m, near_goal ? goal_floor_ : c.footprint_min_gap_m);
const double hx = c.half_x_m + margin, hy = c.half_y_m + margin;
@@ -184,17 +192,18 @@ bool LatticeCSpace::poseFree(double x, double y, double yaw, double sweep_margin
const double cx = g.origin_x + (ix + 0.5) * g.resolution;
const double cy = g.origin_y + (iy + 0.5) * g.resolution;
const double h = heading * 2.0 * kPi / bins;
const double expanded = gap_margin_ > c.footprint_min_gap_m ? gap_margin_ : 0.0;
const double quantization = g.resolution / std::sqrt(2.0) +
2.0 * std::hypot(c.half_x_m, c.half_y_m) * std::sin(kPi / (2.0 * bins));
2.0 * std::hypot(c.half_x_m + expanded, c.half_y_m + expanded) * std::sin(kPi / (2.0 * bins));
if (cached == 0)
{
const double margin = c.footprint_min_gap_m + quantization;
const double margin = gap_margin_ + quantization;
const bool safe = !footprintCollides(g, cx, cy, h, c.half_x_m + margin, c.half_y_m + margin);
cached = safe ? 1 : 2;
packed = static_cast<uint8_t>(packed | (cached << shift));
}
// A cached free sample must cover the actual displacement and yaw difference.
const double actual_error = std::hypot(x - cx, y - cy) + 2.0 * std::hypot(c.half_x_m, c.half_y_m) *
const double actual_error = std::hypot(x - cx, y - cy) + 2.0 * std::hypot(c.half_x_m + expanded, c.half_y_m + expanded) *
std::sin(std::abs(wrapAngle(yaw - h)) / 2.0);
if (cached == 1 && actual_error + sweep_margin <= quantization) return true;
// Away from the ends the final check demands the full gap. Testing only collision there would let the search reach the goal area
@@ -244,8 +253,7 @@ bool LatticeCSpace::segmentLegal(const PathPoint& a, const PathPoint& b)
}
const int count = std::max({ 1, static_cast<int>(std::ceil(distance / 0.025)), static_cast<int>(std::ceil(std::abs(yaw_delta) / 0.03)) });
const double centre_sweep = distance / (2.0 * count) + curve_error;
const double radius = std::hypot(context_.config.half_x_m + context_.config.footprint_min_gap_m,
context_.config.half_y_m + context_.config.footprint_min_gap_m);
const double radius = std::hypot(context_.config.half_x_m + gap_margin_, context_.config.half_y_m + gap_margin_);
const double sweep = centre_sweep + 2.0 * radius * std::sin(std::abs(yaw_delta) / (4.0 * count));
for (int i = 0; i < count; ++i)
{

View File

@@ -56,6 +56,7 @@ struct LatticeContext
LatticeMemory& memory;
uint64_t* smooth_checks = nullptr;
uint64_t smooth_limit = 0;
double gap_scan_margin_m = 0.0; ///< [m] Additional gap reporting range; minimum search keeps the original scan.
bool cancelled() const { return request.cancel && request.cancel->load(std::memory_order_relaxed); }
bool expired() const { return cancelled() || Clock::now() >= deadline || (smooth_checks && *smooth_checks >= smooth_limit); }
@@ -121,6 +122,8 @@ class LatticeCSpace
{
public:
explicit LatticeCSpace(LatticeContext& context);
void setGapMargin(double margin);
double gapMargin() const { return gap_margin_; }
bool poseFree(double x, double y, double yaw, double sweep_margin = 0.0, double centre_sweep = 0.0);
bool segmentLegal(const PathPoint& a, const PathPoint& b);
bool spinLegal(double x, double y, double from, double to);
@@ -137,6 +140,7 @@ private:
bool prefix_ready_ = false;
double start_floor_ = 0.0; // [m] Gap margin allowed near the start, see endFloor().
double goal_floor_ = 0.0; // [m] Same near the goal.
double gap_margin_; // [m] Away from endpoints; changing it invalidates tile samples.
double endFloor(const Pose2& pose) const;
bool buildBlockedPrefix();
uint32_t blockedCount(int32_t x0, int32_t y0, int32_t x1, int32_t y1) const;

View File

@@ -242,9 +242,11 @@ bool verifyCandidateImpl(LatticeContext& context, const Path& path, PathCheck* c
if (context.expired()) return false;
const GapCheck gap = checkFootprintGap(path, context.grid, context.config.footprint_min_gap_m,
context.config.half_x_m, context.config.half_y_m, context.request.cancel, &context.deadline,
true);
true, context.gap_scan_margin_m);
if (context.expired() || !gap.valid) return false;
context.info.min_gap_m = gap.min_gap_m;
context.info.lattice_gap_exact = std::isfinite(gap.min_gap_m) && gap.min_gap_m <=
std::max(context.config.footprint_min_gap_m, context.gap_scan_margin_m);
context.info.gap_exemption_used = gap.exemption_used;
return true;
}

View File

@@ -316,7 +316,8 @@ private:
context_.deviation_deg*kPi/180.0 + 1e-9) return false;
}
}
const double radius = std::hypot(context_.config.half_x_m, context_.config.half_y_m) + context_.config.footprint_min_gap_m;
const double radius = std::hypot(context_.config.half_x_m + space_.gapMargin(),
context_.config.half_y_m + space_.gapMargin());
return space_.poseFree(a.x, a.y, a.yaw, bound + 2.0*radius*std::sin(turn/2.0), bound);
}

View File

@@ -2,8 +2,13 @@
#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstddef>
#include <new>
#include <string>
#include <utility>
#include <lane_route_core/grid_checks.h>
namespace lane_route_core
{
@@ -36,13 +41,18 @@ bool planLatticeTier(const LatticeTierInput& input, GraphTierOutput output)
LatticeMemory memory(limit);
LatticeContext context{ input.request, input.grid, input.config, input.deviation_deg, input.deadline, output.info, memory };
output.info.lattice_reason.clear();
output.info.lattice_gap_pass.clear();
output.info.lattice_gap_exact = false;
bool ok = false;
bool search_timed_out = false;
// A preferred gap at or below the minimum would repeat the minimum pass, so it disables the preferred pass instead of failing
// validation: raising footprint_min_gap_m alone must not stop the planner from starting.
const bool preferred = input.config.lattice_preferred_gap_m > input.config.footprint_min_gap_m;
const auto slice = input.deadline - began;
try
{
LatticeCSpace space(context);
LatticeField field(&memory);
const auto slice = input.deadline - began;
const TimePoint field_deadline = began + std::chrono::duration_cast<Clock::duration>(slice * input.config.lattice_field_time_fraction);
bool field_ok = false;
{
@@ -55,7 +65,66 @@ bool planLatticeTier(const LatticeTierInput& input, GraphTierOutput output)
StageTimer timer(output.info.lattice_search_ms, &output.info.lattice_verify_ms);
ok = searchLattice(context, space, field, &output.path, &output.check);
}
search_timed_out = Clock::now() >= input.deadline;
search_timed_out = !ok && Clock::now() >= input.deadline;
if (ok) output.info.lattice_gap_pass = "minimum";
if (ok && preferred && !context.cancelled() && Clock::now() < input.deadline)
{
StageTimer timer(output.info.lattice_upgrade_ms);
const double saved_gap = output.info.min_gap_m;
const bool saved_gap_exact = output.info.lattice_gap_exact;
const bool saved_exemption = output.info.gap_exemption_used;
const std::string saved_reason = output.info.lattice_reason;
bool upgraded = false;
try
{
context.deadline = input.deadline;
space.setGapMargin(input.config.lattice_preferred_gap_m);
bool already_preferred = true;
for (std::size_t i = 1; i < output.path.size(); ++i)
if (!space.segmentLegal(output.path[i-1], output.path[i])) { already_preferred = false; break; }
if (already_preferred)
upgraded = true;
else if (!context.cancelled() && Clock::now() < input.deadline)
{
Path candidate;
PathCheck candidate_check;
const TimePoint search_began = Clock::now();
context.deadline = std::min(input.deadline, search_began +
std::chrono::duration_cast<Clock::duration>(slice * input.config.lattice_preferred_time_fraction));
context.gap_scan_margin_m = input.config.lattice_preferred_gap_m;
bool found = false;
{
StageTimer search_timer(output.info.lattice_search_ms, &output.info.lattice_verify_ms);
found = searchLattice(context, space, field, &candidate, &candidate_check);
}
if (found)
{
output.path = std::move(candidate);
output.check = candidate_check;
upgraded = true;
}
}
}
catch (const std::bad_alloc&)
{
// The minimum path is already verified; an upgrade allocation failure cannot discard it.
}
if (upgraded)
{
output.info.lattice_gap_pass = "preferred";
context.gap_scan_margin_m = input.config.lattice_preferred_gap_m;
}
else
{
output.info.lattice_reason = saved_reason;
output.info.min_gap_m = saved_gap;
output.info.lattice_gap_exact = saved_gap_exact;
output.info.gap_exemption_used = saved_exemption;
context.gap_scan_margin_m = 0.0;
space.setGapMargin(input.config.footprint_min_gap_m);
}
context.deadline = input.deadline;
}
if (ok && !search_timed_out && input.config.lattice_smooth_enabled && !context.cancelled())
{
// The wavefront is no longer needed; release it before the smoother allocates its anchor graph.
@@ -73,6 +142,23 @@ bool planLatticeTier(const LatticeTierInput& input, GraphTierOutput output)
catch (const std::bad_alloc&) { output.info.lattice_smooth_stop = "budget"; }
output.info.lattice_smooth_ms += msSince(smooth_began);
}
// A found obstacle can bound a second scan, making the reported minimum exact without changing the gap rule.
// If there is no nearby obstacle, retain the bounded result rather than flood an open map just for diagnostics.
if (ok && !search_timed_out && !context.cancelled() && !output.info.lattice_gap_exact &&
std::isfinite(output.info.min_gap_m) && Clock::now() < input.smooth_deadline)
{
context.smooth_checks = nullptr;
context.deadline = input.smooth_deadline;
const double scan = output.info.min_gap_m + 1e-9;
StageTimer timer(output.info.lattice_verify_ms);
const GapCheck gap = checkFootprintGap(output.path, input.grid, input.config.footprint_min_gap_m,
input.config.half_x_m, input.config.half_y_m, input.request.cancel, &context.deadline, true, scan);
if (!context.expired() && gap.valid && std::isfinite(gap.min_gap_m) && gap.min_gap_m <= scan)
{
output.info.min_gap_m = gap.min_gap_m;
output.info.lattice_gap_exact = true;
}
}
}
}
catch (const std::bad_alloc&)
@@ -90,6 +176,8 @@ bool planLatticeTier(const LatticeTierInput& input, GraphTierOutput output)
if (!ok || cancelled || timed_out)
{
output.path.clear();
output.info.lattice_gap_pass.clear();
output.info.lattice_gap_exact = false;
return false;
}
output.info.reason = "verified";