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:
@@ -32,6 +32,47 @@ bool isFinitePose(const robot_geometry_msgs::PoseStamped& pose)
|
||||
std::isfinite(pose.pose.orientation.z) && std::isfinite(pose.pose.orientation.w);
|
||||
}
|
||||
|
||||
/// @brief Các khoá được phép chép từ profile marker sang namespace của global planner.
|
||||
///
|
||||
/// Danh sách đóng, không chép cả cụm: `docking_marker_profiles/<marker>` còn chứa `global_planner`
|
||||
/// và `local_planner`, chép sang sẽ đè lên `library_path`-lân cận và tạo ra khoá vô nghĩa trong
|
||||
/// namespace planner. Thêm khoá mới ở đây thì thêm cả doc trong `maker_sources.yaml`.
|
||||
///
|
||||
/// `footprint_dock` chứ không phải `footprint`: đây là kích thước ĐỐI TƯỢNG DOCK, còn `footprint`
|
||||
/// trong hệ này luôn có nghĩa là footprint robot (costmap, recovery, collision check). Hai thứ vào
|
||||
/// hai công thức khác nhau, trùng tên là mời gọi đặt nhầm giá trị.
|
||||
///
|
||||
/// `dock_anchor` khai goal pose của marker nằm ở tâm footprint dock (`center`) hay ngay trên mặt
|
||||
/// tiếp cận (`face`). `face` thì DockPlanner bỏ qua `footprint_dock` hoàn toàn.
|
||||
constexpr const char* kMarkerPlannerParams[] = {"footprint_dock", "dock_anchor"};
|
||||
|
||||
/**
|
||||
* @brief Chép MỘT khoá giữa hai namespace của param tree, giữ nguyên kiểu.
|
||||
*
|
||||
* Cùng ý với `robot_costmap_2d::move_parameter`, khác hai điểm:
|
||||
* - dùng `YAML::Node` nên chép được cả cấu trúc lồng (footprint là sequence của sequence), không
|
||||
* phải khai sẵn kiểu C++ cho từng khoá;
|
||||
* - trả về việc có chép hay không, để bên gọi biết khi nào phải dựng lại instance.
|
||||
*
|
||||
* @return true nếu khoá tồn tại ở nguồn và đã được ghi sang đích.
|
||||
*/
|
||||
bool copyParamNode(const robot::NodeHandle& from, robot::NodeHandle& to, const std::string& key)
|
||||
{
|
||||
if (!from.hasParam(key))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
YAML::Node value;
|
||||
if (!from.getParam(key, value) || !value.IsDefined())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
to.setParam(key, value);
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
PlannerRunner::PlannerRunner() = default;
|
||||
@@ -103,6 +144,44 @@ robot_nav_core::BaseGlobalPlanner* PlannerRunner::acquire(const std::string& nam
|
||||
const auto cached = planners_.find(name);
|
||||
if (cached != planners_.end())
|
||||
{
|
||||
if (!marker_dirty_)
|
||||
{
|
||||
return cached->second.instance.get();
|
||||
}
|
||||
|
||||
// Marker vừa đổi: instance này đọc footprint của dock trong initialize() và không bao giờ đọc
|
||||
// lại. Dựng lại từ factory sẵn có (không dlopen lại). Instance mới chỉ thay chỗ khi
|
||||
// initialize() thành công — thất bại thì giữ nguyên cache và trả lỗi để bên gọi từ chối yêu
|
||||
// cầu, không để lại trạng thái nửa vời.
|
||||
robot_nav_core::BaseGlobalPlanner::Ptr fresh;
|
||||
try
|
||||
{
|
||||
fresh = cached->second.factory();
|
||||
if (!fresh)
|
||||
{
|
||||
robot::log_error("[move_base2] PlannerRunner: factory of '%s' returned nullptr while "
|
||||
"rebuilding for the new marker.\n", name.c_str());
|
||||
return nullptr;
|
||||
}
|
||||
if (!fresh->initialize(name, costmap_))
|
||||
{
|
||||
robot::log_error("[move_base2] PlannerRunner: '%s' reported an initialize() failure while "
|
||||
"rebuilding for the new marker.\n", name.c_str());
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
catch (const std::exception& ex)
|
||||
{
|
||||
robot::log_error("[move_base2] PlannerRunner: rebuilding '%s' for the new marker failed: %s\n",
|
||||
name.c_str(), ex.what());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Instance cũ được GIỮ, không huỷ: thread lập plan có thể đang ở trong makePlan() của nó với
|
||||
// mutex đã nhả. Xem doc của `Loaded::retired`.
|
||||
cached->second.retired.push_back(std::move(cached->second.instance));
|
||||
cached->second.instance = std::move(fresh);
|
||||
marker_dirty_ = false;
|
||||
return cached->second.instance.get();
|
||||
}
|
||||
|
||||
@@ -179,9 +258,72 @@ robot_nav_core::BaseGlobalPlanner* PlannerRunner::acquire(const std::string& nam
|
||||
// Chỉ đưa vào cache khi đã khởi tạo xong — cache một instance hỏng nghĩa là mọi lần thử lại sau
|
||||
// đều nhận lại đúng cái hỏng đó mà không báo gì.
|
||||
const auto inserted = planners_.emplace(name, std::move(loaded));
|
||||
// Instance mới vừa initialize() với param marker hiện hành — marker không còn "chưa được đọc".
|
||||
marker_dirty_ = false;
|
||||
return inserted.first->second.instance.get();
|
||||
}
|
||||
|
||||
bool PlannerRunner::bindDockingMarker(const std::string& marker, const std::string& planner_name)
|
||||
{
|
||||
if (!configured_)
|
||||
{
|
||||
robot::log_error("[move_base2] PlannerRunner: bindDockingMarker() before configure().\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (marker.empty() || planner_name.empty())
|
||||
{
|
||||
return true; // Chặng không phải docking, hoặc profile không khai global planner riêng.
|
||||
}
|
||||
|
||||
const std::string profile_ns = "docking_marker_profiles/" + marker;
|
||||
if (!nh_.hasParam(profile_ns))
|
||||
{
|
||||
// Marker hợp lệ nhưng không có profile riêng: cặp planner mặc định, không có gì để chép. Không
|
||||
// phải lỗi — `maker_sources` mới là allow-list, bảng profile chỉ là override.
|
||||
return true;
|
||||
}
|
||||
|
||||
// Namespace phải được kiểm TRƯỚC khi dựng NodeHandle: `robot::NodeHandle` không báo lỗi khi
|
||||
// namespace không resolve được mà lặng lẽ rơi về namespace cha. Bỏ qua bước này thì marker gõ sai
|
||||
// sẽ đọc `footprint` ở ROOT — tức footprint ROBOT — rồi dùng nó làm kích thước trạm dock.
|
||||
robot::NodeHandle marker_nh(nh_, profile_ns);
|
||||
robot::NodeHandle planner_nh(nh_, planner_name);
|
||||
if (marker_nh.getNamespace() == nh_.getNamespace() ||
|
||||
planner_nh.getNamespace() == nh_.getNamespace())
|
||||
{
|
||||
robot::log_error("[move_base2] PlannerRunner: namespace '%s' or '%s' does not resolve in the "
|
||||
"param tree; refusing to bind marker '%s'.\n",
|
||||
profile_ns.c_str(), planner_name.c_str(), marker.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
bool copied = false;
|
||||
for (const char* key : kMarkerPlannerParams)
|
||||
{
|
||||
copied = copyParamNode(marker_nh, planner_nh, key) || copied;
|
||||
}
|
||||
|
||||
if (!copied)
|
||||
{
|
||||
return true; // Profile có tồn tại nhưng không khai khoá nào của planner — giữ nguyên cấu hình.
|
||||
}
|
||||
|
||||
// Dựng lại instance CHỈ khi marker nối vào planner này thực sự đổi. Dock lại đúng marker cũ thì
|
||||
// param tree không đổi, initialize() lại cũng ra đúng ngần ấy — đánh dấu dirty vô điều kiện là mỗi
|
||||
// chặng docking một lần initialize() thừa. Bảng theo TỪNG planner vì hai marker có thể trỏ về hai
|
||||
// planner khác nhau và không được xoá dấu của nhau.
|
||||
const auto bound = bound_marker_.find(planner_name);
|
||||
if (bound == bound_marker_.end() || bound->second != marker)
|
||||
{
|
||||
bound_marker_[planner_name] = marker;
|
||||
marker_dirty_ = true;
|
||||
robot::log_info("[move_base2] PlannerRunner: bound marker '%s' params into '%s'.\n",
|
||||
marker.c_str(), planner_name.c_str());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool PlannerRunner::swapPlanner(const std::string& planner_name)
|
||||
{
|
||||
if (!configured_)
|
||||
@@ -196,7 +338,7 @@ bool PlannerRunner::swapPlanner(const std::string& planner_name)
|
||||
return false;
|
||||
}
|
||||
|
||||
if (planner_name == active_name_)
|
||||
if (planner_name == active_name_ && !marker_dirty_)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (active_ != nullptr)
|
||||
@@ -204,6 +346,8 @@ bool PlannerRunner::swapPlanner(const std::string& planner_name)
|
||||
return true; // Đã đúng planner; không log để khỏi spam ở cửa vào mỗi yêu cầu.
|
||||
}
|
||||
}
|
||||
// Marker vừa đổi thì KHÔNG được đi tắt kể cả khi tên planner không đổi: phải rơi xuống acquire()
|
||||
// để instance được dựng lại và initialize() đọc footprint mới.
|
||||
|
||||
robot_nav_core::BaseGlobalPlanner* planner = acquire(planner_name);
|
||||
if (planner == nullptr)
|
||||
|
||||
Reference in New Issue
Block a user