fix(nav_2d): xoá UB trong TwoPointsPlanner và pointer hoang trong tf_help

Sửa tay theo commit 37e44eb (T800_ws pnkx_nav_core, branch
dev_recovery_and_manager_mission) thay vì cherry-pick, theo yêu cầu.

- two_points_planner.cpp: nhánh suy biến (start == goal, ca quay tại chỗ)
  đọc theta chưa khởi tạo trước khi dùng trong cos()/sin(). Khởi tạo bằng
  yaw hiện tại của start.

- tf_help.cpp: canTransform() nhận std::string *error_msg chưa khởi tạo —
  tf3 ghi qua con trỏ đó khi transform thất bại, tức ghi vào vùng nhớ bất
  kỳ. Đổi sang std::string thật + log tên hai frame khi thất bại.

- tf_help.cpp: transformPose() gọi transform() rồi return true vô điều
  kiện ở cả nhánh chính lẫn nhánh ExtrapolationException — lookup hỏng để
  lại out_pose mặc định (yaw 0) mà vẫn báo thành công. Trả thẳng kết quả
  của transform().

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-11 13:36:53 +07:00
parent 203279eb03
commit 418686ff29
2 changed files with 43 additions and 11 deletions

View File

@@ -190,21 +190,37 @@ namespace two_points_planner
const double dx = goal.pose.position.x - start.pose.position.x; const double dx = goal.pose.position.x - start.pose.position.x;
const double dy = goal.pose.position.y - start.pose.position.y; const double dy = goal.pose.position.y - start.pose.position.y;
const double distance = std::sqrt(dx * dx + dy * dy); const double distance = std::sqrt(dx * dx + dy * dy);
double theta; // Travel heading of the plan. Seeded with the CURRENT robot yaw, not left
// uninitialised: an in-place rotation leg (profile `rotate`) asks for a goal
// whose position is the robot's own, so dx = dy = 0 and the branch below
// never assigns theta. Reading it then was undefined behaviour, and the
// degenerate branch fed cos()/sin() of that garbage into the first plan pose.
// The robot's own heading is the only reference frame that leg carries.
double theta = start_2d.pose.theta;
// Lấy độ phân giải của costmap // Lấy độ phân giải của costmap
double resolution = costmap_robot_->getCostmap()->getResolution(); double resolution = costmap_robot_->getCostmap()->getResolution();
if(fabs(dx) > 1e-9 || fabs(dy) > 1e-9) if(fabs(dx) > 1e-9 || fabs(dy) > 1e-9)
{ {
theta = std::atan2(dy, dx); theta = std::atan2(dy, dx);
// tf3::Quaternion goal_quat(goal.pose.orientation.x, goal.pose.orientation.y, // tf3::Quaternion goal_quat(goal.pose.orientation.x, goal.pose.orientation.y,
// goal.pose.orientation.z, goal.pose.orientation.w); // goal.pose.orientation.z, goal.pose.orientation.w);
// double goal_theta = tf3::getYaw(goal_quat); // double goal_theta = tf3::getYaw(goal_quat);
double goal_theta = robot_nav_2d_utils::poseStampedToPose2D(goal).pose.theta; double goal_theta = robot_nav_2d_utils::poseStampedToPose2D(goal).pose.theta;
if(cos(goal_theta - theta) < 0) theta += M_PI; if(cos(goal_theta - theta) < 0) theta += M_PI;
} }
else else
{ {
// Start and goal share a position: nothing to interpolate. This is the
// in-place rotation case. Emit two poses so the plan is never empty
// (ControllerRunner rejects an empty plan before the local planner is even
// called) and so the LAST pose stays `goal` — the controller takes
// plan.back() as the goal pose handed to setGoalPose(), which is what
// carries the requested heading.
//
// The first pose is offset one cell along the robot's current heading
// rather than being a copy of `start`: consecutive identical poses make
// any consumer that derives a direction from them compute atan2(0, 0).
robot_geometry_msgs::PoseStamped pose = start; robot_geometry_msgs::PoseStamped pose = start;
pose.pose.position.x += resolution * cos(theta); pose.pose.position.x += resolution * cos(theta);
pose.pose.position.y += resolution * sin(theta); pose.pose.position.y += resolution * sin(theta);

View File

@@ -48,14 +48,25 @@ namespace robot_nav_2d_utils
{ {
const std::string target_frame = frame; const std::string target_frame = frame;
const std::string source_frame = in_pose.header.frame_id; const std::string source_frame = in_pose.header.frame_id;
std::string *error_msg;
if(tf->canTransform(target_frame, source_frame, tf3::Time(), error_msg)) // A real string, not an uninitialised pointer. tf3 documents error_msg as
// "filled with why the transform failed, if not NULL", so it WRITES through
// whatever it is handed on the failure path. This used to be a raw
// `std::string *error_msg;` with no initialiser: every unavailable
// transform wrote a string through a wild pointer.
std::string error_msg;
if(tf->canTransform(target_frame, source_frame, tf3::Time(), &error_msg))
{ {
tf3::TransformStampedMsg transform =tf->lookupTransform(target_frame, source_frame, tf3::Time()); tf3::TransformStampedMsg transform =tf->lookupTransform(target_frame, source_frame, tf3::Time());
tf3::doTransform(in_pose, out_pose, transform); tf3::doTransform(in_pose, out_pose, transform);
return true; return true;
} }
// Say which transform is missing. Silence here surfaces later as a planner
// steering on a pose of all zeros, with nothing pointing at TF.
robot::log_error_throttle(2.0, "transform: no transform from '%s' to '%s': %s",
source_frame.c_str(), target_frame.c_str(), error_msg.c_str());
return false; return false;
} }
@@ -71,8 +82,12 @@ namespace robot_nav_2d_utils
try try
{ {
transform(tf, in_pose, out_pose, frame); // Report what transform() actually did. It used to `return true`
return true; // unconditionally: a lookup that failed left out_pose default-constructed
// — a pose of all zeros, with a yaw of 0 that reads like a real heading —
// and told the caller it had succeeded. Callers that ignore the result
// are unaffected; callers that check it now get the truth.
return transform(tf, in_pose, out_pose, frame);
} }
catch (tf3::ExtrapolationException &ex) catch (tf3::ExtrapolationException &ex)
{ {
@@ -81,8 +96,9 @@ namespace robot_nav_2d_utils
robot_geometry_msgs::PoseStamped latest_in_pose; robot_geometry_msgs::PoseStamped latest_in_pose;
latest_in_pose.header.frame_id = in_pose.header.frame_id; latest_in_pose.header.frame_id = in_pose.header.frame_id;
latest_in_pose.pose = in_pose.pose; latest_in_pose.pose = in_pose.pose;
transform(tf, latest_in_pose, out_pose, frame); // Same correction as above: the fallback can fail too, and saying it
return true; // succeeded hands the caller a pose of all zeros.
return transform(tf, latest_in_pose, out_pose, frame);
} }
catch (tf3::TransformException &ex) catch (tf3::TransformException &ex)
{ {