diff --git a/include/lane_route_core/route_planner.h b/include/lane_route_core/route_planner.h index 4999a92..8607d6e 100644 --- a/include/lane_route_core/route_planner.h +++ b/include/lane_route_core/route_planner.h @@ -75,6 +75,7 @@ struct PlanInfo uint64_t lattice_smooth_candidates = 0; ///< Whole-path verification attempts. uint64_t lattice_smooth_rejects_verify = 0; ///< Whole paths rejected by verification, excluding interruption. std::string lattice_smooth_stop; ///< accepted, no_route, verify_failed, budget (checks/states/memory), or deadline; empty when not run. + std::string graph_radius_pass; ///< "regular" or "tight" for a graph path; see PlannerConfig::regular_turn_radius_m. std::string lattice_gap_pass; ///< "preferred" or "minimum" for a successful lattice path; empty otherwise. bool lattice_gap_exact = false; ///< True when the nearest lethal/unknown gap is certified within the scan margin; excludes map boundary. }; diff --git a/include/lane_route_core/types.h b/include/lane_route_core/types.h index 592d872..6be216e 100644 --- a/include/lane_route_core/types.h +++ b/include/lane_route_core/types.h @@ -106,8 +106,14 @@ struct PlannerConfig double clearance_min_m = 0.335; ///< Centre clearance of the final path outside zones. double clearance_preferred_m = 0.60; ///< Clearance tried first when pulling a line straight. double collision_check_distance_m = 0.48; ///< Closer than this, a curve pose gets the full footprint test. - double min_turn_radius_m = 1.06; ///< Tightest curve the final check accepts. - std::vector corner_radii_m{ 1.9, 1.75, 1.6, 1.45, 1.3, 1.18, 1.08 }; ///< Tried in order. + double min_turn_radius_m = 1.06; ///< [m] Tightest curve any tier may use; the final check rejects tighter curves. + /// [m] Corner radii tried in order, largest first, so a smaller radius is used only where the larger ones do not fit. + std::vector corner_radii_m{ 1.9, 1.75, 1.6, 1.45, 1.3, 1.18, 1.08 }; + /// [m] Radius ladder. Every tier first plans with radii at or above this value (the graph's route search and corners, the + /// lattice arcs, the smoother). Only when min_turn_radius_m is lower does the graph try a second pass with all corner_radii_m, + /// 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; 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. @@ -125,6 +131,11 @@ struct PlannerConfig bool lattice_enabled = false; ///< Enable fallback after the graph tier. double lattice_preferred_gap_m = 0.15; ///< [m] Preferred footprint gap away from endpoints; values <= footprint_min_gap_m (e.g. 0) disable the preferred pass. double lattice_preferred_time_fraction = 0.25; ///< Maximum fraction of the rung slice for the optional preferred search, after recheck. + /// [m] Extra cost per heading bin turned on a tight lattice arc; only with lattice_tight_arcs. + double lattice_tight_turn_cost_m = 0.15; + /// Give the lattice arcs at min_turn_radius_m besides its regular arcs. Off: on regress7 with a 0.4 m floor they cost + /// 38 (hook_150) and 84 (kit) queries to lattice timeouts, because the branching grows; tight corners come from the graph. + bool lattice_tight_arcs = false; int lattice_heading_bins = 72; ///< Heading divisions per revolution, even. double lattice_closed_resolution_m = 0.10; ///< [m] Maximum x/y search bin size. double lattice_heuristic_weight = 3.0; ///< Weighted A* heuristic multiplier. 3.0 solved the most regress7 runs within 3 s (kit +10 over 1.5). diff --git a/src/grid_checks.cpp b/src/grid_checks.cpp index fa466be..86d0989 100644 --- a/src/grid_checks.cpp +++ b/src/grid_checks.cpp @@ -108,6 +108,10 @@ 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 (!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 (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_search.cpp b/src/lattice_search.cpp index 6b5090a..e20bee9 100644 --- a/src/lattice_search.cpp +++ b/src/lattice_search.cpp @@ -323,7 +323,12 @@ bool searchLattice(LatticeContext& context, LatticeCSpace& space, const LatticeF const PlannerConfig& c = context.config; const GridRef& grid = context.grid; const double angle = 2.0 * kPi / c.lattice_heading_bins; - const double basic = c.min_turn_radius_m * angle; + // Regular arcs keep the radius the hand-drawn paths were graded at. Tight arcs at min_turn_radius_m exist only with + // lattice_tight_arcs and a lower floor; each tight heading bin pays lattice_tight_turn_cost_m. + const double regular_radius = std::max(c.regular_turn_radius_m, c.min_turn_radius_m); + const double basic = regular_radius * angle; + const double tight_basic = c.min_turn_radius_m * angle; + const bool tight_arcs = c.lattice_tight_arcs && c.min_turn_radius_m < regular_radius - 1e-9; const std::size_t grid_cells = static_cast(grid.width) * static_cast(grid.height); const double bin_resolution = grid_cells > 2000000u ? c.lattice_closed_resolution_m : std::min(c.lattice_closed_resolution_m, grid.resolution); @@ -485,11 +490,15 @@ bool searchLattice(LatticeContext& context, LatticeCSpace& space, const LatticeF } const int multiples[] = { c.lattice_primitive_multiple_1, c.lattice_primitive_multiple_2, c.lattice_primitive_multiple_3 }; for (int multi : multiples) - for (int steer : { 0, 1, -1 }) + for (int choice : { 0, 1, -1, 2, -2 }) { if (context.expired()) return false; - const double length = basic * multi; - const double curvature = steer / c.min_turn_radius_m; + const bool tight = choice == 2 || choice == -2; + // A tight step shorter than a closed bin would stay in its parent's bin; the longer multiples cover those turns. + if (tight && (!tight_arcs || tight_basic * multi < bin_resolution)) continue; + const int steer = choice > 0 ? 1 : choice < 0 ? -1 : 0; + const double length = (tight ? tight_basic : basic) * multi; + const double curvature = steer / (tight ? c.min_turn_radius_m : regular_radius); if (multi > 1) { const int32_t x = static_cast(std::floor((current.pose.x-grid.origin_x)/grid.resolution)); @@ -514,7 +523,8 @@ bool searchLattice(LatticeContext& context, LatticeCSpace& space, const LatticeF const int32_t y = static_cast(std::floor((next.y-grid.origin_y)/grid.resolution)); if (x < 0 || y < 0 || x >= grid.width || y >= grid.height) continue; const double clear = std::max(0.05, grid.clearance_mm[static_cast(y)*grid.width+x]/1000.0); - const double cost = latticeMotionCost(length, clear, steer, current.last_steer, multi, c); + const double cost = latticeMotionCost(length, clear, steer, current.last_steer, multi, c) + + (tight ? c.lattice_tight_turn_cost_m * multi : 0.0); offer({ next, current.g+cost, static_cast(entry.node), curvature, length, heading, last }); } } diff --git a/src/lattice_smoother.cpp b/src/lattice_smoother.cpp index c7db24b..37aa274 100644 --- a/src/lattice_smoother.cpp +++ b/src/lattice_smoother.cpp @@ -89,7 +89,14 @@ class Smoother { public: Smoother(LatticeContext& context, LatticeCSpace& space, Path* output, PathCheck* check) - : context_(context), space_(space), output_(output), check_(check), witness_(*output), best_(score(*output)) {} + : context_(context), space_(space), output_(output), check_(check), witness_(*output), best_(score(*output)) + { + // Connectors use the regular radii only: a tight turn stays where the lattice needed it and is never introduced by smoothing. + const double regular = std::max(context.config.regular_turn_radius_m, context.config.min_turn_radius_m); + for (double radius : context.config.corner_radii_m) + if (radius >= regular - 1e-9) regular_radii_.push_back(radius); + if (regular_radii_.empty()) regular_radii_.push_back(regular); + } void run() { @@ -119,6 +126,7 @@ public: private: static double distance(const PathPoint& a, const PathPoint& b) { return std::hypot(b.x-a.x, b.y-a.y); } + const std::vector& regularRadii() const { return regular_radii_; } bool chordBlocked(const PathPoint& a, const PathPoint& b) const { @@ -201,7 +209,7 @@ private: bool connector(const PathPoint& start, const PathPoint& goal, Path* piece, double* edge_score = nullptr) { std::pmr::vector choices(&context_.memory); - const auto& configured = context_.config.corner_radii_m; + const std::vector& configured = regularRadii(); const std::array radii{{configured.front(), configured[2*(configured.size()-1)/3], configured.back()}}; const double chord = distance(start, goal); const double heading_change = std::abs(wrapAngle(goal.yaw-start.yaw)); @@ -383,6 +391,7 @@ private: std::vector unwrapped_; std::vector filter_; std::vector anchor_; + std::vector regular_radii_; // [m] corner_radii_m at or above the regular lattice radius, largest first. }; bool Smoother::runGreedy() @@ -446,7 +455,7 @@ void Smoother::runGraph(bool improve_smoothed, uint64_t check_limit) if (j + 1 != anchor_.size() || context_.request.goal.has_yaw) { const std::array end_yaws{{anchor_[j].low_yaw, target.yaw}}; - const auto& radii = context_.config.corner_radii_m; + const std::vector& radii = regularRadii(); for (double end_yaw : end_yaws) for (double radius : {radii.front(), radii.back()}) for (int side : {1, -1}) diff --git a/src/route_planner.cpp b/src/route_planner.cpp index 5974686..dc4092e 100644 --- a/src/route_planner.cpp +++ b/src/route_planner.cpp @@ -27,6 +27,21 @@ using detail::Clock; using detail::TimePoint; using detail::msSince; +namespace +{ +// A tight-corner graph path replaces a large-radius one only when it is at least this much shorter, so a large radius stays the +// default shape and a tight corner appears where it removes a real detour. +constexpr double kTightMinSaving = 0.2; + +double polylineLength(const Path& path) +{ + double length = 0.0; + for (std::size_t i = 1; i < path.size(); ++i) + length += std::hypot(path[i].x - path[i - 1].x, path[i].y - path[i - 1].y); + return length; +} +} // namespace + PlanResult planRoute(const GraphProvider& graphs, const RouteRequest& request, const GridRef& live, const PlannerConfig& config, const RidgeBackend& backend) { @@ -100,6 +115,22 @@ PlanResult planRoute(const GraphProvider& graphs, const RouteRequest& request, c } rungs.push_back(config.max_deviation_deg); + // Radius ladder. The first graph pass and the lattice use only radii at or above regular_turn_radius_m, which is how every + // tier planned before the floor could be lowered. A lower min_turn_radius_m only adds a second graph pass with all corner + // radii, for a failed first pass or a long detour (measured 9/11: letting the route search and the lattice use the low floor + // from the start lost 25 to 84 regress7 queries). + PlannerConfig regular = config; + regular.min_turn_radius_m = std::max(config.regular_turn_radius_m, config.min_turn_radius_m); + regular.corner_radii_m.clear(); + for (double radius : config.corner_radii_m) + if (radius >= regular.min_turn_radius_m - 1e-9) + 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; + 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); + // R2 smoothing must share this fallback deadline instead of starting a new budget. TimePoint fallback_deadline = deadline; bool fallback_started = false; @@ -123,8 +154,35 @@ PlanResult planRoute(const GraphProvider& graphs, const RouteRequest& request, c else { result.info.live_ridge.clear(); - graph_ok = detail::planGraphTier({ *graph, request, live, deadline, config, backend }, + graph_ok = detail::planGraphTier({ *graph, request, live, deadline, tight_corners ? regular : config, backend }, { result.path, result.check, result.info }); + const bool detour = graph_ok && polylineLength(result.path) > config.suspicious_ratio * chord + config.suspicious_margin_m; + const bool stopped = (request.cancel && request.cancel->load(std::memory_order_relaxed)) || Clock::now() >= deadline || + result.info.reason == "timeout" || result.info.reason == "no_route_enclosed"; + if (tight_corners && (!graph_ok || detour) && !stopped) + { + Path tight_path; + PathCheck tight_check; + PlanInfo tight_info = result.info; + tight_info.live_ridge.clear(); + const bool tight_ok = detail::planGraphTier({ *graph, request, live, deadline, config, backend }, + { tight_path, tight_check, tight_info }); + // A tight corner must buy a clearly shorter path; otherwise the large-radius shape stays. + if (tight_ok && (!graph_ok || polylineLength(tight_path) < (1.0 - kTightMinSaving) * polylineLength(result.path))) + { + result.path = std::move(tight_path); + result.check = tight_check; + result.info = tight_info; + result.info.graph_radius_pass = "tight"; + graph_ok = true; + } + else if (!graph_ok) + { + result.info = tight_info; + } + } + if (graph_ok && result.info.graph_radius_pass.empty()) + result.info.graph_radius_pass = "regular"; } result.info.graph_reason = result.info.reason; result.info.graph_rung_reasons.push_back(result.info.graph_reason); @@ -168,7 +226,7 @@ PlanResult planRoute(const GraphProvider& graphs, const RouteRequest& request, c break; } const TimePoint rung_deadline = now + (fallback_deadline - now) / static_cast(rungs.size() - rung); - if (detail::planLatticeTier({ request, live, config, deviation, rung_deadline, fallback_deadline }, + if (detail::planLatticeTier({ request, live, lattice_config, deviation, rung_deadline, fallback_deadline }, { result.path, result.check, result.info })) { result.ok = true; diff --git a/test/lane_route_core_test.cpp b/test/lane_route_core_test.cpp index 113b560..ed23a9b 100644 --- a/test/lane_route_core_test.cpp +++ b/test/lane_route_core_test.cpp @@ -2032,3 +2032,31 @@ TEST(LatticePreferredGap, ChangingMarginInvalidatesCachedPoses) space.setGapMargin(config.footprint_min_gap_m); EXPECT_TRUE(space.poseFree(3.5, 2.525, 0.0)); } + +TEST(LatticeTightArcs, TurnRadiusSettingsAreValidated) +{ + PlannerConfig config = latticeConfig(); + config.lattice_tight_turn_cost_m = -0.1; + EXPECT_FALSE(config.validate().empty()); + config = latticeConfig(); + config.regular_turn_radius_m = 0.0; + EXPECT_FALSE(config.validate().empty()); + // A regular radius below the floor acts as the floor; it must not stop the planner from starting. + config = latticeConfig(); + config.regular_turn_radius_m = 0.5; + EXPECT_TRUE(config.validate().empty()); +} + +TEST(LatticeTightArcs, OpenSpaceKeepsRegularRadius) +{ + // With a 0.4 m floor and the optional tight arcs on, no tight turn saves enough path in open space to pay its cost. + LatticeTestGrid map(160, 160); + PlannerConfig config = latticeConfig(); + config.min_turn_radius_m = 0.4; + config.lattice_tight_arcs = true; + const auto result = map.plan(latticeRequest(pose(1.0, 1.0, 0.0), pose(5.5, 5.5, kPi / 2.0)), config); + ASSERT_TRUE(result.ok) << result.info.reason; + EXPECT_EQ(result.info.tier_used, "lattice"); + for (const PathPoint& point : result.path) + EXPECT_LE(std::abs(point.curvature), 1.0 / 1.06 + 1e-6); +} diff --git a/tools/lane_route_eval.cpp b/tools/lane_route_eval.cpp index ff6f23b..eefe6f2 100644 --- a/tools/lane_route_eval.cpp +++ b/tools/lane_route_eval.cpp @@ -117,7 +117,8 @@ GridRef gridOf(const MapData& map, const std::vector& cost, const uint1 return grid; } -/** Offline tuning without a rebuild: LANE_ROUTE_EVAL_SET="name=value,name=value" overrides lattice and fallback fields. +/** Offline tuning without a rebuild: LANE_ROUTE_EVAL_SET="name=value,name=value" overrides lattice, fallback and turn radius + * fields. * Unknown names are an error, so a typo cannot silently run the defaults. */ void applyOverrides(PlannerConfig* config) { @@ -139,6 +140,9 @@ void applyOverrides(PlannerConfig* config) { "lattice_field_time_fraction", &config->lattice_field_time_fraction }, { "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 }, + { "regular_turn_radius_m", &config->regular_turn_radius_m }, + { "lattice_tight_turn_cost_m", &config->lattice_tight_turn_cost_m }, }; const std::map ints{ { "lattice_heading_bins", &config->lattice_heading_bins }, @@ -155,6 +159,7 @@ void applyOverrides(PlannerConfig* config) }; const std::map bools{ { "lattice_smooth_enabled", &config->lattice_smooth_enabled }, + { "lattice_tight_arcs", &config->lattice_tight_arcs }, }; std::stringstream stream(text); std::string item; @@ -173,6 +178,15 @@ void applyOverrides(PlannerConfig* config) *longs.at(key) = std::stoll(value); else if (bools.count(key) != 0) *bools.at(key) = std::stoi(value) != 0; + else if (key == "corner_radii_m") + { + // A list inside the comma-separated overrides uses '/', e.g. corner_radii_m=1.9/1.3/0.6, largest first. + std::stringstream radii(value); + std::string radius; + config->corner_radii_m.clear(); + while (std::getline(radii, radius, '/')) + config->corner_radii_m.push_back(std::stod(radius)); + } else throw std::runtime_error("unknown LANE_ROUTE_EVAL_SET key: " + key); std::cerr << "override " << key << "=" << value << std::endl; @@ -357,7 +371,7 @@ int main(int argc, char** argv) << ",\"lattice_ms\":" << info.lattice_ms << ",\"lattice_smooth_ms\":" << info.lattice_smooth_ms << ",\"lattice_smooth_checks\":" << info.lattice_smooth_checks << ",\"lattice_smoothed\":" << info.lattice_smoothed << ",\"curvature_sign_changes\":" << info.curvature_sign_changes - << ",\"path_length_m\":" << info.path_length_m << ",\"straight_share\":" << info.straight_share + << ",\"graph_radius_pass\":\"" << info.graph_radius_pass << "\"" << ",\"max_curvature\":" << result.check.max_curvature << ",\"path_length_m\":" << info.path_length_m << ",\"straight_share\":" << info.straight_share << ",\"lattice_smooth_connectors_tried\":" << info.lattice_smooth_connectors_tried << ",\"lattice_smooth_connectors_ok\":" << info.lattice_smooth_connectors_ok << ",\"lattice_smooth_candidates\":" << info.lattice_smooth_candidates