fix(nav_2d): xoá UB trong TwoPointsPlanner và pointer hoang trong tf_help
- 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; comment nêu rõ vì sao chặng rotate chạm nhánh này. - 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, trông như hướng thật) 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:
@@ -190,7 +190,13 @@ namespace two_points_planner
|
||||
const double dx = goal.pose.position.x - start.pose.position.x;
|
||||
const double dy = goal.pose.position.y - start.pose.position.y;
|
||||
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
|
||||
double resolution = costmap_robot_->getCostmap()->getResolution();
|
||||
|
||||
@@ -205,6 +211,16 @@ namespace two_points_planner
|
||||
}
|
||||
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;
|
||||
pose.pose.position.x += resolution * cos(theta);
|
||||
pose.pose.position.y += resolution * sin(theta);
|
||||
|
||||
@@ -48,14 +48,25 @@ namespace robot_nav_2d_utils
|
||||
{
|
||||
const std::string target_frame = frame;
|
||||
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::doTransform(in_pose, out_pose, transform);
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -71,8 +82,12 @@ namespace robot_nav_2d_utils
|
||||
|
||||
try
|
||||
{
|
||||
transform(tf, in_pose, out_pose, frame);
|
||||
return true;
|
||||
// Report what transform() actually did. It used to `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)
|
||||
{
|
||||
@@ -81,8 +96,9 @@ namespace robot_nav_2d_utils
|
||||
robot_geometry_msgs::PoseStamped latest_in_pose;
|
||||
latest_in_pose.header.frame_id = in_pose.header.frame_id;
|
||||
latest_in_pose.pose = in_pose.pose;
|
||||
transform(tf, latest_in_pose, out_pose, frame);
|
||||
return true;
|
||||
// Same correction as above: the fallback can fail too, and saying it
|
||||
// succeeded hands the caller a pose of all zeros.
|
||||
return transform(tf, latest_in_pose, out_pose, frame);
|
||||
}
|
||||
catch (tf3::TransformException &ex)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user