Feed per-marker docking parameters to the global planner
A docking global planner has no way to learn which marker the leg is for: BaseGlobalPlanner::initialize() takes only the plugin name and the costmap, and the plugin builds its own NodeHandle to read parameters. The only channel left is the parameter tree, so copy the marker's keys into the planner's namespace before the instance is built - the same shape as Costmap2DROBOT::copyParentParameters() pouring parameters into each layer. PlannerPort::bindDockingMarker() is called at the request gate, right before swapPlanner(), because swapPlanner is where the instance is built and reads its parameters. Copying afterwards would be a no-op: initialize() runs once. - The destination is whatever global_planner the profile names, so a marker bound to a planner other than DockPlanner writes into that planner's namespace. Only DockPlanner reads footprint_dock today; the config documents that the value is inert elsewhere. - The namespace is verified after construction. robot::NodeHandle silently falls back to its parent when a namespace does not resolve, so a mistyped marker would otherwise read footprint from ROOT - the robot footprint - and use it as the dock's size. - The cached instance is rebuilt when the marker changes, and swapPlanner no longer short-circuits on an unchanged planner name, otherwise the second dock would run with the first dock's geometry. Rebuilds are skipped when the same marker is bound again. - The replaced instance is retired rather than destroyed: the planner thread captures the active pointer and releases the mutex before calling into the plugin, so destroying it from the control thread is a use-after-free. Add eight PlannerRunner tests. The test plugin now exports its initialize() count and the footprint it read, which is the only way to observe from outside that the rebuild actually happened. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -213,6 +213,29 @@ actions_missing_library:
|
||||
NoopActionHandler:
|
||||
library_path: libaction_core_noop_action_handler
|
||||
|
||||
# --- Profile marker docking ở ROOT, cho PlannerRunner::bindDockingMarker ------------------------
|
||||
#
|
||||
# Ở runtime bảng này nằm tại ROOT (maker_sources.yaml) và `PlannerRunner` đọc nó qua NodeHandle gốc,
|
||||
# khác với bản lồng trong `move_base2:`/`root_profiles:` ở trên — bản đó là đầu vào của
|
||||
# MoveBase2Config, dùng cho config_validation_test. Hai bản cố ý tách nhau: chúng kiểm hai đường đọc
|
||||
# khác nhau của cùng một khái niệm.
|
||||
docking_marker_profiles:
|
||||
dock_a:
|
||||
global_planner: TestPlannerOk
|
||||
local_planner: TestControllerOk
|
||||
footprint_dock: [[0.5, -0.4], [0.5, 0.4], [-0.5, 0.4], [-0.5, -0.4]] # [m]
|
||||
|
||||
dock_b:
|
||||
global_planner: TestPlannerOk
|
||||
local_planner: TestControllerOk
|
||||
footprint_dock: [[0.2, -0.15], [0.2, 0.15], [-0.2, 0.15], [-0.2, -0.15]] # [m], nhỏ hơn dock_a
|
||||
|
||||
# Có profile nhưng KHÔNG khai footprint_dock: bindDockingMarker phải chấp nhận và không đánh dấu
|
||||
# phải dựng lại instance.
|
||||
dock_no_footprint:
|
||||
global_planner: TestPlannerOk
|
||||
local_planner: TestControllerOk
|
||||
|
||||
# --- Global planner giả cho planner_runner_test -------------------------------------------------
|
||||
#
|
||||
# Bốn alias cùng nằm trong một thư viện; mỗi alias là một hành vi mà PlannerRunner phải xử lý đúng.
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <cstdlib>
|
||||
#include <cstdint>
|
||||
#include <limits>
|
||||
@@ -17,6 +18,9 @@
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include <boost/dll/import.hpp>
|
||||
#include <yaml-cpp/yaml.h>
|
||||
|
||||
#include <robot/node_handle.h>
|
||||
|
||||
#include <move_base2/runners/planner_runner.h>
|
||||
@@ -422,6 +426,149 @@ TEST(PlannerRunner, DestructorJoinsWhileAPlanIsInFlight)
|
||||
SUCCEED();
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
// Nối tham số marker docking vào namespace của global planner
|
||||
// ================================================================================================
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
/// @brief Đọc cửa sổ quan sát trong thư viện plugin test (xem test_global_planner.cpp).
|
||||
std::size_t pluginInitializeCount()
|
||||
{
|
||||
return boost::dll::import_alias<std::size_t()>(
|
||||
"libmove_base2_test_global_planner", "TestPlannerInitializeCount",
|
||||
boost::dll::load_mode::append_decorations | boost::dll::load_mode::search_system_folders)();
|
||||
}
|
||||
|
||||
std::string pluginLastFootprintDock()
|
||||
{
|
||||
return boost::dll::import_alias<std::string()>(
|
||||
"libmove_base2_test_global_planner", "TestPlannerLastFootprintDock",
|
||||
boost::dll::load_mode::append_decorations | boost::dll::load_mode::search_system_folders)();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST(PlannerRunnerDockingMarker, CopiesTheMarkerFootprintIntoThePlannerNamespace)
|
||||
{
|
||||
Fixture fixture;
|
||||
ASSERT_TRUE(fixture.ok()) << fixture.error();
|
||||
|
||||
ASSERT_TRUE(fixture.runner_.bindDockingMarker("dock_a", "TestPlannerOk"));
|
||||
|
||||
// Đích là namespace của PLANNER, không phải của marker: plugin chỉ đọc namespace mang tên chính
|
||||
// nó, nên chép sai chỗ là khoá nằm im mãi mãi mà không có lỗi nào.
|
||||
robot::NodeHandle nh;
|
||||
YAML::Node copied;
|
||||
ASSERT_TRUE(nh.getParam("TestPlannerOk/footprint_dock", copied));
|
||||
ASSERT_TRUE(copied.IsSequence());
|
||||
ASSERT_EQ(copied.size(), 4U);
|
||||
EXPECT_DOUBLE_EQ(copied[0][0].as<double>(), 0.5);
|
||||
EXPECT_DOUBLE_EQ(copied[0][1].as<double>(), -0.4);
|
||||
}
|
||||
|
||||
TEST(PlannerRunnerDockingMarker, TargetsWhateverPlannerTheProfileNamesNotJustDockPlanner)
|
||||
{
|
||||
Fixture fixture;
|
||||
ASSERT_TRUE(fixture.ok()) << fixture.error();
|
||||
|
||||
// Tên planner đến từ profile, không hard-code. Planner này còn chưa có block config nào —
|
||||
// `setParam` phải tự tạo, nếu không thì mọi planner mới thêm đều lặng lẽ không nhận được param.
|
||||
ASSERT_TRUE(fixture.runner_.bindDockingMarker("dock_b", "SomeOtherGlobalPlanner"));
|
||||
|
||||
robot::NodeHandle nh;
|
||||
YAML::Node copied;
|
||||
ASSERT_TRUE(nh.getParam("SomeOtherGlobalPlanner/footprint_dock", copied));
|
||||
ASSERT_TRUE(copied.IsSequence());
|
||||
EXPECT_DOUBLE_EQ(copied[0][0].as<double>(), 0.2);
|
||||
}
|
||||
|
||||
TEST(PlannerRunnerDockingMarker, RebuildsTheInstanceSoTheNewMarkerFootprintIsActuallyRead)
|
||||
{
|
||||
Fixture fixture;
|
||||
ASSERT_TRUE(fixture.ok()) << fixture.error();
|
||||
|
||||
const std::size_t after_configure = pluginInitializeCount();
|
||||
|
||||
ASSERT_TRUE(fixture.runner_.bindDockingMarker("dock_a", "TestPlannerOk"));
|
||||
ASSERT_TRUE(fixture.runner_.swapPlanner("TestPlannerOk"));
|
||||
EXPECT_EQ(pluginInitializeCount(), after_configure + 1)
|
||||
<< "instance phải được dựng lại thì initialize() mới đọc được footprint mới";
|
||||
const std::string dock_a = pluginLastFootprintDock();
|
||||
EXPECT_NE(dock_a.find("0.5"), std::string::npos) << dock_a;
|
||||
|
||||
// Marker khác, CÙNG planner: đường tắt "đã đúng planner" trong swapPlanner không được nuốt lần
|
||||
// dựng lại này — nuốt là dock vào trạm thứ hai bằng kích thước của trạm thứ nhất.
|
||||
ASSERT_TRUE(fixture.runner_.bindDockingMarker("dock_b", "TestPlannerOk"));
|
||||
ASSERT_TRUE(fixture.runner_.swapPlanner("TestPlannerOk"));
|
||||
EXPECT_EQ(pluginInitializeCount(), after_configure + 2);
|
||||
const std::string dock_b = pluginLastFootprintDock();
|
||||
EXPECT_NE(dock_b.find("0.2"), std::string::npos) << dock_b;
|
||||
|
||||
// Thư viện chỉ được dlopen một lần: dựng lại là gọi lại factory sẵn có, không nạp lại .so.
|
||||
EXPECT_EQ(fixture.runner_.loadedCount(), 1U);
|
||||
}
|
||||
|
||||
TEST(PlannerRunnerDockingMarker, DockingTheSameMarkerTwiceDoesNotReinitialiseThePlanner)
|
||||
{
|
||||
Fixture fixture;
|
||||
ASSERT_TRUE(fixture.ok()) << fixture.error();
|
||||
|
||||
ASSERT_TRUE(fixture.runner_.bindDockingMarker("dock_a", "TestPlannerOk"));
|
||||
ASSERT_TRUE(fixture.runner_.swapPlanner("TestPlannerOk"));
|
||||
const std::size_t after_first = pluginInitializeCount();
|
||||
|
||||
ASSERT_TRUE(fixture.runner_.bindDockingMarker("dock_a", "TestPlannerOk"));
|
||||
ASSERT_TRUE(fixture.runner_.swapPlanner("TestPlannerOk"));
|
||||
EXPECT_EQ(pluginInitializeCount(), after_first)
|
||||
<< "param không đổi thì initialize() lại chỉ tốn thời gian ngay lúc robot đang chờ lệnh";
|
||||
}
|
||||
|
||||
TEST(PlannerRunnerDockingMarker, MarkerWithoutAProfileIsAcceptedAndChangesNothing)
|
||||
{
|
||||
Fixture fixture;
|
||||
ASSERT_TRUE(fixture.ok()) << fixture.error();
|
||||
|
||||
ASSERT_TRUE(fixture.runner_.swapPlanner("TestPlannerOk"));
|
||||
const std::size_t before = pluginInitializeCount();
|
||||
|
||||
// `maker_sources` mới là allow-list; bảng profile chỉ là override. Marker hợp lệ nhưng không có
|
||||
// entry riêng phải chạy tiếp bằng cặp planner mặc định, không bị từ chối.
|
||||
EXPECT_TRUE(fixture.runner_.bindDockingMarker("marker_without_profile", "TestPlannerOk"));
|
||||
ASSERT_TRUE(fixture.runner_.swapPlanner("TestPlannerOk"));
|
||||
EXPECT_EQ(pluginInitializeCount(), before);
|
||||
}
|
||||
|
||||
TEST(PlannerRunnerDockingMarker, ProfileWithoutFootprintDockIsAcceptedAndChangesNothing)
|
||||
{
|
||||
Fixture fixture;
|
||||
ASSERT_TRUE(fixture.ok()) << fixture.error();
|
||||
|
||||
ASSERT_TRUE(fixture.runner_.swapPlanner("TestPlannerOk"));
|
||||
const std::size_t before = pluginInitializeCount();
|
||||
|
||||
EXPECT_TRUE(fixture.runner_.bindDockingMarker("dock_no_footprint", "TestPlannerOk"));
|
||||
ASSERT_TRUE(fixture.runner_.swapPlanner("TestPlannerOk"));
|
||||
EXPECT_EQ(pluginInitializeCount(), before);
|
||||
}
|
||||
|
||||
TEST(PlannerRunnerDockingMarker, EmptyMarkerOrPlannerNameIsANoOp)
|
||||
{
|
||||
Fixture fixture;
|
||||
ASSERT_TRUE(fixture.ok()) << fixture.error();
|
||||
|
||||
// Chặng không phải docking, hoặc profile không khai global planner riêng — cả hai đều hợp lệ.
|
||||
EXPECT_TRUE(fixture.runner_.bindDockingMarker("", "TestPlannerOk"));
|
||||
EXPECT_TRUE(fixture.runner_.bindDockingMarker("dock_a", ""));
|
||||
}
|
||||
|
||||
TEST(PlannerRunnerDockingMarker, RefusesBeforeConfigure)
|
||||
{
|
||||
PlannerRunner runner;
|
||||
EXPECT_FALSE(runner.bindDockingMarker("dock_a", "TestPlannerOk"));
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
// ctest không mang theo biến môi trường của shell; binary tự trỏ vào cây config và thư viện của
|
||||
|
||||
@@ -8,11 +8,16 @@
|
||||
* đú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.
|
||||
*
|
||||
* 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 đủ).
|
||||
*
|
||||
* Author: DuongTD
|
||||
*********************************************************************/
|
||||
#include <cstddef>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@@ -20,7 +25,9 @@
|
||||
#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>
|
||||
|
||||
namespace move_base2
|
||||
@@ -31,6 +38,18 @@ 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.
|
||||
@@ -54,6 +73,17 @@ public:
|
||||
{
|
||||
// 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;
|
||||
}
|
||||
|
||||
@@ -119,6 +149,18 @@ 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;
|
||||
}
|
||||
|
||||
} // namespace testing
|
||||
} // namespace move_base2
|
||||
|
||||
@@ -126,3 +168,5 @@ 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::initializeCount, TestPlannerInitializeCount)
|
||||
BOOST_DLL_ALIAS(move_base2::testing::lastFootprintDock, TestPlannerLastFootprintDock)
|
||||
|
||||
Reference in New Issue
Block a user