/********************************************************************* * * Software License Agreement (BSD License) * * Kiểm grid_analysis: EDT, mặt nạ tự do, trường địa trắc (cấm cắt góc), cost_to_go theo plan, hành * lang vuông góc với plan, khoảng trống thân, kéo footprint dọc quỹ đạo. * * Lưới 10 x 10 m, ô 0.05 m — cùng resolution với local costmap runtime. Thân 1.166 x 0.96 m như * footprint sim (front = rear = 0.583, side = 0.48), inscribed 0.48. * * Author: DuongTD *********************************************************************/ #include #include #include #include #include #include #include #include #include namespace { using move_base2::analyzeGrid; using move_base2::bodyClearance; using move_base2::computeGeodesic; using move_base2::CorridorParams; using move_base2::CorridorResult; using move_base2::costToGo; using move_base2::CostToGoParams; using move_base2::CostToGoResult; using move_base2::distanceAt; using move_base2::GridScratch; using move_base2::PlanarTransform; using move_base2::scanCorridor; using move_base2::SweepParams; using move_base2::SweepResult; using move_base2::sweepTrajectory; constexpr double kRes = 0.05; constexpr double kInscribed = 0.48; constexpr unsigned char kLethal = robot_costmap_2d::LETHAL_OBSTACLE; constexpr double kInf = std::numeric_limits::infinity(); robot_geometry_msgs::PoseStamped makePose(double x, double y, double yaw = 0.0) { robot_geometry_msgs::PoseStamped pose; pose.header.frame_id = "odom"; pose.pose.position.x = x; pose.pose.position.y = y; pose.pose.orientation.z = std::sin(0.5 * yaw); pose.pose.orientation.w = std::cos(0.5 * yaw); return pose; } std::vector linePlan(double from_x, double to_x, double step, double y = 0.0) { std::vector plan; for (double x = from_x; x <= to_x + 1e-9; x += step) { plan.push_back(makePose(x, y)); } return plan; } nav_test_harness::FakeCostmap makeGrid() { return nav_test_harness::FakeCostmap::centered(10.0, kRes); } /// Vạch ô lethal thẳng từ (x0,y0) tới (x1,y1), bước nửa ô. void lethalLine(nav_test_harness::FakeCostmap& grid, double x0, double y0, double x1, double y1) { const double len = std::hypot(x1 - x0, y1 - y0); const int n = std::max(1, static_cast(std::ceil(len / (0.5 * kRes)))); for (int i = 0; i <= n; ++i) { const double t = static_cast(i) / n; grid.setLethalAt(x0 + (x1 - x0) * t, y0 + (y1 - y0) * t); } } bool cellOf(const GridScratch& s, double x, double y, unsigned int& mx, unsigned int& my) { return s.toCell(x, y, mx, my); } double geoAt(const GridScratch& s, double x, double y) { unsigned int mx = 0; unsigned int my = 0; if (!cellOf(s, x, y, mx, my)) { return std::numeric_limits::quiet_NaN(); } return s.geo[s.index(mx, my)]; } // ------------------------------------------------------------------------------------------ EDT TEST(GridAnalysisEdt, EmptyGridIsAllFreeAndInfinitelyFar) { nav_test_harness::FakeCostmap grid = makeGrid(); GridScratch s; ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s)); EXPECT_TRUE(std::isinf(distanceAt(s, 0.0, 0.0))); unsigned int mx = 0; unsigned int my = 0; ASSERT_TRUE(cellOf(s, 2.0, -3.0, mx, my)); EXPECT_EQ(1, s.free[s.index(mx, my)]); } TEST(GridAnalysisEdt, DistanceToASingleLethalCellIsEuclidean) { nav_test_harness::FakeCostmap grid = makeGrid(); grid.setLethalAt(0.0, 0.0); GridScratch s; ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s)); EXPECT_NEAR(0.0, distanceAt(s, 0.0, 0.0), 1e-6); EXPECT_NEAR(1.0, distanceAt(s, 1.0, 0.0), 1.5 * kRes); EXPECT_NEAR(std::hypot(0.5, 0.5), distanceAt(s, 0.5, 0.5), 1.5 * kRes); EXPECT_NEAR(3.0, distanceAt(s, -3.0, 0.0), 1.5 * kRes); // Mặt nạ tự do theo inscribed: 0.3 m thì tâm không đặt được, 0.6 m thì được. unsigned int mx = 0; unsigned int my = 0; ASSERT_TRUE(cellOf(s, 0.3, 0.0, mx, my)); EXPECT_EQ(0, s.free[s.index(mx, my)]); ASSERT_TRUE(cellOf(s, 0.6, 0.0, mx, my)); EXPECT_EQ(1, s.free[s.index(mx, my)]); } TEST(GridAnalysisEdt, UnknownAndInflationAreNotObstacles) { nav_test_harness::FakeCostmap grid = makeGrid(); grid.setCostAt(0.0, 0.0, robot_costmap_2d::NO_INFORMATION); grid.setCostAt(1.0, 1.0, robot_costmap_2d::INSCRIBED_INFLATED_OBSTACLE); GridScratch s; ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s)); EXPECT_TRUE(std::isinf(distanceAt(s, 0.0, 0.0))); EXPECT_TRUE(std::isinf(distanceAt(s, 1.0, 1.0))); } TEST(GridAnalysisEdt, OutsideTheGridIsNaN) { nav_test_harness::FakeCostmap grid = makeGrid(); GridScratch s; ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s)); EXPECT_TRUE(std::isnan(distanceAt(s, 7.0, 0.0))); } // ------------------------------------------------------------------------------------- geodesic TEST(GridAnalysisGeodesic, OpenFieldMatchesOctileDistance) { nav_test_harness::FakeCostmap grid = makeGrid(); GridScratch s; ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s)); unsigned int tx = 0; unsigned int ty = 0; ASSERT_TRUE(cellOf(s, 0.0, 0.0, tx, ty)); std::size_t visited = 0; ASSERT_TRUE(computeGeodesic(tx, ty, s, &visited)); EXPECT_GT(visited, 0u); EXPECT_NEAR(0.0, geoAt(s, 0.0, 0.0), 1e-6); EXPECT_NEAR(2.0, geoAt(s, 2.0, 0.0), 2.0 * kRes); EXPECT_NEAR(2.0 * std::sqrt(2.0), geoAt(s, 2.0, 2.0), 2.0 * kRes); } TEST(GridAnalysisGeodesic, WallForcesTheWayAround) { nav_test_harness::FakeCostmap grid = makeGrid(); lethalLine(grid, 1.0, -1.5, 1.0, 1.5); GridScratch s; ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s)); unsigned int tx = 0; unsigned int ty = 0; ASSERT_TRUE(cellOf(s, 0.0, 0.0, tx, ty)); ASSERT_TRUE(computeGeodesic(tx, ty, s)); const double behind = geoAt(s, 2.0, 0.0); EXPECT_TRUE(std::isfinite(behind)); // Thẳng là 2.0 m; phải vòng qua đầu tường (1.5 m + inscribed) nên dài hơn hẳn. EXPECT_GT(behind, 3.5); // Ô ngay trong dải cấm quanh tường không có đường (không free). EXPECT_TRUE(std::isinf(geoAt(s, 1.0, 0.0))); } TEST(GridAnalysisGeodesic, SealedRoomIsUnreachable) { nav_test_harness::FakeCostmap grid = makeGrid(); lethalLine(grid, -1.5, -1.5, 1.5, -1.5); lethalLine(grid, 1.5, -1.5, 1.5, 1.5); lethalLine(grid, 1.5, 1.5, -1.5, 1.5); lethalLine(grid, -1.5, 1.5, -1.5, -1.5); GridScratch s; ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s)); unsigned int tx = 0; unsigned int ty = 0; ASSERT_TRUE(cellOf(s, 0.0, 0.0, tx, ty)); ASSERT_TRUE(computeGeodesic(tx, ty, s)); EXPECT_TRUE(std::isfinite(geoAt(s, 0.5, 0.5))); EXPECT_TRUE(std::isinf(geoAt(s, 3.0, 3.0))); } TEST(GridAnalysisGeodesic, DiagonalWallOfSingleCellsIsNotLeaked) { // Bán kính free ~0 để mặt nạ = "không lethal": tường chéo gồm các ô (i, i) chỉ chạm nhau ở góc. nav_test_harness::FakeCostmap grid = makeGrid(); for (int i = -100; i < 100; ++i) // trọn đường chéo của lưới 200 ô, không để hở hai đầu { grid.setLethalAt(i * kRes + 0.5 * kRes, i * kRes + 0.5 * kRes); } GridScratch s; ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, 0.01, s)); unsigned int tx = 0; unsigned int ty = 0; ASSERT_TRUE(cellOf(s, 1.0, -1.0, tx, ty)); ASSERT_TRUE(computeGeodesic(tx, ty, s)); // Không cắt góc: bên kia tường chéo phải là +inf, cùng bên thì tới được. EXPECT_TRUE(std::isinf(geoAt(s, -1.0, 1.0))); EXPECT_TRUE(std::isfinite(geoAt(s, 2.0, -2.0))); } TEST(GridAnalysisGeodesic, TargetInsideTheForbiddenBandIsRejected) { nav_test_harness::FakeCostmap grid = makeGrid(); grid.setLethalAt(0.0, 0.0); GridScratch s; ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s)); unsigned int tx = 0; unsigned int ty = 0; ASSERT_TRUE(cellOf(s, 0.2, 0.0, tx, ty)); EXPECT_FALSE(computeGeodesic(tx, ty, s)); } // ----------------------------------------------------------------------------------- cost to go TEST(GridAnalysisCostToGo, StraightPlanInTheWindowEqualsRemainingLength) { nav_test_harness::FakeCostmap grid = makeGrid(); GridScratch s; ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s)); const auto plan = linePlan(-3.0, 3.0, 0.1); CostToGoResult r = costToGo(plan, 0, -3.0, 0.0, CostToGoParams(), PlanarTransform(), s); ASSERT_TRUE(r.conclusive); EXPECT_TRUE(r.reachable); EXPECT_EQ(CostToGoResult::Target::kGoal, r.target); EXPECT_NEAR(6.0, r.cost_to_go, 0.15); EXPECT_NEAR(0.0, r.arc_rest, 1e-9); // Robot giữa plan: còn 3 m. r = costToGo(plan, 30, 0.0, 0.0, CostToGoParams(), PlanarTransform(), s); ASSERT_TRUE(r.conclusive); EXPECT_NEAR(3.0, r.cost_to_go, 0.15); } TEST(GridAnalysisCostToGo, LateralDeviationCostsTheOctileDetour) { nav_test_harness::FakeCostmap grid = makeGrid(); GridScratch s; ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s)); const auto plan = linePlan(-3.0, 3.0, 0.1); // Robot lệch 1.5 m ngang tại x = 0: đường 8-liên-thông tới (3, 0) dài 1.5*sqrt2 + 1.5 = 3.62 m. const CostToGoResult r = costToGo(plan, 30, 0.0, 1.5, CostToGoParams(), PlanarTransform(), s); ASSERT_TRUE(r.conclusive); EXPECT_TRUE(r.reachable); EXPECT_NEAR(3.62, r.cost_to_go, 0.2); } TEST(GridAnalysisCostToGo, PlanLeavingTheWindowUsesTheExitPointPlusRemainingArc) { nav_test_harness::FakeCostmap grid = makeGrid(); GridScratch s; ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s)); const auto plan = linePlan(0.0, 8.0, 0.1); // rời lưới ở x = 5 const CostToGoResult r = costToGo(plan, 0, 0.0, 0.0, CostToGoParams(), PlanarTransform(), s); ASSERT_TRUE(r.conclusive); EXPECT_TRUE(r.reachable); EXPECT_EQ(CostToGoResult::Target::kExit, r.target); EXPECT_GT(r.arc_rest, 2.9); EXPECT_LT(r.arc_rest, 3.2); EXPECT_NEAR(8.0, r.cost_to_go, 0.2); } TEST(GridAnalysisCostToGo, ExitAheadPinsTheTargetAtAFixedArcLength) { nav_test_harness::FakeCostmap grid = makeGrid(); GridScratch s; ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s)); const auto plan = linePlan(0.0, 8.0, 0.1); // rời lưới ở x = 5 CostToGoParams params; params.exit_ahead = 2.0; const CostToGoResult r = costToGo(plan, 0, 0.0, 0.0, params, PlanarTransform(), s); ASSERT_TRUE(r.conclusive); EXPECT_TRUE(r.reachable); EXPECT_EQ(CostToGoResult::Target::kAhead, r.target); EXPECT_EQ(20u, r.target_index); // pose ở x = 2.0 EXPECT_NEAR(6.0, r.arc_rest, 0.05); // 8 − 2 EXPECT_NEAR(8.0, r.cost_to_go, 0.2); // geo 2 + arc 6: cùng tổng với bản cũ khi đi thẳng // Cửa sổ trôi không kéo T lùi: robot tiến 1 m -> from_index 10 -> T tiến theo (x = 3.0), arc_rest giảm đúng 1 m. const CostToGoResult r2 = costToGo(plan, 10, 1.0, 0.0, params, PlanarTransform(), s); EXPECT_EQ(30u, r2.target_index); EXPECT_NEAR(5.0, r2.arc_rest, 0.05); // Quãng cố định vượt cửa sổ thì vẫn kẹp ở điểm rời cửa sổ (kExit). params.exit_ahead = 7.0; const CostToGoResult r3 = costToGo(plan, 0, 0.0, 0.0, params, PlanarTransform(), s); EXPECT_EQ(CostToGoResult::Target::kExit, r3.target); // Goal trong quãng thì T = goal. params.exit_ahead = 2.0; const CostToGoResult r4 = costToGo(linePlan(0.0, 1.0, 0.1), 0, 0.0, 0.0, params, PlanarTransform(), s); EXPECT_EQ(CostToGoResult::Target::kGoal, r4.target); } TEST(GridAnalysisCostToGo, BlockedExitBacksOffAlongThePlanOrReportsBlocked) { nav_test_harness::FakeCostmap grid = makeGrid(); grid.setLethalCircle(3.0, 0.0, 0.3); GridScratch s; ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s)); const auto plan = linePlan(0.0, 3.0, 0.1); // T = (3, 0) nằm trong vật cản; ô free gần nhất dọc plan cách ~0.78 m > backoff 0.5 -> đích bị bịt. CostToGoParams params; params.target_backoff = 0.5; CostToGoResult r = costToGo(plan, 0, 0.0, 0.0, params, PlanarTransform(), s); ASSERT_TRUE(r.conclusive); EXPECT_FALSE(r.reachable); EXPECT_EQ(CostToGoResult::Target::kBlockedExit, r.target); // Cho lùi 1.0 m thì tìm được T free và có đường. params.target_backoff = 1.0; r = costToGo(plan, 0, 0.0, 0.0, params, PlanarTransform(), s); ASSERT_TRUE(r.conclusive); EXPECT_TRUE(r.reachable); EXPECT_EQ(CostToGoResult::Target::kBackedOff, r.target); EXPECT_GT(r.arc_rest, 0.7); EXPECT_NEAR(3.0, r.cost_to_go, 0.2); } TEST(GridAnalysisCostToGo, RobotHuggingAnObstacleSnapsToTheNearestFreeCell) { nav_test_harness::FakeCostmap grid = makeGrid(); lethalLine(grid, -3.0, 0.4, 3.0, 0.4); // tường sát bên trái tuyến GridScratch s; ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s)); const auto plan = linePlan(-3.0, 3.0, 0.1, -0.6); // Robot tại (0, 0.1): cách tường 0.3 < inscribed -> ô không free, snap trong 0.35 m. const CostToGoResult r = costToGo(plan, 30, 0.0, 0.1, CostToGoParams(), PlanarTransform(), s); ASSERT_TRUE(r.conclusive); EXPECT_TRUE(r.robot_in_ring); EXPECT_TRUE(r.reachable); } TEST(GridAnalysisCostToGo, EmptyPlanOrRobotOutsideIsInconclusive) { nav_test_harness::FakeCostmap grid = makeGrid(); GridScratch s; ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s)); std::vector empty; EXPECT_FALSE(costToGo(empty, 0, 0.0, 0.0, CostToGoParams(), PlanarTransform(), s).conclusive); const auto plan = linePlan(0.0, 3.0, 0.1); EXPECT_FALSE(costToGo(plan, 0, 7.0, 0.0, CostToGoParams(), PlanarTransform(), s).conclusive); } TEST(GridAnalysisCostToGo, TransformIsAppliedToThePlan) { nav_test_harness::FakeCostmap grid = makeGrid(); GridScratch s; ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s)); // Plan trong frame "map" = lưới dịch +10 m: (10..13, 0) map -> (0..3, 0) lưới. const auto plan = linePlan(10.0, 13.0, 0.1); PlanarTransform map_to_grid; map_to_grid.x = -10.0; const CostToGoResult r = costToGo(plan, 0, 0.0, 0.0, CostToGoParams(), map_to_grid, s); ASSERT_TRUE(r.conclusive); EXPECT_NEAR(3.0, r.cost_to_go, 0.15); } // ------------------------------------------------------------------------------------- corridor TEST(GridAnalysisCorridor, GapNarrowerThanTheBodyIsABlock) { nav_test_harness::FakeCostmap grid = makeGrid(); grid.setLethalCircle(1.0, 0.6, 0.2); // mép trong ở y = +0.4 grid.setLethalCircle(1.0, -0.6, 0.2); // mép trong ở y = -0.4 -> khe 0.8 m < thân 0.96 GridScratch s; ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s)); const auto plan = linePlan(-2.0, 3.0, 0.1); const CorridorResult r = scanCorridor(plan, 0, CorridorParams(), PlanarTransform(), s); EXPECT_GT(r.scanned, 0u); EXPECT_NEAR(0.8, r.min_width, 0.12); EXPECT_NEAR(3.0, r.block_dist, 0.25); // Tâm robot không đặt được ở khe (0.4 < 0.48): plan hết sạch trước chỗ khe. EXPECT_LT(r.plan_clear_ahead, 3.0); } TEST(GridAnalysisCorridor, GapWiderThanTheBodyIsNotABlock) { nav_test_harness::FakeCostmap grid = makeGrid(); grid.setLethalCircle(1.0, 0.8, 0.2); // mép trong ở y = +0.6 -> khe 1.2 m grid.setLethalCircle(1.0, -0.8, 0.2); GridScratch s; ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s)); const auto plan = linePlan(-2.0, 3.0, 0.1); const CorridorResult r = scanCorridor(plan, 0, CorridorParams(), PlanarTransform(), s); EXPECT_TRUE(std::isinf(r.block_dist)); EXPECT_NEAR(1.2, r.min_width, 0.12); } TEST(GridAnalysisCorridor, WallOnOneSideWithRoomOnTheOtherIsNotABlock) { // Ca mission 1 ngày 5/9: plan sượt tường nhưng bên kia trống -> hành lang vẫn rộng. nav_test_harness::FakeCostmap grid = makeGrid(); lethalLine(grid, 0.0, 0.3, 2.0, 0.3); GridScratch s; ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s)); const auto plan = linePlan(-2.0, 3.0, 0.1); const CorridorResult r = scanCorridor(plan, 0, CorridorParams(), PlanarTransform(), s); EXPECT_TRUE(std::isinf(r.block_dist)); EXPECT_NEAR(0.3 + 1.5, r.min_width, 0.12); // trái 0.3 + phải chạm trần 1.5 // Tâm cách đầu tường (0, 0.3) đúng 0.48 m tại x = -sqrt(0.48^2 - 0.3^2) = -0.375 -> 1.625 m từ x = -2. EXPECT_NEAR(1.625, r.plan_clear_ahead, 0.15); } TEST(GridAnalysisCorridor, HorizonLimitsTheScan) { nav_test_harness::FakeCostmap grid = makeGrid(); grid.setLethalCircle(2.5, 0.6, 0.2); grid.setLethalCircle(2.5, -0.6, 0.2); GridScratch s; ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s)); const auto plan = linePlan(-2.0, 3.0, 0.1); CorridorParams params; params.horizon = 3.0; // khe ở 4.5 m dọc plan: ngoài tầm const CorridorResult r = scanCorridor(plan, 0, params, PlanarTransform(), s); EXPECT_TRUE(std::isinf(r.block_dist)); EXPECT_NEAR(3.0, r.plan_clear_ahead, 1e-9); } // --------------------------------------------------------------------------------- body clearance TEST(GridAnalysisBody, ClearanceBesideAWall) { nav_test_harness::FakeCostmap grid = makeGrid(); lethalLine(grid, -2.0, 0.7, 2.0, 0.7); GridScratch s; ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s)); // Cạnh trái thân ở y = 0.48, tường ở 0.7 -> 0.22 m. EXPECT_NEAR(0.22, bodyClearance(s, 0.0, 0.0, 0.0, 0.583, 0.583, 0.48), 1.5 * kRes); // Quay 90 độ: mũi (0.583) hướng về tường -> 0.117 m. EXPECT_NEAR(0.117, bodyClearance(s, 0.0, 0.0, M_PI / 2.0, 0.583, 0.583, 0.48), 1.5 * kRes); } TEST(GridAnalysisBody, EmptyGridClearanceIsInfinite) { nav_test_harness::FakeCostmap grid = makeGrid(); GridScratch s; ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s)); EXPECT_TRUE(std::isinf(bodyClearance(s, 0.0, 0.0, 0.3, 0.583, 0.583, 0.48))); } // ---------------------------------------------------------------------------------------- sweep std::vector straightTrajectory(double length, double step) { std::vector traj; for (double x = 0.0; x <= length + 1e-9; x += step) { traj.push_back(makePose(x, 0.0, 0.0)); } return traj; } TEST(GridAnalysisSweep, StraightTrajectoryIntoAWallReportsTheNoseContact) { nav_test_harness::FakeCostmap grid = makeGrid(); grid.setLethalAt(1.5, 0.0); GridScratch s; ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s)); const SweepResult r = sweepTrajectory(straightTrajectory(2.0, 0.1), 0.0, 0.0, 0.0, 0.3, SweepParams(), *grid.costmap(), s); ASSERT_TRUE(r.conclusive); EXPECT_TRUE(r.blocked); EXPECT_EQ(1, r.hit_edge); // mũi // Mũi đệm 0.633 m chạm ô tại 1.5 m (mép ô ~1.475): còn ~0.84 m đường đi. EXPECT_NEAR(0.84, r.distance, 0.1); } TEST(GridAnalysisSweep, ClearTrajectoryIsNotBlocked) { nav_test_harness::FakeCostmap grid = makeGrid(); grid.setLethalAt(1.5, 1.5); GridScratch s; ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s)); const SweepResult r = sweepTrajectory(straightTrajectory(1.4, 0.1), 0.0, 0.0, 0.0, 0.3, SweepParams(), *grid.costmap(), s); ASSERT_TRUE(r.conclusive); EXPECT_FALSE(r.blocked); EXPECT_GT(r.scanned, 0u); } TEST(GridAnalysisSweep, TrajectoryNotStartingAtTheRobotIsInconclusive) { nav_test_harness::FakeCostmap grid = makeGrid(); GridScratch s; ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s)); // Quỹ đạo bắt đầu ở (0,0) nhưng robot ở (1.0, 0): đây là global plan, không phải chuyển động sắp xảy ra. const SweepResult r = sweepTrajectory(straightTrajectory(2.0, 0.1), 1.0, 0.0, 0.0, 0.3, SweepParams(), *grid.costmap(), s); EXPECT_FALSE(r.conclusive); } TEST(GridAnalysisSweep, LethalCellUnderTheBodyIsExcludedByTheBaseline) { nav_test_harness::FakeCostmap grid = makeGrid(); grid.setLethalAt(0.3, 0.50); // kề hông trái, trong thân đệm (0.53) GridScratch s; ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s)); const SweepResult r = sweepTrajectory(straightTrajectory(2.0, 0.1), 0.0, 0.0, 0.0, 0.3, SweepParams(), *grid.costmap(), s); ASSERT_TRUE(r.conclusive); EXPECT_GE(r.baseline_cells, 1); EXPECT_FALSE(r.blocked); // đi thẳng: ô đó trượt ra sau, không "thêm" ô mới vào thân SweepParams no_baseline; no_baseline.exclude_baseline = false; const SweepResult r2 = sweepTrajectory(straightTrajectory(2.0, 0.1), 0.0, 0.0, 0.0, 0.3, no_baseline, *grid.costmap(), s); EXPECT_TRUE(r2.blocked); } TEST(GridAnalysisSweep, TrajectoryCurvingAwayFromAWallIsClear) { nav_test_harness::FakeCostmap grid = makeGrid(); // Tường cách 1.3 m: đi thẳng thì mũi đệm (0.633) chạm sau ~0.65 m; cung R 0.4 quay 90 độ có góc // mũi-phải quét xa nhất sqrt(0.633^2 + 0.93^2) = 1.13 m < 1.3 -> không chạm. lethalLine(grid, 1.3, -2.0, 1.3, 2.0); GridScratch s; ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s)); // Quỹ đạo quay gắt sang trái: cung R = 0.4 m, 90 độ, kết thúc song song tường ở x ~ 0.4. std::vector traj; for (int i = 0; i <= 20; ++i) { const double a = (M_PI / 2.0) * i / 20.0; traj.push_back(makePose(0.4 * std::sin(a), 0.4 * (1.0 - std::cos(a)), a)); } SweepParams params; params.lookahead = 1.0; const SweepResult straight = sweepTrajectory(straightTrajectory(1.5, 0.1), 0.0, 0.0, 0.0, 0.3, params, *grid.costmap(), s); const SweepResult curve = sweepTrajectory(traj, 0.0, 0.0, 0.0, 0.3, params, *grid.costmap(), s); ASSERT_TRUE(straight.conclusive); EXPECT_TRUE(straight.blocked); ASSERT_TRUE(curve.conclusive); EXPECT_FALSE(curve.blocked); } TEST(GridAnalysisSweep, LookaheadStopsTheSweep) { nav_test_harness::FakeCostmap grid = makeGrid(); grid.setLethalAt(3.0, 0.0); GridScratch s; ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s)); SweepParams params; params.lookahead = 1.5; const SweepResult r = sweepTrajectory(straightTrajectory(4.0, 0.1), 0.0, 0.0, 0.0, 0.3, params, *grid.costmap(), s); ASSERT_TRUE(r.conclusive); EXPECT_FALSE(r.blocked); } // ---------------------------------------------------------------------------------- scratch reuse TEST(GridAnalysisScratch, ReusedAcrossGridsWithoutStaleState) { GridScratch s; nav_test_harness::FakeCostmap a = makeGrid(); a.setLethalAt(0.0, 0.0); ASSERT_TRUE(analyzeGrid(*a.costmap(), kLethal, kInscribed, s)); EXPECT_NEAR(0.0, distanceAt(s, 0.0, 0.0), 1e-6); nav_test_harness::FakeCostmap b = makeGrid(); // trống ASSERT_TRUE(analyzeGrid(*b.costmap(), kLethal, kInscribed, s)); EXPECT_TRUE(std::isinf(distanceAt(s, 0.0, 0.0))); nav_test_harness::FakeCostmap c(100, 60, kRes, -2.5, -1.5); // kích thước khác c.setLethalAt(1.0, 0.0); ASSERT_TRUE(analyzeGrid(*c.costmap(), kLethal, kInscribed, s)); EXPECT_EQ(100u, s.width); EXPECT_EQ(60u, s.height); EXPECT_NEAR(1.0, distanceAt(s, 0.0, 0.0), 1.5 * kRes); } // ------------------------------------------------------------------------------------- chi phí TEST(GridAnalysisBench, ReportCostOnRuntimeSizedGrid) { // 160 x 160 @ 0.05 m như local costmap runtime, bãi 30 trụ rải ngẫu nhiên. Chỉ in số, không phán // pass/fail: chi phí tuyệt đối phụ thuộc máy; plan (§2.5) dự tính EDT ~0.3 ms + Dijkstra <= 0.4 ms. nav_test_harness::FakeCostmap grid = nav_test_harness::FakeCostmap::centered(8.0, kRes); unsigned int seed = 12345u; for (int i = 0; i < 30; ++i) { seed = seed * 1103515245u + 12345u; const double x = -3.5 + 7.0 * ((seed >> 8) % 1000) / 1000.0; seed = seed * 1103515245u + 12345u; const double y = -3.5 + 7.0 * ((seed >> 8) % 1000) / 1000.0; grid.setLethalCircle(x, y, 0.25); } // Dọn một hành lang trống dọc plan để T và ô robot luôn free -> Dijkstra chạy thật (thăm toàn bộ // thành phần liên thông chứa T = trường hợp xấu nhất về chi phí). // Lấy mẫu tại TÂM ô: bước đúng một ô nhưng đặt trên biên ô thì sai số float bỏ sót ô. for (double x = -0.6 + 0.5 * kRes; x < 4.0; x += kRes) { for (double y = -0.8 + 0.5 * kRes; y < 0.8; y += kRes) { grid.setCostAt(x, y, robot_costmap_2d::FREE_SPACE); } } GridScratch s; const auto plan = linePlan(0.0, 3.9, 0.1); ASSERT_TRUE(analyzeGrid(*grid.costmap(), kLethal, kInscribed, s)); // ensure() cấp phát một lần constexpr int kIters = 200; auto t0 = std::chrono::steady_clock::now(); for (int i = 0; i < kIters; ++i) { analyzeGrid(*grid.costmap(), kLethal, kInscribed, s); } auto t1 = std::chrono::steady_clock::now(); CostToGoResult last; for (int i = 0; i < kIters; ++i) { last = costToGo(plan, 0, 0.0, 0.0, CostToGoParams(), PlanarTransform(), s); } auto t2 = std::chrono::steady_clock::now(); CorridorResult corr; for (int i = 0; i < kIters; ++i) { corr = scanCorridor(plan, 0, CorridorParams(), PlanarTransform(), s); } auto t3 = std::chrono::steady_clock::now(); SweepResult sw; const auto traj = straightTrajectory(1.5, 0.1); for (int i = 0; i < kIters; ++i) { sw = sweepTrajectory(traj, 0.0, 0.0, 0.0, 0.3, SweepParams(), *grid.costmap(), s); } auto t4 = std::chrono::steady_clock::now(); const auto us = [](auto a, auto b) { return std::chrono::duration_cast(b - a).count() / static_cast(kIters); }; std::printf("[bench] 160x160: analyzeGrid %.0f us | costToGo %.0f us (visited %zu, reachable %d, " "conclusive %d, in_ring %d, target %d, edt_robot %.2f, edt_T %.2f, cost %.2f) | " "scanCorridor %.0f us (scanned %zu) | sweepTrajectory %.0f us\n", us(t0, t1), us(t1, t2), last.cells_visited, last.reachable ? 1 : 0, last.conclusive ? 1 : 0, last.robot_in_ring ? 1 : 0, static_cast(last.target), distanceAt(s, 0.0, 0.0), distanceAt(s, 3.9, 0.0), last.cost_to_go, us(t2, t3), corr.scanned, us(t3, t4)); EXPECT_TRUE(last.conclusive); EXPECT_TRUE(last.reachable); EXPECT_GT(last.cells_visited, 1000u); EXPECT_TRUE(sw.conclusive); } } // namespace int main(int argc, char** argv) { testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }