diff --git a/include/lane_route_core/path_verifier.h b/include/lane_route_core/path_verifier.h index 1a0a7db..2f8ac29 100644 --- a/include/lane_route_core/path_verifier.h +++ b/include/lane_route_core/path_verifier.h @@ -16,7 +16,8 @@ struct PathCheck int zone_violations = 0; ///< Segment and cell pairs against the direction limit. int footprint_collisions = 0; ///< Sampled poses hitting lethal or unknown cells, at most 1 cm and 0.03 rad apart. int self_crossings = 0; ///< Crossings between non-neighbouring parts of the path. - int kinks = 0; ///< In-place turns away from both ends, plus bends above 8 deg over 0.1 m. + int kinks = 0; ///< In-place turns away from both ends, plus bends over 0.1 m above the curve and corner limits. + int corners = 0; ///< Sharp corners within max_kink_deg: one turning step between two straight steps. double max_curvature = 0.0; ///< [1/m] double max_bend_deg = 0.0; ///< [deg] Largest bend over 0.1 m. }; diff --git a/include/lane_route_core/types.h b/include/lane_route_core/types.h index 6be216e..a6814df 100644 --- a/include/lane_route_core/types.h +++ b/include/lane_route_core/types.h @@ -114,6 +114,10 @@ struct PlannerConfig /// used when the first pass fails or returns a detour longer than suspicious_ratio x chord + suspicious_margin_m, and kept /// only if clearly shorter. Values below min_turn_radius_m act as min_turn_radius_m. double regular_turn_radius_m = 1.06; + /// [deg] Largest heading change at a sharp corner: two straight lines meeting at a vertex, without a turn in place. The bottom + /// rung of the radius ladder: used only in the graph's second pass, only at a corner where no corner radius fits, and only when + /// the footprint can rotate at the vertex without touching an obstacle. 0 disables sharp corners. + double max_kink_deg = 45.0; double footprint_min_gap_m = 0.03; ///< Smallest footprint to lethal gap on the whole path. std::vector footprint_gap_shortcut_raise_m{ 0.05, 0.10 }; ///< Empty: check only. double max_deviation_deg = 90.0; ///< Configured direction limit, the last ladder rung. @@ -151,6 +155,9 @@ struct PlannerConfig int lattice_analytic_interval = 20000; ///< Expansions between analytic attempts. double lattice_analytic_near_goal_m = 2.0; ///< [m] Also attempt analytic connection inside this distance. int lattice_analytic_goal_headings = 16; ///< Uniform arrival headings for analytic Dubins candidates, 0 disables the set. + /// [m] Extra clearance a cell needs to count as passable in the heuristic field only; the search itself is unchanged. + /// 0 keeps the half-width rule. Higher keeps the field out of gaps barely wider than the robot. + double lattice_field_extra_clearance_m = 0.0; double lattice_field_time_fraction = 0.4; ///< Fraction of a rung's fallback slice for the field. int lattice_field_cell_factor = 0; ///< 0 selects automatically, 1 uses the original grid resolution. int64_t lattice_field_max_cells = 1000000; ///< Maximum cells for automatic field sizing. diff --git a/src/grid_checks.cpp b/src/grid_checks.cpp index 86d0989..5976a68 100644 --- a/src/grid_checks.cpp +++ b/src/grid_checks.cpp @@ -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]"; diff --git a/src/lattice_heuristic.cpp b/src/lattice_heuristic.cpp index bd2d06d..77d1f02 100644 --- a/src/lattice_heuristic.cpp +++ b/src/lattice_heuristic.cpp @@ -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(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 passable(&context.memory), direction(&context.memory); diff --git a/src/lattice_internal.h b/src/lattice_internal.h index 2f0887c..1c200af 100644 --- a/src/lattice_internal.h +++ b/src/lattice_internal.h @@ -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); diff --git a/src/lattice_tier.cpp b/src/lattice_tier.cpp index 4a174e7..7c39724 100644 --- a/src/lattice_tier.cpp +++ b/src/lattice_tier.cpp @@ -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) { diff --git a/src/path_verifier.cpp b/src/path_verifier.cpp index 011ed63..0d76b10 100644 --- a/src/path_verifier.cpp +++ b/src/path_verifier.cpp @@ -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* cancel, const std::chrono::steady_clock::time_point* deadline) { std::vector 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))); diff --git a/src/route_graph_path.cpp b/src/route_graph_path.cpp index e389e86..c1375b2 100644 --- a/src/route_graph_path.cpp +++ b/src/route_graph_path.cpp @@ -169,6 +169,25 @@ bool simplifyRoute(const QueryGraph& query, const std::vector& 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(fy) * grid.width + static_cast(fx)] >= kInscribedCost) + return false; + const double delta = wrapAngle(to - from); + const int samples = std::max(1, static_cast(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& vertices, const Pose2& start, const Po options.push_back(Option{ trim, std::make_shared(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(Path{ PathPoint{ b.x, b.y, in, 0.0 } }), 0.0 }); if (complete) context->corner_options[key] = options; } diff --git a/src/route_planner.cpp b/src/route_planner.cpp index dc4092e..37f3299 100644 --- a/src/route_planner.cpp +++ b/src/route_planner.cpp @@ -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); diff --git a/test/lane_route_core_test.cpp b/test/lane_route_core_test.cpp index ed23a9b..1c0dcf3 100644 --- a/test/lane_route_core_test.cpp +++ b/test/lane_route_core_test.cpp @@ -288,11 +288,24 @@ TEST(Verifier, FlagsEachHardRule) spin_end.push_back({ straight.back().x, straight.back().y, 0.1 * i, 0.0 }); EXPECT_TRUE(verifyPath(spin_end, grid, 90.0, config).valid); - // Sharp bend without a turn in place. + // Sharp bend without a turn in place: a kink when sharp corners are off, a counted corner within max_kink_deg. Path bend(straight.begin(), straight.begin() + 51); for (int i = 1; i <= 50; ++i) bend.push_back({ bend[50].x + 0.04 * i * std::cos(0.5), bend[50].y + 0.04 * i * std::sin(0.5), 0.5, 0.0 }); - EXPECT_GT(verifyPath(bend, grid, 90.0, config).kinks, 0); + PlannerConfig no_corners = config; + no_corners.max_kink_deg = 0.0; + EXPECT_GT(verifyPath(bend, grid, 90.0, no_corners).kinks, 0); + PlannerConfig corners_45 = config; + corners_45.max_kink_deg = 45.0; + const PathCheck corner_check = verifyPath(bend, grid, 90.0, corners_45); + EXPECT_EQ(corner_check.kinks, 0); + EXPECT_EQ(corner_check.corners, 1); + EXPECT_LE(corner_check.max_curvature, 1.0 / corners_45.min_turn_radius_m + 1e-6); + // A 1.0 rad (57 deg) corner exceeds 45 deg. + Path steep(straight.begin(), straight.begin() + 51); + for (int i = 1; i <= 50; ++i) + steep.push_back({ steep[50].x + 0.04 * i * std::cos(1.0), steep[50].y + 0.04 * i * std::sin(1.0), 1.0, 0.0 }); + EXPECT_FALSE(verifyPath(steep, grid, 90.0, corners_45).valid); // A path that loops over itself. Path loop; @@ -2060,3 +2073,16 @@ TEST(LatticeTightArcs, OpenSpaceKeepsRegularRadius) for (const PathPoint& point : result.path) EXPECT_LE(std::abs(point.curvature), 1.0 / 1.06 + 1e-6); } + +TEST(LatticeFieldMargin, MarginThatClosesTheOnlyWayFallsBackToThePlainField) +{ + // A 1.0 m corridor: with a 0.5 m extra field clearance no cell between the ends is passable, so the margin field never reaches + // the start. The tier must rebuild the plain field and still find the corridor path. + LatticeTestGrid map(120, 100); + preferredGapCorridor(&map, 40, 59); + PlannerConfig config = latticeConfig(); + config.lattice_field_extra_clearance_m = 0.5; + 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.tier_used, "lattice"); +} diff --git a/tools/lane_route_eval.cpp b/tools/lane_route_eval.cpp index eefe6f2..1a10633 100644 --- a/tools/lane_route_eval.cpp +++ b/tools/lane_route_eval.cpp @@ -141,8 +141,10 @@ void applyOverrides(PlannerConfig* config) { "fallback_time_limit_s", &config->fallback_time_limit_s }, { "lattice_end_relax_radius_m", &config->lattice_end_relax_radius_m }, { "min_turn_radius_m", &config->min_turn_radius_m }, + { "max_kink_deg", &config->max_kink_deg }, { "regular_turn_radius_m", &config->regular_turn_radius_m }, { "lattice_tight_turn_cost_m", &config->lattice_tight_turn_cost_m }, + { "lattice_field_extra_clearance_m", &config->lattice_field_extra_clearance_m }, }; const std::map ints{ { "lattice_heading_bins", &config->lattice_heading_bins }, @@ -352,7 +354,7 @@ int main(int argc, char** argv) if (result.ok) { out << ",\"min_gap\":" << info.min_gap_m << ",\"exempt\":" << (info.gap_exemption_used ? 1 : 0) << ",\"kinks\":" << result.check.kinks - << ",\"crossings\":" << result.check.self_crossings << ",\"zone_violations\":" << result.check.zone_violations + << ",\"corners\":" << result.check.corners << ",\"crossings\":" << result.check.self_crossings << ",\"zone_violations\":" << result.check.zone_violations << ",\"collisions\":" << result.check.footprint_collisions << ",\"path\":["; for (std::size_t i = 0; i < result.path.size(); ++i) {