feat(rotate): áp relative_yaw trong resolveDeferredGoal, đổi binding rotate

- NavigationRequest: thêm relative_yaw [rad], NaN = không dùng, đi kèm
  được với goal_frame/relative_distance thay vì loại trừ.

- resolveDeferredGoal(): thêm nhánh has_yaw. Trước đây relative_distance
  = 0 khiến goal CHÉP NGUYÊN pose hiện tại kể cả orientation — chặng
  rotate coi như đã tới đích ngay khi nhận, không quay độ nào. Giờ tính
  yaw nền (từ pose hiện tại, hoặc từ frame nếu goal_frame có) rồi cộng
  relative_yaw, dựng lại quaternion đủ 4 thành phần. Log "Goal received"
  thêm yaw= để chặng quay không còn trông giống hệt chặng vị trí.

- mission_adapter_bridge.cpp: chở relative_yaw qua nguyên vẹn.

- Thêm 3 dòng debug (nhóm debug_plan, mặc định bật) trong
  runController(): kết quả isGoalReached() khi true (candidate=0, chưa
  từng tính lệnh), v/omega trước arbiter, v/omega sau arbiter — phân
  biệt "chưa từng có lệnh" / "lệnh bị cắt" / "lỗi ngoài move_base2".
  Bật khối log đã có sẵn nhưng bị comment từ trước.

- Binding rotate: PNKXRotateLocalPlanner -> HybridRotateLocalPlanner.

- 13 test mới (deferred_goal_test.cpp + 2 ca bridge): quay giữ nguyên
  x/y chỉ đổi yaw, góc là delta theo hướng hiện tại, dấu âm quay ngược,
  quaternion chuẩn hoá, go_straight không bị đụng hướng, distance+yaw
  đi cùng nhau, frame+yaw lấy nền là hướng robot chứ không phải hướng
  frame đích, mất pose bị từ chối, NaN passthrough.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-11 10:46:58 +07:00
parent 180c0e1cd4
commit 7d00c5ab66
10 changed files with 606 additions and 30 deletions

View File

