optimal & fix file cmake

This commit is contained in:
2026-08-03 22:32:40 +07:00
parent 89add78c7f
commit 887bff1b97
98 changed files with 8971 additions and 1339 deletions

View File

@@ -1,79 +1,167 @@
/*********************************************************************
* recovery_core — phần chung (template-method) của RecoveryBehavior.
*
* configure()/start()/update()/cancel() là NON-VIRTUAL: base lo guard vòng đời và xử lý
* cancel; plugin chỉ triển khai onConfigure()/onStart()/onUpdate().
* Base giữ toàn bộ bất biến vòng đời, thời gian, và họ output; plugin chỉ triển khai hook.
*
* Author: DuongTD
*********************************************************************/
#include <recovery_core/recovery_behavior.h>
#include <cmath>
#include <robot/robot.h>
namespace recovery_core
{
namespace
{
robot_geometry_msgs::Twist zeroTwist()
constexpr double kDefaultTimeout = 0.0; ///< [s] 0 = không giới hạn.
constexpr double kMaxTimeout = 600.0; ///< [s] trần vệ sinh cho param cấu hình sai.
/// Một giá trị optional hợp lệ phải hữu hạn.
bool finiteIfSet(const std::optional<double>& value)
{
return robot_geometry_msgs::Twist();
return !value.has_value() || std::isfinite(*value);
}
} // namespace
void RecoveryBehavior::configure(const std::string& name, const RecoveryContext& ctx)
bool RecoveryBehavior::configure(const std::string& name, const RecoveryContext& ctx,
robot::NodeHandle& nh)
{
if (configured_)
{
return;
// Gọi lại với ctx khác là lỗi lập trình của caller. Bản cũ im lặng return, nên caller không bao
// giờ biết context thứ hai đã bị bỏ đi.
robot::log_error("[recovery_core] '%s': configure() called twice, ignored.", name_.c_str());
return false;
}
if (name.empty())
{
robot::log_error("[recovery_core] configure() with an empty instance name.");
return false;
}
name_ = name;
ctx_ = ctx;
if (!validateContext())
{
name_.clear();
ctx_ = RecoveryContext();
return false;
}
nh.param("timeout", timeout_, kDefaultTimeout);
if (!std::isfinite(timeout_) || timeout_ < 0.0 || timeout_ > kMaxTimeout)
{
robot::log_warning("[recovery_core] '%s': timeout=%.3f s outside [0, %.0f]; using 0 (no "
"limit).", name_.c_str(), timeout_, kMaxTimeout);
timeout_ = kDefaultTimeout;
}
if (!onConfigure(nh))
{
robot::log_error("[recovery_core] '%s': onConfigure() failed.", name_.c_str());
name_.clear();
ctx_ = RecoveryContext();
return false;
}
status_ = RecoveryStatus::kIdle;
onConfigure();
configured_ = true;
return true;
}
RecoveryResult RecoveryBehavior::start(const RecoveryGoal& goal)
bool RecoveryBehavior::start(const RecoveryGoal& goal, const robot::Time& now)
{
if (!configured_)
{
status_ = RecoveryStatus::kFailed;
return RecoveryResult::Failed().withMessage("start() before configure()");
robot::log_error("[recovery_core] start() before configure().");
return false;
}
if (started_ && status_ == RecoveryStatus::kRunning && !cancel_requested_)
{
// Không từ chối: sau cancel(), state machine có thể start lượt mới mà lượt cũ chưa kịp về
// terminal (nó không tick recovery ở state CANCELLING). Nhưng vẫn phải báo, vì nếu KHÔNG phải
// đường cancel thì đây là caller đang bỏ dở một behavior đang lái robot.
robot::log_warning("[recovery_core] '%s': start() while the previous run is still going — "
"resetting.",
name_.c_str());
}
if (!validateGoal(goal))
{
return false;
}
goal_ = goal;
cancel_requested_ = false;
started_ = true;
start_time_ = now;
last_update_ = now;
elapsed_ = 0.0;
status_ = RecoveryStatus::kRunning;
started_ = true;
RecoveryResult result = onStart(goal_);
status_ = result.status;
return result;
if (!onStart(goal_))
{
status_ = RecoveryStatus::kFailed;
robot::log_warning("[recovery_core] '%s': refused to start (trigger=%s).", name_.c_str(),
toString(goal_.trigger));
return false;
}
return true;
}
RecoveryResult RecoveryBehavior::update()
RecoveryResult RecoveryBehavior::update(const robot::Time& now)
{
if (!configured_ || !started_)
{
status_ = RecoveryStatus::kFailed;
return RecoveryResult::Failed().withMessage("update() before start()");
return stopResult(RecoveryStatus::kFailed).withMessage("update() before start()");
}
// Đã kết thúc: giữ nguyên trạng thái, không tick thêm.
if (status_ != RecoveryStatus::kRunning)
{
return RecoveryResult::Velocity(zeroTwist(), status_);
// Lượt đã kết thúc: giữ nguyên kết luận, không tick thêm.
return stopResult(status_);
}
// dt đo THẬT. Đây là điểm sửa cốt lõi so với bản cũ: bản cũ tích phân vận tốc lệnh nhân với
// control_period lấy từ config, nên control loop chạy chậm là robot đi quá quãng yêu cầu.
double dt = (now - last_update_).toSec();
if (!std::isfinite(dt) || dt < 0.0)
{
// Đồng hồ đi lùi (đổi nguồn thời gian, hoặc sim reset). Coi như không có thời gian trôi thay vì
// tích phân một dt âm vào tiến độ.
robot::log_warning("[recovery_core] '%s': dt=%.6f s is invalid, treated as 0.", name_.c_str(),
dt);
dt = 0.0;
}
last_update_ = now;
elapsed_ = (now - start_time_).toSec();
if (!std::isfinite(elapsed_) || elapsed_ < 0.0)
{
elapsed_ = 0.0;
}
if (cancel_requested_)
{
status_ = RecoveryStatus::kCancelled;
return RecoveryResult::Velocity(zeroTwist(), status_)
.withMessage("cancelled by caller");
RecoveryResult result = finalize(onCancel());
status_ = result.status;
return result;
}
RecoveryResult result = onUpdate();
if (timeout_ > 0.0 && elapsed_ >= timeout_)
{
status_ = RecoveryStatus::kFailed;
return stopResult(RecoveryStatus::kFailed)
.withMessage("exceeded the timeout of " + std::to_string(timeout_) + " s");
}
RecoveryResult result = finalize(onUpdate(now, dt));
status_ = result.status;
return result;
}
@@ -83,4 +171,126 @@ void RecoveryBehavior::cancel()
cancel_requested_ = true;
}
RecoveryResult RecoveryBehavior::onCancel()
{
return stopResult(RecoveryStatus::kCancelled).withMessage("cancelled by caller");
}
RecoveryResult RecoveryBehavior::stopResult(RecoveryStatus status) const
{
RecoveryResult result;
result.status = status;
result.elapsed = elapsed_;
if (outputKind() == RecoveryOutputType::kVelocity)
{
// Họ velocity: phát lệnh dừng TƯỜNG MINH. Caller đang lấy cmd_vel từ behavior này nên "không
// output" và "output vận tốc 0" là hai chuyện khác nhau.
result.output_type = RecoveryOutputType::kVelocity;
result.command = robot_geometry_msgs::Twist();
}
else
{
// Họ khác: KHÔNG bịa ra output vận tốc. Bản cũ trả Velocity(zero) cho mọi họ, nên một behavior
// clear-costmap báo cáo mình phát vận tốc.
result.output_type = RecoveryOutputType::kNone;
}
return result;
}
bool RecoveryBehavior::validateContext() const
{
const RecoveryOutputType kind = outputKind();
if (kind == RecoveryOutputType::kVelocity)
{
if (ctx_.pose == nullptr)
{
robot::log_error("[recovery_core] '%s': the velocity family requires a PoseProvider — "
"progress must be measured from a real pose, not dead-reckoned.",
name_.c_str());
return false;
}
if (ctx_.collision == nullptr)
{
robot::log_error("[recovery_core] '%s': the velocity family requires a CollisionChecker — "
"driving blind is not allowed.", name_.c_str());
return false;
}
}
if (kind == RecoveryOutputType::kPath && ctx_.plan == nullptr)
{
robot::log_error("[recovery_core] '%s': the path family requires a PlanProvider.",
name_.c_str());
return false;
}
return true;
}
bool RecoveryBehavior::validateGoal(const RecoveryGoal& goal) const
{
if (!finiteIfSet(goal.angle) || !finiteIfSet(goal.distance))
{
robot::log_error("[recovery_core] '%s': goal contains NaN/Inf.", name_.c_str());
return false;
}
if (goal.distance.has_value() && *goal.distance <= 0.0)
{
robot::log_error("[recovery_core] '%s': goal.distance=%.3f m must be > 0.", name_.c_str(),
*goal.distance);
return false;
}
for (const auto& entry : goal.params)
{
if (!std::isfinite(entry.second))
{
robot::log_error("[recovery_core] '%s': goal.params['%s'] is not finite.", name_.c_str(),
entry.first.c_str());
return false;
}
}
return true;
}
RecoveryResult RecoveryBehavior::finalize(RecoveryResult result) const
{
const RecoveryOutputType kind = outputKind();
if (result.output_type != kind && result.output_type != RecoveryOutputType::kNone)
{
// Plugin trả sai họ. Hạ về kNone thay vì tin theo: caller route bằng output_type, nên một họ
// sai ở đây là caller đọc nhầm trường.
robot::log_error("[recovery_core] '%s': returned output_type='%s' but outputKind()='%s'; "
"downgraded to 'none'.", name_.c_str(), toString(result.output_type), toString(kind));
result.output_type = RecoveryOutputType::kNone;
result.command = robot_geometry_msgs::Twist();
result.path = robot_nav_msgs::Path();
}
if (result.output_type == RecoveryOutputType::kVelocity)
{
const robot_geometry_msgs::Twist& cmd = result.command;
if (!std::isfinite(cmd.linear.x) || !std::isfinite(cmd.linear.y) ||
!std::isfinite(cmd.linear.z) || !std::isfinite(cmd.angular.x) ||
!std::isfinite(cmd.angular.y) || !std::isfinite(cmd.angular.z))
{
// NaN/Inf lọt ra cmd_vel là lỗi không được phép đi tiếp: đổi thành lệnh dừng + kFailed.
robot::log_error("[recovery_core] '%s': velocity command contains NaN/Inf — forcing a stop.",
name_.c_str());
RecoveryResult stop = stopResult(RecoveryStatus::kFailed);
stop.message = "velocity command is not finite";
return stop;
}
}
result.elapsed = elapsed_;
return result;
}
} // namespace recovery_core

