temporary storage 7/9/2026 19:41

This commit is contained in:
2026-07-09 19:41:20 +07:00
parent 915cf85cc5
commit e9394434ba
17 changed files with 658 additions and 566 deletions

View File

@@ -1,19 +1,118 @@
/*********************************************************************
* recovery_core — default impl cho RecoveryBehavior.
* recovery_core — phần chung (template-method) của RecoveryBehavior.
*
* Chỉ chứa default computeCommand() (họ A/B không override). initialize()/runBehavior()/
* status() là thuần ảo, do plugin cụ thể triển khai.
* configure()/start()/update()/cancel() là NON-VIRTUAL: base lo guard vòng đời, timeout, đo
* elapsed, và xử lý cancel; plugin chỉ triển khai onConfigure()/onStart()/onUpdate().
*
* Author: DuongTD
*********************************************************************/
#include <recovery_core/recovery_behavior.h>
#include <cmath>
#include <robot/robot.h>
namespace recovery_core
{
RecoveryResult RecoveryBehavior::computeCommand(double /*dt*/)
namespace
{
return RecoveryResult::Failed();
robot_geometry_msgs::Twist zeroTwist()
{
return robot_geometry_msgs::Twist();
}
bool validCycle(double dt)
{
return std::isfinite(dt) && dt > 0.0;
}
} // namespace
void RecoveryBehavior::configure(const std::string& name, const RecoveryContext& ctx)
{
if (configured_)
{
return;
}
name_ = name;
ctx_ = ctx;
status_ = RecoveryStatus::kIdle;
// Param chung duy nhất còn lại: timeout (s, 0 = không giới hạn).
robot::NodeHandle private_nh("~/" + name_);
private_nh.param("timeout", timeout_, 0.0);
if (!std::isfinite(timeout_) || timeout_ < 0.0)
{
robot::log_warning("[recovery_core] Invalid timeout for '%s'; using 0 (no timeout).",
name_.c_str());
timeout_ = 0.0;
}
onConfigure();
configured_ = true;
}
RecoveryResult RecoveryBehavior::start(const RecoveryGoal& goal)
{
if (!configured_)
{
status_ = RecoveryStatus::kFailed;
return RecoveryResult::Failed().withMessage("start() before configure()");
}
goal_ = goal;
elapsed_ = 0.0;
cancel_requested_ = false;
started_ = true;
status_ = RecoveryStatus::kRunning;
return onStart(goal_);
}
RecoveryResult RecoveryBehavior::update(double dt)
{
if (!configured_ || !started_)
{
status_ = RecoveryStatus::kFailed;
return RecoveryResult::Failed().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_);
}
if (cancel_requested_)
{
status_ = RecoveryStatus::kCancelled;
return RecoveryResult::Velocity(zeroTwist(), status_)
.withMessage("cancelled by caller");
}
if (!validCycle(dt))
{
status_ = RecoveryStatus::kFailed;
return RecoveryResult::Velocity(zeroTwist(), status_).withMessage("invalid dt");
}
elapsed_ += dt;
if (config_.timeout > 0.0 && elapsed_ > config_.timeout)
{
status_ = RecoveryStatus::kFailed;
return RecoveryResult::Velocity(zeroTwist(), status_).withMessage("timeout");
}
RecoveryResult result = onUpdate(dt);
result.elapsed = elapsed_;
status_ = result.status;
return result;
}
void RecoveryBehavior::cancel()
{
cancel_requested_ = true;
}
} // namespace recovery_core

View File

@@ -1,102 +0,0 @@
/*********************************************************************
* recovery_core — validate + đọc RecoveryConfig.
*
* Author: DuongTD
*********************************************************************/
#include <recovery_core/recovery_config.h>
#include <cmath>
#include <string>
#include <robot/robot.h>
namespace recovery_core
{
namespace
{
constexpr double kDefaultControlFrequency = 20.0;
constexpr double kDefaultTimeout = 0.0;
void appendError(std::string* error, const std::string& message)
{
if (error == nullptr)
{
return;
}
if (!error->empty())
{
*error += "; ";
}
*error += message;
}
bool invalidControlFrequency(double value)
{
return !std::isfinite(value) || value <= 0.0;
}
bool invalidTimeout(double value)
{
return !std::isfinite(value) || value < 0.0;
}
} // namespace
bool RecoveryConfig::validate(std::string* error) const
{
if (error != nullptr)
{
error->clear();
}
bool valid = true;
if (invalidControlFrequency(control_frequency))
{
appendError(error, "control_frequency must be finite and > 0 Hz");
valid = false;
}
if (invalidTimeout(timeout))
{
appendError(error, "timeout must be finite and >= 0 s");
valid = false;
}
return valid;
}
RecoveryConfig RecoveryConfig::fromNodeHandle(robot::NodeHandle& nh)
{
RecoveryConfig config;
nh.param("control_frequency", config.control_frequency, kDefaultControlFrequency);
nh.param("timeout", config.timeout, kDefaultTimeout);
std::string error;
if (config.validate(&error))
{
return config;
}
robot::log_warning("[recovery_core] Invalid common recovery config: %s. "
"Replacing invalid values with defaults.",
error.c_str());
if (invalidControlFrequency(config.control_frequency))
{
config.control_frequency = kDefaultControlFrequency;
}
if (invalidTimeout(config.timeout))
{
config.timeout = kDefaultTimeout;
}
if (!config.validate(nullptr))
{
return RecoveryConfig{};
}
return config;
}
} // namespace recovery_core

View File

@@ -5,6 +5,8 @@
*********************************************************************/
#include <recovery_core/recovery_types.h>
#include <utility>
namespace recovery_core
{
@@ -21,6 +23,7 @@ RecoveryResult RecoveryResult::Succeeded()
RecoveryResult result;
result.status = RecoveryStatus::kSucceeded;
result.output_type = RecoveryOutputType::kNone;
result.progress = 1.0;
return result;
}
@@ -32,6 +35,14 @@ RecoveryResult RecoveryResult::Failed()
return result;
}
RecoveryResult RecoveryResult::Cancelled()
{
RecoveryResult result;
result.status = RecoveryStatus::kCancelled;
result.output_type = RecoveryOutputType::kNone;
return result;
}
RecoveryResult RecoveryResult::Velocity(const robot_geometry_msgs::Twist& command,
RecoveryStatus status)
{
@@ -39,6 +50,10 @@ RecoveryResult RecoveryResult::Velocity(const robot_geometry_msgs::Twist& comman
result.status = status;
result.output_type = RecoveryOutputType::kVelocity;
result.command = command;
if (status == RecoveryStatus::kSucceeded)
{
result.progress = 1.0;
}
return result;
}
@@ -49,7 +64,24 @@ RecoveryResult RecoveryResult::PathOut(const robot_nav_msgs::Path& path,
result.status = status;
result.output_type = RecoveryOutputType::kPath;
result.path = path;
if (status == RecoveryStatus::kSucceeded)
{
result.progress = 1.0;
}
return result;
}
RecoveryResult& RecoveryResult::withProgress(double progress_value, double remaining_value)
{
progress = progress_value < 0.0 ? 0.0 : (progress_value > 1.0 ? 1.0 : progress_value);
remaining = remaining_value < 0.0 ? 0.0 : remaining_value;
return *this;
}
RecoveryResult& RecoveryResult::withMessage(std::string text)
{
message = std::move(text);
return *this;
}
} // namespace recovery_core