@@ -25,6 +25,13 @@ namespace
/// [-] Sai lệch chuẩn quaternion còn chấp nhận được trước khi coi goal là hỏng.
constexpr double kQuaternionNormTolerance = 1e-2;
/// @brief Yaw [rad] của một pose. Robot chạy trên mặt phẳng nên chỉ z-w của quaternion có nghĩa.
double yawOf(const robot_geometry_msgs::PoseStamped& pose)
{
const auto& q = pose.pose.orientation;
return std::atan2(2.0 * (q.w * q.z + q.x * q.y), 1.0 - 2.0 * (q.y * q.y + q.z * q.z));
}
/// [s] Giãn cách log cho guard "không đi mù". Tình trạng này kéo dài hàng giây, và đây là vòng lặp
/// điều khiển — log mỗi cycle sẽ nhấn chìm mọi dòng khác.
constexpr double kStaleCostmapLogThrottle = 5.0;
@@ -370,8 +377,11 @@ bool ControlLoop::resolveDeferredGoal(NavigationRequest& request, std::string& r
{
const bool has_frame = !request.goal_frame.empty();
const bool has_distance = std::isfinite(request.relative_distance);
// Góc quay ĐI KÈM được với hai nguồn đích kia, không loại trừ: chặng quay tại chỗ là
// relative_distance = 0 cộng một góc khác 0.
const bool has_yaw = std::isfinite(request.relative_yaw);
if (!has_frame && !has_distance)
if (!has_frame && !has_distance && !has_yaw)
{
return true; // Goal tuyệt đối, không có gì phải quy.
}
@@ -389,6 +399,19 @@ bool ControlLoop::resolveDeferredGoal(NavigationRequest& request, std::string& r
return false;
}
// Pose hiện tại: cần cho quãng tương đối, và cần cho góc tương đối kể cả khi vị trí đích tới từ
// một TF frame — góc luôn tính so với hướng robot đang quay về LÚC chặng được nhận.
robot_geometry_msgs::PoseStamped robot_pose;
const bool needs_robot_pose = has_distance || has_yaw;
if (needs_robot_pose && !deps_.pose->getRobotPose(robot_pose))
{
// Mất định vị thì không quy được quãng đường tương đối. Đoán ở đây là robot đi mù một đoạn.
reason = "a relative goal needs the robot pose, which is not available";
return false;
}
const double robot_yaw = needs_robot_pose ? yawOf(robot_pose) : 0.0;
if (has_frame)
{
if (!deps_.pose->lookupPose(request.goal_frame, request.goal))
@@ -396,23 +419,34 @@ bool ControlLoop::resolveDeferredGoal(NavigationRequest& request, std::string& r
reason = "cannot resolve goal_frame '" + request.goal_frame + "'";
return false;
}
return true;
}
robot_geometry_msgs::PoseStamped robot_pose;
if (!deps_.pose->getRobotPose(robot_pose))
else if (has_distance)
{
// Mất định vị thì không quy được quãng đường tương đối. Đoán ở đây là robot đi mù một đoạn.
reason = "relative_distance needs the robot pose, which is not available";
return false;
request.goal = robot_pose;
request.goal.pose.position.x += request.relative_distance * std::cos(robot_yaw);
request.goal.pose.position.y += request.relative_distance * std::sin(robot_yaw);
}
else
{
// Chỉ có góc: đích là chính chỗ robot đang đứng, chỉ khác hướng.
request.goal = robot_pose;
}
if (has_yaw)
{
// Hướng đích = hướng HIỆN TẠI cộng góc yêu cầu. Ghi đè orientation của goal chứ không cộng dồn
// vào orientation vừa lấy ở trên: với has_frame, orientation của frame đích là hướng của trạm
// chứ không phải hướng robot, và chặng quay nói về robot.
//
// Không normalize góc tổng: mission layer đã chặn ở [-pi, pi], và quaternion tự bọc.
const double goal_yaw = robot_yaw + request.relative_yaw;
const double half = 0.5 * goal_yaw;
request.goal.pose.orientation.x = 0.0;
request.goal.pose.orientation.y = 0.0;
request.goal.pose.orientation.z = std::sin(half);
request.goal.pose.orientation.w = std::cos(half);
}
const auto& q = robot_pose.pose.orientation;
const double yaw = std::atan2(2.0 * (q.w * q.z + q.x * q.y),
1.0 - 2.0 * (q.y * q.y + q.z * q.z)); // [rad]
request.goal = robot_pose;
request.goal.pose.position.x += request.relative_distance * std::cos(yaw);
request.goal.pose.position.y += request.relative_distance * std::sin(yaw);
return true;
}
@@ -811,6 +845,16 @@ void ControlLoop::runController(robot_geometry_msgs::Twist& candidate)
if (deps_.controller->isGoalReached())
{
controller_feedback_ = ControllerFeedback::kGoalReached;
if (config_.debug_plan)
{
// Nhánh này trả candidate RỖNG — không có lệnh nào được tính ở cycle này. Một planner báo
// "đã tới" ngay từ cycle đầu (trước khi computeVelocityCommands từng chạy lần nào) là triệu
// chứng ĐÚNG của "robot đứng im, không phát lệnh": không phải lệnh bị chặn, mà chưa bao giờ
// có lệnh nào được tính.
robot::log_info_throttle(kDebugControlLogThrottle,
"[move_base2][debug] runController: isGoalReached() = true -- "
"không tính lệnh cycle này (candidate = 0).\n");
}
return;
}
@@ -819,6 +863,16 @@ void ControlLoop::runController(robot_geometry_msgs::Twist& candidate)
{
controller_feedback_ = ControllerFeedback::kCommandValid;
candidate = cmd;
if (config_.debug_plan)
{
// v/omega TRƯỚC VelocityArbiter — phân biệt "planner không phát lệnh" (giá trị ở đây đã là
// 0 hoặc gần 0) với "lệnh phát ra nhưng bị guard/arbiter cắt về 0 phía sau" (giá trị ở đây
// khác 0 nhưng /cmd_vel lại là 0 — xem log EnvMonitor / VelocityArbiter cho cycle này).
robot::log_info_throttle(kDebugControlLogThrottle,
"[move_base2][debug] runController: command_valid v=%.3f m/s "
"omega=%.4f rad/s (trước arbiter).\n",
cmd.linear.x, cmd.angular.z);
}
return;
}
@@ -958,10 +1012,12 @@ bool ControlLoop::step()
// Log một lần tại sườn nhận goal — không nằm trên đường lặp của control loop.
if (active_request_.has_goal)
{
robot::log_info("[move_base2] Goal received (mission %llu): x=%.3f y=%.3f frame=%s.\n",
// yaw có mặt ở đây vì một chặng quay tại chỗ KHÁC chặng thường đúng ở con số này: x, y giữ
// nguyên và chỉ hướng đổi. Thiếu nó thì một chặng quay hỏng trông y hệt một chặng quay đúng.
robot::log_info("[move_base2] Goal received (mission %llu): x=%.3f y=%.3f yaw=%.4f frame=%s.\n",
static_cast<unsigned long long>(active_request_.mission_sequence_id),
active_request_.goal.pose.position.x, active_request_.goal.pose.position.y,
active_request_.goal.header.frame_id.c_str());
yawOf(active_request_.goal), active_request_.goal.header.frame_id.c_str());
}
else
{
@@ -1022,13 +1078,16 @@ bool ControlLoop::step()
if (config_.debug_plan)
{
// yaw của pose CUỐI: đây là thứ setGoalPose() thực sự trao cho local planner. Chặng quay
// tại chỗ có first/last CÙNG vị trí — không có yaw ở đây thì log không phân biệt được
// "goal đúng nhưng chưa quay tới" với "goal sai ngay từ khi lập plan".
robot::log_info("[move_base2][debug] controller accepted plan with %zu pose(s); "
"first=(%.3f, %.3f, frame=%s), last=(%.3f, %.3f, frame=%s).\n",
"first=(%.3f, %.3f, frame=%s), last=(%.3f, %.3f, yaw=%.4f, frame=%s).\n",
latest_plan_.size(), latest_plan_.front().pose.position.x,
latest_plan_.front().pose.position.y,
latest_plan_.front().header.frame_id.c_str(),
latest_plan_.back().pose.position.x, latest_plan_.back().pose.position.y,
latest_plan_.back().header.frame_id.c_str());
yawOf(latest_plan_.back()), latest_plan_.back().header.frame_id.c_str());
}
}
}
@@ -1270,12 +1329,6 @@ bool ControlLoop::step()
output.recovery_route_cursor, output.recovery_route_size);
}
}
// else if (controller_feedback_ == ControllerFeedback::kCommandValid)
// {
// robot::log_info_throttle(kDebugControlLogThrottle,
// "[move_base2][debug] controller command valid: v=%.3f wz=%.3f.\n",
// candidate.linear.x, candidate.angular.z);
// }
}
// Replan nền (contract 2026-08-04): đang bám plan và tới nhịp `planner_frequency` thì khởi động
@@ -1324,6 +1377,21 @@ bool ControlLoop::step()
arbiter_.arbitrate((costmap_stale || env_brake_) ? VelocitySource::kNone : output.velocity_source,
candidate, dt);
if (config_.debug_plan && output.run_controller)
{
// Sau arbiter — thứ AmrPublisher thật sự lấy để phát /cmd_vel. Đối chiếu với dòng
// "runController: command_valid v=... omega=... (trước arbiter)" ở trên: hai dòng khác
// nhau nghĩa là VelocityArbiter (kẹp trần/gia tốc) hoặc EnvMonitor brake đã sửa lệnh; hai
// dòng giống hệt mà /cmd_vel vẫn đo được 0 thì lỗi nằm ngoài move_base2 (AmrPublisher, driver
// bánh xe, hoặc sim).
const robot_geometry_msgs::Twist& published = arbiter_.lastCommand();
robot::log_info_throttle(kDebugControlLogThrottle,
"[move_base2][debug] runController: sau arbiter v=%.3f m/s "
"omega=%.4f rad/s (nguồn=%s, source_in=%s).\n",
published.linear.x, published.angular.z,
toString(controller_feedback_), toString(output.velocity_source));
}
// --- 5. Báo kết quả -------------------------------------------------------------------------
if (output.report_outcome)
{