feat(rotate): đọc angle_deg từ actionParameters, quy sang relative_yaw

- Mission: thêm relative_yaw [rad], NaN = không dùng, đi kèm được với
  relative_distance (khác cặp goal_frame/relative_distance vốn loại
  trừ nhau) — chặng quay tại chỗ là distance 0 + góc khác 0.

- Step: thêm turn_param (cùng vai trò move_to_param — bảng compound chỉ
  giữ cấu trúc, số đo tới từ actionParameters của order). Validate lúc
  boot: turn_param gắn vào profile khác rotate thì configure() từ chối.

- expandCompound(): parseTurnDegrees() đọc actionParameters theo
  turn_param, parse ĐỘ bằng std::stod (bắt hết ký tự đuôi rác kiểu
  "90abc"), validate biên [-180,180] — vượt biên thì TỪ CHỐI thay vì
  normalize âm thầm, vì goal là một pose chỉ diễn tả được hướng cuối,
  không diễn tả được chiều/số vòng quay. Quy sang rad, log cả hai đơn
  vị. Thiếu khoá/parse hỏng/ngoài biên đều bỏ CẢ message, đúng pattern
  move_to_param đã có.

- 7 test mới: chuyển đổi độ->rad, giữ dấu, thiếu tham số, tham số không
  phải số, vượt biên (cả hai đầu + đúng biên), compound khác không đụng
  relative_yaw, turn_param sai profile trượt lúc boot.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-11 10:46:31 +07:00
parent ec11dc42ed
commit ab0bed7248
5 changed files with 211 additions and 1 deletions

View File

