Files
move_base2/test/plan_inspector_test.cpp
duongtd 261cc20ad4 feat: EnvMonitor thay PathMonitor — giám sát môi trường bằng EDT/geodesic/quỹ đạo thật
Bộ giám sát mới trong ControlLoop (core/env_monitor, core/grid_analysis, ports/environment_port,
io/env_logger), thay bốn phép đo của PathMonitor (code cũ còn, tắt bằng path_monitor/enabled: false):

- kContact: dò tiếp xúc dọc quỹ đạo local planner (thay cung v/omega), bao tốc độ sqrt(2a(d - margin))
  có dư, phanh chỉ khi vượt bao, cờ collision khi kề mũi tĩnh 1 s; cổng xoay tại chỗ theo lệnh
  controller muốn; hysteresis + xác nhận khi nhả; đứng ra sau detour không dịch robot (nhường
  controller); giữ phanh khi route khoá thì báo no valid command để controller_patience leo thang.
- kStall: cost_to_go = geodesic tới điểm thoát 2 m dọc plan + phần plan còn lại, index plan bám cửa
  sổ, bộ lọc lật liên thông; cờ no_progress chỉ khi cổng dịch chuyển thực mở (robot không đi được
  đâu trong 12 s dù lệnh >= 1,5 m) — thay ngân sách replan cho stall.
- kUnreachable: liên thông tới điểm thoát, đang ở action log.
- EnvLogger ghi JSONL mỗi tick nặng; config/runtime/env_monitor_params.yaml đủ khoá, comment tiếng
  Việt; ControllerPort::measuredVelocity; FakeEnvironmentPort cho test.

Sim 5–6/9: hết churn detour, khe 1,25 m đi qua 0,5 m/s, lượt 6/9 10:15 2 detour/142 s không oan.
Test: grid_analysis 31, env_monitor 43, plan_inspector 37, walking_skeleton 87, các bộ khác pass.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017xXjFjr2DQ6rhmhHSW4fk4
2026-09-06 10:30:31 +07:00

