283 lines
9.4 KiB
C++
283 lines
9.4 KiB
C++
/*********************************************************************
|
|
*
|
|
* 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::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, 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();
|
|
}
|