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

@@ -137,6 +137,18 @@ namespace mission_adapters
/// Quãng đường tương đối [m] so với pose hiện tại; dương = tiến, âm = lùi. NaN = không dùng.
double relative_distance = std::numeric_limits<double>::quiet_NaN();
/**
* Góc quay tương đối [rad] so với hướng hiện tại; dương = ngược chiều kim đồng hồ (REP-103).
* NaN = không dùng.
*
* Đơn vị ở ĐÂY là radian, trong khi order của fleet mang ĐỘ: quy đổi xảy ra một lần duy
* nhất, tại adapter đọc payload. Mọi thứ từ đây trở vào lõi đều là radian.
*
* Đi kèm được với @ref relative_distance (chặng quay là distance 0 + góc khác 0), khác với
* cặp goal_frame/relative_distance vốn loại trừ nhau.
*/
double relative_yaw = std::numeric_limits<double>::quiet_NaN();
};
} // namespace mission_adapters

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));

View File

@@ -73,7 +73,8 @@ protected:
* @struct Step
* @brief Một bước trong chuỗi mở rộng của compound action.
*
* Engine hiểu đúng bốn từ khoá và **không biết** `charge`, `dock_target` hay `LiftFork` nghĩa là
* Engine hiểu đúng bốn từ khoá chính (cộng ba khoá tuỳ chọn) và **không biết** `charge`,
* `dock_target` hay `LiftFork` nghĩa là
* gì — đó là điều kiện để cùng một engine dùng lại cho model robot khác: thêm model = thêm YAML
* + một `.so` handler, không sửa dòng nào ở đây.
*
@@ -88,6 +89,15 @@ protected:
std::string motion_hint; ///< `profile:` -> position | docking | go_straight | rotate
std::string marker; ///< `marker:` -> khoá override planner, chỉ hợp lệ với docking
/**
* `turn_param: <key>` -> góc quay lấy từ actionParameters[<key>] của chính order, tính bằng
* ĐỘ. Chỉ hợp lệ với `profile: rotate`; `configure()` từ chối lúc boot nếu gắn nhầm profile.
*
* Cùng vai trò với @ref move_to_param: bảng compound giữ CẤU TRÚC (khoá nào mang góc), còn
* số đo tới từ order. Nhờ vậy đổi góc quay không phải sửa file YAML nào.
*/
std::string turn_param;
};
/// @brief Chuỗi chặng thay cho một action. Bảng chỉ giữ CẤU TRÚC, dữ liệu tới từ order.

View File

