Files
move_base2/test/plugins/test_global_planner.cpp
duongtd 1e8abf2ce4 perf: global planner doc snapshot costmap thay vi giu mutex het luot makePlan
PlannerRunner truoc gio giu Costmap2D mutex suot makePlan() (giao thuc gen-1) - SBPL search
6-14s la costmap update thread bi block het tung do. Them capability boost::dll
"<alias>ExternalGrid" (cung mau sbpl_recovery dang dung) de planner nhan ban chup thay vi
lock song. threadBody(): planner co setter + costmap bat snapshot_for_readers thi
refreshSnapshot() + dua ban chup vao, khong lock; khong thi rot ve giu mutex nhu cu (rollback
khong build lai). SBPLLatticePlanner them setExternalGrid()/activeGrid(); sua luon check
size external_grid_ so voi grid (grid = activeGrid() nen so voi chinh no, khong bao gio bat
duoc lech) - so dung costmap_robot_->getCostmap() (luoi song).

Test: ReportsExternalGridCapabilityPerPlanner, ExternalGridIsNotCalledWhileTheCostmapLockIsDisabled.
config/runtime: bat snapshot_for_readers: true (cay sim). docs/THREADING.md: bang so sanh khoa
dai vs ban chup.
2026-09-18 13:32:52 +07:00

292 lines
10 KiB
C++

