802 lines
32 KiB
C++
802 lines
32 KiB
C++
/*********************************************************************
|
|
*
|
|
* Software License Agreement (BSD License)
|
|
*
|
|
* Kiểm DetourPathRecovery: chọn điểm nối, splice với plan cũ, thử-lại theo ứng viên, quy đổi frame
|
|
* qua TF, và các đường từ chối. Planner là plugin giả (TestLinePlanner) nạp qua đúng đường
|
|
* Boost.DLL mà runtime đi; costmap là placeholder — hai test seam `lock_costmap` và
|
|
* `costmap_frame_override` giữ cho không có dereference nào chạm vào nó.
|
|
*
|
|
* Author: DuongTD
|
|
*********************************************************************/
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <cmath>
|
|
#include <cstdint>
|
|
#include <cstdlib>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include <robot/node_handle.h>
|
|
#include <robot_costmap_2d/cost_values.h>
|
|
#include <robot_costmap_2d/costmap_2d.h>
|
|
#include <tf3/buffer_core.h>
|
|
|
|
#include <recovery_core/recovery_registry.h>
|
|
|
|
#include <sbpl_recovery/merge_grids.h>
|
|
|
|
namespace
|
|
{
|
|
|
|
using recovery_core::RecoveryBehavior;
|
|
using recovery_core::RecoveryContext;
|
|
using recovery_core::RecoveryGoal;
|
|
using recovery_core::RecoveryRegistry;
|
|
using recovery_core::RecoveryResult;
|
|
using recovery_core::RecoveryStatus;
|
|
using recovery_core::RecoveryTrigger;
|
|
|
|
/// Costmap placeholder: địa chỉ hợp lệ nhưng KHÔNG phải costmap — mọi đường dereference đã bị tắt
|
|
/// qua test seam. Cùng khuôn với planner_runner_test của move_base2.
|
|
robot_costmap_2d::Costmap2DROBOT* dummyCostmap()
|
|
{
|
|
static std::uintptr_t placeholder = 0;
|
|
return reinterpret_cast<robot_costmap_2d::Costmap2DROBOT*>(&placeholder);
|
|
}
|
|
|
|
class FakePoseProvider final : public recovery_core::PoseProvider
|
|
{
|
|
public:
|
|
bool ok = true;
|
|
robot_geometry_msgs::PoseStamped pose;
|
|
|
|
bool getRobotPose(robot_geometry_msgs::PoseStamped& out) const override
|
|
{
|
|
if (!ok)
|
|
{
|
|
return false;
|
|
}
|
|
out = pose;
|
|
return true;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* @class BandCollisionChecker
|
|
* @brief Chặn một dải theo trục x: [from, to] không đặt được robot vào.
|
|
*
|
|
* Mô phỏng đúng tình huống sim: một vật cản nằm CHÍNH TRÊN tuyến, kéo dài vài mét — thứ khiến các
|
|
* ứng viên điểm nối chọn thuần theo khoảng cách đều rơi vào vùng cấm.
|
|
*/
|
|
class BandCollisionChecker final : public recovery_core::CollisionChecker
|
|
{
|
|
public:
|
|
void blockBand(double from_x, double to_x)
|
|
{
|
|
from_x_ = from_x;
|
|
to_x_ = to_x;
|
|
}
|
|
|
|
/// Vật cản THỨ HAI xa hơn trên tuyến — plan cũ không replan nên đuôi nó xuyên vật cản này mãi.
|
|
void blockSecondBand(double from_x, double to_x)
|
|
{
|
|
second_from_x_ = from_x;
|
|
second_to_x_ = to_x;
|
|
}
|
|
|
|
/// Dải "chưa quan sát": costmap local lăn theo robot nên đây là phần lớn tuyến phía trước.
|
|
void unknownBand(double from_x, double to_x)
|
|
{
|
|
unknown_from_x_ = from_x;
|
|
unknown_to_x_ = to_x;
|
|
}
|
|
|
|
/// Rìa lưới costmap: từ x này trở đi pose nằm NGOÀI lưới — quét phải dừng, không phải "chắn".
|
|
void outsideFrom(double from_x)
|
|
{
|
|
outside_from_x_ = from_x;
|
|
}
|
|
|
|
double footprintCost(double x, double y, double theta) const override
|
|
{
|
|
(void)y;
|
|
(void)theta;
|
|
++query_count_;
|
|
if ((x >= from_x_ && x <= to_x_) || (x >= second_from_x_ && x <= second_to_x_))
|
|
{
|
|
return -1.0;
|
|
}
|
|
return (x >= unknown_from_x_ && x <= unknown_to_x_) ? -3.0 : 0.0;
|
|
}
|
|
|
|
Placement classify(double x, double y, double theta) const override
|
|
{
|
|
if (x >= outside_from_x_)
|
|
{
|
|
return Placement::kOutside;
|
|
}
|
|
const double cost = footprintCost(x, y, theta);
|
|
if (cost >= 0.0)
|
|
{
|
|
return Placement::kFree;
|
|
}
|
|
return cost == -3.0 ? Placement::kUnknown : Placement::kBlocked;
|
|
}
|
|
|
|
std::size_t queryCount() const
|
|
{
|
|
return query_count_;
|
|
}
|
|
|
|
private:
|
|
double from_x_ = 1.0;
|
|
double to_x_ = 0.0; // dải rỗng: mặc định không chặn gì
|
|
double second_from_x_ = 1.0;
|
|
double second_to_x_ = 0.0; // dải thứ hai, mặc định rỗng
|
|
double unknown_from_x_ = 1.0;
|
|
double unknown_to_x_ = 0.0;
|
|
double outside_from_x_ = 1e9; // mặc định: lưới phủ hết
|
|
mutable std::size_t query_count_ = 0;
|
|
};
|
|
|
|
class FakePlanProvider final : public recovery_core::PlanProvider
|
|
{
|
|
public:
|
|
std::vector<robot_geometry_msgs::PoseStamped> plan;
|
|
|
|
bool getGlobalPlan(std::vector<robot_geometry_msgs::PoseStamped>& out) const override
|
|
{
|
|
if (plan.empty())
|
|
{
|
|
return false;
|
|
}
|
|
out = plan;
|
|
return true;
|
|
}
|
|
};
|
|
|
|
robot_geometry_msgs::PoseStamped makePose(const std::string& frame, double x, double y)
|
|
{
|
|
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: [from_x, to_x] bước @p step, y cố định.
|
|
std::vector<robot_geometry_msgs::PoseStamped> makeLinePlan(const std::string& frame, double from_x,
|
|
double to_x, double step, double y)
|
|
{
|
|
std::vector<robot_geometry_msgs::PoseStamped> plan;
|
|
for (double x = from_x; x <= to_x + 1e-9; x += step)
|
|
{
|
|
plan.push_back(makePose(frame, x, y));
|
|
}
|
|
return plan;
|
|
}
|
|
|
|
RecoveryBehavior* findBehavior(RecoveryRegistry& registry, const std::string& name)
|
|
{
|
|
for (std::size_t i = 0; i < registry.size(); ++i)
|
|
{
|
|
if (registry.nameAt(i) == name)
|
|
{
|
|
return registry.at(i);
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
RecoveryGoal controllingGoal()
|
|
{
|
|
RecoveryGoal goal;
|
|
goal.trigger = RecoveryTrigger::kControllingFailed;
|
|
return goal;
|
|
}
|
|
|
|
/**
|
|
* Fixture: registry thật + plugin thật (nạp qua Boost.DLL) trên cổng giả. TF có sẵn một transform
|
|
* tĩnh map->odom (odom gốc tại (2, 1) trong map) cho các ca đổi frame.
|
|
*/
|
|
class DetourPathRecoveryTest : public ::testing::Test
|
|
{
|
|
protected:
|
|
void SetUp() override
|
|
{
|
|
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; // [m] p_map = p_odom + (2, 1)
|
|
map_to_odom.transform.translation.y = 1.0; // [m]
|
|
map_to_odom.transform.rotation.w = 1.0;
|
|
ASSERT_TRUE(tf_.setTransform(map_to_odom, "test", /*is_static=*/true));
|
|
|
|
ctx_.pose = &pose_;
|
|
ctx_.plan = &plan_;
|
|
// Tuyến tham chiếu để RỖNG mặc định: phần lớn test kiểm hình học trên plan hiện hành, và
|
|
// behavior phải tự quay về `plan` khi tham chiếu chưa có. Test nào cần thì tự nạp.
|
|
ctx_.reference_plan = &reference_plan_;
|
|
ctx_.collision = &collision_;
|
|
ctx_.local_costmap = dummyCostmap();
|
|
ctx_.tf = &tf_;
|
|
|
|
robot::NodeHandle nh;
|
|
ASSERT_TRUE(registry_.loadFromConfig(nh, "recovery", ctx_));
|
|
ASSERT_EQ(registry_.size(), 6u);
|
|
}
|
|
|
|
FakePoseProvider pose_;
|
|
FakePlanProvider plan_;
|
|
FakePlanProvider reference_plan_;
|
|
BandCollisionChecker collision_;
|
|
tf3::BufferCore tf_;
|
|
RecoveryContext ctx_;
|
|
RecoveryRegistry registry_;
|
|
robot::Time t0_{100, 0};
|
|
};
|
|
|
|
TEST_F(DetourPathRecoveryTest, StartRefusesWithoutAPlan)
|
|
{
|
|
RecoveryBehavior* detour = findBehavior(registry_, "detour");
|
|
ASSERT_NE(detour, nullptr);
|
|
|
|
pose_.pose = makePose("odom", 0.0, 0.0);
|
|
plan_.plan.clear(); // Không có plan thì không có gì để nối lại.
|
|
|
|
EXPECT_FALSE(detour->start(controllingGoal(), t0_));
|
|
}
|
|
|
|
TEST_F(DetourPathRecoveryTest, SplicesTheDetourOntoTheRemainder)
|
|
{
|
|
RecoveryBehavior* detour = findBehavior(registry_, "detour");
|
|
ASSERT_NE(detour, nullptr);
|
|
|
|
// Plan cùng frame với costmap (odom): không cần quy đổi. Robot ở gốc, plan 0..5 m bước 0.5.
|
|
pose_.pose = makePose("odom", 0.0, 0.0);
|
|
plan_.plan = makeLinePlan("odom", 0.0, 5.0, 0.5, 0.0);
|
|
|
|
ASSERT_TRUE(detour->start(controllingGoal(), t0_));
|
|
const RecoveryResult result = detour->update(t0_);
|
|
|
|
ASSERT_EQ(result.status, RecoveryStatus::kSucceeded);
|
|
const robot_nav_msgs::Path* path = result.pathOut();
|
|
ASSERT_NE(path, nullptr);
|
|
EXPECT_EQ(path->header.frame_id, "odom");
|
|
|
|
// Ứng viên đầu tiên: pose đầu cách robot >= planning_distance (1.0 m) => x = 1.0 (index 2).
|
|
// Đường vòng 5 điểm (0,0)->(1,0) + phần còn lại của plan x = 1.5..5.0 (8 pose) = 13 pose.
|
|
ASSERT_EQ(path->poses.size(), 13u);
|
|
EXPECT_NEAR(path->poses.front().pose.position.x, 0.0, 1e-9);
|
|
EXPECT_NEAR(path->poses[4].pose.position.x, 1.0, 1e-9); // cuối đường vòng = điểm nối
|
|
EXPECT_NEAR(path->poses[5].pose.position.x, 1.5, 1e-9); // pose đầu của phần plan cũ
|
|
EXPECT_NEAR(path->poses.back().pose.position.x, 5.0, 1e-9);
|
|
}
|
|
|
|
TEST_F(DetourPathRecoveryTest, FlakyPlannerRetriesUntilACandidateWorks)
|
|
{
|
|
RecoveryBehavior* detour = findBehavior(registry_, "detour_flaky");
|
|
ASSERT_NE(detour, nullptr);
|
|
|
|
pose_.pose = makePose("odom", 0.0, 0.0);
|
|
plan_.plan = makeLinePlan("odom", 0.0, 5.0, 0.5, 0.0);
|
|
|
|
ASSERT_TRUE(detour->start(controllingGoal(), t0_));
|
|
|
|
// Hai ứng viên đầu (x = 1.0, 1.5) bị planner giả từ chối; ứng viên ba (x = 2.0) thành công.
|
|
EXPECT_EQ(detour->update(t0_).status, RecoveryStatus::kRunning);
|
|
EXPECT_EQ(detour->update(t0_).status, RecoveryStatus::kRunning);
|
|
|
|
const RecoveryResult result = detour->update(t0_);
|
|
ASSERT_EQ(result.status, RecoveryStatus::kSucceeded);
|
|
const robot_nav_msgs::Path* path = result.pathOut();
|
|
ASSERT_NE(path, nullptr);
|
|
|
|
// Cuối đường vòng phải là điểm nối thứ BA — bằng chứng cursor ứng viên có tiến.
|
|
EXPECT_NEAR(path->poses[4].pose.position.x, 2.0, 1e-9);
|
|
EXPECT_NEAR(path->poses[5].pose.position.x, 2.5, 1e-9);
|
|
}
|
|
|
|
TEST_F(DetourPathRecoveryTest, FailsAfterAllCandidates)
|
|
{
|
|
RecoveryBehavior* detour = findBehavior(registry_, "detour_dead");
|
|
ASSERT_NE(detour, nullptr);
|
|
|
|
pose_.pose = makePose("odom", 0.0, 0.0);
|
|
plan_.plan = makeLinePlan("odom", 0.0, 5.0, 0.5, 0.0);
|
|
|
|
ASSERT_TRUE(detour->start(controllingGoal(), t0_));
|
|
|
|
EXPECT_EQ(detour->update(t0_).status, RecoveryStatus::kRunning);
|
|
EXPECT_EQ(detour->update(t0_).status, RecoveryStatus::kRunning);
|
|
EXPECT_EQ(detour->update(t0_).status, RecoveryStatus::kRunning);
|
|
|
|
// Hết attempts_per_run (3) ứng viên: kFailed để state machine chuyển behavior kế tiếp.
|
|
EXPECT_EQ(detour->update(t0_).status, RecoveryStatus::kFailed);
|
|
}
|
|
|
|
TEST_F(DetourPathRecoveryTest, ConvertsFramesThroughTf)
|
|
{
|
|
RecoveryBehavior* detour = findBehavior(registry_, "detour_map");
|
|
ASSERT_NE(detour, nullptr);
|
|
|
|
// Plan ở MAP, costmap ở ODOM (odom gốc tại map (2,1)). Robot đứng ở odom (0,0) = map (2,1).
|
|
pose_.pose = makePose("odom", 0.0, 0.0);
|
|
plan_.plan = makeLinePlan("map", 2.0, 7.0, 0.5, 1.0);
|
|
|
|
ASSERT_TRUE(detour->start(controllingGoal(), t0_));
|
|
const RecoveryResult result = detour->update(t0_);
|
|
|
|
ASSERT_EQ(result.status, RecoveryStatus::kSucceeded);
|
|
const robot_nav_msgs::Path* path = result.pathOut();
|
|
ASSERT_NE(path, nullptr);
|
|
|
|
// Kết quả phải nằm TRỌN trong frame của plan: đường vòng (planner trả ở odom) đã được quy về map.
|
|
EXPECT_EQ(path->header.frame_id, "map");
|
|
EXPECT_NEAR(path->poses.front().pose.position.x, 2.0, 1e-9); // robot tại map (2,1)
|
|
EXPECT_NEAR(path->poses.front().pose.position.y, 1.0, 1e-9);
|
|
EXPECT_NEAR(path->poses[4].pose.position.x, 3.0, 1e-9); // điểm nối map x=3.0
|
|
EXPECT_NEAR(path->poses[5].pose.position.x, 3.5, 1e-9); // plan cũ tiếp tục
|
|
EXPECT_NEAR(path->poses.back().pose.position.x, 7.0, 1e-9);
|
|
EXPECT_NEAR(path->poses.back().pose.position.y, 1.0, 1e-9);
|
|
}
|
|
|
|
TEST_F(DetourPathRecoveryTest, RejoinPointsSkipPastTheBlockedStretch)
|
|
{
|
|
// Lỗi sim 2026-08-05: ứng viên chọn thuần theo khoảng cách đều rơi vào chính vật cản đang chắn
|
|
// tuyến, nên cả ba lượt SBPL đều "Solution not found" — mà mỗi lượt hỏng vẫn giữ mutex costmap.
|
|
// Đúng: bỏ qua cả dải bẩn, điểm nối đầu tiên phải nằm sau nó + rejoin_clearance.
|
|
RecoveryBehavior* detour = findBehavior(registry_, "detour");
|
|
ASSERT_NE(detour, nullptr);
|
|
|
|
pose_.pose = makePose("odom", 0.0, 0.0);
|
|
plan_.plan = makeLinePlan("odom", 0.0, 6.0, 0.5, 0.0);
|
|
collision_.blockBand(1.0, 2.4); // [m] vật cản trải dài trên tuyến
|
|
|
|
ASSERT_TRUE(detour->start(controllingGoal(), t0_));
|
|
const RecoveryResult result = detour->update(t0_);
|
|
|
|
ASSERT_EQ(result.status, RecoveryStatus::kSucceeded);
|
|
const robot_nav_msgs::Path* path = result.pathOut();
|
|
ASSERT_NE(path, nullptr);
|
|
|
|
// Đường vòng 5 điểm; điểm cuối của nó là điểm nối. Pose bẩn cuối cùng trên plan là x = 2.0
|
|
// (2.5 đã ra ngoài dải), cộng `rejoin_clearance` 0.5 -> ứng viên đầu tiên đạt là x = 2.5.
|
|
// KHÔNG phải x = 1.0 như luật chọn thuần-khoảng-cách cũ, vốn rơi thẳng vào vật cản.
|
|
EXPECT_NEAR(path->poses[4].pose.position.x, 2.5, 1e-9);
|
|
EXPECT_NEAR(path->poses[5].pose.position.x, 3.0, 1e-9);
|
|
EXPECT_NEAR(path->poses.back().pose.position.x, 6.0, 1e-9);
|
|
}
|
|
|
|
TEST_F(DetourPathRecoveryTest, PosesBeyondReachStopTheScanInsteadOfCountingAsBlocked)
|
|
{
|
|
// Lỗi sim 2026-08-05 (lượt 2): pose ngoài cửa sổ costmap bị đọc thành "bẩn" — plan dài vài chục
|
|
// mét trên cửa sổ 8x8 m nên phần xa làm mọi ứng viên bị loại. Ngữ nghĩa đúng: `kOutside` là HẾT
|
|
// TẦM QUÉT — dừng lại, không phải vật cản.
|
|
//
|
|
// Mô phỏng: rìa lưới ở 3.5 m, plan dài 20 m, KHÔNG có vật cản thật trong lưới. Ứng viên phải
|
|
// chọn bình thường trong tầm, không bị phần ngoài lưới làm hỏng.
|
|
RecoveryBehavior* detour = findBehavior(registry_, "detour");
|
|
ASSERT_NE(detour, nullptr);
|
|
|
|
pose_.pose = makePose("odom", 0.0, 0.0);
|
|
plan_.plan = makeLinePlan("odom", 0.0, 20.0, 0.5, 0.0);
|
|
collision_.outsideFrom(3.5); // rìa lưới: quét dừng ở đây, không phải "chắn"
|
|
|
|
ASSERT_TRUE(detour->start(controllingGoal(), t0_));
|
|
const RecoveryResult result = detour->update(t0_);
|
|
|
|
ASSERT_EQ(result.status, RecoveryStatus::kSucceeded);
|
|
const robot_nav_msgs::Path* path = result.pathOut();
|
|
ASSERT_NE(path, nullptr);
|
|
|
|
// Ứng viên đầu tiên vẫn là x = 1.0 (planning_distance của instance `detour`), không phải một pose
|
|
// nào đó bị phần ngoài lưới đẩy đi.
|
|
EXPECT_NEAR(path->poses[4].pose.position.x, 1.0, 1e-9);
|
|
EXPECT_NEAR(path->poses.back().pose.position.x, 20.0, 1e-9);
|
|
}
|
|
|
|
TEST_F(DetourPathRecoveryTest, RejoinsOntoTheReferenceRouteNotTheDriftedPlan)
|
|
{
|
|
// Contract 2026-08-05e: điểm nối nằm trên TUYẾN GỐC của chặng, không phải plan đang bám. Nối vào
|
|
// plan đang bám thì mỗi lượt detour lấy kết quả lượt trước làm mốc — sim đo tuyến bị ăn mòn dần
|
|
// (rejoin 106/305 -> 130/289 -> 132/273) và robot không bao giờ quay về tuyến order.
|
|
RecoveryBehavior* detour = findBehavior(registry_, "detour");
|
|
ASSERT_NE(detour, nullptr);
|
|
|
|
pose_.pose = makePose("odom", 0.0, 0.0);
|
|
// Plan ĐANG BÁM đã trôi sang y = 3.0 sau vài lượt detour trước đó.
|
|
plan_.plan = makeLinePlan("odom", 0.0, 6.0, 0.5, 3.0);
|
|
// Tuyến GỐC vẫn là tuyến order ở y = 0.0.
|
|
reference_plan_.plan = makeLinePlan("odom", 0.0, 6.0, 0.5, 0.0);
|
|
|
|
ASSERT_TRUE(detour->start(controllingGoal(), t0_));
|
|
const RecoveryResult result = detour->update(t0_);
|
|
|
|
ASSERT_EQ(result.status, RecoveryStatus::kSucceeded);
|
|
const robot_nav_msgs::Path* path = result.pathOut();
|
|
ASSERT_NE(path, nullptr);
|
|
|
|
// Đuôi splice phải là đuôi TUYẾN GỐC (y = 0), không phải plan đã trôi (y = 3): đó chính là thứ
|
|
// kéo robot trở lại tuyến fleet đã duyệt.
|
|
EXPECT_NEAR(path->poses.back().pose.position.y, 0.0, 1e-9);
|
|
EXPECT_NEAR(path->poses.back().pose.position.x, 6.0, 1e-9);
|
|
}
|
|
|
|
TEST_F(DetourPathRecoveryTest, FallsBackToTheCurrentPlanWhenNoReferenceRouteExists)
|
|
{
|
|
// Runtime chưa lập được plan nào (hoặc không cấp tuyến tham chiếu): vẫn phải vòng được, chỉ mất
|
|
// khả năng tự về tuyến. Im lặng bỏ cuộc thì tệ hơn nhiều.
|
|
RecoveryBehavior* detour = findBehavior(registry_, "detour");
|
|
ASSERT_NE(detour, nullptr);
|
|
|
|
pose_.pose = makePose("odom", 0.0, 0.0);
|
|
plan_.plan = makeLinePlan("odom", 0.0, 6.0, 0.5, 3.0);
|
|
reference_plan_.plan.clear(); // chưa có tuyến gốc
|
|
|
|
ASSERT_TRUE(detour->start(controllingGoal(), t0_));
|
|
const RecoveryResult result = detour->update(t0_);
|
|
|
|
ASSERT_EQ(result.status, RecoveryStatus::kSucceeded);
|
|
const robot_nav_msgs::Path* path = result.pathOut();
|
|
ASSERT_NE(path, nullptr);
|
|
EXPECT_NEAR(path->poses.back().pose.position.y, 3.0, 1e-9); // đuôi của plan đang bám
|
|
}
|
|
|
|
TEST_F(DetourPathRecoveryTest, RejoinsPastEveryVisibleObstacleNotJustTheFirst)
|
|
{
|
|
// Luật chốt 2026-08-05f: điểm nối phải qua HẾT phần chắn còn NHÌN THẤY ĐƯỢC. Hai luật trước đều
|
|
// sai ở PHẠM VI:
|
|
// - "sau pose bẩn cuối toàn tầm quét": đuôi plan không bao giờ được replan nên nó cắt qua vật
|
|
// cản ở xa; vết bẩn ở 7.96 m giết sạch ứng viên hợp lệ ngay sau vật cản đang chắn ở 2.5 m ->
|
|
// refuse -> ABORT (log sim 11:00);
|
|
// - "sau cụm chắn ĐẦU TIÊN": nối vào khe giữa hai vật cản cùng nhìn thấy, robot chạy ~0.5 m rồi
|
|
// bị chắn lại -> detour lặp vô ích quanh đúng vật cản thứ nhất.
|
|
RecoveryBehavior* detour = findBehavior(registry_, "detour");
|
|
ASSERT_NE(detour, nullptr);
|
|
|
|
pose_.pose = makePose("odom", 0.0, 0.0);
|
|
plan_.plan = makeLinePlan("odom", 0.0, 10.0, 0.5, 0.0);
|
|
collision_.blockBand(1.0, 2.0); // vật cản đang chắn tuyến
|
|
collision_.blockSecondBand(4.0, 4.5); // vật cản thứ hai, CÙNG nhìn thấy -> phải vòng qua luôn
|
|
|
|
ASSERT_TRUE(detour->start(controllingGoal(), t0_));
|
|
const RecoveryResult result = detour->update(t0_);
|
|
|
|
ASSERT_EQ(result.status, RecoveryStatus::kSucceeded);
|
|
const robot_nav_msgs::Path* path = result.pathOut();
|
|
ASSERT_NE(path, nullptr);
|
|
|
|
// Pose bẩn cuối nhìn thấy được là x = 4.5; + rejoin_clearance 0.5 -> ứng viên đầu tiên x = 5.0.
|
|
// KHÔNG phải x = 2.5 trong khe giữa hai vật cản như luật "cụm đầu tiên".
|
|
EXPECT_NEAR(path->poses[4].pose.position.x, 5.0, 1e-9);
|
|
}
|
|
|
|
TEST_F(DetourPathRecoveryTest, NarrowGapBetweenObstaclesIsSkipped)
|
|
{
|
|
// Clearance tính CẢ HAI PHÍA: khoảng sạch giữa hai vật cản mà hẹp hơn 2*rejoin_clearance thì
|
|
// điểm nối trong đó đặt robot sát mép vật cản kế tiếp — monitor sẽ bắn lại ngay sau một nhịp.
|
|
// Đúng: nhảy qua, nối sau cụm thứ hai.
|
|
RecoveryBehavior* detour = findBehavior(registry_, "detour");
|
|
ASSERT_NE(detour, nullptr);
|
|
|
|
pose_.pose = makePose("odom", 0.0, 0.0);
|
|
plan_.plan = makeLinePlan("odom", 0.0, 10.0, 0.5, 0.0);
|
|
collision_.blockBand(1.0, 2.0);
|
|
collision_.blockSecondBand(2.5, 3.0); // khoảng giữa không còn pose sạch đủ clearance hai phía
|
|
|
|
ASSERT_TRUE(detour->start(controllingGoal(), t0_));
|
|
const RecoveryResult result = detour->update(t0_);
|
|
|
|
ASSERT_EQ(result.status, RecoveryStatus::kSucceeded);
|
|
const robot_nav_msgs::Path* path = result.pathOut();
|
|
ASSERT_NE(path, nullptr);
|
|
|
|
// Sau cụm thứ hai (x = 3.0) + clearance 0.5 -> ứng viên đầu tiên x = 3.5.
|
|
EXPECT_NEAR(path->poses[4].pose.position.x, 3.5, 1e-9);
|
|
}
|
|
|
|
TEST_F(DetourPathRecoveryTest, UnobservedSpaceIsNotAnObstacle)
|
|
{
|
|
// Lỗi sim 2026-08-05 (lượt 3): costmap local lăn theo robot nên mọi ô ngoài tầm sensor là
|
|
// NO_INFORMATION, và `footprintCost` trả âm cho cả ca đó. Detour đọc gần hết tuyến phía trước
|
|
// thành vật cản -> "no placeable rejoin point" -> từ chối mọi lượt.
|
|
//
|
|
// Phải khớp hai bên còn lại: `checkPlan` bỏ qua ô unknown, SBPL chạy `allow_unknown: true`.
|
|
RecoveryBehavior* detour = findBehavior(registry_, "detour");
|
|
ASSERT_NE(detour, nullptr);
|
|
|
|
pose_.pose = makePose("odom", 0.0, 0.0);
|
|
plan_.plan = makeLinePlan("odom", 0.0, 20.0, 0.5, 0.0);
|
|
collision_.unknownBand(0.6, 100.0); // gần như toàn bộ tuyến phía trước là "chưa quan sát"
|
|
|
|
ASSERT_TRUE(detour->start(controllingGoal(), t0_));
|
|
const RecoveryResult result = detour->update(t0_);
|
|
|
|
ASSERT_EQ(result.status, RecoveryStatus::kSucceeded);
|
|
ASSERT_NE(result.pathOut(), nullptr);
|
|
EXPECT_NEAR(result.pathOut()->poses[4].pose.position.x, 1.0, 1e-9);
|
|
}
|
|
|
|
TEST_F(DetourPathRecoveryTest, RealObstacleStillBlocksEvenAmidUnobservedSpace)
|
|
{
|
|
// Nới unknown KHÔNG được nới luôn vật cản thật: vật cản nằm giữa vùng chưa quan sát vẫn phải đẩy
|
|
// điểm nối ra sau nó.
|
|
RecoveryBehavior* detour = findBehavior(registry_, "detour");
|
|
ASSERT_NE(detour, nullptr);
|
|
|
|
pose_.pose = makePose("odom", 0.0, 0.0);
|
|
plan_.plan = makeLinePlan("odom", 0.0, 20.0, 0.5, 0.0);
|
|
collision_.unknownBand(0.6, 100.0);
|
|
collision_.blockBand(1.0, 1.6); // vật cản THẬT nằm trong vùng unknown
|
|
|
|
ASSERT_TRUE(detour->start(controllingGoal(), t0_));
|
|
const RecoveryResult result = detour->update(t0_);
|
|
|
|
ASSERT_EQ(result.status, RecoveryStatus::kSucceeded);
|
|
ASSERT_NE(result.pathOut(), nullptr);
|
|
// Pose bẩn cuối là x = 1.5; cộng rejoin_clearance 0.5 -> điểm nối đầu tiên là x = 2.0.
|
|
EXPECT_NEAR(result.pathOut()->poses[4].pose.position.x, 2.0, 1e-9);
|
|
}
|
|
|
|
TEST_F(DetourPathRecoveryTest, RefusesWhenTheOnlyCleanPosesAreBeforeTheObstacle)
|
|
{
|
|
// Đường lùi ghi TRƯỚC chỗ chắn phải bị vứt: nối lại vào pose nằm trước vật cản là đưa robot về
|
|
// đúng chỗ vừa bị chặn, rồi monitor bắn lại ngay lượt sau và cạn route.
|
|
RecoveryBehavior* detour = findBehavior(registry_, "detour");
|
|
ASSERT_NE(detour, nullptr);
|
|
|
|
pose_.pose = makePose("odom", 0.0, 0.0);
|
|
plan_.plan = makeLinePlan("odom", 0.0, 20.0, 0.5, 0.0);
|
|
// Sạch tới 1.4 m rồi chắn hết phần còn lại TRONG TẦM QUÉT (3.0 m).
|
|
collision_.blockBand(1.5, 100.0);
|
|
|
|
EXPECT_FALSE(detour->start(controllingGoal(), t0_));
|
|
}
|
|
|
|
TEST_F(DetourPathRecoveryTest, RefusesToStartWhenNoRejoinPointIsPlaceable)
|
|
{
|
|
// Toàn bộ phần còn lại của tuyến bị chắn: từ chối NGAY. Chạy đủ attempts_per_run lượt SBPL chắc
|
|
// chắn hỏng chỉ để tới cùng kết luận, trong khi mỗi lượt giữ mutex costmap trọn allocated_time —
|
|
// đó chính là thứ làm đói thread cập nhật costmap trên sim.
|
|
RecoveryBehavior* detour = findBehavior(registry_, "detour");
|
|
ASSERT_NE(detour, nullptr);
|
|
|
|
pose_.pose = makePose("odom", 0.0, 0.0);
|
|
plan_.plan = makeLinePlan("odom", 0.0, 5.0, 0.5, 0.0);
|
|
collision_.blockBand(0.5, 100.0);
|
|
|
|
EXPECT_FALSE(detour->start(controllingGoal(), t0_));
|
|
}
|
|
|
|
TEST_F(DetourPathRecoveryTest, NoCollisionCheckerFallsBackToDistanceOnlyCandidates)
|
|
{
|
|
// Context không có collision checker: "không biết" không được biến thành "loại hết" — hành vi
|
|
// phải y như trước, planner là bên phán quyết cuối.
|
|
FakePoseProvider pose;
|
|
FakePlanProvider plan;
|
|
RecoveryContext ctx;
|
|
ctx.pose = &pose;
|
|
ctx.plan = &plan;
|
|
ctx.collision = nullptr;
|
|
ctx.local_costmap = dummyCostmap();
|
|
ctx.tf = &tf_;
|
|
|
|
robot::NodeHandle nh;
|
|
RecoveryRegistry registry;
|
|
ASSERT_TRUE(registry.loadFromConfig(nh, "recovery", ctx));
|
|
|
|
RecoveryBehavior* detour = findBehavior(registry, "detour");
|
|
ASSERT_NE(detour, nullptr);
|
|
|
|
pose.pose = makePose("odom", 0.0, 0.0);
|
|
plan.plan = makeLinePlan("odom", 0.0, 5.0, 0.5, 0.0);
|
|
|
|
ASSERT_TRUE(detour->start(controllingGoal(), t0_));
|
|
const RecoveryResult result = detour->update(t0_);
|
|
ASSERT_EQ(result.status, RecoveryStatus::kSucceeded);
|
|
ASSERT_NE(result.pathOut(), nullptr);
|
|
EXPECT_NEAR(result.pathOut()->poses[4].pose.position.x, 1.0, 1e-9); // ứng viên đầu như cũ
|
|
}
|
|
|
|
TEST_F(DetourPathRecoveryTest, PlannerSymbolMayDifferFromTheInstanceName)
|
|
{
|
|
// Đường mà runtime dùng để cho detour một instance SBPL riêng: `planner_name` là namespace param
|
|
// (`DetourOnlyTuning`, khai `points: 9`), `planner_symbol` là alias có thật trong .so.
|
|
// `libsbpl_lattice_planner` chỉ export một symbol, nên nếu hai thứ này bị buộc phải trùng nhau
|
|
// thì không thể có instance tuning riêng — đúng lỗi làm route path_blocked rỗng trên sim.
|
|
RecoveryBehavior* detour = findBehavior(registry_, "detour_alias");
|
|
ASSERT_NE(detour, nullptr) << "behavior bị loại lúc nạp: planner_symbol không resolve được";
|
|
|
|
pose_.pose = makePose("odom", 0.0, 0.0);
|
|
plan_.plan = makeLinePlan("odom", 0.0, 5.0, 0.5, 0.0);
|
|
|
|
ASSERT_TRUE(detour->start(controllingGoal(), t0_));
|
|
const RecoveryResult result = detour->update(t0_);
|
|
|
|
ASSERT_EQ(result.status, RecoveryStatus::kSucceeded);
|
|
const robot_nav_msgs::Path* path = result.pathOut();
|
|
ASSERT_NE(path, nullptr);
|
|
|
|
// Param đọc từ `DetourOnlyTuning`: đường vòng 9 điểm (mặc định là 5) + 8 pose còn lại của plan.
|
|
EXPECT_EQ(path->poses.size(), 17u);
|
|
EXPECT_NEAR(path->poses[8].pose.position.x, 1.0, 1e-9); // điểm nối
|
|
}
|
|
|
|
TEST_F(DetourPathRecoveryTest, RejectsDetoursThatStrayTooFarFromTheRoute)
|
|
{
|
|
// Guard lệch tuyến: order chỉ định tuyến, robot chỉ được rời khỏi nó trong hành lang fleet cho
|
|
// phép. Planner giả luôn phình 1.0 m, ngưỡng đặt 0.2 m -> mọi ứng viên bị loại, behavior kFailed
|
|
// để route chuyển sang cách xử lý khác.
|
|
RecoveryBehavior* detour = findBehavior(registry_, "detour_narrow");
|
|
ASSERT_NE(detour, nullptr);
|
|
|
|
pose_.pose = makePose("odom", 0.0, 0.0);
|
|
plan_.plan = makeLinePlan("odom", 0.0, 5.0, 0.5, 0.0);
|
|
|
|
ASSERT_TRUE(detour->start(controllingGoal(), t0_));
|
|
|
|
// Ba ứng viên, ứng viên nào cũng lập được đường nhưng đều vượt ngưỡng lệch.
|
|
EXPECT_EQ(detour->update(t0_).status, RecoveryStatus::kRunning);
|
|
EXPECT_EQ(detour->update(t0_).status, RecoveryStatus::kRunning);
|
|
EXPECT_EQ(detour->update(t0_).status, RecoveryStatus::kRunning);
|
|
EXPECT_EQ(detour->update(t0_).status, RecoveryStatus::kFailed);
|
|
}
|
|
|
|
TEST_F(DetourPathRecoveryTest, AcceptsDetoursInsideTheDeviationLimit)
|
|
{
|
|
// Cùng ngưỡng 0.2 m nhưng planner mặc định (`detour`) không phình: đường vòng nằm trên tuyến nên
|
|
// guard không được chặn nhầm.
|
|
RecoveryBehavior* detour = findBehavior(registry_, "detour");
|
|
ASSERT_NE(detour, nullptr);
|
|
|
|
pose_.pose = makePose("odom", 0.0, 0.0);
|
|
plan_.plan = makeLinePlan("odom", 0.0, 5.0, 0.5, 0.0);
|
|
|
|
ASSERT_TRUE(detour->start(controllingGoal(), t0_));
|
|
EXPECT_EQ(detour->update(t0_).status, RecoveryStatus::kSucceeded);
|
|
}
|
|
|
|
TEST_F(DetourPathRecoveryTest, FailsWhenPoseIsLostMidRun)
|
|
{
|
|
RecoveryBehavior* detour = findBehavior(registry_, "detour");
|
|
ASSERT_NE(detour, nullptr);
|
|
|
|
pose_.pose = makePose("odom", 0.0, 0.0);
|
|
plan_.plan = makeLinePlan("odom", 0.0, 5.0, 0.5, 0.0);
|
|
|
|
ASSERT_TRUE(detour->start(controllingGoal(), t0_));
|
|
|
|
// Mất pose giữa lượt: không được lập đường vòng bằng pose cũ.
|
|
pose_.ok = false;
|
|
EXPECT_EQ(detour->update(t0_).status, RecoveryStatus::kFailed);
|
|
}
|
|
|
|
TEST(DetourPathRecoveryNoTfTest, RefusesWhenFramesDifferWithoutTf)
|
|
{
|
|
// Registry riêng với context KHÔNG có TF: plan (map) khác frame costmap (odom) phải bị từ chối
|
|
// ngay tại start, kèm lý do — không đoán, không trộn frame.
|
|
FakePoseProvider pose;
|
|
FakePlanProvider plan;
|
|
RecoveryContext ctx;
|
|
ctx.pose = &pose;
|
|
ctx.plan = &plan;
|
|
ctx.local_costmap = dummyCostmap();
|
|
ctx.tf = nullptr;
|
|
|
|
robot::NodeHandle nh;
|
|
RecoveryRegistry registry;
|
|
ASSERT_TRUE(registry.loadFromConfig(nh, "recovery", ctx));
|
|
|
|
RecoveryBehavior* detour = findBehavior(registry, "detour_map");
|
|
ASSERT_NE(detour, nullptr);
|
|
|
|
pose.pose = makePose("odom", 0.0, 0.0);
|
|
plan.plan = makeLinePlan("map", 2.0, 7.0, 0.5, 1.0);
|
|
|
|
EXPECT_FALSE(detour->start(controllingGoal(), robot::Time{100, 0}));
|
|
}
|
|
|
|
} // namespace
|
|
|
|
// ================================================================================================
|
|
// merge_grids.h — ngữ nghĩa gộp lưới (thuần, lưới tay, không cần costmap sống)
|
|
// ================================================================================================
|
|
|
|
TEST(MergeGrids, CellMergeSemantics)
|
|
{
|
|
using robot_costmap_2d::FREE_SPACE;
|
|
using robot_costmap_2d::LETHAL_OBSTACLE;
|
|
using robot_costmap_2d::NO_INFORMATION;
|
|
|
|
// local chưa quét -> tin mắt rộng.
|
|
EXPECT_EQ(sbpl_recovery::mergeCellCost(LETHAL_OBSTACLE, NO_INFORMATION), LETHAL_OBSTACLE);
|
|
EXPECT_EQ(sbpl_recovery::mergeCellCost(FREE_SPACE, NO_INFORMATION), FREE_SPACE);
|
|
// global chưa biết nhưng local ĐÃ biết -> lấy local (max mù sẽ biến 255 thành "chắn").
|
|
EXPECT_EQ(sbpl_recovery::mergeCellCost(NO_INFORMATION, FREE_SPACE), FREE_SPACE);
|
|
EXPECT_EQ(sbpl_recovery::mergeCellCost(NO_INFORMATION, LETHAL_OBSTACLE), LETHAL_OBSTACLE);
|
|
// Cả hai đều biết -> một bên chắn là chắn; local FREE không xoá tường global.
|
|
EXPECT_EQ(sbpl_recovery::mergeCellCost(LETHAL_OBSTACLE, FREE_SPACE), LETHAL_OBSTACLE);
|
|
EXPECT_EQ(sbpl_recovery::mergeCellCost(FREE_SPACE, LETHAL_OBSTACLE), LETHAL_OBSTACLE);
|
|
EXPECT_EQ(sbpl_recovery::mergeCellCost(FREE_SPACE, FREE_SPACE), FREE_SPACE);
|
|
}
|
|
|
|
TEST(MergeGrids, OverlayWritesLocalWindowOntoGlobalBase)
|
|
{
|
|
using robot_costmap_2d::Costmap2D;
|
|
using robot_costmap_2d::FREE_SPACE;
|
|
using robot_costmap_2d::LETHAL_OBSTACLE;
|
|
using robot_costmap_2d::NO_INFORMATION;
|
|
|
|
// Nền global 10x10 @ 0.1 m, gốc (0,0): FREE, một bức tường tĩnh tại (0.55, 0.55).
|
|
Costmap2D merged(10, 10, 0.1, 0.0, 0.0, FREE_SPACE);
|
|
merged.setCost(5, 5, LETHAL_OBSTACLE);
|
|
|
|
// Cửa sổ local 4x4 @ 0.1 m, gốc (0.2, 0.2): một vật cản tươi, một cell FREE trùng tường global,
|
|
// phần còn lại chưa quét (NO_INFORMATION).
|
|
Costmap2D local(4, 4, 0.1, 0.2, 0.2, NO_INFORMATION);
|
|
local.setCost(1, 1, LETHAL_OBSTACLE); // (0.35, 0.35): vật cản sensor
|
|
local.setCost(3, 3, FREE_SPACE); // (0.55, 0.55): local nói sạch NGAY TRÊN tường global
|
|
|
|
sbpl_recovery::PlanarTransform2D identity;
|
|
sbpl_recovery::overlayLocalGrid(local, identity, merged);
|
|
|
|
unsigned int mx = 0;
|
|
unsigned int my = 0;
|
|
ASSERT_TRUE(merged.worldToMap(0.35, 0.35, mx, my));
|
|
EXPECT_EQ(merged.getCost(mx, my), LETHAL_OBSTACLE); // vật cản tươi được đè vào nền
|
|
ASSERT_TRUE(merged.worldToMap(0.55, 0.55, mx, my));
|
|
EXPECT_EQ(merged.getCost(mx, my), LETHAL_OBSTACLE); // tường global KHÔNG bị local FREE xoá
|
|
ASSERT_TRUE(merged.worldToMap(0.25, 0.25, mx, my));
|
|
EXPECT_EQ(merged.getCost(mx, my), FREE_SPACE); // local chưa quét -> giữ nền global
|
|
ASSERT_TRUE(merged.worldToMap(0.95, 0.95, mx, my));
|
|
EXPECT_EQ(merged.getCost(mx, my), FREE_SPACE); // ngoài cửa sổ local -> nền global nguyên vẹn
|
|
}
|
|
|
|
TEST(MergeGrids, OverlayTransformsLocalFrameIntoGridFrame)
|
|
{
|
|
using robot_costmap_2d::Costmap2D;
|
|
using robot_costmap_2d::FREE_SPACE;
|
|
using robot_costmap_2d::LETHAL_OBSTACLE;
|
|
using robot_costmap_2d::NO_INFORMATION;
|
|
|
|
Costmap2D merged(20, 20, 0.1, 0.0, 0.0, FREE_SPACE);
|
|
|
|
// Local frame (odom) lệch map: tịnh tiến (1.0, 0.5), quay 90 độ.
|
|
Costmap2D local(4, 4, 0.1, 0.0, 0.0, NO_INFORMATION);
|
|
local.setCost(2, 0, LETHAL_OBSTACLE); // tâm cell tại odom (0.25, 0.05)
|
|
|
|
sbpl_recovery::PlanarTransform2D odom_to_map;
|
|
odom_to_map.x = 1.0;
|
|
odom_to_map.y = 0.5;
|
|
odom_to_map.cos_yaw = 0.0;
|
|
odom_to_map.sin_yaw = 1.0; // yaw = +90 độ: (x, y) -> (-y, x)
|
|
|
|
sbpl_recovery::overlayLocalGrid(local, odom_to_map, merged);
|
|
|
|
// odom (0.25, 0.05) -> map (1.0 - 0.05, 0.5 + 0.25) = (0.95, 0.75).
|
|
unsigned int mx = 0;
|
|
unsigned int my = 0;
|
|
ASSERT_TRUE(merged.worldToMap(0.95, 0.75, mx, my));
|
|
EXPECT_EQ(merged.getCost(mx, my), LETHAL_OBSTACLE);
|
|
}
|
|
|
|
int main(int argc, char** argv)
|
|
{
|
|
#ifdef SBPL_RECOVERY_TEST_CONFIG_DIR
|
|
// Trỏ cây config vào bản test TRƯỚC khi bất kỳ NodeHandle nào được dựng.
|
|
setenv("PNKX_NAV_CORE_CONFIG_DIR", SBPL_RECOVERY_TEST_CONFIG_DIR, 1);
|
|
#endif
|
|
#ifdef SBPL_RECOVERY_TEST_LIBRARY_DIR
|
|
// PluginLoaderHelper chỉ ghép ".so" trong nhánh PNKX_NAV_CORE_LIBRARY_PATH — không đặt biến này
|
|
// thì `library_path: libsbpl_recovery` không resolve được khi LD_LIBRARY_PATH thiếu devel/lib.
|
|
// Cùng khuôn với bộ test của recovery_core.
|
|
setenv("PNKX_NAV_CORE_LIBRARY_PATH", SBPL_RECOVERY_TEST_LIBRARY_DIR, 0);
|
|
#endif
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
return RUN_ALL_TESTS();
|
|
}
|