feat(lane_route): second heuristic field and diagonal zone margin (R2.10)

- lattice_alt_field_enabled (default false), lattice_alt_field_relax_m,
  lattice_alt_field_switch_fraction: when the search on the first field
  has not finished at the switch point, it restarts once on a field with
  no extra clearance and the alternate relax. Exhausted searches do not
  switch. PlanInfo/eval/debug line report field_pass.
- lattice_field_zone_margin_deg (default 0): diagonal field steps through
  a direction zone must stay that far inside the deviation limit; at
  exactly 45 deg the field otherwise follows a diagonal a turning robot
  cannot hold (sim M4, M7, 4a timed out even with 10 s).
- lane_route_eval compiles the graph with the live master when it holds
  preferred cells, as the plugin does.
- Measured 10/11 (sim settings, 10 s, margin 15, alternate on): regress7
  hook_150 1719, kit 1718, no query lost, independent recheck 0 bad.
This commit is contained in:
2026-11-10 13:42:23 +07:00
parent 1d26e29899
commit a4b655b74b
8 changed files with 160 additions and 10 deletions

View File

@@ -117,6 +117,12 @@ std::string PlannerConfig::validate() const
else if (lattice_enabled && (!not_negative(lattice_tight_turn_cost_m) || !not_negative(lattice_field_extra_clearance_m) ||
!not_negative(lattice_field_standability_relax_m)))
error << "lattice_tight_turn_cost_m, lattice_field_extra_clearance_m and lattice_field_standability_relax_m must be finite and >= 0";
else if (lattice_enabled && (!not_negative(lattice_field_zone_margin_deg) || lattice_field_zone_margin_deg >= 45.0))
error << "lattice_field_zone_margin_deg must be in [0, 45)";
else if (lattice_enabled && lattice_alt_field_enabled &&
(!not_negative(lattice_alt_field_relax_m) || !positive(lattice_alt_field_switch_fraction) ||
lattice_alt_field_switch_fraction >= 1.0))
error << "lattice_alt_field_relax_m must be finite and >= 0; lattice_alt_field_switch_fraction must be in (0, 1)";
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

