feat(lane_route): sharp corners as the last radius rung, heuristic field margin (R2.7)

max_kink_deg (default 45): in the graph's second pass, a corner where no
radius fits may become a sharp corner (two straight lines meeting at one
vertex pose, no turn in place) when the footprint can rotate at the
vertex; the verifier counts it in PathCheck::corners. The verifier's
bend limit now follows min_turn_radius_m (it blocked radii below ~0.72 m).

lattice_field_extra_clearance_m (default 0): extra clearance for a cell
to be passable in the lattice heuristic field only, with a rebuild of
the plain field when the margin field never reaches the start.

regress7 with the sim settings (0.4 m floor, margin 0.1): hook_150
1717/1757, kit 1716/1757 (R2.5: 1718, 1709); Python recheck finds no
curvature, corner, spin, zone or crossing errors. The graph tier's
30 mm gap still ignores unknown cells, as since R0.
This commit is contained in:
2026-11-09 23:30:46 +07:00
parent 907fa0c4dc
commit 527071827f
11 changed files with 112 additions and 15 deletions

View File

@@ -110,8 +110,10 @@ std::string PlannerConfig::validate() const
error << "lattice_heading_bins must be even and in [8, 1024]";
else if (!positive(regular_turn_radius_m))
error << "regular_turn_radius_m must be finite and > 0";
else if (lattice_enabled && !not_negative(lattice_tight_turn_cost_m))
error << "lattice_tight_turn_cost_m must be finite and >= 0";
else if (!not_negative(max_kink_deg) || max_kink_deg > 90.0)
error << "max_kink_deg must be in [0, 90]";
else if (lattice_enabled && (!not_negative(lattice_tight_turn_cost_m) || !not_negative(lattice_field_extra_clearance_m)))
error << "lattice_tight_turn_cost_m and lattice_field_extra_clearance_m must be finite and >= 0";
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]";

View File

@@ -45,7 +45,7 @@ double LatticeField::at(double x, double y, const LatticeContext& context) const
return complete ? std::min(maximum, frontier + fallback) : std::max(frontier, fallback);
}
bool buildLatticeField(LatticeContext& context, LatticeField* field, TimePoint field_deadline)
bool buildLatticeField(LatticeContext& context, LatticeField* field, TimePoint field_deadline, double extra_clearance_m)
{
const GridRef& g = context.grid;
const auto cellCount = [&](int factor) -> int64_t {
@@ -87,7 +87,11 @@ bool buildLatticeField(LatticeContext& context, LatticeField* field, TimePoint f
// a grid without an inscribed ring has walls one or two cells thick, and a block rule on cost alone opened passages through them,
// so the heuristic pointed through walls and the search flooded (warehouse p0: 1.1 M states, no path; full resolution: 339 states).
const PlannerConfig& config = context.config;
const double fit_m = std::min(config.half_x_m, config.half_y_m) + config.footprint_min_gap_m - g.resolution;
// extra_clearance_m closes gaps that are only as wide as the robot: a long robot can stand in a gap between two posts but rarely
// turn in and out of it, and a field that runs through such gaps pulls the search there until it times out (sim 9/11, hook_150
// with a 0.196 m inscribed ring: four queries timed out that solve in about 1 s with the gaps closed).
const double fit_m = std::min(config.half_x_m, config.half_y_m) + config.footprint_min_gap_m - g.resolution +
std::max(0.0, extra_clearance_m);
const uint16_t fit_mm = static_cast<uint16_t>(std::max(0.0, std::min(65535.0, std::floor(fit_m * 1000.0))));
const auto fits = [&](std::size_t cell) { return g.cost[cell] < kInscribedCost && g.clearance_mm[cell] >= fit_mm; };
std::pmr::vector<uint8_t> passable(&context.memory), direction(&context.memory);

View File

@@ -160,7 +160,9 @@ struct LatticeField
bool rejectLatticeCrossing(LatticeContext& context, const Path& path);
bool verifyLatticeCandidate(LatticeContext& context, const Path& path, PathCheck* check);
bool buildLatticeField(LatticeContext& context, LatticeField* field, TimePoint field_deadline);
/** Reverse field from the goal. extra_clearance_m raises the passable threshold of the field only (see
* PlannerConfig::lattice_field_extra_clearance_m). The field must be empty. */
bool buildLatticeField(LatticeContext& context, LatticeField* field, TimePoint field_deadline, double extra_clearance_m = 0.0);
bool searchLattice(LatticeContext& context, LatticeCSpace& space, const LatticeField& field, Path* path, PathCheck* check);
void smoothLatticePath(LatticeContext& context, LatticeCSpace& space, Path* path, PathCheck* check);
bool planLatticeTier(const LatticeTierInput& input, GraphTierOutput output);

View File

@@ -57,7 +57,19 @@ bool planLatticeTier(const LatticeTierInput& input, GraphTierOutput output)
bool field_ok = false;
{
StageTimer timer(output.info.lattice_field_ms);
field_ok = buildLatticeField(context, &field, field_deadline);
const double extra = input.config.lattice_field_extra_clearance_m;
field_ok = buildLatticeField(context, &field, field_deadline, extra);
// Closing robot-wide gaps can also close the only way to the start (regress7 hook_150 box_full_zone_width at 22.5 deg:
// solved in 0.5 s without the margin, timed out with 0.1 m). A complete field that never reached the start means exactly
// that, so the plain field is built instead.
if (field_ok && extra > 0.0 && field.complete && !context.cancelled() && Clock::now() < field_deadline)
{
field.values.clear();
field.settled.clear();
field.frontier = 0.0;
field.complete = false;
field_ok = buildLatticeField(context, &field, field_deadline, 0.0);
}
}
if (field_ok)
{

View File

@@ -72,7 +72,7 @@ int zoneViolations(const PathPoint& a, const PathPoint& b, const GridRef& grid,
return count;
}
bool kinkMetrics(const Path& path, int* kinks, double* max_bend_deg,
bool kinkMetrics(const Path& path, double bend_limit_deg, int* kinks, double* max_bend_deg,
const std::atomic<bool>* cancel, const std::chrono::steady_clock::time_point* deadline)
{
std::vector<double> along(path.size(), 0.0);
@@ -118,7 +118,7 @@ bool kinkMetrics(const Path& path, int* kinks, double* max_bend_deg,
const PathPoint& c = path[keep[k + 2]];
const double bend = std::abs(wrapAngle(std::atan2(c.y - b.y, c.x - b.x) - std::atan2(b.y - a.y, b.x - a.x))) * kRadToDeg;
largest = std::max(largest, bend);
if (bend > kBendLimitDeg)
if (bend > bend_limit_deg)
++over;
}
*kinks = spins + over;
@@ -215,7 +215,16 @@ PathCheck verifyPath(const Path& path, const GridRef& grid, double deviation_deg
if (!std::isfinite(point.x) || !std::isfinite(point.y) || !std::isfinite(point.yaw))
return check;
if (!kinkMetrics(path, &check.kinks, &check.max_bend_deg, cancel, deadline)) return check;
// The bend limit follows the tightest legal curve (a 0.4 m floor turns 14.3 deg over 0.1 m), and a sharp corner may bend up
// to max_kink_deg; the curvature test below still tells a sharp corner from a too-tight curve.
const double kink_rad = config.max_kink_deg * kPi / 180.0;
const double bend_limit_deg = std::max({ kBendLimitDeg, 1.1 * kBendSpacingM / config.min_turn_radius_m * kRadToDeg,
config.max_kink_deg > 0.0 ? config.max_kink_deg + 1e-6 : 0.0 });
if (!kinkMetrics(path, bend_limit_deg, &check.kinks, &check.max_bend_deg, cancel, deadline)) return check;
const auto straightStep = [&path](std::size_t i) {
return i + 1 < path.size() && std::hypot(path[i + 1].x - path[i].x, path[i + 1].y - path[i].y) > 1e-7 &&
std::abs(wrapAngle(path[i + 1].yaw - path[i].yaw)) < 1e-6;
};
for (std::size_t i = 0; i + 1 < path.size(); ++i)
{
if ((cancel && cancel->load(std::memory_order_relaxed)) || (deadline && std::chrono::steady_clock::now() >= *deadline)) return check;
@@ -225,7 +234,12 @@ PathCheck verifyPath(const Path& path, const GridRef& grid, double deviation_deg
const double dy = b.y - a.y;
const double distance = std::hypot(dx, dy);
const double turn = wrapAngle(b.yaw - a.yaw);
if (distance > 1e-7)
// A sharp corner is one turning step between two straight steps; it is counted, not measured as a curve.
const bool corner = kink_rad > 0.0 && distance > 1e-7 && std::abs(turn) > 1e-6 && std::abs(turn) <= kink_rad + 1e-9 &&
i > 0 && straightStep(i - 1) && straightStep(i + 1);
if (corner)
++check.corners;
else if (distance > 1e-7)
{
// The displacement is a chord: recover arc length before estimating curvature.
const double arc = std::abs(turn) < 1e-9 ? distance : distance * std::abs(turn) / (2.0 * std::abs(std::sin(turn / 2.0)));

View File

@@ -169,6 +169,25 @@ bool simplifyRoute(const QueryGraph& query, const std::vector<int32_t>& route, c
return true;
}
/** A sharp corner is usable when the vertex is free and the footprint can rotate there from the incoming to the outgoing
* heading without touching lethal or unknown cells; the robot may then pivot at the vertex even if it does not cut it. */
bool kinkLegal(const Point2& vertex, double from, double to, const QueryContext& context)
{
const GridRef& grid = context.grid;
const double fx = std::floor((vertex.x - grid.origin_x) / grid.resolution);
const double fy = std::floor((vertex.y - grid.origin_y) / grid.resolution);
if (fx < 0.0 || fy < 0.0 || fx >= grid.width || fy >= grid.height)
return false;
if (grid.cost[static_cast<std::size_t>(fy) * grid.width + static_cast<std::size_t>(fx)] >= kInscribedCost)
return false;
const double delta = wrapAngle(to - from);
const int samples = std::max(1, static_cast<int>(std::ceil(std::abs(delta) / 0.03)));
for (int i = 0; i <= samples; ++i)
if (footprintCollides(grid, vertex.x, vertex.y, from + delta * i / samples, context.config.half_x_m, context.config.half_y_m))
return false;
return true;
}
/** A curve is usable when every pose is free, poses near obstacles pass the footprint test, and the direction holds. */
bool curveLegal(const Path& curve, const QueryContext& context)
{
@@ -283,6 +302,12 @@ bool buildPath(const std::vector<Point2>& vertices, const Pose2& start, const Po
options.push_back(Option{ trim, std::make_shared<const Path>(std::move(curve)), radius });
}
}
// Bottom rung of the radius ladder: a sharp corner, only where no radius fits (PlannerConfig::max_kink_deg).
const double in = headingOf(a, b), out_heading = headingOf(b, c);
if (complete && options.empty() && context->config.max_kink_deg > 0.0 &&
std::abs(wrapAngle(out_heading - in)) <= context->config.max_kink_deg * kPi / 180.0 + 1e-9 &&
kinkLegal(b, in, out_heading, *context))
options.push_back(Option{ 0.0, std::make_shared<const Path>(Path{ PathPoint{ b.x, b.y, in, 0.0 } }), 0.0 });
if (complete)
context->corner_options[key] = options;
}

View File

@@ -127,7 +127,9 @@ PlanResult planRoute(const GraphProvider& graphs, const RouteRequest& request, c
regular.corner_radii_m.push_back(radius);
if (regular.corner_radii_m.empty())
regular.corner_radii_m.push_back(regular.min_turn_radius_m);
const bool tight_corners = config.min_turn_radius_m < regular.min_turn_radius_m - 1e-9;
regular.max_kink_deg = 0.0;
// The second pass adds the tight radii and, at its bottom, sharp corners up to max_kink_deg.
const bool tight_corners = config.min_turn_radius_m < regular.min_turn_radius_m - 1e-9 || config.max_kink_deg > 0.0;
const PlannerConfig& lattice_config = config.lattice_tight_arcs ? config : regular;
const double chord = std::hypot(request.goal.x - request.start.x, request.goal.y - request.start.y);