196 lines
6.8 KiB
C++
196 lines
6.8 KiB
C++
/*********************************************************************
|
|
*
|
|
* Software License Agreement (BSD License)
|
|
*
|
|
* action_core — handler mặc định cho action `wait`: chờ đúng khoảng thời gian order yêu cầu.
|
|
*
|
|
* Author: DuongTD
|
|
*********************************************************************/
|
|
|
|
#include <action_core/action_handler.h>
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <memory>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include <boost/dll/alias.hpp>
|
|
#include <robot/robot.h>
|
|
|
|
namespace action_core
|
|
{
|
|
namespace
|
|
{
|
|
constexpr double kDefaultDuration = 0.0; // [s] khi order không kèm tham số nào.
|
|
constexpr double kDefaultMaxDuration = 600.0; // [s] trần vệ sinh cho tham số gõ nhầm.
|
|
constexpr const char* kDurationKey = "duration";
|
|
} // namespace
|
|
|
|
/**
|
|
* @class WaitActionHandler
|
|
* @brief Chờ một khoảng thời gian lấy từ **actionParameters của chính order**.
|
|
*
|
|
* Khác @ref NoopActionHandler ở điểm quyết định: `Noop` là stub — thời lượng nằm trong YAML và mọi
|
|
* lần chạy đều như nhau. `Wait` là handler **thật**: chờ chính là công việc, và thời lượng do fleet
|
|
* master quyết định cho từng action một qua `actionParameters: [{key: duration, value: "5.0"}]`.
|
|
*
|
|
* Đây là handler duy nhất trong bộ default có thể chạy trên robot thật mà không nói dối ai: nó
|
|
* không giả vờ điều khiển thiết bị nào, nó chỉ đếm giờ — và đó đúng là thứ action `wait` yêu cầu.
|
|
*
|
|
* Param (namespace của instance):
|
|
* - `action_types` [list, mặc định `["wait"]`]
|
|
* - `default_duration` [s, mặc định 0.0]: dùng khi action **không** kèm tham số `duration`.
|
|
* - `max_duration` [s, mặc định 600.0]: trần cứng. Vượt trần thì action **thất bại**, không phải
|
|
* bị cắt xuống — một order yêu cầu chờ 2 giờ gần như chắc chắn là gõ nhầm, và im lặng rút ngắn
|
|
* nó xuống 10 phút là giấu đi lỗi cấu hình của fleet.
|
|
*/
|
|
class WaitActionHandler final : public ActionHandler
|
|
{
|
|
public:
|
|
WaitActionHandler() = default;
|
|
|
|
static ActionHandler::Ptr create()
|
|
{
|
|
return std::make_shared<WaitActionHandler>();
|
|
}
|
|
|
|
bool configure(const std::string& name, const ActionContext& /*ctx*/,
|
|
robot::NodeHandle& nh) override
|
|
{
|
|
name_ = name;
|
|
|
|
nh.param("action_types", action_types_, std::vector<std::string>{ "wait" });
|
|
nh.param("default_duration", default_duration_, kDefaultDuration);
|
|
nh.param("max_duration", max_duration_, kDefaultMaxDuration);
|
|
|
|
if (action_types_.empty())
|
|
{
|
|
robot::log_error("[action_core] '%s': action_types is empty — this handler would never be "
|
|
"called.", name_.c_str());
|
|
return false;
|
|
}
|
|
|
|
if (!std::isfinite(max_duration_) || max_duration_ <= 0.0)
|
|
{
|
|
robot::log_error("[action_core] '%s': max_duration=%.3f s must be > 0.", name_.c_str(),
|
|
max_duration_);
|
|
return false;
|
|
}
|
|
|
|
if (!std::isfinite(default_duration_) || default_duration_ < 0.0 ||
|
|
default_duration_ > max_duration_)
|
|
{
|
|
robot::log_error("[action_core] '%s': default_duration=%.3f s outside [0, %.3f].",
|
|
name_.c_str(), default_duration_, max_duration_);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
std::vector<std::string> supportedActionTypes() const override
|
|
{
|
|
return action_types_;
|
|
}
|
|
|
|
bool start(const robot_protocol_msgs::Action& action, const robot::Time& now) override
|
|
{
|
|
action_id_ = action.actionId;
|
|
started_at_ = now;
|
|
|
|
if (!readDuration(action, duration_))
|
|
{
|
|
// Từ chối khởi động thay vì chờ một khoảng thời gian đoán bừa: caller coi như action hỏng và
|
|
// fleet nhận được lý do, thay vì robot đứng im một lúc không ai giải thích được.
|
|
return false;
|
|
}
|
|
|
|
robot::log_info("[action_core] '%s': waiting %.3f s for action '%s' (id '%s').", name_.c_str(),
|
|
duration_, action.actionType.c_str(), action_id_.c_str());
|
|
return true;
|
|
}
|
|
|
|
ActionTick update(const robot::Time& now) override
|
|
{
|
|
ActionTick tick;
|
|
const double elapsed = (now - started_at_).toSec(); // [s]
|
|
|
|
// Timeout tầng 1 chính là điều kiện hoàn tất: `duration_` đã bị chặn trên bởi `max_duration_`
|
|
// ngay tại start(), nên handler này không thể chạy vô hạn.
|
|
if (elapsed >= duration_)
|
|
{
|
|
tick.status = ActionStatus::kSucceeded;
|
|
tick.message = "action '" + action_id_ + "' waited " + std::to_string(duration_) + " s";
|
|
return tick;
|
|
}
|
|
|
|
tick.status = ActionStatus::kRunning;
|
|
return tick;
|
|
}
|
|
|
|
void cancel() override
|
|
{
|
|
// Không có thiết bị nào để đưa về trạng thái an toàn — chờ thì dừng chờ là xong.
|
|
robot::log_info("[action_core] '%s': wait '%s' cancelled.", name_.c_str(), action_id_.c_str());
|
|
}
|
|
|
|
private:
|
|
/// @brief Đọc `duration` từ actionParameters. Trả false kèm log nếu giá trị không dùng được.
|
|
bool readDuration(const robot_protocol_msgs::Action& action, double& duration) const
|
|
{
|
|
const auto it = std::find_if(action.actionParameters.begin(), action.actionParameters.end(),
|
|
[](const robot_protocol_msgs::ActionParameter& p) {
|
|
return p.key == kDurationKey;
|
|
});
|
|
|
|
if (it == action.actionParameters.end())
|
|
{
|
|
duration = default_duration_;
|
|
return true;
|
|
}
|
|
|
|
try
|
|
{
|
|
duration = std::stod(it->value);
|
|
}
|
|
catch (const std::exception&)
|
|
{
|
|
robot::log_error("[action_core] '%s': action '%s' has duration='%s' which is not a number.",
|
|
name_.c_str(), action.actionId.c_str(), it->value.c_str());
|
|
return false;
|
|
}
|
|
|
|
if (!std::isfinite(duration) || duration < 0.0)
|
|
{
|
|
robot::log_error("[action_core] '%s': action '%s' has duration=%.3f s — must be >= 0.",
|
|
name_.c_str(), action.actionId.c_str(), duration);
|
|
return false;
|
|
}
|
|
|
|
if (duration > max_duration_)
|
|
{
|
|
robot::log_error("[action_core] '%s': action '%s' asks for %.3f s but max_duration is "
|
|
"%.3f s — refusing instead of silently shortening it.",
|
|
name_.c_str(), action.actionId.c_str(), duration, max_duration_);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
std::string name_;
|
|
std::vector<std::string> action_types_{ "wait" };
|
|
double default_duration_ = kDefaultDuration; ///< [s]
|
|
double max_duration_ = kDefaultMaxDuration; ///< [s]
|
|
|
|
robot::Time started_at_;
|
|
double duration_ = 0.0; ///< [s] của action đang chạy
|
|
std::string action_id_;
|
|
};
|
|
|
|
} // namespace action_core
|
|
|
|
BOOST_DLL_ALIAS(action_core::WaitActionHandler::create, WaitActionHandler)
|