568 lines
21 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*********************************************************************
*
* Software License Agreement (BSD License)
*
* Kiểm plan_inspector: quét tuyến phía trước, chiếu pose lên plan, đo lệch tuyến, quy đổi frame.
*
* Dùng `nav_test_harness::FakeCostmap` (sở hữu một `Costmap2D` thật) nên ngữ nghĩa cost là ngữ
* nghĩa thật của workspace — kể cả `FREE_SPACE = 60` của fork này.
*
* Author: DuongTD
*********************************************************************/
#include <gtest/gtest.h>
#include <cmath>
#include <string>
#include <vector>
#include <nav_test_harness/fake_costmap.h>
#include <robot_costmap_2d/cost_values.h>
#include <tf3/buffer_core.h>
#include <move_base2/core/plan_inspector.h>
namespace
{
using move_base2::checkPlan;
using move_base2::crossTrackError;
using move_base2::nearestIndex;
using move_base2::trackedNearestIndex;
using move_base2::PlanarTransform;
using move_base2::PlanBlockResult;
using move_base2::planarTransform;
robot_geometry_msgs::PoseStamped makePose(double x, double y, const std::string& frame = "map")
{
robot_geometry_msgs::PoseStamped pose;
pose.header.frame_id = frame;
pose.pose.position.x = x;
pose.pose.position.y = y;
pose.pose.orientation.w = 1.0;
return pose;
}
/// Plan thẳng dọc trục x, bước @p step, y cố định.
std::vector<robot_geometry_msgs::PoseStamped> linePlan(double from_x, double to_x, double step,
double y = 0.0)
{
std::vector<robot_geometry_msgs::PoseStamped> plan;
for (double x = from_x; x <= to_x + 1e-9; x += step)
{
plan.push_back(makePose(x, y));
}
return plan;
}
/// Lưới 10 x 10 m, gốc tại (-5, -5), ô 0.05 m — cùng resolution với local costmap runtime.
nav_test_harness::FakeCostmap makeGrid()
{
return nav_test_harness::FakeCostmap::centered(10.0, 0.05);
}
TEST(PlanInspectorCheck, CleanPlanIsNotBlocked)
{
nav_test_harness::FakeCostmap grid = makeGrid();
const auto plan = linePlan(0.0, 3.0, 0.1);
const PlanBlockResult result = checkPlan(plan, 0, 3.0, *grid.costmap(), PlanarTransform());
EXPECT_FALSE(result.blocked);
EXPECT_GT(result.scanned, 0u);
}
TEST(PlanInspectorCheck, FreeSpaceValueIsNotBlocked)
{
// Bẫy của fork này: FREE_SPACE = 60, không phải 0. Đặt tường minh giá trị đó lên tuyến để khoá
// lại bất biến "so sánh phải dùng hằng số có tên".
nav_test_harness::FakeCostmap grid = makeGrid();
ASSERT_TRUE(grid.setCostAt(1.0, 0.0, robot_costmap_2d::FREE_SPACE));
const auto plan = linePlan(0.0, 3.0, 0.1);
const PlanBlockResult result = checkPlan(plan, 0, 3.0, *grid.costmap(), PlanarTransform());
EXPECT_FALSE(result.blocked);
}
TEST(PlanInspectorCheck, LethalCellOnThePlanBlocks)
{
nav_test_harness::FakeCostmap grid = makeGrid();
ASSERT_TRUE(grid.setLethalAt(1.5, 0.0));
const auto plan = linePlan(0.0, 3.0, 0.1);
const PlanBlockResult result = checkPlan(plan, 0, 3.0, *grid.costmap(), PlanarTransform());
ASSERT_TRUE(result.blocked);
EXPECT_NEAR(plan[result.index].pose.position.x, 1.5, 0.06);
EXPECT_NEAR(result.distance, 1.5, 0.06);
}
TEST(PlanInspectorCheck, InscribedInflatedCountsAsBlocked)
{
// Ngưỡng là INSCRIBED_INFLATED_OBSTACLE chứ không phải LETHAL: ở đó tâm robot đặt vào là footprint
// đã đệm chạm vật cản.
nav_test_harness::FakeCostmap grid = makeGrid();
ASSERT_TRUE(grid.setCostAt(1.0, 0.0, robot_costmap_2d::INSCRIBED_INFLATED_OBSTACLE));
const auto plan = linePlan(0.0, 3.0, 0.1);
const PlanBlockResult result = checkPlan(plan, 0, 3.0, *grid.costmap(), PlanarTransform());
EXPECT_TRUE(result.blocked);
}
TEST(PlanInspectorCheck, UnknownCellIsNotBlocked)
{
nav_test_harness::FakeCostmap grid = makeGrid();
ASSERT_TRUE(grid.setCostAt(1.0, 0.0, robot_costmap_2d::NO_INFORMATION));
const auto plan = linePlan(0.0, 3.0, 0.1);
const PlanBlockResult result = checkPlan(plan, 0, 3.0, *grid.costmap(), PlanarTransform());
// Vùng chưa quét mà tính là chắn thì robot dừng ở mọi tuyến đi vào chỗ chưa thấy.
EXPECT_FALSE(result.blocked);
}
TEST(PlanInspectorCheck, HorizonCutsTheScanShort)
{
nav_test_harness::FakeCostmap grid = makeGrid();
ASSERT_TRUE(grid.setLethalAt(2.5, 0.0));
const auto plan = linePlan(0.0, 3.0, 0.1);
EXPECT_FALSE(checkPlan(plan, 0, 1.0, *grid.costmap(), PlanarTransform()).blocked);
EXPECT_TRUE(checkPlan(plan, 0, 3.0, *grid.costmap(), PlanarTransform()).blocked);
}
TEST(PlanInspectorCheck, ScanStartsAtFromIndex)
{
// Vật cản NẰM SAU lưng (phần plan đã đi qua) không được tính.
nav_test_harness::FakeCostmap grid = makeGrid();
ASSERT_TRUE(grid.setLethalAt(0.5, 0.0));
const auto plan = linePlan(0.0, 3.0, 0.1);
EXPECT_TRUE(checkPlan(plan, 0, 3.0, *grid.costmap(), PlanarTransform()).blocked);
EXPECT_FALSE(checkPlan(plan, 15, 3.0, *grid.costmap(), PlanarTransform()).blocked);
}
TEST(PlanInspectorCheck, OutOfGridStopsTheScanInsteadOfBlocking)
{
nav_test_harness::FakeCostmap grid = makeGrid(); // ±5 m
const auto plan = linePlan(0.0, 20.0, 0.5);
const PlanBlockResult result = checkPlan(plan, 0, 20.0, *grid.costmap(), PlanarTransform());
EXPECT_FALSE(result.blocked);
EXPECT_LT(result.scanned, plan.size()); // dừng ở rìa lưới, không quét hết
}
TEST(PlanInspectorCheck, EmptyPlanAndBadArgumentsAreSafe)
{
nav_test_harness::FakeCostmap grid = makeGrid();
const auto plan = linePlan(0.0, 3.0, 0.1);
EXPECT_FALSE(checkPlan({}, 0, 3.0, *grid.costmap(), PlanarTransform()).blocked);
EXPECT_FALSE(checkPlan(plan, 999, 3.0, *grid.costmap(), PlanarTransform()).blocked);
EXPECT_FALSE(checkPlan(plan, 0, 0.0, *grid.costmap(), PlanarTransform()).blocked);
}
TEST(PlanInspectorCheck, TransformIsAppliedToEveryPose)
{
// Plan ở "map", lưới ở "odom" lệch (2, 1): vật cản đặt theo toạ độ LƯỚI.
nav_test_harness::FakeCostmap grid = makeGrid();
ASSERT_TRUE(grid.setLethalAt(-0.5, -1.0)); // = map (1.5, 0.0) sau khi trừ offset
PlanarTransform to_grid;
to_grid.x = -2.0;
to_grid.y = -1.0;
const auto plan = linePlan(0.0, 3.0, 0.1);
EXPECT_TRUE(checkPlan(plan, 0, 3.0, *grid.costmap(), to_grid).blocked);
EXPECT_FALSE(checkPlan(plan, 0, 3.0, *grid.costmap(), PlanarTransform()).blocked);
}
TEST(PlanInspectorNearest, ProjectsOntoSegmentsNotVertices)
{
const auto plan = linePlan(0.0, 3.0, 1.0); // pose tại x = 0,1,2,3
EXPECT_EQ(nearestIndex(plan, makePose(0.1, 0.0)), 0u);
EXPECT_EQ(nearestIndex(plan, makePose(1.5, 0.4)), 1u);
EXPECT_EQ(nearestIndex(plan, makePose(2.9, 0.0)), 2u);
}
TEST(PlanInspectorNearest, PlanFoldingBackOnItselfPicksTheNearerLeg)
{
// Plan đi ra rồi vòng lại: đo tới đỉnh sẽ cho index sai, chiếu lên đoạn thì không.
std::vector<robot_geometry_msgs::PoseStamped> plan{
makePose(0.0, 0.0), makePose(2.0, 0.0), makePose(2.0, 1.0), makePose(0.0, 1.0)
};
EXPECT_EQ(nearestIndex(plan, makePose(1.0, 0.05)), 0u);
EXPECT_EQ(nearestIndex(plan, makePose(1.0, 0.95)), 2u);
}
TEST(PlanInspectorNearest, TrackedIndexStaysOnTheCurrentLegOfAFoldedPlan)
{
// Plan ra 4 m rồi vòng lại song song cách 0.6 m: robot đứng giữa hai đoạn (0.35 m tới đoạn đầu, 0.25 m tới
// đoạn về). nearestIndex toàn plan nhảy sang đoạn về (index 2); bám từ hint 0 với cửa sổ 1 m lùi / 3 m tiến
// thì đoạn về nằm ngoài 3 m dọc plan -> giữ đoạn đầu.
std::vector<robot_geometry_msgs::PoseStamped> plan{ makePose(0.0, 0.0), makePose(4.0, 0.0), makePose(4.0, 0.6),
makePose(0.0, 0.6) };
const auto robot = makePose(1.0, 0.35);
EXPECT_EQ(nearestIndex(plan, robot), 2u);
EXPECT_EQ(trackedNearestIndex(plan, robot, 0, 1.0, 3.0), 0u);
// Cửa sổ đủ dài thì bằng nearest toàn plan.
EXPECT_EQ(trackedNearestIndex(plan, robot, 0, 1.0, 20.0), 2u);
}
TEST(PlanInspectorNearest, TrackedIndexAdvancesAndClampsTheHint)
{
const auto plan = linePlan(0.0, 5.0, 0.5); // 11 pose, 10 đoạn
EXPECT_EQ(trackedNearestIndex(plan, makePose(2.3, 0.1), 2, 1.0, 3.0), 4u); // tiến
EXPECT_EQ(trackedNearestIndex(plan, makePose(1.2, 0.0), 4, 1.0, 3.0), 2u); // lùi trong 1 m
EXPECT_EQ(trackedNearestIndex(plan, makePose(0.1, 0.0), 8, 1.0, 3.0), 6u); // lùi bị chặn ở 1 m: đoạn 6
EXPECT_EQ(trackedNearestIndex(plan, makePose(4.9, 0.0), 99, 1.0, 3.0), 9u); // hint vượt cỡ -> kẹp
EXPECT_EQ(trackedNearestIndex({}, makePose(0.0, 0.0), 3, 1.0, 3.0), 0u);
}
TEST(PlanInspectorNearest, ShortPlansAreSafe)
{
EXPECT_EQ(nearestIndex({}, makePose(0.0, 0.0)), 0u);
EXPECT_EQ(nearestIndex({ makePose(5.0, 5.0) }, makePose(0.0, 0.0)), 0u);
}
TEST(PlanInspectorCrossTrack, MeasuresPerpendicularDistance)
{
const auto plan = linePlan(0.0, 3.0, 0.5);
EXPECT_NEAR(crossTrackError(plan, makePose(1.0, 0.0)), 0.0, 1e-9);
EXPECT_NEAR(crossTrackError(plan, makePose(1.0, 0.6)), 0.6, 1e-9);
EXPECT_NEAR(crossTrackError(plan, makePose(1.0, -0.6)), 0.6, 1e-9);
}
TEST(PlanInspectorCrossTrack, DiagonalSegmentUsesTruePerpendicular)
{
// Đoạn 45°: điểm (1,0) cách đoạn sqrt(2)/2, KHÔNG phải 1.0 (khoảng cách tới đỉnh gần nhất).
std::vector<robot_geometry_msgs::PoseStamped> plan{ makePose(0.0, 0.0), makePose(2.0, 2.0) };
EXPECT_NEAR(crossTrackError(plan, makePose(1.0, 0.0)), std::sqrt(2.0) / 2.0, 1e-9);
}
TEST(PlanInspectorCrossTrack, BeyondTheEndClampsToTheEndpoint)
{
const auto plan = linePlan(0.0, 2.0, 1.0);
EXPECT_NEAR(crossTrackError(plan, makePose(3.0, 0.0)), 1.0, 1e-9);
EXPECT_NEAR(crossTrackError(plan, makePose(-1.0, 0.0)), 1.0, 1e-9);
}
TEST(PlanInspectorCrossTrack, EmptyPlanIsZero)
{
EXPECT_NEAR(crossTrackError({}, makePose(1.0, 1.0)), 0.0, 1e-9);
}
TEST(PlanInspectorTransform, LooksUpTfOnceAndConvertsBothWays)
{
tf3::BufferCore tf;
tf3::TransformStampedMsg map_to_odom;
map_to_odom.header.frame_id = "map";
map_to_odom.child_frame_id = "odom";
map_to_odom.transform.translation.x = 2.0; // odom gốc tại map (2, 1)
map_to_odom.transform.translation.y = 1.0;
map_to_odom.transform.rotation.w = 1.0;
ASSERT_TRUE(tf.setTransform(map_to_odom, "test", /*is_static=*/true));
PlanarTransform map_to_grid;
ASSERT_TRUE(planarTransform(tf, "map", "odom", map_to_grid));
double x = 0.0;
double y = 0.0;
map_to_grid.apply(3.0, 2.0, x, y); // map (3,2) -> odom (1,1)
EXPECT_NEAR(x, 1.0, 1e-9);
EXPECT_NEAR(y, 1.0, 1e-9);
}
TEST(PlanInspectorTransform, SameFrameIsIdentityAndUnknownFrameFails)
{
tf3::BufferCore tf;
PlanarTransform same;
EXPECT_TRUE(planarTransform(tf, "map", "map", same));
EXPECT_NEAR(same.x, 0.0, 1e-9);
EXPECT_NEAR(same.cos_yaw, 1.0, 1e-9);
PlanarTransform missing;
EXPECT_FALSE(planarTransform(tf, "map", "odom", missing));
EXPECT_FALSE(planarTransform(tf, "", "odom", missing));
}
} // namespace
int main(int argc, char** argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
// =================================================================================================
// Phép đo 3: cung lệnh — thân robot có sắp chạm vật cản THẬT không (2026-09-03).
// =================================================================================================
namespace
{
using move_base2::checkMotion;
using move_base2::MotionBlockResult;
using move_base2::MotionCheckParams;
MotionCheckParams bodyParams()
{
MotionCheckParams p;
p.lookahead = 0.8;
p.max_samples = 6;
p.front_offset = 0.58;
p.rear_offset = 0.58;
p.side_offset = 0.48;
p.padding = 0.05;
p.cost_threshold = robot_costmap_2d::LETHAL_OBSTACLE;
return p;
}
TEST(MotionCheck, ClearArcIsNotBlocked)
{
nav_test_harness::FakeCostmap grid = makeGrid();
const MotionBlockResult r = checkMotion(0.0, 0.0, 0.0, 0.4, 0.0, bodyParams(), *grid.costmap());
EXPECT_FALSE(r.blocked);
// Bước dò bị kẹp về một ô lưới (0.05 m) nên 0.8 m là 16 mẫu, không phải max_samples = 6.
EXPECT_EQ(r.scanned, 16);
}
TEST(MotionCheck, ThinWallBetweenCoarseSamplesIsStillCaught)
{
// Tường dày đúng MỘT ô tại x=1.30. Với bước thưa 0.8/6 = 0.133 m, mũi (0.63 m trước tâm) ở
// mẫu 5 là 1.297 (chưa tới), mẫu 6 là 1.43 (đã qua) — cả hai sạch, robot đâm xuyên. Kẹp bước
// về một ô lưới thì không mẫu nào nhảy qua được.
nav_test_harness::FakeCostmap grid = makeGrid();
for (double y = -0.6; y <= 0.6; y += 0.05)
{
ASSERT_TRUE(grid.setLethalAt(1.30, y));
}
MotionCheckParams p = bodyParams();
p.max_samples = 6;
const MotionBlockResult r = checkMotion(0.0, 0.0, 0.0, 0.4, 0.0, p, *grid.costmap());
ASSERT_TRUE(r.blocked);
EXPECT_NEAR(r.distance, 1.30 - 0.63, 0.06);
}
TEST(MotionCheck, LethalAheadOfTheNoseBlocksWithDistance)
{
// Tường tại x=1.2. Mũi robot vươn 0.58+0.05 = 0.63 m trước tâm, nên tâm chỉ cần đi ~0.57 m là
// mũi chạm — phải báo ở mẫu ~0.53–0.67 m, KHÔNG phải khi tâm tới 1.2 m.
nav_test_harness::FakeCostmap grid = makeGrid();
for (double y = -0.6; y <= 0.6; y += 0.05)
{
ASSERT_TRUE(grid.setLethalAt(1.2, y));
}
const MotionBlockResult r = checkMotion(0.0, 0.0, 0.0, 0.4, 0.0, bodyParams(), *grid.costmap());
ASSERT_TRUE(r.blocked);
EXPECT_GT(r.distance, 0.4);
EXPECT_LT(r.distance, 0.8);
}
TEST(MotionCheck, InflationOnlyIsNotBlocked)
{
// Đây là toàn bộ lý do phép đo này tồn tại: ô INSCRIBED (253) là biên cho TÂM robot, đặt lên mũi
// là đếm biên hai lần. Phủ 253 kín đường đi — robot vẫn phải được chạy.
nav_test_harness::FakeCostmap grid = makeGrid();
for (double x = 0.0; x <= 2.0; x += 0.05)
{
for (double y = -0.7; y <= 0.7; y += 0.05)
{
ASSERT_TRUE(grid.setCostAt(x, y, robot_costmap_2d::INSCRIBED_INFLATED_OBSTACLE));
}
}
const MotionBlockResult r = checkMotion(0.0, 0.0, 0.0, 0.4, 0.0, bodyParams(), *grid.costmap());
EXPECT_FALSE(r.blocked);
}
TEST(MotionCheck, LethalUnderTheBodyDoesNotBlockParallelMotion)
{
// Người kề hông trái: hàng ô LETHAL ở y=0.51 (ngoài thân 0.48, trong thân đệm 0.53), dọc x từ
// -0.2 tới 0.3 — đúng ca sim 4/9 làm guard báo 0.05 m mãi. Đi thẳng thì mép trái chỉ quét lại
// đúng những ô đã nằm dưới thân -> không có ô mới -> phải cho đi.
nav_test_harness::FakeCostmap grid = makeGrid();
for (double x = -0.2; x <= 0.3; x += 0.05)
{
ASSERT_TRUE(grid.setLethalAt(x, 0.51));
}
const MotionBlockResult r = checkMotion(0.0, 0.0, 0.0, 0.4, 0.0, bodyParams(), *grid.costmap());
EXPECT_FALSE(r.blocked) << "blocked at " << r.distance << " m";
EXPECT_GE(r.baseline_cells, 1);
}
TEST(MotionCheck, LethalUnderTheBodyStillBlocksWhenTheArcAddsNewCells)
{
// Khối 3 hàng x 9 cột ở hông trái-sau (y=0.51/0.56/0.61, x từ -0.6 tới -0.2). Hàng trong cùng là
// baseline; rẽ phải làm góc trái-sau văng ra ngoài lên hai hàng còn lại -> ô MỚI -> vẫn chặn, và
// ở khoảng cách thật (> mẫu đầu 0.05 m) chứ không phải "0.05 m" máy móc.
nav_test_harness::FakeCostmap grid = makeGrid();
for (double x = -0.6; x <= -0.2; x += 0.05)
{
for (double y = 0.51; y <= 0.62; y += 0.05)
{
ASSERT_TRUE(grid.setLethalAt(x, y));
}
}
const MotionBlockResult r = checkMotion(0.0, 0.0, 0.0, 0.4, -0.1, bodyParams(), *grid.costmap());
ASSERT_TRUE(r.blocked);
EXPECT_GT(r.distance, 0.05);
EXPECT_LT(r.distance, 0.5);
EXPECT_GE(r.baseline_cells, 1);
// Chi tiết debug: chạm bằng cạnh TRÁI, ô chạm nằm bên trái phía sau tâm, baseline liệt kê ô bên trái.
EXPECT_EQ(r.hit_edge, 2);
EXPECT_GT(r.hit_y, 0.4);
EXPECT_LT(r.hit_x, 0.0);
ASSERT_GE(r.baseline_listed, 1);
EXPECT_GT(r.baseline_xy[0][1], 0.4);
}
TEST(MotionCheck, LethalAheadOfTheNoseIsUnaffectedByBaseline)
{
// Có người kề hông (baseline) VÀ tường trước mũi: tường là ô mới, phải chặn y như không có baseline.
nav_test_harness::FakeCostmap grid = makeGrid();
ASSERT_TRUE(grid.setLethalAt(0.0, 0.51));
for (double y = -0.6; y <= 0.6; y += 0.05)
{
ASSERT_TRUE(grid.setLethalAt(1.2, y));
}
const MotionBlockResult r = checkMotion(0.0, 0.0, 0.0, 0.4, 0.0, bodyParams(), *grid.costmap());
ASSERT_TRUE(r.blocked);
EXPECT_GT(r.distance, 0.4);
EXPECT_LT(r.distance, 0.8);
EXPECT_GE(r.baseline_cells, 1);
}
TEST(MotionCheck, LookaheadAngleTruncatesTheArc)
{
// R = v/omega = 0.1 m: 0.8 m cung là quay 8 rad. Cắt ở 1.0 rad -> chỉ còn 0.1 m cung = 2 mẫu;
// không cắt (pi) -> 0.31 m = 6 mẫu. Không có vật cản: chỉ đo số mẫu.
nav_test_harness::FakeCostmap grid = makeGrid();
MotionCheckParams p = bodyParams();
p.lookahead_angle = 1.0;
const MotionBlockResult cut = checkMotion(0.0, 0.0, 0.0, 0.4, 4.0, p, *grid.costmap());
EXPECT_FALSE(cut.blocked);
EXPECT_EQ(cut.scanned, 2);
p.lookahead_angle = M_PI;
const MotionBlockResult full = checkMotion(0.0, 0.0, 0.0, 0.4, 4.0, p, *grid.costmap());
EXPECT_FALSE(full.blocked);
EXPECT_GE(full.scanned, 6);
}
TEST(MotionCheck, BaselineDisabledKeepsTheOldVerdict)
{
// Đường lùi: tắt cờ thì ca "người kề hông + đi thẳng" lại bị chặn ngay mẫu đầu như trước 4/9.
nav_test_harness::FakeCostmap grid = makeGrid();
for (double x = -0.2; x <= 0.3; x += 0.05)
{
ASSERT_TRUE(grid.setLethalAt(x, 0.51));
}
MotionCheckParams p = bodyParams();
p.exclude_baseline = false;
const MotionBlockResult r = checkMotion(0.0, 0.0, 0.0, 0.4, 0.0, p, *grid.costmap());
ASSERT_TRUE(r.blocked);
EXPECT_LE(r.distance, 0.1);
EXPECT_EQ(r.baseline_cells, 0);
}
TEST(MotionCheck, ArcCurvingAwayFromTheWallIsClear)
{
// Tường ngay trước mặt tại x=1.0 nhưng lệnh rẽ gắt trái (kappa = 1.5/0.3 = 5 1/m, R=0.2 m):
// cung quay đi trước khi mũi tới tường. Gate quỹ đạo giấy cũng nói vậy — và đó là hành vi ĐÚNG
// của phép đo này: nó đo lệnh thật, không đoán.
nav_test_harness::FakeCostmap grid = makeGrid();
for (double y = -0.3; y <= 0.3; y += 0.05)
{
ASSERT_TRUE(grid.setLethalAt(1.0, y));
}
MotionCheckParams p = bodyParams();
p.side_offset = 0.0; // chỉ tâm/mũi/đuôi để test hình học cung, không dính probe hông
const MotionBlockResult r = checkMotion(0.0, 0.0, 0.0, 0.3, 1.5, p, *grid.costmap());
EXPECT_FALSE(r.blocked);
}
TEST(MotionCheck, ThinPillarAtTheFrontCornerIsCaught)
{
// Trụ mảnh (một ô) tại x=1.0, y=+0.30: nằm giữa đường quét mũi (ngang 0) và đường quét hông
// (ngang 0.53) của bản 5 điểm — bản đó không probe nào chạm, robot ôm trụ bằng góc trước-trái
// (đo 4/9: near=0.36 m @ +70°). Chu vi bắt được ở mẫu mà cạnh mũi đi qua trụ.
nav_test_harness::FakeCostmap grid = makeGrid();
ASSERT_TRUE(grid.setLethalAt(1.0, 0.30));
const MotionBlockResult r = checkMotion(0.0, 0.0, 0.0, 0.4, 0.0, bodyParams(), *grid.costmap());
ASSERT_TRUE(r.blocked);
// Cạnh mũi ở 0.58 + 0.05 = 0.63 trước tâm -> chạm khi tâm đi được ~0.37 m.
EXPECT_NEAR(r.distance, 1.0 - 0.63, 0.06);
}
TEST(MotionCheck, PillarBetweenTwoWheelsIsCaughtByTheFrontEdge)
{
// Hai trụ cách nhau 0.45 m, robot chui giữa: bản 5 điểm để lọt cả hai; chu vi mũi chạm ngay.
nav_test_harness::FakeCostmap grid = makeGrid();
ASSERT_TRUE(grid.setLethalAt(1.2, 0.225));
ASSERT_TRUE(grid.setLethalAt(1.2, -0.225));
const MotionBlockResult r = checkMotion(0.0, 0.0, 0.0, 0.4, 0.0, bodyParams(), *grid.costmap());
EXPECT_TRUE(r.blocked);
}
TEST(MotionCheck, SideProbeCatchesAWallBesideTheArc)
{
// Tường dọc bên trái tại y=+0.50, robot đi thẳng: chu vi với nửa rộng 0.48 + 0.05 = 0.53 chạm;
// thu side_offset về 0 (dải rộng 2 x padding) thì không.
nav_test_harness::FakeCostmap grid = makeGrid();
for (double x = 0.0; x <= 1.5; x += 0.05)
{
ASSERT_TRUE(grid.setLethalAt(x, 0.50));
}
MotionCheckParams p = bodyParams();
const MotionBlockResult with_side = checkMotion(0.0, 0.0, 0.0, 0.4, 0.0, p, *grid.costmap());
EXPECT_TRUE(with_side.blocked);
p.side_offset = 0.0;
const MotionBlockResult no_side = checkMotion(0.0, 0.0, 0.0, 0.4, 0.0, p, *grid.costmap());
EXPECT_FALSE(no_side.blocked);
}
TEST(MotionCheck, StandingStillScansNothing)
{
nav_test_harness::FakeCostmap grid = makeGrid();
ASSERT_TRUE(grid.setLethalAt(0.3, 0.0));
const MotionBlockResult r = checkMotion(0.0, 0.0, 0.0, 0.0, 0.5, bodyParams(), *grid.costmap());
EXPECT_FALSE(r.blocked);
EXPECT_EQ(r.scanned, 0);
}
TEST(MotionCheck, ReverseMotionProbesBehind)
{
// Tường SAU lưng tại x=-1.2, lệnh lùi: đuôi (0.58+0.05) chạm trước khi tâm tới.
nav_test_harness::FakeCostmap grid = makeGrid();
for (double y = -0.6; y <= 0.6; y += 0.05)
{
ASSERT_TRUE(grid.setLethalAt(-1.2, y));
}
const MotionBlockResult fwd = checkMotion(0.0, 0.0, 0.0, 0.4, 0.0, bodyParams(), *grid.costmap());
EXPECT_FALSE(fwd.blocked);
const MotionBlockResult rev = checkMotion(0.0, 0.0, 0.0, -0.4, 0.0, bodyParams(), *grid.costmap());
ASSERT_TRUE(rev.blocked);
EXPECT_LT(rev.distance, 0.8);
}
TEST(MotionCheck, UnknownCellsAreNotBlocked)
{
nav_test_harness::FakeCostmap grid = makeGrid();
for (double x = 0.0; x <= 2.0; x += 0.05)
{
ASSERT_TRUE(grid.setCostAt(x, 0.0, robot_costmap_2d::NO_INFORMATION));
}
const MotionBlockResult r = checkMotion(0.0, 0.0, 0.0, 0.4, 0.0, bodyParams(), *grid.costmap());
EXPECT_FALSE(r.blocked);
}
} // namespace