@@ -1,7 +1,9 @@
#include "vda5050_source_adapter.h"
#include <algorithm>
#include <cctype>
#include <cmath>
#include <stdexcept>
#include <string>
#include <utility>
@@ -21,6 +23,90 @@ bool isNavigationProfile(const std::string& profile)
profile == "go_straight" || profile == "rotate";
}
/// [deg] Biên của một chặng quay. Vượt biên là dấu hiệu order muốn thứ mà một pose không diễn tả
/// được — xem @ref parseTurnDegrees.
constexpr double kMaxTurnDegrees = 180.0;
/**
* @brief Đọc góc quay [deg] từ actionParameters và quy sang [rad].
*
* @param action Action gốc của order, nơi giữ tham số.
* @param key Tên khoá lấy từ `turn_param` của step.
* @param source Tên adapter, chỉ dùng cho log.
* @param radians Nơi ghi kết quả khi thành công.
* @return false nếu thiếu khoá, không parse được, hoặc ngoài biên — bên gọi phải bỏ CẢ message.
*
* Đơn vị vào là ĐỘ vì đó là thứ fleet và người vận hành gửi; ra là RADIAN vì đó là thứ mọi API bên
* trong dùng. Quy đổi nằm đúng ở đây, biên duy nhất chạm payload, và log in cả hai đơn vị để một
* lần lệch đơn vị lộ ra trong log thay vì phải suy ngược từ hành vi robot.
*
* Biên +/-180 độ KHÔNG phải để cho đẹp: đích của chặng là một POSE, mà pose chỉ nói hướng cuối chứ
* không nói quay bao nhiêu hay quay chiều nào. "Quay 270 độ ngược kim đồng hồ" quy về pose sẽ thành
* "quay 90 độ thuận kim đồng hồ" — robot làm đúng pose, sai ý định. Từ chối thẳng còn hơn quay
* ngược chiều người gửi mong đợi.
*/
bool parseTurnDegrees(const robot_protocol_msgs::Action& action, const std::string& key,
const std::string& source, double& radians)
{
const auto it = std::find_if(action.actionParameters.begin(), action.actionParameters.end(),
[&key](const robot_protocol_msgs::ActionParameter& p) {
return p.key == key;
});
if (it == action.actionParameters.end() || it->value.empty())
{
robot::log_error("VDA5050SourceAdapter[%s]: rotation action '%s' (actionId '%s') requires "
"parameter '%s' [deg] but it is missing",
source.c_str(), action.actionType.c_str(), action.actionId.c_str(),
key.c_str());
return false;
}
double degrees = 0.0;
try
{
std::size_t consumed = 0;
degrees = std::stod(it->value, &consumed);
// stod dừng ở ký tự lạ đầu tiên và vẫn trả về phần đọc được: "90abc" sẽ thành 90 nếu không
// kiểm phần đuôi. Chỉ chấp nhận khoảng trắng phía sau.
while (consumed < it->value.size() &&
std::isspace(static_cast<unsigned char>(it->value[consumed])))
{
++consumed;
}
if (consumed != it->value.size())
{
throw std::invalid_argument("trailing characters");
}
}
catch (const std::exception& ex)
{
robot::log_error("VDA5050SourceAdapter[%s]: rotation action '%s' (actionId '%s') has "
"parameter '%s' = '%s', which is not a number [deg]: %s",
source.c_str(), action.actionType.c_str(), action.actionId.c_str(),
key.c_str(), it->value.c_str(), ex.what());
return false;
}
if (!std::isfinite(degrees) || std::fabs(degrees) > kMaxTurnDegrees)
{
robot::log_error("VDA5050SourceAdapter[%s]: rotation action '%s' (actionId '%s') asks for "
"%.3f deg; only [-%.0f, %.0f] deg can be expressed as a goal pose. A "
"larger turn would silently become the short way round.",
source.c_str(), action.actionType.c_str(), action.actionId.c_str(),
degrees, kMaxTurnDegrees, kMaxTurnDegrees);
return false;
}
radians = degrees * M_PI / 180.0;
robot::log_info("VDA5050SourceAdapter[%s]: rotation action '%s' (actionId '%s'): %s=%.3f [deg] "
"-> relative_yaw=%.4f [rad]",
source.c_str(), action.actionType.c_str(), action.actionId.c_str(), key.c_str(),
degrees, radians);
return true;
}
using mission_adapters::Action;
using mission_adapters::ActionType;
using mission_adapters::ConversionResult;
@@ -127,6 +213,7 @@ bool VDA5050SourceAdapter::loadCompoundActions(const std::string& name, robot::N
if (n["move"]) step.move = n["move"].as<double>();
if (n["profile"]) step.motion_hint = n["profile"].as<std::string>();
if (n["marker"]) step.marker = n["marker"].as<std::string>();
if (n["turn_param"]) step.turn_param = n["turn_param"].as<std::string>();
}
catch (const YAML::Exception& ex)
{
@@ -168,6 +255,17 @@ bool VDA5050SourceAdapter::loadCompoundActions(const std::string& name, robot::N
name.c_str(), action_type.c_str(), i);
return false;
}
if (!step.turn_param.empty() && step.motion_hint != "rotate")
{
// Góc quay chỉ có nghĩa với planner quay tại chỗ. Gắn vào profile khác thì hoặc là
// gõ nhầm, hoặc là mong đợi một hành vi không tồn tại — cả hai đều phải nổ ở boot
// chứ không phải bị bỏ qua lặng lẽ lúc order đầu tiên tới.
robot::log_error("VDA5050SourceAdapter[%s]: '%s' step %zu sets turn_param but its "
"profile is '%s'; turn_param is only valid with 'rotate'",
name.c_str(), action_type.c_str(), i,
step.motion_hint.empty() ? "position" : step.motion_hint.c_str());
return false;
}
steps.push_back(step);
}
@@ -427,6 +525,14 @@ bool VDA5050SourceAdapter::expandCompound(
{
sub->relative_distance = step.move;
}
// Góc quay đi kèm được với quãng tịnh tiến: chặng `rotate` là move 0 m + góc khác 0.
// Đặt sau khối trên để nó áp cho mọi dạng đích, không riêng dạng tương đối.
if (!step.turn_param.empty() &&
!parseTurnDegrees(entry.action, step.turn_param, name_, sub->relative_yaw))
{
return false;
}
}
out.push_back(std::move(sub));