This commit is contained in:
2026-08-10 22:26:10 +07:00
parent 4a6b541773
commit ec11dc42ed
9 changed files with 724 additions and 9 deletions

View File

@@ -572,6 +572,176 @@ ConversionResult VDA5050SourceAdapter::convert(const mission_adapters::MissionRe
return result;
}
// ═════════════════════════════════════════════════════════════════════════════════════════════════
// InstantActionSourceAdapter
// ═════════════════════════════════════════════════════════════════════════════════════════════════
mission_adapters::MissionSourceAdapter::Ptr InstantActionSourceAdapter::create()
{
return std::make_shared<InstantActionSourceAdapter>();
}
std::string InstantActionSourceAdapter::schema() const
{
return mission_adapters::schema::kInstantAction;
}
bool InstantActionSourceAdapter::configure(const std::string& name, robot::NodeHandle& nh)
{
// Lớp cha gán name_/global_frame_ và nạp bảng compound theo namespace CỦA CHÍNH instance này —
// ở đó không có bảng nào, nên nó về rỗng. Đó là điều mong muốn: bảng thật được nạp lại ngay bên
// dưới từ namespace của nguồn order.
if (!VDA5050SourceAdapter::configure(name, nh))
return false;
nh.getParam(name + "/compound_actions_from", compound_source_, std::string("vda5050_src"));
if (compound_source_.empty())
{
robot::log_error("InstantActionSourceAdapter[%s]: compound_actions_from is empty — it must "
"name the mission source that owns the compound_actions table",
name.c_str());
return false;
}
if (!loadCompoundActions(compound_source_, nh))
return false;
robot::log_info("InstantActionSourceAdapter[%s]: sharing the compound_actions table of '%s'",
name.c_str(), compound_source_.c_str());
return true;
}
mission_adapters::SubmitMode
InstantActionSourceAdapter::toSubmitMode(const std::string& blocking_type)
{
// CỐ Ý bỏ qua `blocking_type`: một message instantActions LUÔN preempt.
//
// `kAppend` không còn ca dùng nào đúng. Fleet gửi message tiếp theo trong lúc message trước còn
// chạy, và ý nghĩa của lệnh mới là thay lệnh cũ — xếp nó sau chuỗi cũ thì lệnh "instant" chạy
// sau vài chục giây, và message cũ vẫn thao tác thiết bị dù đã bị lệnh mới thay thế.
//
// Tham số giữ lại vì nó là hợp đồng đọc được: chữ ký nói rằng đây là chỗ `blockingType` từng
// quyết định hàng đợi, và ai muốn khôi phục sự phân biệt đó thì sửa đúng một hàm này.
(void)blocking_type;
return SubmitMode::kReplace;
}
bool InstantActionSourceAdapter::validate(const mission_adapters::MissionRequest& request,
std::string& reason) const
{
if (!request.actions)
{
reason = "request is missing the instantActions payload";
return false;
}
if (request.actions->empty())
{
reason = "instantActions message carries no action";
return false;
}
// Một action hỏng làm hỏng CẢ message. VDA5050 chỉ đòi robot báo done/fail cho lượt gửi này, và
// fleet đang chặn mọi lệnh khác để đợi — chạy được ba trên bốn rồi báo "xong" là báo sai.
for (std::size_t i = 0; i < request.actions->size(); ++i)
{
const robot_protocol_msgs::Action& action = (*request.actions)[i];
if (action.actionType.empty())
{
reason = "action " + std::to_string(i) + " has an empty actionType";
return false;
}
// VDA5050 bắt buộc actionId. Ở đây nó còn là thứ host dùng để dựng `actionStates` trả về.
if (action.actionId.empty())
{
reason = "action '" + action.actionType + "' (index " + std::to_string(i) +
") has an empty actionId";
return false;
}
}
return true;
}
void InstantActionSourceAdapter::logBlockingTypeNarrowing(
const robot_protocol_msgs::Action& action) const
{
if (action.blockingType != robot_protocol_msgs::Action::NONE &&
action.blockingType != robot_protocol_msgs::Action::SOFT &&
action.blockingType != robot_protocol_msgs::Action::HARD)
{
robot::log_warning("InstantActionSourceAdapter[%s]: '%s' (id '%s') has blockingType '%s' — "
"not one of NONE/SOFT/HARD; treating it as HARD",
name_.c_str(), action.actionType.c_str(), action.actionId.c_str(),
action.blockingType.c_str());
return;
}
if (action.blockingType == robot_protocol_msgs::Action::NONE)
{
// Thu hẹp có chủ đích, xem doc của lớp. Log để người đọc log biết vì sao robot đứng chờ thay
// vì chạy song song như fleet mong đợi.
robot::log_warning("InstantActionSourceAdapter[%s]: '%s' (id '%s') asks for blockingType "
"NONE (parallel with movement) — this runtime cannot drive while an "
"action runs, so it is queued after the current leg instead",
name_.c_str(), action.actionType.c_str(), action.actionId.c_str());
}
}
mission_adapters::ConversionResult
InstantActionSourceAdapter::convert(const mission_adapters::MissionRequest& request)
{
ConversionResult result;
// Cả message preempt, không phụ thuộc `blockingType` — xem @ref toSubmitMode.
result.mode = SubmitMode::kReplace;
for (const robot_protocol_msgs::Action& action : *request.actions)
{
logBlockingTypeNarrowing(action);
// Chặng chỉ-action mang đúng action vừa nhận. Nếu nó là compound, `expandCompound` sẽ GỠ
// action khỏi đây và phát ra chuỗi chặng thay thế — lúc đó chặng này rỗng và bị bỏ bên dưới.
auto leg = std::make_shared<Mission>();
leg->type = MissionType::VDA5050_ORDER;
leg->has_goal = false;
Action carried;
carried.type = ActionType::NODE_ACTION;
carried.sequenceId = 0;
carried.action = action;
leg->actions.push_back(std::move(carried));
std::vector<std::shared_ptr<Mission>> expanded;
if (!expandCompound(leg, expanded))
{
// Thiếu tham số cấu trúc (ví dụ `move_to_param` không có trong actionParameters).
//
// Bỏ CẢ MESSAGE, kể cả những action đã dựng xong trước đó trong vòng lặp này. Fleet gửi
// một message và chờ một câu trả lời done/fail cho nó; chạy được ba action rồi tắc ở
// action thứ tư mà vẫn báo "xong" là báo sai — và những action đã chạy thì đã đụng vào
// thiết bị thật rồi. Trả rỗng: EventProcessor giữ nguyên hàng đợi đang chạy.
robot::log_error("InstantActionSourceAdapter[%s]: action '%s' (id '%s') could not be "
"expanded — dropping the whole instantActions message (%zu action(s))",
name_.c_str(), action.actionType.c_str(), action.actionId.c_str(),
request.actions->size());
return ConversionResult{};
}
// Sau khi nở, chặng gốc chỉ còn ý nghĩa khi action KHÔNG phải compound.
if (leg->has_goal || !leg->actions.empty())
result.missions.push_back(std::move(leg));
for (auto& sub : expanded)
result.missions.push_back(std::move(sub));
}
return result;
}
} // namespace mission_plugins
BOOST_DLL_ALIAS(mission_plugins::VDA5050SourceAdapter::create, VDA5050SourceAdapter)
BOOST_DLL_ALIAS(mission_plugins::InstantActionSourceAdapter::create, InstantActionSourceAdapter)