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:
@@ -66,6 +66,31 @@ public:
|
||||
*/
|
||||
virtual bool swapPlanner(const std::string& planner_name) = 0;
|
||||
|
||||
/**
|
||||
* @brief Nối tham số riêng của một marker docking vào namespace của global planner sắp dùng.
|
||||
* Gọi TRƯỚC @ref swapPlanner của chính chặng đó.
|
||||
*
|
||||
* Global planner không nhận marker qua chữ ký nào cả: `BaseGlobalPlanner::initialize(name, costmap)`
|
||||
* chỉ có tên plugin, và plugin tự dựng `robot::NodeHandle` của nó để đọc param. Nên kênh duy nhất
|
||||
* là param tree — chép `docking_marker_profiles/<marker>/…` sang `<planner_name>/…` trước khi
|
||||
* instance được dựng, đúng cách `Costmap2DROBOT::copyParentParameters` rót param xuống từng layer.
|
||||
*
|
||||
* Hiện thực phải coi đây là điểm đánh dấu "marker đã đổi": instance đang cache đã đọc param trong
|
||||
* `initialize()` và không bao giờ đọc lại, nên phải dựng lại nó — nếu không, dock lần hai với
|
||||
* marker khác vẫn chạy bằng footprint của lần đầu.
|
||||
*
|
||||
* @param marker Tên marker; rỗng = chặng không phải docking, không làm gì.
|
||||
* @param planner_name Alias plugin global planner của chặng đó (đích chép sang).
|
||||
* @return false nếu marker có khai profile nhưng param của nó hỏng — bên gọi phải từ chối yêu cầu
|
||||
* thay vì dock bằng hình học sai.
|
||||
*
|
||||
* Default trả true (không làm gì): chỉ hiện thực thật (PlannerRunner) mới có param tree.
|
||||
*/
|
||||
virtual bool bindDockingMarker(const std::string& /*marker*/, const std::string& /*planner_name*/)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Khởi động một lượt lập plan. **Không chặn.**
|
||||
*
|
||||
|
||||
@@ -110,6 +110,8 @@ public:
|
||||
|
||||
bool swapPlanner(const std::string& planner_name) override;
|
||||
|
||||
bool bindDockingMarker(const std::string& marker, const std::string& planner_name) override;
|
||||
|
||||
/**
|
||||
* @brief Gắn bộ telemetry (non-owning, có thể null = tắt đo).
|
||||
*
|
||||
@@ -154,6 +156,16 @@ private:
|
||||
{
|
||||
std::function<robot_nav_core::BaseGlobalPlanner::Ptr()> factory;
|
||||
robot_nav_core::BaseGlobalPlanner::Ptr instance;
|
||||
|
||||
/**
|
||||
* Instance đã bị thay khi marker đổi, giữ lại chứ KHÔNG huỷ.
|
||||
*
|
||||
* Thread lập plan chụp `active_` rồi nhả mutex trước khi gọi `makePlan` — huỷ instance cũ ở
|
||||
* control thread trong lúc đó là use-after-free trên một plugin đang chạy. Cùng lý do với việc
|
||||
* cache không bao giờ xoá entry. Mỗi lần đổi marker thêm đúng một instance; số marker hữu hạn
|
||||
* nên bộ nhớ không trôi.
|
||||
*/
|
||||
std::vector<robot_nav_core::BaseGlobalPlanner::Ptr> retired;
|
||||
};
|
||||
|
||||
/// @brief Nạp @p name nếu chưa có trong cache. @return nullptr khi thất bại (đã log lý do).
|
||||
@@ -177,6 +189,18 @@ private:
|
||||
std::map<std::string, Loaded> planners_;
|
||||
std::string active_name_;
|
||||
|
||||
/// Marker vừa đổi và instance đang cache chưa đọc lại param của nó — @ref acquire phải dựng lại.
|
||||
bool marker_dirty_ = false;
|
||||
|
||||
/**
|
||||
* Marker đã nối vào từng planner, khoá theo alias plugin.
|
||||
*
|
||||
* Dùng để KHÔNG dựng lại instance khi dock lại đúng marker cũ: giá trị trong param tree không đổi
|
||||
* thì `initialize()` cũng đọc ra đúng ngần ấy. Không có bảng này thì mỗi chặng docking là một lần
|
||||
* initialize() thừa, và với planner nặng thì nó rơi đúng vào lúc robot đang chờ lệnh.
|
||||
*/
|
||||
std::map<std::string, std::string> bound_marker_;
|
||||
|
||||
// --- Chia sẻ giữa hai thread, bảo vệ bởi mutex_ ------------------------------------------------
|
||||
mutable std::mutex mutex_;
|
||||
std::condition_variable cv_;
|
||||
|
||||
Reference in New Issue
Block a user