optimal & fix file cmake
This commit is contained in:
@@ -1,15 +1,11 @@
|
||||
/*********************************************************************
|
||||
* move_base2 — hiện thực ActionPort bằng các ActionHandler plugin.
|
||||
* move_base2 — hiện thực ActionPort bằng framework action_core.
|
||||
*
|
||||
* Author: DuongTD
|
||||
*********************************************************************/
|
||||
#include <move_base2/runners/action_runner.h>
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include <boost/dll/import.hpp>
|
||||
#include <boost/system/system_error.hpp>
|
||||
#include <yaml-cpp/yaml.h>
|
||||
#include <string>
|
||||
|
||||
#include <robot/robot.h>
|
||||
|
||||
@@ -18,11 +14,9 @@ namespace move_base2
|
||||
|
||||
ActionRunner::~ActionRunner()
|
||||
{
|
||||
// Handler phải chết TRƯỚC factory: factory là thứ giữ .so còn nạp.
|
||||
// Con trỏ này trỏ vào handler thuộc registry_. Bỏ nó trước khi registry_ bị huỷ để không ai còn
|
||||
// đường chạm vào một handler đã chết.
|
||||
active_ = nullptr;
|
||||
by_type_.clear();
|
||||
handlers_.clear();
|
||||
factories_.clear();
|
||||
}
|
||||
|
||||
void ActionRunner::setClock(ClockPort* clock)
|
||||
@@ -35,199 +29,59 @@ void ActionRunner::setNamespace(const std::string& ns)
|
||||
namespace_ = ns;
|
||||
}
|
||||
|
||||
bool ActionRunner::registerHandler(const ActionHandler::Ptr& handler)
|
||||
void ActionRunner::setContext(const action_core::ActionContext& context)
|
||||
{
|
||||
if (!handler)
|
||||
{
|
||||
robot::log_error("[move_base2] ActionRunner: handler null.");
|
||||
return false;
|
||||
}
|
||||
|
||||
const std::vector<std::string> types = handler->supportedActionTypes();
|
||||
if (types.empty())
|
||||
{
|
||||
robot::log_error("[move_base2] ActionRunner: handler không khai actionType nào — sẽ không bao "
|
||||
"giờ được gọi.");
|
||||
return false;
|
||||
}
|
||||
|
||||
for (const std::string& type : types)
|
||||
{
|
||||
if (type.empty())
|
||||
{
|
||||
robot::log_error("[move_base2] ActionRunner: handler khai một actionType rỗng.");
|
||||
return false;
|
||||
}
|
||||
if (by_type_.find(type) != by_type_.end())
|
||||
{
|
||||
// Hai handler cùng nhận một type thì việc định tuyến phụ thuộc thứ tự nạp — từ chối thay vì
|
||||
// im lặng ghi đè.
|
||||
robot::log_error("[move_base2] ActionRunner: actionType '%s' đã có handler khác đăng ký.",
|
||||
type.c_str());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
handlers_.push_back(handler);
|
||||
for (const std::string& type : types)
|
||||
{
|
||||
by_type_[type] = handler.get();
|
||||
}
|
||||
return true;
|
||||
context_ = context;
|
||||
}
|
||||
|
||||
ActionHandler* ActionRunner::find(const std::string& action_type) const
|
||||
bool ActionRunner::registerHandler(const action_core::ActionHandler::Ptr& handler)
|
||||
{
|
||||
const auto it = by_type_.find(action_type);
|
||||
return it == by_type_.end() ? nullptr : it->second;
|
||||
return registry_.registerHandler(handler);
|
||||
}
|
||||
|
||||
std::vector<std::string> ActionRunner::supportedActionTypes() const
|
||||
ActionTick ActionRunner::toTick(const action_core::ActionTick& tick)
|
||||
{
|
||||
std::vector<std::string> types;
|
||||
types.reserve(by_type_.size());
|
||||
for (const auto& entry : by_type_)
|
||||
{
|
||||
types.push_back(entry.first);
|
||||
}
|
||||
return types;
|
||||
}
|
||||
ActionTick out;
|
||||
out.message = tick.message;
|
||||
|
||||
bool ActionRunner::loadOne(const std::string& name, const std::string& type,
|
||||
robot::NodeHandle& nh, const std::string& ns)
|
||||
{
|
||||
robot::PluginLoaderHelper loader(nh);
|
||||
const std::string library_path = loader.findLibraryPath(type);
|
||||
|
||||
if (library_path.empty())
|
||||
switch (tick.status)
|
||||
{
|
||||
robot::log_error("[move_base2] ActionRunner: không tìm được thư viện cho '%s' — kiểm khoá "
|
||||
"'%s/library_path' trong YAML và sự tồn tại của file .so.",
|
||||
type.c_str(), type.c_str());
|
||||
return false;
|
||||
case action_core::ActionStatus::kRunning:
|
||||
out.status = ActionTick::Status::kRunning;
|
||||
break;
|
||||
case action_core::ActionStatus::kSucceeded:
|
||||
out.status = ActionTick::Status::kSucceeded;
|
||||
break;
|
||||
case action_core::ActionStatus::kFailed:
|
||||
out.status = ActionTick::Status::kFailed;
|
||||
break;
|
||||
}
|
||||
|
||||
std::function<ActionHandler::Ptr()> factory;
|
||||
try
|
||||
{
|
||||
factory = boost::dll::import_alias<ActionHandler::Ptr()>(
|
||||
library_path, type, boost::dll::load_mode::append_decorations);
|
||||
}
|
||||
catch (const boost::system::system_error& ex)
|
||||
{
|
||||
robot::log_error("[move_base2] ActionRunner: không nạp được symbol '%s' từ '%s': %s",
|
||||
type.c_str(), library_path.c_str(), ex.what());
|
||||
return false;
|
||||
}
|
||||
catch (const std::exception& ex)
|
||||
{
|
||||
robot::log_error("[move_base2] ActionRunner: lỗi khi nạp '%s': %s", type.c_str(), ex.what());
|
||||
return false;
|
||||
}
|
||||
|
||||
ActionHandler::Ptr handler;
|
||||
try
|
||||
{
|
||||
handler = factory();
|
||||
}
|
||||
catch (const std::exception& ex)
|
||||
{
|
||||
robot::log_error("[move_base2] ActionRunner: factory của '%s' ném exception: %s", type.c_str(),
|
||||
ex.what());
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!handler)
|
||||
{
|
||||
robot::log_error("[move_base2] ActionRunner: factory của '%s' trả về null.", type.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
const std::string param_ns = ns.empty() ? name : ns + "/" + name;
|
||||
robot::NodeHandle handler_nh(nh, param_ns);
|
||||
|
||||
if (!handler->configure(name, handler_nh))
|
||||
{
|
||||
robot::log_error("[move_base2] ActionRunner: '%s' (instance '%s') configure() thất bại.",
|
||||
type.c_str(), name.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!registerHandler(handler))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
factories_.push_back(std::move(factory));
|
||||
|
||||
robot::log_info("[move_base2] ActionRunner: nạp '%s' (instance '%s').", type.c_str(),
|
||||
name.c_str());
|
||||
return true;
|
||||
return out;
|
||||
}
|
||||
|
||||
bool ActionRunner::configure(robot::NodeHandle& nh)
|
||||
{
|
||||
if (configured_)
|
||||
{
|
||||
robot::log_error("[move_base2] ActionRunner: configure() gọi lần thứ hai.");
|
||||
robot::log_error("[move_base2] ActionRunner: configure() called twice.");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (clock_ == nullptr)
|
||||
{
|
||||
robot::log_error("[move_base2] ActionRunner: thiếu ClockPort — handler không có mốc timeout.");
|
||||
// Registry không biết thời gian; handler thì cần mốc để tự timeout. Thiếu đồng hồ là mọi
|
||||
// handler mất tầng timeout chính của contract.
|
||||
robot::log_error("[move_base2] ActionRunner: missing ClockPort — handlers would have no "
|
||||
"timeout reference.");
|
||||
return false;
|
||||
}
|
||||
|
||||
const std::string key = namespace_.empty() ? std::string("handlers") : namespace_ + "/handlers";
|
||||
|
||||
YAML::Node list;
|
||||
if (!nh.getParam(key, list) || !list.IsSequence())
|
||||
{
|
||||
// Không có handler nào là hợp lệ: hệ không có thiết bị thì mọi mission đều nav-only, và
|
||||
// ControlLoop::submit đã từ chối yêu cầu mang action ngay tại cửa.
|
||||
robot::log_warning("[move_base2] ActionRunner: '%s' không có danh sách handler — runtime sẽ "
|
||||
"từ chối mọi yêu cầu mang action.", key.c_str());
|
||||
configured_ = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool all_ok = true;
|
||||
|
||||
for (std::size_t i = 0; i < list.size(); ++i)
|
||||
{
|
||||
const YAML::Node& entry = list[i];
|
||||
|
||||
if (!entry.IsMap() || !entry["type"])
|
||||
{
|
||||
robot::log_error("[move_base2] ActionRunner: '%s[%zu]' thiếu khoá 'type'.", key.c_str(), i);
|
||||
all_ok = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
std::string type;
|
||||
std::string name;
|
||||
try
|
||||
{
|
||||
type = entry["type"].as<std::string>();
|
||||
name = entry["name"] ? entry["name"].as<std::string>() : type;
|
||||
}
|
||||
catch (const YAML::Exception& ex)
|
||||
{
|
||||
robot::log_error("[move_base2] ActionRunner: '%s[%zu]' không đọc được: %s", key.c_str(), i,
|
||||
ex.what());
|
||||
all_ok = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!loadOne(name, type, nh, namespace_))
|
||||
{
|
||||
all_ok = false;
|
||||
}
|
||||
}
|
||||
registry_.setContext(context_);
|
||||
|
||||
const bool ok = registry_.loadFromConfig(nh, namespace_);
|
||||
configured_ = true;
|
||||
return all_ok;
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool ActionRunner::start(const robot_protocol_msgs::Action& action)
|
||||
@@ -237,27 +91,40 @@ bool ActionRunner::start(const robot_protocol_msgs::Action& action)
|
||||
|
||||
if (!configured_)
|
||||
{
|
||||
robot::log_error("[move_base2] ActionRunner: start() trước configure().");
|
||||
robot::log_error("[move_base2] ActionRunner: start() before configure().");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (action.actionType.empty())
|
||||
{
|
||||
robot::log_error("[move_base2] ActionRunner: action không có actionType.");
|
||||
robot::log_error("[move_base2] ActionRunner: action has no actionType.");
|
||||
return false;
|
||||
}
|
||||
|
||||
ActionHandler* handler = find(action.actionType);
|
||||
action_core::ActionHandler* handler = registry_.find(action.actionType);
|
||||
if (handler == nullptr)
|
||||
{
|
||||
robot::log_error("[move_base2] ActionRunner: không handler nào nhận actionType '%s' (id '%s').",
|
||||
action.actionType.c_str(), action.actionId.c_str());
|
||||
// Liệt kê luôn những gì ĐƯỢC nhận. Không có nó, người đọc log mở config ra thấy đúng chữ mình
|
||||
// vừa gửi (tên instance) và tưởng đã khai rồi — trong khi khoá định tuyến là `action_types`,
|
||||
// một tên khác nằm ngay bên dưới.
|
||||
std::string accepted;
|
||||
for (const std::string& type : registry_.actionTypes())
|
||||
{
|
||||
accepted += accepted.empty() ? "" : ", ";
|
||||
accepted += type;
|
||||
}
|
||||
|
||||
robot::log_error("[move_base2] ActionRunner: no handler accepts actionType '%s' (id '%s'). "
|
||||
"Accepted actionTypes: [%s]. Lưu ý: khoá định tuyến là `action_types`, không "
|
||||
"phải tên instance trong `handlers`.",
|
||||
action.actionType.c_str(), action.actionId.c_str(),
|
||||
accepted.empty() ? "<none>" : accepted.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!handler->start(action, clock_->now()))
|
||||
{
|
||||
robot::log_warning("[move_base2] ActionRunner: handler từ chối khởi động action '%s' (id '%s').",
|
||||
robot::log_warning("[move_base2] ActionRunner: handler refused to start action '%s' (id '%s').",
|
||||
action.actionType.c_str(), action.actionId.c_str());
|
||||
return false;
|
||||
}
|
||||
@@ -269,18 +136,17 @@ bool ActionRunner::start(const robot_protocol_msgs::Action& action)
|
||||
|
||||
ActionTick ActionRunner::update()
|
||||
{
|
||||
ActionTick tick;
|
||||
|
||||
if (active_ == nullptr)
|
||||
{
|
||||
// Contract nói update() chỉ được gọi sau start() trả true. Vẫn guard: lỗi thứ tự gọi phải thành
|
||||
// "action này hỏng" chứ không phải dereference null.
|
||||
ActionTick tick;
|
||||
tick.status = ActionTick::Status::kFailed;
|
||||
tick.message = "update() khi không có action nào đang chạy";
|
||||
tick.message = "update() with no action running";
|
||||
return tick;
|
||||
}
|
||||
|
||||
tick = active_->update(clock_->now());
|
||||
const ActionTick tick = toTick(active_->update(clock_->now()));
|
||||
|
||||
if (tick.status != ActionTick::Status::kRunning)
|
||||
{
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
#include <cmath>
|
||||
#include <exception>
|
||||
#include <sstream>
|
||||
#include <utility>
|
||||
|
||||
#include <boost/dll/import.hpp>
|
||||
@@ -39,6 +40,17 @@ bool isFiniteTwist(const robot_geometry_msgs::Twist& twist)
|
||||
ControllerRunner::ControllerRunner() = default;
|
||||
ControllerRunner::~ControllerRunner() = default;
|
||||
|
||||
void ControllerRunner::attachStats(RuntimeStats* stats)
|
||||
{
|
||||
stats_ = stats;
|
||||
if (stats_ == nullptr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
section_compute_ = stats_->section("controller.compute");
|
||||
section_local_plan_ = stats_->section("controller.getLocalPlan");
|
||||
}
|
||||
|
||||
bool ControllerRunner::configure(const robot::NodeHandle& nh,
|
||||
const std::shared_ptr<tf3::BufferCore>& tf,
|
||||
robot_costmap_2d::Costmap2DROBOT* costmap, const PosePort* pose,
|
||||
@@ -46,13 +58,13 @@ bool ControllerRunner::configure(const robot::NodeHandle& nh,
|
||||
{
|
||||
if (configured_)
|
||||
{
|
||||
error = "ControllerRunner::configure() gọi lần thứ hai";
|
||||
error = "ControllerRunner::configure() called twice";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (costmap == nullptr)
|
||||
{
|
||||
error = "ControllerRunner cần costmap local khác null";
|
||||
error = "ControllerRunner needs a non-null local costmap";
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -60,7 +72,7 @@ bool ControllerRunner::configure(const robot::NodeHandle& nh,
|
||||
{
|
||||
// Gen-2 nhận pose làm THAM SỐ của computeVelocityCommands/isGoalReached; không có nguồn pose
|
||||
// thì không gọi được hàm nào trong hai hàm đó.
|
||||
error = "ControllerRunner cần PosePort khác null";
|
||||
error = "ControllerRunner needs a non-null PosePort";
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -72,7 +84,7 @@ bool ControllerRunner::configure(const robot::NodeHandle& nh,
|
||||
|
||||
if (!initial_controller.empty() && !swapPlanner(initial_controller))
|
||||
{
|
||||
error = "không nạp được local planner khởi đầu '" + initial_controller + "'";
|
||||
error = "could not load the initial local planner '" + initial_controller + "'";
|
||||
configured_ = false;
|
||||
return false;
|
||||
}
|
||||
@@ -85,6 +97,35 @@ robot_nav_core2::LocalPlanner* ControllerRunner::acquire(const std::string& name
|
||||
const auto cached = controllers_.find(name);
|
||||
if (cached != controllers_.end())
|
||||
{
|
||||
if (!marker_dirty_)
|
||||
{
|
||||
return cached->second.instance.get();
|
||||
}
|
||||
|
||||
// Marker vừa đổi: instance này có thể đã đọc `maker_name` 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 thay chỗ instance cũ
|
||||
// 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.
|
||||
try
|
||||
{
|
||||
robot_nav_core2::LocalPlanner::Ptr fresh = cached->second.factory();
|
||||
if (!fresh)
|
||||
{
|
||||
robot::log_error("[move_base2] ControllerRunner: factory of '%s' returned nullptr while "
|
||||
"rebuilding for the new marker.\n", name.c_str());
|
||||
return nullptr;
|
||||
}
|
||||
fresh->initialize(nh_, name, tf_, costmap_);
|
||||
cached->second.instance = std::move(fresh);
|
||||
}
|
||||
catch (const std::exception& ex)
|
||||
{
|
||||
robot::log_error("[move_base2] ControllerRunner: rebuilding '%s' for the new marker failed: "
|
||||
"%s\n",
|
||||
name.c_str(), ex.what());
|
||||
return nullptr;
|
||||
}
|
||||
marker_dirty_ = false;
|
||||
return cached->second.instance.get();
|
||||
}
|
||||
|
||||
@@ -93,8 +134,8 @@ robot_nav_core2::LocalPlanner* ControllerRunner::acquire(const std::string& name
|
||||
|
||||
if (library_path.empty())
|
||||
{
|
||||
robot::log_error("[move_base2] ControllerRunner: không tìm được thư viện cho '%s' — kiểm khoá "
|
||||
"'%s/library_path' trong YAML và sự tồn tại của file .so trong devel/lib.\n",
|
||||
robot::log_error("[move_base2] ControllerRunner: no library found for '%s' — check the key "
|
||||
"'%s/library_path' in the YAML and that the .so file exists in devel/lib.\n",
|
||||
name.c_str(), name.c_str());
|
||||
return nullptr;
|
||||
}
|
||||
@@ -108,13 +149,13 @@ robot_nav_core2::LocalPlanner* ControllerRunner::acquire(const std::string& name
|
||||
}
|
||||
catch (const boost::system::system_error& ex)
|
||||
{
|
||||
robot::log_error("[move_base2] ControllerRunner: không nạp được symbol '%s' từ '%s': %s\n",
|
||||
robot::log_error("[move_base2] ControllerRunner: could not load symbol '%s' from '%s': %s\n",
|
||||
name.c_str(), library_path.c_str(), ex.what());
|
||||
return nullptr;
|
||||
}
|
||||
catch (const std::exception& ex)
|
||||
{
|
||||
robot::log_error("[move_base2] ControllerRunner: lỗi khi nạp '%s': %s\n", name.c_str(),
|
||||
robot::log_error("[move_base2] ControllerRunner: error while loading '%s': %s\n", name.c_str(),
|
||||
ex.what());
|
||||
return nullptr;
|
||||
}
|
||||
@@ -125,14 +166,15 @@ robot_nav_core2::LocalPlanner* ControllerRunner::acquire(const std::string& name
|
||||
}
|
||||
catch (const std::exception& ex)
|
||||
{
|
||||
robot::log_error("[move_base2] ControllerRunner: factory của '%s' ném exception: %s\n",
|
||||
robot::log_error("[move_base2] ControllerRunner: factory of '%s' threw an exception: %s\n",
|
||||
name.c_str(), ex.what());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (!loaded.instance)
|
||||
{
|
||||
robot::log_error("[move_base2] ControllerRunner: factory của '%s' trả nullptr.\n", name.c_str());
|
||||
robot::log_error("[move_base2] ControllerRunner: factory of '%s' returned nullptr.\n",
|
||||
name.c_str());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -144,15 +186,59 @@ robot_nav_core2::LocalPlanner* ControllerRunner::acquire(const std::string& name
|
||||
}
|
||||
catch (const std::exception& ex)
|
||||
{
|
||||
robot::log_error("[move_base2] ControllerRunner: initialize() của '%s' ném exception: %s\n",
|
||||
robot::log_error("[move_base2] ControllerRunner: initialize() of '%s' threw an exception: %s\n",
|
||||
name.c_str(), ex.what());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const auto inserted = controllers_.emplace(name, std::move(loaded));
|
||||
// Instance mới vừa initialize() với `maker_name` hiện hành — marker không còn "chưa được đọc".
|
||||
marker_dirty_ = false;
|
||||
return inserted.first->second.instance.get();
|
||||
}
|
||||
|
||||
bool ControllerRunner::setDockingMarker(const std::string& marker)
|
||||
{
|
||||
if (!configured_)
|
||||
{
|
||||
robot::log_error("[move_base2] ControllerRunner: setDockingMarker() before configure().\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Validate với danh sách `maker_sources` (chuỗi cách nhau bằng space, maker_sources.yaml) —
|
||||
// đúng phép kiểm bản cũ làm ở cửa dockTo (move_base.cpp:1161-1173). Marker lạ phải bị chặn ở
|
||||
// đây: để lọt xuống thì getMaker() của docking planner âm thầm không match source nào và robot
|
||||
// đứng im không lý do.
|
||||
std::string sources;
|
||||
nh_.param("maker_sources", sources, std::string(""));
|
||||
std::stringstream ss(sources);
|
||||
std::string source;
|
||||
bool known = false;
|
||||
while (ss >> source)
|
||||
{
|
||||
if (source == marker)
|
||||
{
|
||||
known = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!known)
|
||||
{
|
||||
robot::log_error("[move_base2] ControllerRunner: marker '%s' is not listed in maker_sources "
|
||||
"('%s').\n", marker.c_str(), sources.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string current;
|
||||
nh_.param("maker_name", current, std::string(""));
|
||||
if (current != marker)
|
||||
{
|
||||
nh_.setParam("maker_name", marker);
|
||||
marker_dirty_ = true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void ControllerRunner::applyPendingLimits(robot_nav_core2::LocalPlanner* controller)
|
||||
{
|
||||
if (controller == nullptr)
|
||||
@@ -180,7 +266,8 @@ void ControllerRunner::applyPendingLimits(robot_nav_core2::LocalPlanner* control
|
||||
}
|
||||
catch (const std::exception& ex)
|
||||
{
|
||||
robot::log_error("[move_base2] ControllerRunner: lỗi khi áp lại trần vận tốc: %s\n", ex.what());
|
||||
robot::log_error("[move_base2] ControllerRunner: error while re-applying the velocity limits: "
|
||||
"%s\n", ex.what());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -188,19 +275,22 @@ bool ControllerRunner::swapPlanner(const std::string& planner_name)
|
||||
{
|
||||
if (!configured_)
|
||||
{
|
||||
robot::log_error("[move_base2] ControllerRunner: swapPlanner() trước configure().\n");
|
||||
robot::log_error("[move_base2] ControllerRunner: swapPlanner() before configure().\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (planner_name.empty())
|
||||
{
|
||||
robot::log_error("[move_base2] ControllerRunner: tên controller rỗng.\n");
|
||||
robot::log_error("[move_base2] ControllerRunner: empty controller name.\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (planner_name == active_name_ && active_ != nullptr)
|
||||
if (planner_name == active_name_ && active_ != nullptr && !marker_dirty_)
|
||||
{
|
||||
return true; // Đã đúng controller; không log để khỏi spam ở cửa vào mỗi yêu cầu.
|
||||
// Đã đúng controller; không log để khỏi spam ở cửa vào mỗi yêu cầu. Riêng khi marker vừa đổi
|
||||
// thì KHÔNG được đi tắt: dock lại cùng planner với marker khác phải rơi xuống acquire() để
|
||||
// instance được dựng lại và initialize() đọc `maker_name` mới.
|
||||
return true;
|
||||
}
|
||||
|
||||
robot_nav_core2::LocalPlanner* controller = acquire(planner_name);
|
||||
@@ -213,33 +303,21 @@ bool ControllerRunner::swapPlanner(const std::string& planner_name)
|
||||
active_ = controller;
|
||||
active_name_ = planner_name;
|
||||
has_active_goal_ = false; // Instance mới chưa biết goal nào.
|
||||
active_plan_.clear();
|
||||
applyPendingLimits(active_);
|
||||
|
||||
robot::log_info("[move_base2] ControllerRunner: local planner đang dùng là '%s'.\n",
|
||||
robot::log_info("[move_base2] ControllerRunner: active local planner is '%s'.\n",
|
||||
planner_name.c_str());
|
||||
return true;
|
||||
}
|
||||
|
||||
void ControllerRunner::setTolerance(double xy_m, double yaw_rad)
|
||||
{
|
||||
if (!configured_)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Interface gen-1 không có hàm đặt sai số; bản cũ ghi vào param rồi để planner tự đọc lại. Kênh
|
||||
// gián tiếp này được giữ nguyên để không đổi hành vi của các planner đang chạy — nhưng planner
|
||||
// nào chỉ đọc param lúc initialize sẽ KHÔNG thấy giá trị mới. Xem doc của lớp.
|
||||
nh_.setParam("xy_goal_tolerance", xy_m);
|
||||
nh_.setParam("yaw_goal_tolerance", yaw_rad);
|
||||
}
|
||||
|
||||
bool ControllerRunner::setPlan(const std::vector<robot_geometry_msgs::PoseStamped>& plan)
|
||||
{
|
||||
if (!configured_ || active_ == nullptr)
|
||||
{
|
||||
robot::log_error_throttle(kHotPathLogThrottle,
|
||||
"[move_base2] ControllerRunner: setPlan() khi chưa có controller.\n");
|
||||
"[move_base2] ControllerRunner: setPlan() with no controller "
|
||||
"loaded.\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -247,7 +325,7 @@ bool ControllerRunner::setPlan(const std::vector<robot_geometry_msgs::PoseStampe
|
||||
{
|
||||
// Plan rỗng lọt xuống sẽ thành front()/back() trên vector rỗng bên trong planner.
|
||||
robot::log_error_throttle(kHotPathLogThrottle,
|
||||
"[move_base2] ControllerRunner: từ chối plan rỗng.\n");
|
||||
"[move_base2] ControllerRunner: rejecting an empty plan.\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -259,7 +337,8 @@ bool ControllerRunner::setPlan(const std::vector<robot_geometry_msgs::PoseStampe
|
||||
if (path.poses.empty())
|
||||
{
|
||||
robot::log_error_throttle(kHotPathLogThrottle,
|
||||
"[move_base2] ControllerRunner: plan chuyển sang Path2D bị rỗng.\n");
|
||||
"[move_base2] ControllerRunner: plan converted to Path2D came out "
|
||||
"empty.\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -269,17 +348,84 @@ bool ControllerRunner::setPlan(const std::vector<robot_geometry_msgs::PoseStampe
|
||||
active_->setGoalPose(goal_pose);
|
||||
active_->setPlan(path);
|
||||
has_active_goal_ = true;
|
||||
active_plan_ = plan;
|
||||
return true;
|
||||
}
|
||||
catch (const std::exception& ex)
|
||||
{
|
||||
robot::log_error_throttle(kHotPathLogThrottle,
|
||||
"[move_base2] ControllerRunner: '%s' ném exception trong setPlan: "
|
||||
"[move_base2] ControllerRunner: '%s' threw an exception in setPlan: "
|
||||
"%s\n", active_name_.c_str(), ex.what());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool ControllerRunner::refreshActivePlanner()
|
||||
{
|
||||
if (!configured_ || active_ == nullptr)
|
||||
{
|
||||
// Chưa có controller active thì không có cache nào cần refresh. Hành vi này giúp footprint có
|
||||
// thể được host đặt trước goal đầu tiên mà không biến thành lỗi khởi tạo.
|
||||
return true;
|
||||
}
|
||||
|
||||
const auto loaded = controllers_.find(active_name_);
|
||||
if (loaded == controllers_.end())
|
||||
{
|
||||
robot::log_error("[move_base2] ControllerRunner: active local planner '%s' is absent from "
|
||||
"the plugin cache.\n", active_name_.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
const bool had_active_goal = has_active_goal_;
|
||||
const std::vector<robot_geometry_msgs::PoseStamped> saved_plan = active_plan_;
|
||||
if (had_active_goal && saved_plan.empty())
|
||||
{
|
||||
// Không thay instance cũ nếu không thể khôi phục goal đang chạy. Giữ controller hiện tại vẫn
|
||||
// an toàn hơn việc âm thầm biến navigation thành controller không có plan.
|
||||
robot::log_error("[move_base2] ControllerRunner: active planner '%s' has a goal but no "
|
||||
"cached plan to restore after a footprint change.\n", active_name_.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
robot_nav_core2::LocalPlanner::Ptr fresh;
|
||||
try
|
||||
{
|
||||
fresh = loaded->second.factory();
|
||||
if (!fresh)
|
||||
{
|
||||
robot::log_error("[move_base2] ControllerRunner: factory of '%s' returned nullptr while "
|
||||
"refreshing its footprint cache.\n", active_name_.c_str());
|
||||
return false;
|
||||
}
|
||||
fresh->initialize(nh_, active_name_, tf_, costmap_);
|
||||
}
|
||||
catch (const std::exception& ex)
|
||||
{
|
||||
// Chỉ thay cache SAU initialize thành công, nên lỗi này không làm mất controller cũ.
|
||||
robot::log_error("[move_base2] ControllerRunner: refreshing '%s' after a footprint change "
|
||||
"failed: %s\n", active_name_.c_str(), ex.what());
|
||||
return false;
|
||||
}
|
||||
|
||||
loaded->second.instance = std::move(fresh);
|
||||
active_ = loaded->second.instance.get();
|
||||
has_active_goal_ = false;
|
||||
active_plan_.clear();
|
||||
applyPendingLimits(active_);
|
||||
|
||||
if (had_active_goal && !setPlan(saved_plan))
|
||||
{
|
||||
robot::log_error("[move_base2] ControllerRunner: could not restore the active plan after "
|
||||
"refreshing '%s' for a footprint change.\n", active_name_.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
robot::log_info("[move_base2] ControllerRunner: refreshed '%s' after the local costmap "
|
||||
"footprint changed.\n", active_name_.c_str());
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ControllerRunner::currentPose(robot_nav_2d_msgs::Pose2DStamped& pose) const
|
||||
{
|
||||
if (pose_ == nullptr)
|
||||
@@ -302,8 +448,8 @@ bool ControllerRunner::computeVelocityCommands(robot_geometry_msgs::Twist& cmd)
|
||||
if (!configured_ || active_ == nullptr)
|
||||
{
|
||||
robot::log_error_throttle(kHotPathLogThrottle,
|
||||
"[move_base2] ControllerRunner: computeVelocityCommands() khi chưa "
|
||||
"có controller.\n");
|
||||
"[move_base2] ControllerRunner: computeVelocityCommands() with no "
|
||||
"controller loaded.\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -317,7 +463,8 @@ bool ControllerRunner::computeVelocityCommands(robot_geometry_msgs::Twist& cmd)
|
||||
if (!currentPose(pose))
|
||||
{
|
||||
robot::log_error_throttle(kHotPathLogThrottle,
|
||||
"[move_base2] ControllerRunner: mất pose, không tính lệnh.\n");
|
||||
"[move_base2] ControllerRunner: pose lost, not computing a "
|
||||
"command.\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -325,6 +472,9 @@ bool ControllerRunner::computeVelocityCommands(robot_geometry_msgs::Twist& cmd)
|
||||
|
||||
try
|
||||
{
|
||||
// Đo đúng lời gọi vào plugin, không đo cả hàm: phần còn lại (tra pose, kiểm NaN) là chi phí của
|
||||
// move_base2, còn đây mới là chi phí của local planner đang cấu hình.
|
||||
ScopedSection timer(stats_, section_compute_);
|
||||
// Gen-2 trả THẲNG lệnh (không có cờ thành công/thất bại) và ném exception khi không tính được —
|
||||
// ngược với gen-1. Vì vậy nhánh "không có lệnh hợp lệ" ở đây là nhánh catch.
|
||||
const robot_nav_2d_msgs::Twist2DStamped cmd_2d =
|
||||
@@ -334,7 +484,7 @@ bool ControllerRunner::computeVelocityCommands(robot_geometry_msgs::Twist& cmd)
|
||||
catch (const std::exception& ex)
|
||||
{
|
||||
robot::log_error_throttle(kHotPathLogThrottle,
|
||||
"[move_base2] ControllerRunner: '%s' không sinh được lệnh: %s\n",
|
||||
"[move_base2] ControllerRunner: '%s' produced no command: %s\n",
|
||||
active_name_.c_str(), ex.what());
|
||||
return false;
|
||||
}
|
||||
@@ -344,7 +494,8 @@ bool ControllerRunner::computeVelocityCommands(robot_geometry_msgs::Twist& cmd)
|
||||
// VelocityArbiter cũng chặn NaN/Inf, nhưng chặn ngay tại nguồn cho biết ĐÚNG plugin nào đang
|
||||
// trả dữ liệu hỏng — arbiter chỉ thấy một con số vô nghĩa không rõ từ đâu.
|
||||
robot::log_error_throttle(kHotPathLogThrottle,
|
||||
"[move_base2] ControllerRunner: '%s' trả lệnh chứa NaN/Inf.\n",
|
||||
"[move_base2] ControllerRunner: '%s' returned a command containing "
|
||||
"NaN/Inf.\n",
|
||||
active_name_.c_str());
|
||||
return false;
|
||||
}
|
||||
@@ -378,6 +529,7 @@ bool ControllerRunner::isGoalReached()
|
||||
if (reached)
|
||||
{
|
||||
has_active_goal_ = false;
|
||||
active_plan_.clear();
|
||||
}
|
||||
return reached;
|
||||
}
|
||||
@@ -386,7 +538,7 @@ bool ControllerRunner::isGoalReached()
|
||||
// Trả false: "chưa tới đích" là phía an toàn — báo nhầm đã tới sẽ kết thúc chặng đường sớm và
|
||||
// robot dừng ở chỗ không phải đích.
|
||||
robot::log_error_throttle(kHotPathLogThrottle,
|
||||
"[move_base2] ControllerRunner: '%s' ném exception trong "
|
||||
"[move_base2] ControllerRunner: '%s' threw an exception in "
|
||||
"isGoalReached: %s\n", active_name_.c_str(), ex.what());
|
||||
return false;
|
||||
}
|
||||
@@ -403,6 +555,7 @@ void ControllerRunner::getLocalPlan(robot_nav_2d_msgs::Path2D& plan)
|
||||
|
||||
try
|
||||
{
|
||||
ScopedSection timer(stats_, section_local_plan_);
|
||||
active_->getPlan(plan);
|
||||
}
|
||||
catch (const std::exception& ex)
|
||||
@@ -410,8 +563,8 @@ void ControllerRunner::getLocalPlan(robot_nav_2d_msgs::Path2D& plan)
|
||||
// Không phải mọi planner đều hỗ trợ; gen-2 cho phép ném. Đây chỉ là dữ liệu hiển thị nên nuốt
|
||||
// exception là đúng — nhưng vẫn log để không ai tưởng rviz đang hiện quỹ đạo thật.
|
||||
robot::log_error_throttle(kHotPathLogThrottle,
|
||||
"[move_base2] ControllerRunner: '%s' không trả được quỹ đạo cục bộ: "
|
||||
"%s\n", active_name_.c_str(), ex.what());
|
||||
"[move_base2] ControllerRunner: '%s' could not return a local "
|
||||
"trajectory: %s\n", active_name_.c_str(), ex.what());
|
||||
plan = robot_nav_2d_msgs::Path2D();
|
||||
}
|
||||
}
|
||||
@@ -423,7 +576,8 @@ void ControllerRunner::setMeasuredVelocity(const robot_geometry_msgs::Twist& vel
|
||||
// Giữ giá trị cũ thay vì đưa NaN vào hàm tính lệnh — nhiều local planner dùng nó làm mốc giới
|
||||
// hạn gia tốc, và NaN ở đó lan ra toàn bộ cost function.
|
||||
robot::log_error_throttle(kHotPathLogThrottle,
|
||||
"[move_base2] ControllerRunner: bỏ vận tốc đo được chứa NaN/Inf.\n");
|
||||
"[move_base2] ControllerRunner: dropping a measured velocity "
|
||||
"containing NaN/Inf.\n");
|
||||
return;
|
||||
}
|
||||
measured_velocity_ = velocity;
|
||||
@@ -433,7 +587,8 @@ bool ControllerRunner::setTwistLinear(const robot_geometry_msgs::Vector3& linear
|
||||
{
|
||||
if (!std::isfinite(linear.x) || !std::isfinite(linear.y) || !std::isfinite(linear.z))
|
||||
{
|
||||
robot::log_error("[move_base2] ControllerRunner: trần vận tốc thẳng chứa NaN/Inf, bỏ qua.\n");
|
||||
robot::log_error("[move_base2] ControllerRunner: linear velocity limit contains NaN/Inf, "
|
||||
"ignored.\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -463,7 +618,8 @@ bool ControllerRunner::setTwistLinear(const robot_geometry_msgs::Vector3& linear
|
||||
}
|
||||
catch (const std::exception& ex)
|
||||
{
|
||||
robot::log_error("[move_base2] ControllerRunner: '%s' ném exception trong setTwistLinear: %s\n",
|
||||
robot::log_error("[move_base2] ControllerRunner: '%s' threw an exception in setTwistLinear: "
|
||||
"%s\n",
|
||||
active_name_.c_str(), ex.what());
|
||||
return false;
|
||||
}
|
||||
@@ -473,7 +629,8 @@ bool ControllerRunner::setTwistAngular(const robot_geometry_msgs::Vector3& angul
|
||||
{
|
||||
if (!std::isfinite(angular.x) || !std::isfinite(angular.y) || !std::isfinite(angular.z))
|
||||
{
|
||||
robot::log_error("[move_base2] ControllerRunner: trần vận tốc góc chứa NaN/Inf, bỏ qua.\n");
|
||||
robot::log_error("[move_base2] ControllerRunner: angular velocity limit contains NaN/Inf, "
|
||||
"ignored.\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -491,7 +648,8 @@ bool ControllerRunner::setTwistAngular(const robot_geometry_msgs::Vector3& angul
|
||||
}
|
||||
catch (const std::exception& ex)
|
||||
{
|
||||
robot::log_error("[move_base2] ControllerRunner: '%s' ném exception trong setTwistAngular: %s\n",
|
||||
robot::log_error("[move_base2] ControllerRunner: '%s' threw an exception in setTwistAngular: "
|
||||
"%s\n",
|
||||
active_name_.c_str(), ex.what());
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -52,13 +52,20 @@ PlannerRunner::~PlannerRunner()
|
||||
}
|
||||
}
|
||||
|
||||
void PlannerRunner::attachStats(RuntimeStats* stats)
|
||||
{
|
||||
stats_ = stats;
|
||||
section_make_plan_ = (stats_ != nullptr) ? stats_->section("planner.makePlan")
|
||||
: RuntimeStats::kInvalidSection;
|
||||
}
|
||||
|
||||
bool PlannerRunner::configure(const robot::NodeHandle& nh,
|
||||
robot_costmap_2d::Costmap2DROBOT* costmap,
|
||||
const std::string& initial_planner, std::string& error)
|
||||
{
|
||||
if (configured_)
|
||||
{
|
||||
error = "PlannerRunner::configure() gọi lần thứ hai";
|
||||
error = "PlannerRunner::configure() called twice";
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -66,7 +73,7 @@ bool PlannerRunner::configure(const robot::NodeHandle& nh,
|
||||
{
|
||||
// Không có costmap thì `BaseGlobalPlanner::initialize` nhận nullptr và mọi plugin tự quyết định
|
||||
// làm gì với nó — thường là sập. Chặn ở đây, nơi còn nói được lý do.
|
||||
error = "PlannerRunner cần costmap global khác null";
|
||||
error = "PlannerRunner needs a non-null global costmap";
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -76,7 +83,7 @@ bool PlannerRunner::configure(const robot::NodeHandle& nh,
|
||||
|
||||
if (!initial_planner.empty() && !swapPlanner(initial_planner))
|
||||
{
|
||||
error = "không nạp được global planner khởi đầu '" + initial_planner + "'";
|
||||
error = "could not load the initial global planner '" + initial_planner + "'";
|
||||
configured_ = false;
|
||||
return false;
|
||||
}
|
||||
@@ -102,8 +109,8 @@ robot_nav_core::BaseGlobalPlanner* PlannerRunner::acquire(const std::string& nam
|
||||
|
||||
if (library_path.empty())
|
||||
{
|
||||
robot::log_error("[move_base2] PlannerRunner: không tìm được thư viện cho '%s' — kiểm khoá "
|
||||
"'%s/library_path' trong YAML và sự tồn tại của file .so trong devel/lib.\n",
|
||||
robot::log_error("[move_base2] PlannerRunner: no library found for '%s' — check the key "
|
||||
"'%s/library_path' in the YAML and that the .so file exists in devel/lib.\n",
|
||||
name.c_str(), name.c_str());
|
||||
return nullptr;
|
||||
}
|
||||
@@ -117,13 +124,14 @@ robot_nav_core::BaseGlobalPlanner* PlannerRunner::acquire(const std::string& nam
|
||||
}
|
||||
catch (const boost::system::system_error& ex)
|
||||
{
|
||||
robot::log_error("[move_base2] PlannerRunner: không nạp được symbol '%s' từ '%s': %s\n",
|
||||
robot::log_error("[move_base2] PlannerRunner: could not load symbol '%s' from '%s': %s\n",
|
||||
name.c_str(), library_path.c_str(), ex.what());
|
||||
return nullptr;
|
||||
}
|
||||
catch (const std::exception& ex)
|
||||
{
|
||||
robot::log_error("[move_base2] PlannerRunner: lỗi khi nạp '%s': %s\n", name.c_str(), ex.what());
|
||||
robot::log_error("[move_base2] PlannerRunner: error while loading '%s': %s\n",
|
||||
name.c_str(), ex.what());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -133,14 +141,15 @@ robot_nav_core::BaseGlobalPlanner* PlannerRunner::acquire(const std::string& nam
|
||||
}
|
||||
catch (const std::exception& ex)
|
||||
{
|
||||
robot::log_error("[move_base2] PlannerRunner: factory của '%s' ném exception: %s\n",
|
||||
robot::log_error("[move_base2] PlannerRunner: factory of '%s' threw an exception: %s\n",
|
||||
name.c_str(), ex.what());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (!loaded.instance)
|
||||
{
|
||||
robot::log_error("[move_base2] PlannerRunner: factory của '%s' trả nullptr.\n", name.c_str());
|
||||
robot::log_error("[move_base2] PlannerRunner: factory of '%s' returned nullptr.\n",
|
||||
name.c_str());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -151,7 +160,7 @@ robot_nav_core::BaseGlobalPlanner* PlannerRunner::acquire(const std::string& nam
|
||||
}
|
||||
catch (const std::exception& ex)
|
||||
{
|
||||
robot::log_error("[move_base2] PlannerRunner: initialize() của '%s' ném exception: %s\n",
|
||||
robot::log_error("[move_base2] PlannerRunner: initialize() of '%s' threw an exception: %s\n",
|
||||
name.c_str(), ex.what());
|
||||
return nullptr;
|
||||
}
|
||||
@@ -160,7 +169,8 @@ robot_nav_core::BaseGlobalPlanner* PlannerRunner::acquire(const std::string& nam
|
||||
{
|
||||
// Bản cũ chỉ log rồi đi tiếp với một planner chưa khởi tạo. Ở đây coi là thất bại: một planner
|
||||
// báo "tôi chưa sẵn sàng" mà vẫn được gọi makePlan là đường dẫn tới hành vi không xác định.
|
||||
robot::log_error("[move_base2] PlannerRunner: '%s' báo initialize() thất bại.\n", name.c_str());
|
||||
robot::log_error("[move_base2] PlannerRunner: '%s' reported an initialize() failure.\n",
|
||||
name.c_str());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -174,13 +184,13 @@ bool PlannerRunner::swapPlanner(const std::string& planner_name)
|
||||
{
|
||||
if (!configured_)
|
||||
{
|
||||
robot::log_error("[move_base2] PlannerRunner: swapPlanner() trước configure().\n");
|
||||
robot::log_error("[move_base2] PlannerRunner: swapPlanner() before configure().\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (planner_name.empty())
|
||||
{
|
||||
robot::log_error("[move_base2] PlannerRunner: tên planner rỗng.\n");
|
||||
robot::log_error("[move_base2] PlannerRunner: empty planner name.\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -213,7 +223,7 @@ bool PlannerRunner::swapPlanner(const std::string& planner_name)
|
||||
}
|
||||
|
||||
active_name_ = planner_name;
|
||||
robot::log_info("[move_base2] PlannerRunner: global planner đang dùng là '%s'.\n",
|
||||
robot::log_info("[move_base2] PlannerRunner: active global planner is '%s'.\n",
|
||||
planner_name.c_str());
|
||||
return true;
|
||||
}
|
||||
@@ -234,14 +244,14 @@ bool PlannerRunner::startPlan(const robot_geometry_msgs::PoseStamped& start,
|
||||
{
|
||||
if (!configured_)
|
||||
{
|
||||
robot::log_error_throttle(5.0, "[move_base2] PlannerRunner: startPlan() trước configure().\n");
|
||||
robot::log_error_throttle(5.0, "[move_base2] PlannerRunner: startPlan() before configure().\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!isFinitePose(start) || !isFinitePose(goal))
|
||||
{
|
||||
robot::log_error_throttle(5.0, "[move_base2] PlannerRunner: start hoặc goal chứa NaN/Inf, "
|
||||
"không khởi động lượt lập plan.\n");
|
||||
robot::log_error_throttle(5.0, "[move_base2] PlannerRunner: start or goal contains NaN/Inf, "
|
||||
"not starting a planning attempt.\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -249,7 +259,8 @@ bool PlannerRunner::startPlan(const robot_geometry_msgs::PoseStamped& start,
|
||||
|
||||
if (active_ == nullptr)
|
||||
{
|
||||
robot::log_error_throttle(5.0, "[move_base2] PlannerRunner: startPlan() khi chưa có planner.\n");
|
||||
robot::log_error_throttle(5.0, "[move_base2] PlannerRunner: startPlan() with no planner "
|
||||
"loaded.\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -317,6 +328,12 @@ void PlannerRunner::cancelPlan()
|
||||
|
||||
void PlannerRunner::threadBody()
|
||||
{
|
||||
if (stats_ != nullptr)
|
||||
{
|
||||
// Phải đăng ký TỪ chính thread này: nhãn được gắn theo tid của thread đang chạy.
|
||||
stats_->registerCurrentThread("move_base2/planner");
|
||||
}
|
||||
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
|
||||
while (true)
|
||||
@@ -346,6 +363,7 @@ void PlannerRunner::threadBody()
|
||||
|
||||
try
|
||||
{
|
||||
ScopedSection timer(stats_, section_make_plan_);
|
||||
// Hai overload của interface gốc gộp lại: "có Order hay không" là một nhánh, không phải hai
|
||||
// contract. Plugin nào không hiểu Order thì overload mặc định của nó tự lo.
|
||||
ok = (order != nullptr) ? planner->makePlan(*order, start, goal, planning_)
|
||||
@@ -355,14 +373,14 @@ void PlannerRunner::threadBody()
|
||||
{
|
||||
// Plugin bên thứ ba ném ra thì đây là biên duy nhất chặn được — exception thoát khỏi thân
|
||||
// thread là std::terminate, tức mất cả tiến trình navigation vì một lượt lập plan hỏng.
|
||||
robot::log_error_throttle(5.0, "[move_base2] PlannerRunner: plugin ném exception khi lập "
|
||||
"plan: %s\n", ex.what());
|
||||
robot::log_error_throttle(5.0, "[move_base2] PlannerRunner: plugin threw an exception while "
|
||||
"making a plan: %s\n", ex.what());
|
||||
ok = false;
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
robot::log_error_throttle(5.0, "[move_base2] PlannerRunner: plugin ném exception lạ khi lập "
|
||||
"plan.\n");
|
||||
robot::log_error_throttle(5.0, "[move_base2] PlannerRunner: plugin threw an unknown "
|
||||
"exception while making a plan.\n");
|
||||
ok = false;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,9 +5,12 @@
|
||||
*********************************************************************/
|
||||
#include <move_base2/runners/recovery_runner.h>
|
||||
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <utility>
|
||||
|
||||
#include <robot/robot.h>
|
||||
#include <yaml-cpp/yaml.h>
|
||||
|
||||
namespace move_base2
|
||||
{
|
||||
@@ -89,13 +92,13 @@ bool RecoveryRunner::configure(robot::NodeHandle& nh)
|
||||
{
|
||||
if (configured_)
|
||||
{
|
||||
robot::log_error("[move_base2] RecoveryRunner: configure() gọi lần thứ hai.");
|
||||
robot::log_error("[move_base2] RecoveryRunner: configure() called twice.");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (deps_.clock == nullptr || deps_.pose == nullptr)
|
||||
{
|
||||
robot::log_error("[move_base2] RecoveryRunner: thiếu ClockPort hoặc PosePort.");
|
||||
robot::log_error("[move_base2] RecoveryRunner: missing ClockPort or PosePort.");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -105,8 +108,8 @@ bool RecoveryRunner::configure(robot::NodeHandle& nh)
|
||||
|
||||
if (registry_.size() == 0)
|
||||
{
|
||||
robot::log_error("[move_base2] RecoveryRunner: không nạp được behavior nào từ namespace '%s' — "
|
||||
"runtime sẽ không có đường phục hồi.", namespace_.c_str());
|
||||
robot::log_error("[move_base2] RecoveryRunner: could not load any behavior from namespace '%s' "
|
||||
"— the runtime will have no recovery path.", namespace_.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -116,16 +119,170 @@ bool RecoveryRunner::configure(robot::NodeHandle& nh)
|
||||
{
|
||||
// Một số behavior hỏng nhưng phần còn lại dùng được: giữ chúng lại và báo false để bên gọi
|
||||
// quyết định (chạy tiếp với ít đường phục hồi hơn, hay dừng khởi động).
|
||||
robot::log_warning("[move_base2] RecoveryRunner: nạp được %zu behavior, một số entry bị bỏ.",
|
||||
robot::log_warning("[move_base2] RecoveryRunner: loaded %zu behavior(s), some entries were "
|
||||
"dropped.",
|
||||
registry_.size());
|
||||
return false;
|
||||
}
|
||||
|
||||
robot::log_info("[move_base2] RecoveryRunner: nạp %zu recovery behavior từ '%s'.",
|
||||
robot::log_info("[move_base2] RecoveryRunner: loaded %zu recovery behavior(s) from '%s'.",
|
||||
registry_.size(), namespace_.c_str());
|
||||
return true;
|
||||
}
|
||||
|
||||
bool RecoveryRunner::configureRoutes(robot::NodeHandle& nh, std::string& error)
|
||||
{
|
||||
error.clear();
|
||||
if (!configured_)
|
||||
{
|
||||
error = "configureRoutes() called before a usable recovery registry was configured";
|
||||
return false;
|
||||
}
|
||||
|
||||
// Luôn dựng fallback trước: schema legacy không có `routes` có nghĩa từng trigger thử toàn bộ
|
||||
// behavior đã nạp theo đúng thứ tự registry cũ.
|
||||
routes_ = RecoveryRoutes{};
|
||||
std::map<std::string, std::size_t> loaded;
|
||||
for (std::size_t index = 0; index < registry_.size(); ++index)
|
||||
{
|
||||
const std::string name = registry_.nameAt(index);
|
||||
loaded.emplace(name, index);
|
||||
routes_.planning_failed.push_back(index);
|
||||
routes_.controlling_failed.push_back(index);
|
||||
routes_.oscillation.push_back(index);
|
||||
}
|
||||
|
||||
const std::string routes_key = namespace_ + "/routes";
|
||||
if (!nh.hasParam(routes_key))
|
||||
{
|
||||
robot::log_info("[move_base2] RecoveryRunner: '%s' absent; using legacy shared recovery "
|
||||
"route with %zu behavior(s).",
|
||||
routes_key.c_str(), registry_.size());
|
||||
return true;
|
||||
}
|
||||
|
||||
YAML::Node configured_routes;
|
||||
try
|
||||
{
|
||||
configured_routes = nh.getParamValue(routes_key);
|
||||
}
|
||||
catch (const YAML::Exception& exception)
|
||||
{
|
||||
error = "could not read " + routes_key + ": " + exception.what();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!configured_routes.IsMap())
|
||||
{
|
||||
error = routes_key + " must be a YAML map";
|
||||
return false;
|
||||
}
|
||||
|
||||
const std::pair<const char*, RecoveryTrigger> expected_routes[] = {
|
||||
{"planning_failed", RecoveryTrigger::kPlanningFailed},
|
||||
{"controlling_failed", RecoveryTrigger::kControllingFailed},
|
||||
{"oscillation", RecoveryTrigger::kOscillation},
|
||||
};
|
||||
|
||||
const auto is_expected_key = [&expected_routes](const std::string& key) {
|
||||
for (const auto& expected : expected_routes)
|
||||
{
|
||||
if (key == expected.first)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
for (const auto& entry : configured_routes)
|
||||
{
|
||||
if (!entry.first.IsScalar())
|
||||
{
|
||||
error = routes_key + " contains a non-scalar route name";
|
||||
return false;
|
||||
}
|
||||
const std::string key = entry.first.as<std::string>();
|
||||
if (!is_expected_key(key))
|
||||
{
|
||||
error = routes_key + " has unsupported trigger '" + key + "'";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto& expected : expected_routes)
|
||||
{
|
||||
const std::string trigger_name(expected.first);
|
||||
const YAML::Node route_node = configured_routes[trigger_name];
|
||||
if (!route_node || !route_node.IsSequence())
|
||||
{
|
||||
error = routes_key + "/" + trigger_name + " must be a YAML sequence";
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<std::size_t>* resolved = nullptr;
|
||||
switch (expected.second)
|
||||
{
|
||||
case RecoveryTrigger::kPlanningFailed:
|
||||
resolved = &routes_.planning_failed;
|
||||
break;
|
||||
case RecoveryTrigger::kControllingFailed:
|
||||
resolved = &routes_.controlling_failed;
|
||||
break;
|
||||
case RecoveryTrigger::kOscillation:
|
||||
resolved = &routes_.oscillation;
|
||||
break;
|
||||
}
|
||||
resolved->clear();
|
||||
|
||||
std::set<std::size_t> seen;
|
||||
for (const YAML::Node& behavior_node : route_node)
|
||||
{
|
||||
if (!behavior_node.IsScalar())
|
||||
{
|
||||
error = routes_key + "/" + trigger_name + " must contain only behavior names";
|
||||
return false;
|
||||
}
|
||||
|
||||
const std::string behavior_name = behavior_node.as<std::string>();
|
||||
const auto found = loaded.find(behavior_name);
|
||||
if (found == loaded.end())
|
||||
{
|
||||
// Đây là điểm cho phép commit schema trước plugin. Không tạo index giả: StateMachine chỉ
|
||||
// được thấy những index registry thực sự nạp được.
|
||||
robot::log_warning("[move_base2] RecoveryRunner: route '%s' skips '%s' because that "
|
||||
"behavior was not loaded.",
|
||||
trigger_name.c_str(), behavior_name.c_str());
|
||||
continue;
|
||||
}
|
||||
if (!seen.insert(found->second).second)
|
||||
{
|
||||
error = routes_key + "/" + trigger_name + " repeats behavior '" + behavior_name + "'";
|
||||
return false;
|
||||
}
|
||||
resolved->push_back(found->second);
|
||||
}
|
||||
|
||||
if (resolved->empty())
|
||||
{
|
||||
error = routes_key + "/" + trigger_name +
|
||||
" has no behavior that was successfully loaded";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
robot::log_info("[move_base2] RecoveryRunner: routes resolved: planning=%zu controlling=%zu "
|
||||
"oscillation=%zu.",
|
||||
routes_.planning_failed.size(), routes_.controlling_failed.size(),
|
||||
routes_.oscillation.size());
|
||||
return true;
|
||||
}
|
||||
|
||||
const RecoveryRoutes& RecoveryRunner::routes() const
|
||||
{
|
||||
return routes_;
|
||||
}
|
||||
|
||||
std::size_t RecoveryRunner::behaviorCount() const
|
||||
{
|
||||
return registry_.size();
|
||||
@@ -149,15 +306,15 @@ bool RecoveryRunner::start(std::size_t index, RecoveryTrigger trigger)
|
||||
|
||||
if (!configured_)
|
||||
{
|
||||
robot::log_error("[move_base2] RecoveryRunner: start() trước configure().");
|
||||
robot::log_error("[move_base2] RecoveryRunner: start() before configure().");
|
||||
return false;
|
||||
}
|
||||
|
||||
recovery_core::RecoveryBehavior* behavior = registry_.at(index);
|
||||
if (behavior == nullptr)
|
||||
{
|
||||
robot::log_error("[move_base2] RecoveryRunner: index %zu ngoài dải (%zu behavior).", index,
|
||||
registry_.size());
|
||||
robot::log_error("[move_base2] RecoveryRunner: index %zu out of range (%zu behavior(s)).",
|
||||
index, registry_.size());
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -170,12 +327,20 @@ bool RecoveryRunner::start(std::size_t index, RecoveryTrigger trigger)
|
||||
|
||||
if (!behavior->start(goal, deps_.clock->now()))
|
||||
{
|
||||
robot::log_warning("[move_base2] RecoveryRunner: behavior '%s' từ chối khởi động (%s).",
|
||||
robot::log_warning("[move_base2] RecoveryRunner: behavior '%s' refused to start (%s).",
|
||||
registry_.nameAt(index).c_str(), toString(trigger));
|
||||
return false;
|
||||
}
|
||||
|
||||
active_ = behavior;
|
||||
|
||||
// Log tại SƯỜN (một dòng cho mỗi lượt khởi động, không nằm trên đường tick). Không có dòng này
|
||||
// thì trên robot/sim không cách nào biết đang chạy behavior nào: các plugin chỉ log khi chúng TỪ
|
||||
// CHỐI, nên một chuỗi recovery chạy trơn tru sẽ hoàn toàn im lặng và người vận hành chỉ thấy robot
|
||||
// tự nhiên quay hoặc lùi.
|
||||
robot::log_info("[move_base2] recovery: running '%s' (registry index %zu/%zu, trigger %s).",
|
||||
registry_.nameAt(index).c_str(), index + 1, registry_.size(), toString(trigger));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -188,7 +353,7 @@ RecoveryTick RecoveryRunner::update()
|
||||
// Contract nói update() chỉ được gọi sau start() trả true. Vẫn guard: state machine hỏng thì
|
||||
// phải thành "recovery này thất bại" chứ không phải dereference null.
|
||||
tick.status = RecoveryTick::Status::kFailed;
|
||||
tick.message = "update() khi không có behavior nào đang chạy";
|
||||
tick.message = "update() with no behavior running";
|
||||
return tick;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user