@@ -514,6 +514,15 @@ TEST(CompoundConfig, RejectsInvalidOrActionOnlyProfiles)
EXPECT_FALSE(adapter.configure("vda5050_bad_profile", nh));
}
TEST(CompoundConfig, RejectsTurnParamOnANonRotationStep)
{
robot::NodeHandle nh;
VDA5050SourceAdapter adapter;
EXPECT_FALSE(adapter.configure("vda5050_bad_turn_param", nh))
<< "góc quay chỉ có nghĩa với profile rotate; gắn vào profile khác là mong đợi một hành vi "
"không tồn tại, phải nổ ở boot chứ không im lặng lúc order đầu tiên tới";
}
// ═════════════════════════════════════════════════════════════════════════════════════════════════
// InstantActionSourceAdapter
//
@@ -719,6 +728,67 @@ TEST_F(InstantActionAdapterTest, ARelativeMoveCompoundCarriesTheDistance)
EXPECT_DOUBLE_EQ(result.missions[0]->relative_distance, -1.0) << "[m] âm = lùi ra khỏi trạm";
}
TEST_F(InstantActionAdapterTest, ARotationCompoundConvertsDegreesToRadians)
{
// Order của fleet mang ĐỘ; mọi thứ từ mission layer trở vào lõi là RADIAN. Quy đổi phải xảy ra
// đúng một lần, ở đây.
const auto result = convert(makeInstant("rotate", "HARD", "angle_deg", "90"));
ASSERT_EQ(result.missions.size(), 1u);
EXPECT_TRUE(result.missions[0]->has_goal);
EXPECT_EQ(result.missions[0]->motion_hint, "rotate");
EXPECT_DOUBLE_EQ(result.missions[0]->relative_distance, 0.0) << "[m] chặng quay không tịnh tiến";
EXPECT_NEAR(result.missions[0]->relative_yaw, M_PI / 2.0, 1e-9);
}
TEST_F(InstantActionAdapterTest, ARotationKeepsTheSignOfTheRequestedAngle)
{
const auto result = convert(makeInstant("rotate", "HARD", "angle_deg", "-45"));
ASSERT_EQ(result.missions.size(), 1u);
EXPECT_NEAR(result.missions[0]->relative_yaw, -M_PI / 4.0, 1e-9)
<< "âm = thuận chiều kim đồng hồ (REP-103)";
}
TEST_F(InstantActionAdapterTest, ARotationWithoutItsAngleDropsTheWholeAction)
{
// Không có góc thì không có chặng quay nào để chạy. Rơi về 0 độ nghĩa là robot nhận lệnh quay
// rồi đứng im, và fleet vẫn thấy action FINISHED.
EXPECT_TRUE(convert(makeInstant("rotate", "HARD")).missions.empty());
}
TEST_F(InstantActionAdapterTest, ARotationWithANonNumericAngleDropsTheWholeAction)
{
EXPECT_TRUE(convert(makeInstant("rotate", "HARD", "angle_deg", "quarter turn")).missions.empty());
// stod() dừng ở ký tự lạ và vẫn trả phần đọc được: "90abc" phải bị từ chối, không phải hiểu
// thành 90 độ.
EXPECT_TRUE(convert(makeInstant("rotate", "HARD", "angle_deg", "90abc")).missions.empty());
}
TEST_F(InstantActionAdapterTest, ARotationBeyondHalfATurnIsRefused)
{
// Đí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 chiều
// nào. 270 độ ngược kim đồng hồ quy về pose sẽ thành 90 độ thuận kim đồng hồ — robot làm đúng
// pose, sai ý định. Từ chối còn hơn quay ngược chiều người gửi mong đợi.
EXPECT_TRUE(convert(makeInstant("rotate", "HARD", "angle_deg", "270")).missions.empty());
EXPECT_TRUE(convert(makeInstant("rotate", "HARD", "angle_deg", "-181")).missions.empty());
// Đúng biên thì vẫn nhận.
const auto edge = convert(makeInstant("rotate", "HARD", "angle_deg", "180"));
ASSERT_EQ(edge.missions.size(), 1u);
EXPECT_NEAR(edge.missions[0]->relative_yaw, M_PI, 1e-9);
}
TEST_F(InstantActionAdapterTest, ANonRotationCompoundLeavesTheYawUnset)
{
const auto result = convert(makeInstant("UnDockFromStation", "HARD"));
ASSERT_EQ(result.missions.size(), 1u);
EXPECT_TRUE(std::isnan(result.missions[0]->relative_yaw))
<< "NaN = không dùng; một giá trị 0 ở đây sẽ ép chặng go_straight phải giữ nguyên hướng";
}
TEST_F(InstantActionAdapterTest, AMissingCompoundParameterDropsTheWholeAction)
{
// `charge` cần `goal_frame` trong actionParameters; không có thì bước docking không biết lùi vào

View File

@@ -88,6 +88,18 @@ vda5050_src:
steps:
- {move_to: dock_target, profile: docking, marker: trolley}
# Quay tại chỗ: không tịnh tiến, góc [deg] lấy từ actionParameters['angle_deg'] của order.
rotate:
steps:
- {move: 0.0, profile: rotate, turn_param: angle_deg}
# turn_param gắn vào profile không phải rotate -> configure() phải từ chối lúc boot.
vda5050_bad_turn_param:
compound_actions:
Spin:
steps:
- {move: 1.0, profile: go_straight, turn_param: angle_deg}
# Step có hai từ khoá -> configure() phải từ chối lúc boot.
vda5050_bad_step:
compound_actions: