Skip to content

Commit 9189594

Browse files
committed
Added frame checks to controller and completed doxygen
Signed-off-by: silanus23 <[email protected]>
1 parent 1a2bc44 commit 9189594

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

nav2_controller/src/controller_server.cpp

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,7 @@ void ControllerServer::setPlannerPath(const nav_msgs::msg::Path & path)
618618
get_logger(), "Path end point is (%.2f, %.2f)",
619619
end_pose_.pose.position.x, end_pose_.pose.position.y);
620620

621+
// start from zero with every new path
621622
start_index_ = 0;
622623
current_path_ = path;
623624
}
@@ -769,18 +770,40 @@ void ControllerServer::publishTrackingState()
769770
return;
770771
}
771772

772-
const double distance_to_goal = nav2_util::geometry_utils::euclidean_distance(robot_pose,
773-
end_pose_);
774-
const auto path_search_result = nav2_util::distance_from_path(current_path_, robot_pose.pose,
775-
start_index_, search_window_);
773+
// Frame checks
774+
geometry_msgs::msg::PoseStamped robot_pose_in_path_frame;
775+
if (!nav2_util::transformPoseInTargetFrame(
776+
robot_pose, robot_pose_in_path_frame, *costmap_ros_->getTfBuffer(),
777+
current_path_.header.frame_id, costmap_ros_->getTransformTolerance()))
778+
{
779+
RCLCPP_WARN(get_logger(), "Failed to transform robot pose to path frame.");
780+
return;
781+
}
782+
783+
geometry_msgs::msg::PoseStamped end_pose_in_robot_frame;
784+
if (!nav2_util::transformPoseInTargetFrame(
785+
end_pose_, end_pose_in_robot_frame, *costmap_ros_->getTfBuffer(),
786+
robot_pose.header.frame_id, costmap_ros_->getTransformTolerance()))
787+
{
788+
RCLCPP_WARN(get_logger(), "Failed to transform end pose to robot frame.");
789+
return;
790+
}
791+
792+
const double distance_to_goal = nav2_util::geometry_utils::euclidean_distance(
793+
robot_pose, end_pose_in_robot_frame);
794+
795+
const auto path_search_result = nav2_util::distance_from_path(
796+
current_path_, robot_pose_in_path_frame.pose, start_index_, search_window_);
797+
776798
const size_t closest_idx = path_search_result.closest_segment_index;
777799
start_index_ = closest_idx;
778800

779801
const auto & segment_start = current_path_.poses[closest_idx];
780802
const auto & segment_end = current_path_.poses[closest_idx + 1];
781803

804+
// Cross product is for getting which side of the track
782805
double cross_product = nav2_util::geometry_utils::cross_product_2d(
783-
robot_pose.pose.position, segment_start.pose, segment_end.pose);
806+
robot_pose_in_path_frame.pose.position, segment_start.pose, segment_end.pose);
784807

785808
nav2_msgs::msg::TrackingError tracking_error_msg;
786809
tracking_error_msg.header.stamp = now();

nav2_util/include/nav2_util/path_utils.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ struct PathSearchResult
4242
* This function searches for the closest segment on the given path to the robot's pose,
4343
* starting from a specified index and optionally limiting the search to a window length.
4444
* It returns the minimum distance found and the index of the closest segment.
45+
* If possible start_index should be provided. The main optimized version of this
46+
* function is the one using local search. Also it's better to use it with frame check.
4547
*
4648
* @param path The path to search (sequence of poses).
4749
* @param robot_pose The robot's current pose in pose form.

0 commit comments

Comments
 (0)