diff --git a/include/lane_route_core/grid_checks.h b/include/lane_route_core/grid_checks.h index f95947d..d2465d4 100644 --- a/include/lane_route_core/grid_checks.h +++ b/include/lane_route_core/grid_checks.h @@ -77,6 +77,11 @@ 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* cancel, const std::chrono::steady_clock::time_point* deadline, bool count_unknown = false); +/** @brief As above, but scan at least search_margin_m around the footprint for more informative gap measurements. + * The acceptance rule continues to use minimum_m. */ +GapCheck checkFootprintGap(const Path& path, const GridRef& grid, double minimum_m, double half_x, double half_y, + const std::atomic* cancel, const std::chrono::steady_clock::time_point* deadline, + bool count_unknown, double search_margin_m); /** @brief Same rule on a ready gap sequence, separated so it can be tested by hand. */ GapCheck applyGapRule(const std::vector& gaps, double minimum_m); diff --git a/include/lane_route_core/route_planner.h b/include/lane_route_core/route_planner.h index 8dec60c..4999a92 100644 --- a/include/lane_route_core/route_planner.h +++ b/include/lane_route_core/route_planner.h @@ -63,6 +63,7 @@ struct PlanInfo double lattice_field_ms = 0.0; ///< [ms] Field construction across rungs. double lattice_search_ms = 0.0; ///< [ms] Search excluding final candidate verification. double lattice_verify_ms = 0.0; ///< [ms] Final candidate checks, included in lattice_ms. + double lattice_upgrade_ms = 0.0; ///< [ms] Preferred-path recheck and search across rungs; overlaps search/verify time. double lattice_smooth_ms = 0.0; ///< [ms] Smoothing including verification. uint64_t lattice_smooth_checks = 0; ///< Clearance probes, C-space pose checks and verification sample estimates. int lattice_smoothed = 0; ///< 0: raw, 1: S3, 2: S3 plus clothoid. @@ -74,6 +75,8 @@ 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 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. }; /** @brief Outcome of a request. */ diff --git a/include/lane_route_core/types.h b/include/lane_route_core/types.h index 0d3a6d1..592d872 100644 --- a/include/lane_route_core/types.h +++ b/include/lane_route_core/types.h @@ -123,6 +123,8 @@ struct PlannerConfig int64_t live_ridge_max_cells = 1500000; ///< Larger windows skip the live retry. 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. 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 96df84e..fa466be 100644 --- a/src/grid_checks.cpp +++ b/src/grid_checks.cpp @@ -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* 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* cancel, const std::chrono::steady_clock::time_point* deadline, + bool count_unknown, double search_margin_m) { std::vector 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 gaps(poses.size(), kInfinity); for (std::size_t i = 0; i < poses.size(); ++i) diff --git a/src/lattice_cspace.cpp b/src/lattice_cspace.cpp index 5e49075..cc3a01c 100644 --- a/src/lattice_cspace.cpp +++ b/src/lattice_cspace.cpp @@ -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(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(std::ceil(distance / 0.025)), static_cast(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) { diff --git a/src/lattice_internal.h b/src/lattice_internal.h index 75a1cef..2f0887c 100644 --- a/src/lattice_internal.h +++ b/src/lattice_internal.h @@ -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; diff --git a/src/lattice_search.cpp b/src/lattice_search.cpp index ec8e591..6b5090a 100644 --- a/src/lattice_search.cpp +++ b/src/lattice_search.cpp @@ -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; } diff --git a/src/lattice_smoother.cpp b/src/lattice_smoother.cpp index 1215ac6..c7db24b 100644 --- a/src/lattice_smoother.cpp +++ b/src/lattice_smoother.cpp @@ -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); } diff --git a/src/lattice_tier.cpp b/src/lattice_tier.cpp index 9f60170..4a174e7 100644 --- a/src/lattice_tier.cpp +++ b/src/lattice_tier.cpp @@ -2,8 +2,13 @@ #include #include +#include #include #include +#include +#include + +#include 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(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(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"; diff --git a/test/lane_route_core_test.cpp b/test/lane_route_core_test.cpp index 9c80a44..113b560 100644 --- a/test/lane_route_core_test.cpp +++ b/test/lane_route_core_test.cpp @@ -242,6 +242,13 @@ TEST(GapCheck, MatchesHandComputedDistances) // Nothing near. path = { { 1.0, 1.0, 0.0, 0.0 } }; EXPECT_TRUE(std::isinf(checkFootprintGap(path, grid, 0.03, 0.3, 0.3).min_gap_m)); + // Extending the measurement scan does not change the 0.03 m acceptance threshold. + path = { { 2.4, 2.525, 0.0, 0.0 } }; + const GapCheck default_scan = checkFootprintGap(path, grid, 0.03, 0.3, 0.3); + const GapCheck extended = checkFootprintGap(path, grid, 0.03, 0.3, 0.3, nullptr, nullptr, false, 0.3); + EXPECT_EQ(default_scan.valid, extended.valid); + EXPECT_TRUE(std::isinf(default_scan.min_gap_m)); + EXPECT_NEAR(extended.min_gap_m, 0.3, 1e-9); // An in-place spin is sampled too: a square turning 45 deg reaches further than it starts. path = { { 2.69, 2.525, 0.0, 0.0 }, { 2.69, 2.525, kPi / 4.0, 0.0 } }; EXPECT_NEAR(checkFootprintGap(path, grid, 0.03, 0.2, 0.2).min_gap_m, 3.0 - (2.69 + 0.2 * std::sqrt(2.0)), 1e-9); @@ -983,6 +990,7 @@ PlannerConfig latticeConfig() { PlannerConfig c; c.lattice_enabled = true; + c.lattice_preferred_gap_m = 0.0; // Existing lattice tests exercise the minimum-gap search. c.lattice_smooth_enabled = false; // R1 tests inspect the unsmoothed lattice result. c.time_limit_s = 10.0; c.fallback_time_limit_s = 3.0; @@ -1459,6 +1467,21 @@ TEST(Lattice, InvalidParametersAreRejected) EXPECT_FALSE(config.validate().empty()); config.lattice_smooth_max_checks = 0; EXPECT_TRUE(config.validate().empty()); + config.lattice_preferred_gap_m = -0.01; + EXPECT_FALSE(config.validate().empty()); + config.lattice_preferred_gap_m = 0.01; // Below footprint_min_gap_m: the preferred pass is off, the value is legal. + EXPECT_TRUE(config.validate().empty()); + config.lattice_preferred_gap_m = 0.15; + config.lattice_preferred_time_fraction = 0.0; + EXPECT_FALSE(config.validate().empty()); + config.lattice_preferred_time_fraction = 1.01; + EXPECT_FALSE(config.validate().empty()); + config.lattice_preferred_time_fraction = 1.0; + EXPECT_TRUE(config.validate().empty()); + config.lattice_enabled = false; + config.lattice_preferred_gap_m = -1.0; + config.lattice_preferred_time_fraction = 0.0; + EXPECT_TRUE(config.validate().empty()); } TEST(Lattice, DirectionChecksBothCellsOnAnExactBoundary) @@ -1868,3 +1891,144 @@ TEST(LatticeSmoother, PlannerRunsEnabledSmoother) EXPECT_LE(result.info.lattice_smooth_rejects_verify, result.info.lattice_smooth_candidates); EXPECT_FALSE(result.info.lattice_smooth_stop.empty()); } + +namespace +{ +void preferredGapCorridor(LatticeTestGrid* map, int first_free_row, int last_free_row) +{ + for (int y = 0; y < map->height; ++y) + if (y < first_free_row || y > last_free_row) + for (int x = 0; x < map->width; ++x) + map->cost[static_cast(y)*map->width+x] = kLethalCost; + fillClearance(map); +} +} // namespace + +TEST(LatticePreferredGap, WideCorridorUsesPreferredAndSmoothingKeepsGap) +{ + LatticeTestGrid map(120, 100); + preferredGapCorridor(&map, 40, 59); // Free width 1.0 m: a 0.6 m robot has 0.20 m each side. + PlannerConfig config = latticeConfig(); + config.lattice_preferred_gap_m = 0.15; + config.lattice_smooth_enabled = true; + 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.lattice_gap_pass, "preferred"); + EXPECT_TRUE(result.check.valid); + const GapCheck gap = checkFootprintGap(result.path, map.view(), 0.15, config.half_x_m, config.half_y_m, nullptr, nullptr, true); + EXPECT_TRUE(gap.valid); + EXPECT_FALSE(gap.exemption_used); + EXPECT_GE(gap.min_gap_m, 0.15 - 1e-9); +} + +TEST(LatticePreferredGap, PreferredAtOrBelowMinimumSkipsUpgrade) +{ + LatticeTestGrid map(120, 120); + PlannerConfig config = latticeConfig(); + config.footprint_min_gap_m = 0.20; + config.lattice_preferred_gap_m = 0.15; + const auto result = map.plan(latticeRequest(pose(1.0, 3.0, 0.0), pose(4.0, 3.0, 0.0)), config); + ASSERT_TRUE(result.ok) << result.info.reason; + EXPECT_EQ(result.info.lattice_gap_pass, "minimum"); + EXPECT_DOUBLE_EQ(result.info.lattice_upgrade_ms, 0.0); +} + +TEST(LatticePreferredGap, NarrowCorridorFallsBackToMinimum) +{ + LatticeTestGrid map(120, 100); + preferredGapCorridor(&map, 42, 56); // Free width 0.75 m: 0.075 m gap on each side. + PlannerConfig config = latticeConfig(); + config.lattice_preferred_gap_m = 0.15; + config.lattice_preferred_time_fraction = 0.05; + const auto result = map.plan(latticeRequest(pose(1.0, 2.475, 0.0), pose(4.5, 2.475, 0.0)), config); + ASSERT_TRUE(result.ok) << result.info.reason; + EXPECT_EQ(result.info.lattice_gap_pass, "minimum"); + EXPECT_EQ(result.info.lattice_reason, "verified"); + EXPECT_FALSE(result.info.gap_exemption_used); + EXPECT_GE(result.info.lattice_upgrade_ms, 0.0); + EXPECT_TRUE(result.info.lattice_gap_exact); + EXPECT_GE(result.info.min_gap_m, config.footprint_min_gap_m - 1e-9); + EXPECT_LT(result.info.min_gap_m, config.lattice_preferred_gap_m); +} + +TEST(LatticePreferredGap, UpgradeReplacesShortNarrowRouteWithWideDetour) +{ + LatticeTestGrid map(360, 240); + // The straight gap between the two walls is 0.70 m wide; there is open room above and below them. + constexpr int x0 = 120, x1 = 239; + constexpr int lower_y0 = 80, lower_y1 = 92, upper_y0 = 107, upper_y1 = 119; + for (int y = 0; y < map.height; ++y) + for (int x = 0; x < map.width; ++x) + { + if (x >= x0 && x <= x1 && ((y >= lower_y0 && y <= lower_y1) || (y >= upper_y0 && y <= upper_y1))) + map.cost[static_cast(y)*map.width+x] = kLethalCost; + const int dx = std::max({x0-x, 0, x-x1}); + const int lower_dy = std::max({lower_y0-y, 0, y-lower_y1}); + const int upper_dy = std::max({upper_y0-y, 0, y-upper_y1}); + const double cells = std::min(std::hypot(static_cast(dx), static_cast(lower_dy)), + std::hypot(static_cast(dx), static_cast(upper_dy))); + map.clearance[static_cast(y)*map.width+x] = + static_cast(std::min(65535.0, std::floor(cells*kResolution*1000.0))); + } + PlannerConfig config = latticeConfig(); + config.time_limit_s = 20.0; + config.fallback_time_limit_s = 12.0; + config.lattice_preferred_time_fraction = 0.9; + config.lattice_wall_weight_m = 0.0; + config.lattice_field_clearance_weight_m = 0.0; + const RouteRequest request = latticeRequest(pose(2.0, 5.0, 0.0), pose(16.0, 5.0, 0.0)); + const auto minimum = map.plan(request, config); + ASSERT_TRUE(minimum.ok) << minimum.info.reason; + EXPECT_EQ(minimum.info.lattice_gap_pass, "minimum"); + EXPECT_DOUBLE_EQ(minimum.info.lattice_upgrade_ms, 0.0); + const GapCheck narrow = checkFootprintGap(minimum.path, map.view(), 0.15, config.half_x_m, config.half_y_m, nullptr, nullptr, true); + ASSERT_LT(narrow.min_gap_m, 0.10); + EXPECT_NEAR(narrow.min_gap_m, 0.05, 1e-6); + + config.lattice_preferred_gap_m = 0.15; + const auto upgraded = map.plan(request, config); + ASSERT_TRUE(upgraded.ok) << upgraded.info.reason; + EXPECT_EQ(upgraded.info.lattice_gap_pass, "preferred"); + EXPECT_GT(upgraded.info.path_length_m, minimum.info.path_length_m + 0.25); + EXPECT_GT(upgraded.info.lattice_upgrade_ms, 0.0); + const GapCheck wide = checkFootprintGap(upgraded.path, map.view(), 0.15, config.half_x_m, config.half_y_m, nullptr, nullptr, true); + EXPECT_TRUE(wide.valid); + EXPECT_FALSE(wide.exemption_used); + EXPECT_GE(wide.min_gap_m, 0.15 - 1e-6); +} + +TEST(LatticePreferredGap, ParkedStartRetainsEndpointFloor) +{ + LatticeTestGrid map(120, 100); + for (int y = 0; y < map.height; ++y) map.cost[y * map.width + 100] = kLethalCost; + map.clearance[50 * map.width + 93] = 315; + PlannerConfig config = latticeConfig(); + config.clearance_min_m = 0.30; + config.clearance_hard_m = 0.30; + config.lattice_preferred_gap_m = 0.15; + const auto result = map.plan(latticeRequest(pose(4.685, 2.5, kPi), pose(1.0, 2.5, kPi)), config); + ASSERT_TRUE(result.ok) << result.info.reason; + EXPECT_EQ(result.info.lattice_gap_pass, "preferred"); + EXPECT_TRUE(result.info.gap_exemption_used); + EXPECT_TRUE(result.info.lattice_gap_exact); + EXPECT_NEAR(result.info.min_gap_m, 0.015, 1e-6); +} + +TEST(LatticePreferredGap, ChangingMarginInvalidatesCachedPoses) +{ + LatticeTestGrid map(140, 100); + preferredGapCorridor(&map, 42, 58); // 0.125 m gap: a minimum-margin cache entry can be free, preferred cannot. + PlannerConfig config = latticeConfig(); + RouteRequest request = latticeRequest(pose(1.0, 2.525, 0.0), pose(6.0, 2.525, 0.0)); + GridRef grid = map.view(); + PlanInfo info; + detail::LatticeMemory memory(64 * 1024 * 1024); + detail::LatticeContext context{request, grid, config, 90.0, detail::Clock::now()+std::chrono::seconds(10), info, memory}; + detail::LatticeCSpace space(context); + EXPECT_TRUE(space.poseFree(3.5, 2.525, 0.0)); + EXPECT_GT(info.lattice_tiles_built, 0u); + space.setGapMargin(0.15); + EXPECT_FALSE(space.poseFree(3.5, 2.525, 0.0)); + space.setGapMargin(config.footprint_min_gap_m); + EXPECT_TRUE(space.poseFree(3.5, 2.525, 0.0)); +} diff --git a/tools/lane_route_eval.cpp b/tools/lane_route_eval.cpp index f95c298..ff6f23b 100644 --- a/tools/lane_route_eval.cpp +++ b/tools/lane_route_eval.cpp @@ -126,6 +126,8 @@ void applyOverrides(PlannerConfig* config) return; const std::map doubles{ { "lattice_closed_resolution_m", &config->lattice_closed_resolution_m }, + { "lattice_preferred_gap_m", &config->lattice_preferred_gap_m }, + { "lattice_preferred_time_fraction", &config->lattice_preferred_time_fraction }, { "lattice_heuristic_weight", &config->lattice_heuristic_weight }, { "lattice_bucket_width_m", &config->lattice_bucket_width_m }, { "lattice_field_clearance_weight_m", &config->lattice_field_clearance_weight_m }, @@ -360,7 +362,13 @@ int main(int argc, char** argv) << ",\"lattice_smooth_connectors_ok\":" << info.lattice_smooth_connectors_ok << ",\"lattice_smooth_candidates\":" << info.lattice_smooth_candidates << ",\"lattice_smooth_rejects_verify\":" << info.lattice_smooth_rejects_verify - << ",\"lattice_smooth_stop\":\"" << info.lattice_smooth_stop << "\"}\n"; + << ",\"lattice_smooth_stop\":\"" << info.lattice_smooth_stop << "\"" + << ",\"lattice_gap_pass\":\"" << info.lattice_gap_pass << "\"" + << ",\"lattice_gap_exact\":" << (info.lattice_gap_exact ? 1 : 0) + << ",\"lattice_min_gap_m\":"; + if (result.ok && info.tier_used == "lattice" && std::isfinite(info.min_gap_m)) out << info.min_gap_m; + else out << "null"; + out << ",\"lattice_upgrade_ms\":" << info.lattice_upgrade_ms << "}\n"; std::cout << id << " " << deviation << " " << info.reason << " " << info.query_ms << " ms" << std::endl; } }