/*********************************************************************
*
* Software License Agreement (BSD License)
*
* move_base2 — plugin global planner CHỈ dùng cho test.
*
* Bốn alias được export từ cùng một thư viện, mỗi alias là một hành vi mà `PlannerRunner` phải xử lý
* đúng. Chọn cách này thay vì một plugin đọc config: nó làm test không phụ thuộc vào cây config, và
* mỗi kịch bản gọi tên đúng thứ nó kiểm.
*
* Hai alias còn lại (`TestPlannerInitializeCount`, `TestPlannerLastFootprintDock`) không phải
* planner: chúng mở cửa sổ quan sát vào việc `initialize()` chạy mấy lần và đọc được param nào —
* thứ mà `bindDockingMarker` phải làm đúng và không có đường nào khác để kiểm từ ngoài.
*
* `TestPlannerOkExternalGrid` + `TestPlannerExternalGridCalls`: capability lưới ngoài đúng chữ ký
* `sbpl_lattice_planner::setExternalGridOn`, export CHỈ cho `TestPlannerOk`. Nhờ đó test kiểm được
* PlannerRunner tra alias phụ đúng tên và không gọi nó khi khoá costmap bị tắt (costmap giả).
*
* Không alias nào chạm vào con trỏ costmap — đó là điều kiện để test truyền vào một con trỏ giả
* thay vì phải dựng `Costmap2DROBOT` thật (cần tf3::BufferCore và cây config đầy đủ). Setter lưới
* ngoài chỉ ĐẾM và ghi lại con trỏ, không dereference.
*
* Author: DuongTD
*********************************************************************/
#include <atomic>
#include <chrono>
#include <cstddef>
#include <thread>
#include <stdexcept>
#include <string>
#include <vector>
#include <boost/config.hpp>
#include <boost/dll/alias.hpp>
#include <boost/make_shared.hpp>
#include <yaml-cpp/yaml.h>
#include <robot/node_handle.h>
#include <robot_nav_core/base_global_planner.h>
#include <robot_nav_core/preemptable.h>
namespace robot_costmap_2d
{
class Costmap2D; // chỉ cần con trỏ cho chữ ký capability, không dereference
}
namespace move_base2
{
namespace testing
{
/// @brief Số pose trong plan mà `TestPlannerOk` sinh ra.
constexpr std::size_t kPlanLength = 3;
/**
* Số lần `initialize()` được gọi trên MỌI instance của thư viện này, và footprint mà lần gọi cuối
* đọc được.
*
* Cách duy nhất để test quan sát việc PlannerRunner có dựng lại instance khi marker đổi hay không:
* cache của runner không lộ con trỏ instance ra ngoài, và dựng lại đúng là điều kiện để plugin đọc
* lại param. Biến process-global vì mỗi test chạy trong một tiến trình riêng và thư viện chỉ được
* nạp một lần.
*/
std::size_t g_initialize_count = 0;
std::string g_last_footprint_dock;
/**
* @class TestGlobalPlanner
* @brief Planner giả, hành vi cố định theo tham số dựng.
*/
class TestGlobalPlanner : public robot_nav_core::BaseGlobalPlanner
{
public:
enum class Behavior
{
kOk, ///< Trả plan hợp lệ.
kEmptyPlan, ///< Trả true kèm plan RỖNG — bẫy mà PlannerRunner phải quy về false.
kThrow, ///< Ném exception giữa lúc lập plan.
kInitFails ///< initialize() trả false.
};
explicit TestGlobalPlanner(Behavior behavior) : behavior_(behavior)
{
}
bool initialize(std::string name, robot_costmap_2d::Costmap2DROBOT* /*costmap_robot*/) override
{
// Cố ý KHÔNG chạm costmap_robot — xem chú thích đầu file.
name_ = std::move(name);
++g_initialize_count;
// Đọc param theo đúng cách plugin thật làm — namespace là TÊN PLUGIN. Ghi lại dạng chuỗi để
// test so được giá trị nào đã tới nơi, không chỉ là "có tới hay không".
robot::NodeHandle nh(name_);
YAML::Node footprint;
g_last_footprint_dock.clear();
if (nh.getParam("footprint_dock", footprint) && footprint.IsDefined())
{
g_last_footprint_dock = YAML::Dump(footprint);
}
return behavior_ != Behavior::kInitFails;
}
bool makePlan(const robot_geometry_msgs::PoseStamped& start,
const robot_geometry_msgs::PoseStamped& goal,
std::vector<robot_geometry_msgs::PoseStamped>& plan) override
{
++call_count_;
if (behavior_ == Behavior::kThrow)
{
throw std::runtime_error("TestGlobalPlanner was asked to throw an exception");
}
plan.clear();
if (behavior_ == Behavior::kEmptyPlan)
{
return true; // true + rỗng: đúng thứ contract PlannerPort cấm lọt qua.
}
plan.push_back(start);
for (std::size_t i = plan.size(); i + 1 < kPlanLength; ++i)
{
plan.push_back(start);
}
plan.push_back(goal);
return true;
}
bool makePlan(const robot_protocol_msgs::Order& /*msg*/,
const robot_geometry_msgs::PoseStamped& start,
const robot_geometry_msgs::PoseStamped& goal,
std::vector<robot_geometry_msgs::PoseStamped>& plan) override
{
saw_order_ = true;
return makePlan(start, goal, plan);
}
private:
Behavior behavior_;
std::string name_;
std::size_t call_count_ = 0;
bool saw_order_ = false;
};
/**
* @class TestSlowPlanner
* @brief Planner giả "search dài": ngủ từng lát 5 ms trong @p seconds. Bản Preemptable thoát khi
* thấy cờ (mô hình ARA* + abort flag); bản "stubborn" không nhìn cờ (mô hình plugin cũ).
*/
class TestSlowPlannerBase : public robot_nav_core::BaseGlobalPlanner
{
public:
explicit TestSlowPlannerBase(double seconds, bool honours_preempt)
: seconds_(seconds), honours_preempt_(honours_preempt)
{
}
bool initialize(std::string /*name*/, robot_costmap_2d::Costmap2DROBOT* /*costmap_robot*/) override
{
return true;
}
bool makePlan(const robot_geometry_msgs::PoseStamped& start,
const robot_geometry_msgs::PoseStamped& goal,
std::vector<robot_geometry_msgs::PoseStamped>& plan) override
{
preempt_.store(false, std::memory_order_relaxed);
const auto deadline = std::chrono::steady_clock::now() +
std::chrono::duration_cast<std::chrono::steady_clock::duration>(
std::chrono::duration<double>(seconds_));
while (std::chrono::steady_clock::now() < deadline)
{
if (honours_preempt_ && preempt_.load(std::memory_order_relaxed))
{
return false;
}
std::this_thread::sleep_for(std::chrono::milliseconds(5));
}
plan.clear();
plan.push_back(start);
plan.push_back(goal);
return true;
}
protected:
std::atomic<bool> preempt_{false};
private:
double seconds_;
bool honours_preempt_;
};
class TestSlowPreemptablePlanner final : public TestSlowPlannerBase,
public robot_nav_core::Preemptable
{
public:
TestSlowPreemptablePlanner() : TestSlowPlannerBase(2.0, true)
{
}
void preempt() override
{
preempt_.store(true, std::memory_order_relaxed);
}
};
class TestSlowStubbornPlanner final : public TestSlowPlannerBase
{
public:
TestSlowStubbornPlanner() : TestSlowPlannerBase(0.4, false)
{
}
};
robot_nav_core::BaseGlobalPlanner::Ptr createSlowPreemptable()
{
return std::make_shared<TestSlowPreemptablePlanner>();
}
robot_nav_core::BaseGlobalPlanner::Ptr createSlowStubborn()
{
return std::make_shared<TestSlowStubbornPlanner>();
}
robot_nav_core::BaseGlobalPlanner::Ptr createOk()
{
return std::make_shared<TestGlobalPlanner>(TestGlobalPlanner::Behavior::kOk);
}
robot_nav_core::BaseGlobalPlanner::Ptr createEmptyPlan()
{
return std::make_shared<TestGlobalPlanner>(TestGlobalPlanner::Behavior::kEmptyPlan);
}
robot_nav_core::BaseGlobalPlanner::Ptr createThrowing()
{
return std::make_shared<TestGlobalPlanner>(TestGlobalPlanner::Behavior::kThrow);
}
robot_nav_core::BaseGlobalPlanner::Ptr createInitFailing()
{
return std::make_shared<TestGlobalPlanner>(TestGlobalPlanner::Behavior::kInitFails);
}
/// @brief Số lần `initialize()` đã chạy trong thư viện này — cửa sổ quan sát cho test.
std::size_t initializeCount()
{
return g_initialize_count;
}
/// @brief `footprint_dock` mà lần `initialize()` gần nhất đọc được; rỗng nếu khoá vắng mặt.
std::string lastFootprintDock()
{
return g_last_footprint_dock;
}
/// Số lần capability ExternalGrid được gọi trên thư viện này — kể cả với `nullptr`.
std::size_t g_external_grid_calls = 0;
/**
* @brief Capability lưới ngoài của `TestPlannerOk` — cùng chữ ký `sbpl_lattice_planner::setExternalGridOn`.
*
* Chỉ đếm. Không dereference @p grid (test dùng costmap giả) và không đổi hành vi planner: thứ cần
* kiểm là PlannerRunner có TÌM và GỌI đúng alias hay không, không phải planner làm gì với lưới.
*/
void setExternalGridOnOk(robot_nav_core::BaseGlobalPlanner* /*planner*/,
const robot_costmap_2d::Costmap2D* /*grid*/)
{
++g_external_grid_calls;
}
std::size_t externalGridCalls()
{
return g_external_grid_calls;
}
} // namespace testing
} // namespace move_base2
BOOST_DLL_ALIAS(move_base2::testing::createOk, TestPlannerOk)
BOOST_DLL_ALIAS(move_base2::testing::createEmptyPlan, TestPlannerEmptyPlan)
BOOST_DLL_ALIAS(move_base2::testing::createThrowing, TestPlannerThrowing)
BOOST_DLL_ALIAS(move_base2::testing::createInitFailing, TestPlannerInitFails)
BOOST_DLL_ALIAS(move_base2::testing::createSlowPreemptable, TestPlannerSlowPreemptable)
BOOST_DLL_ALIAS(move_base2::testing::createSlowStubborn, TestPlannerSlowStubborn)
BOOST_DLL_ALIAS(move_base2::testing::initializeCount, TestPlannerInitializeCount)
BOOST_DLL_ALIAS(move_base2::testing::lastFootprintDock, TestPlannerLastFootprintDock)
// Capability "<alias>ExternalGrid" — CHỈ cho TestPlannerOk; các alias khác cố ý không có để test
// đường "plugin không export -> khoá dài".
BOOST_DLL_ALIAS(move_base2::testing::setExternalGridOnOk, TestPlannerOkExternalGrid)
BOOST_DLL_ALIAS(move_base2::testing::externalGridCalls, TestPlannerExternalGridCalls)