optimal & fix file cmake
This commit is contained in:
@@ -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