183
src/recovery_registry.cpp Normal file
View File

@@ -0,0 +1,183 @@
/*********************************************************************
* recovery_core — nạp behavior theo YAML bằng Boost.DLL.
*
* Author: DuongTD
*********************************************************************/
#include <recovery_core/recovery_registry.h>
#include <utility>
#include <boost/dll/import.hpp>
#include <boost/system/system_error.hpp>
#include <yaml-cpp/yaml.h>
#include <robot/robot.h>
namespace recovery_core
{
RecoveryRegistry::~RecoveryRegistry()
{
clear();
}
void RecoveryRegistry::clear()
{
// Behavior phải chết TRƯỚC factory: factory là thứ giữ .so còn nạp, thả ngược thứ tự sẽ gỡ thư
// viện trong khi vẫn còn object của nó, và vtable sẽ trỏ vào vùng đã unmap.
behaviors_.clear();
factories_.clear();
}
bool RecoveryRegistry::registerBehavior(const RecoveryBehavior::RecoveryBehaviorPtr& behavior)
{
if (!behavior)
{
robot::log_error("[recovery_core] RecoveryRegistry: behavior null.");
return false;
}
behaviors_.push_back(behavior);
return true;
}
RecoveryBehavior* RecoveryRegistry::at(std::size_t index) const
{
return index < behaviors_.size() ? behaviors_[index].get() : nullptr;
}
std::string RecoveryRegistry::nameAt(std::size_t index) const
{
return index < behaviors_.size() ? behaviors_[index]->name() : std::string();
}
bool RecoveryRegistry::loadOne(const std::string& name, const std::string& type,
robot::NodeHandle& nh, const RecoveryContext& ctx,
const std::string& ns)
{
robot::PluginLoaderHelper loader(nh);
const std::string library_path = loader.findLibraryPath(type);
if (library_path.empty())
{
robot::log_error("[recovery_core] RecoveryRegistry: no library found for '%s' — check the key "
"'%s/library_path' in the YAML and that the .so file exists.",
type.c_str(), type.c_str());
return false;
}
std::function<RecoveryBehavior::RecoveryBehaviorPtr()> factory;
try
{
factory = boost::dll::import_alias<RecoveryBehavior::RecoveryBehaviorPtr()>(
library_path, type, boost::dll::load_mode::append_decorations);
}
catch (const boost::system::system_error& ex)
{
// Sai tên symbol hoặc file không nạp được. Bắt tại đây để một plugin hỏng không giết cả tiến
// trình, nhưng vẫn báo lỗi để không ai tưởng đường phục hồi này đang chạy.
robot::log_error("[recovery_core] RecoveryRegistry: could not load symbol '%s' from '%s': %s",
type.c_str(), library_path.c_str(), ex.what());
return false;
}
catch (const std::exception& ex)
{
robot::log_error("[recovery_core] RecoveryRegistry: error while loading '%s': %s", type.c_str(),
ex.what());
return false;
}
RecoveryBehavior::RecoveryBehaviorPtr behavior;
try
{
behavior = factory();
}
catch (const std::exception& ex)
{
robot::log_error("[recovery_core] RecoveryRegistry: factory of '%s' threw an exception: %s",
type.c_str(), ex.what());
return false;
}
if (!behavior)
{
robot::log_error("[recovery_core] RecoveryRegistry: factory of '%s' returned null.",
type.c_str());
return false;
}
// Namespace param của instance do REGISTRY dựng, không phải plugin tự đi tìm trên disk. Nhờ vậy
// test chỉ cần trỏ NodeHandle vào cây config của mình là chạy được.
const std::string param_ns = ns.empty() ? name : ns + "/" + name;
robot::NodeHandle behavior_nh(nh, param_ns);
if (!behavior->configure(name, ctx, behavior_nh))
{
robot::log_error("[recovery_core] RecoveryRegistry: '%s' (instance '%s') configure() failed.",
type.c_str(), name.c_str());
return false;
}
behaviors_.push_back(behavior);
// Chỉ giữ factory sau khi behavior đã vào danh sách: nếu hỏng thì cũng không giữ .so lại.
factories_.push_back(std::move(factory));
robot::log_info("[recovery_core] RecoveryRegistry: loaded '%s' (instance '%s', family '%s').",
type.c_str(), name.c_str(), toString(behavior->outputKind()));
return true;
}
bool RecoveryRegistry::loadFromConfig(robot::NodeHandle& nh, const std::string& ns,
const RecoveryContext& ctx)
{
const std::string key = ns.empty() ? std::string("behaviors") : ns + "/behaviors";
YAML::Node behaviors;
if (!nh.getParam(key, behaviors) || !behaviors.IsSequence() || behaviors.size() == 0)
{
robot::log_error("[recovery_core] RecoveryRegistry: '%s' is missing or is not a list — no "
"recovery path was loaded.", key.c_str());
return false;
}
bool all_ok = true;
for (std::size_t i = 0; i < behaviors.size(); ++i)
{
const YAML::Node& entry = behaviors[i];
if (!entry.IsMap() || !entry["type"])
{
robot::log_error("[recovery_core] RecoveryRegistry: '%s[%zu]' is missing the 'type' key.",
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("[recovery_core] RecoveryRegistry: '%s[%zu]' could not be read: %s",
key.c_str(), i, ex.what());
all_ok = false;
continue;
}
if (!loadOne(name, type, nh, ctx, ns))
{
all_ok = false;
}
}
return all_ok;
}
} // namespace recovery_core

View File

@@ -1,5 +1,5 @@
/*********************************************************************
* recovery_core — factory cho RecoveryResult.
* recovery_core — factory + tên hiển thị cho kiểu hợp đồng.
*
* Author: DuongTD
*********************************************************************/
@@ -10,6 +10,54 @@
namespace recovery_core
{
const char* toString(RecoveryTrigger trigger)
{
switch (trigger)
{
case RecoveryTrigger::kUnspecified:
return "unspecified";
case RecoveryTrigger::kPlanningFailed:
return "planning_failed";
case RecoveryTrigger::kControllingFailed:
return "controlling_failed";
case RecoveryTrigger::kOscillation:
return "oscillation";
}
return "unknown";
}
const char* toString(RecoveryStatus status)
{
switch (status)
{
case RecoveryStatus::kIdle:
return "idle";
case RecoveryStatus::kRunning:
return "running";
case RecoveryStatus::kSucceeded:
return "succeeded";
case RecoveryStatus::kFailed:
return "failed";
case RecoveryStatus::kCancelled:
return "cancelled";
}
return "unknown";
}
const char* toString(RecoveryOutputType kind)
{
switch (kind)
{
case RecoveryOutputType::kNone:
return "none";
case RecoveryOutputType::kVelocity:
return "velocity";
case RecoveryOutputType::kPath:
return "path";
}
return "unknown";
}
RecoveryResult RecoveryResult::Running()
{
RecoveryResult result;
@@ -57,8 +105,7 @@ RecoveryResult RecoveryResult::Velocity(const robot_geometry_msgs::Twist& comman
return result;
}
RecoveryResult RecoveryResult::PathOut(const robot_nav_msgs::Path& path,
RecoveryStatus status)
RecoveryResult RecoveryResult::PathOut(const robot_nav_msgs::Path& path, RecoveryStatus status)
{
RecoveryResult result;
result.status = status;