@@ -46,7 +46,8 @@ 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, double extra_clearance_m)
bool buildLatticeField(LatticeContext& context, LatticeField* field, TimePoint field_deadline, double extra_clearance_m,
double relax_m)
{
const GridRef& g = context.grid;
const auto cellCount = [&](int factor) -> int64_t {
@@ -94,7 +95,7 @@ bool buildLatticeField(LatticeContext& context, LatticeField* field, TimePoint f
// This changes only the heuristic; search and verification use the full footprint and gap.
Standability standability(g, config, &context.memory, &context.info.lattice_standability_tests,
&context.info.lattice_standability_rejected, context.request.cancel, &field_deadline,
config.lattice_field_standability_relax_m);
relax_m >= 0.0 ? relax_m : config.lattice_field_standability_relax_m);
const auto fits = [&](std::size_t cell) {
return (extra_clearance_m <= 0.0 || g.clearance_mm[cell] >= fit_mm) &&
standability.fits(cell);
@@ -159,6 +160,12 @@ bool buildLatticeField(LatticeContext& context, LatticeField* field, TimePoint f
}
return state == 1;
};
// Grid steps come in 45 deg increments, so a deviation of exactly 45 deg lets the field step diagonally through a zone at
// the very limit of the rule. A robot that has to turn into and out of such a line cannot follow it, and the search floods
// the region until the budget runs out (sim dumps M4, M7: solved in 1 s at 0-30 deg, timeout at 45 deg even with 10 s).
// The margin applies to diagonal steps only: applied to axis steps it also closed the perpendicular steps at 90 deg and
// slowed regress7 sim_005_zones from under 1 s to 2-3 s. Search and verification still use the full rule.
const double diagonal_deviation = std::max(0.0, context.deviation_deg - context.config.lattice_field_zone_margin_deg);
// Start and goal are always free here: a robot parked closer than the fit rule must still get a field; the C-space decides the rest.
const auto isFree = [&](int32_t cell) {
if (cell == start || cell == goal) return true;
@@ -202,7 +209,8 @@ bool buildLatticeField(LatticeContext& context, LatticeField* field, TimePoint f
const int32_t next = ny * width + nx;
if (!isFree(next)) continue;
const double angle = std::atan2(-dy, -dx);
if (!allows(zone(next), angle, context.deviation_deg) || !allows(zone(top.cell), angle, context.deviation_deg)) continue;
const double step_deviation = dx != 0 && dy != 0 ? diagonal_deviation : context.deviation_deg;
if (!allows(zone(next), angle, step_deviation) || !allows(zone(top.cell), angle, step_deviation)) continue;
const double clearance_m = std::max(0.05, clear(top.cell) / 1000.0);
const float next_distance = static_cast<float>(top.distance + std::hypot(dx, dy) * field_resolution *
(1.0 + context.config.lattice_field_clearance_weight_m / clearance_m));

View File

@@ -162,7 +162,9 @@ bool rejectLatticeCrossing(LatticeContext& context, const Path& path);
bool verifyLatticeCandidate(LatticeContext& context, const Path& path, PathCheck* check);
/** 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);
/** @param relax_m Standability relax of this field; a negative value takes lattice_field_standability_relax_m. */
bool buildLatticeField(LatticeContext& context, LatticeField* field, TimePoint field_deadline, double extra_clearance_m = 0.0,
double relax_m = -1.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

@@ -42,6 +42,7 @@ bool planLatticeTier(const LatticeTierInput& input, GraphTierOutput output)
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_field_pass.clear();
output.info.lattice_gap_exact = false;
bool ok = false;
bool search_timed_out = false;
@@ -53,7 +54,17 @@ bool planLatticeTier(const LatticeTierInput& input, GraphTierOutput output)
{
LatticeCSpace space(context);
LatticeField field(&memory);
LatticeField alternate(&memory);
const LatticeField* used = &field;
const TimePoint field_deadline = began + std::chrono::duration_cast<Clock::duration>(slice * input.config.lattice_field_time_fraction);
// The second field runs only when it differs from the first one.
const double alternate_relax = input.config.lattice_alt_field_relax_m;
const bool use_alternate = input.config.lattice_alt_field_enabled &&
(input.config.lattice_field_extra_clearance_m > 0.0 ||
std::abs(input.config.lattice_field_standability_relax_m - alternate_relax) > 1e-9);
const TimePoint switch_at = use_alternate
? began + std::chrono::duration_cast<Clock::duration>(slice * input.config.lattice_alt_field_switch_fraction)
: input.deadline;
bool field_ok = false;
{
StageTimer timer(output.info.lattice_field_ms);
@@ -73,10 +84,34 @@ bool planLatticeTier(const LatticeTierInput& input, GraphTierOutput output)
}
if (field_ok)
{
output.info.lattice_field_pass = "primary";
context.deadline = switch_at;
{
StageTimer timer(output.info.lattice_search_ms, &output.info.lattice_verify_ms);
ok = searchLattice(context, space, field, &output.path, &output.check);
}
context.deadline = input.deadline;
// Only an interrupted search can gain from another heuristic: the field orders the states but does not change which
// are legal, so an exhausted search already proved there is no path.
if (!ok && use_alternate && !context.cancelled() && Clock::now() >= switch_at && Clock::now() < input.deadline &&
output.info.lattice_reason != "lattice_exhausted" && output.info.lattice_reason != "lattice_resource_limit")
{
bool alternate_ok = false;
{
StageTimer timer(output.info.lattice_field_ms);
const TimePoint now = Clock::now();
const TimePoint alternate_deadline =
now + std::chrono::duration_cast<Clock::duration>((input.deadline - now) * input.config.lattice_field_time_fraction);
alternate_ok = buildLatticeField(context, &alternate, alternate_deadline, 0.0, alternate_relax);
}
if (alternate_ok && !context.cancelled() && Clock::now() < input.deadline)
{
output.info.lattice_field_pass = "alternate";
used = &alternate;
StageTimer timer(output.info.lattice_search_ms, &output.info.lattice_verify_ms);
ok = searchLattice(context, space, alternate, &output.path, &output.check);
}
}
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)
@@ -107,7 +142,7 @@ bool planLatticeTier(const LatticeTierInput& input, GraphTierOutput output)
bool found = false;
{
StageTimer search_timer(output.info.lattice_search_ms, &output.info.lattice_verify_ms);
found = searchLattice(context, space, field, &candidate, &candidate_check);
found = searchLattice(context, space, *used, &candidate, &candidate_check);
}
if (found)
{
@@ -139,12 +174,13 @@ bool planLatticeTier(const LatticeTierInput& input, GraphTierOutput output)
}
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.
// The wavefronts are no longer needed; release them before the smoother allocates its anchor graph.
for (LatticeField* done : { &field, &alternate })
{
std::pmr::vector<float> values(&memory);
std::pmr::vector<uint8_t> settled(&memory);
values.swap(field.values);
settled.swap(field.settled);
values.swap(done->values);
settled.swap(done->settled);
}
const TimePoint smooth_began = Clock::now();
context.deadline = input.smooth_deadline;