-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Tracking critic dwb #5603
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
silanus23
wants to merge
5
commits into
ros-navigation:main
Choose a base branch
from
silanus23:tracking-critics-dwb-and-mppi
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Tracking critic dwb #5603
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0bf9368
DWB critics addition
silanus23 2397e7c
mppi critic addition
silanus23 4fc9b0e
Taking back later additions creating problem
silanus23 115e700
addressing reviews and adding bideractional window
silanus23 dfab167
fixing absolute value problem
silanus23 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
nav2_dwb_controller/dwb_critics/include/dwb_critics/path_hug.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| // Copyright (c) 2025, Berkan Tali | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #ifndef DWB_CRITICS__PATH_HUG_HPP_ | ||
| #define DWB_CRITICS__PATH_HUG_HPP_ | ||
|
|
||
| #include <string> | ||
|
|
||
| #include "dwb_core/trajectory_critic.hpp" | ||
| #include "nav_msgs/msg/path.hpp" | ||
|
|
||
| namespace dwb_critics | ||
| { | ||
| /** | ||
| * @class PathHugCritic | ||
| * @brief Critic that penalizes trajectories based on their distance from the global path | ||
| * | ||
| * This critic encourages the robot to stay close to the planned path while allowing | ||
| * deviations for obstacle avoidance. Uses a bidirectional search window to handle | ||
| * both forward and backward trajectory motion. | ||
| */ | ||
| class PathHugCritic : public dwb_core::TrajectoryCritic | ||
| { | ||
| public: | ||
| /** | ||
| * @brief Result structure for segment search operations | ||
| */ | ||
| struct SegmentSearchResult | ||
| { | ||
| size_t closest_index; ///< Index of the closest path segment | ||
| double distance; ///< Distance to the closest segment | ||
| }; | ||
|
|
||
| void onInit() override; | ||
|
|
||
| bool prepare( | ||
| const geometry_msgs::msg::Pose & pose, | ||
| const nav_2d_msgs::msg::Twist2D & vel, | ||
| const geometry_msgs::msg::Pose & goal, | ||
| const nav_msgs::msg::Path & global_plan) override; | ||
|
|
||
| double scoreTrajectory(const dwb_msgs::msg::Trajectory2D & traj) override; | ||
|
|
||
| protected: | ||
| /** | ||
| * @brief Find closest path segment using bidirectional search window | ||
| * | ||
| * Searches both forward and backward from the hint index to handle trajectories | ||
| * that may move in any direction. The search window prevents matching to | ||
| * geometrically close but topologically distant path segments. | ||
| * | ||
| * @param path Global path to search | ||
| * @param pose Current pose to find closest segment for | ||
| * @param hint_index Starting index for search (typically previous closest segment) | ||
| * @param search_window Size of bidirectional search window in meters | ||
| * @return SegmentSearchResult containing closest segment index and distance | ||
| */ | ||
| SegmentSearchResult findClosestSegmentWithLookback( | ||
| const nav_msgs::msg::Path & path, | ||
| const geometry_msgs::msg::Pose & pose, | ||
| size_t hint_index, | ||
| double search_window) const; | ||
|
|
||
| nav_msgs::msg::Path global_path_; | ||
| double search_window_; | ||
| }; | ||
|
|
||
| } // namespace dwb_critics | ||
|
|
||
| #endif // DWB_CRITICS__PATH_HUG_HPP_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| // Copyright (c) 2025, Berkan Tali | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include "dwb_critics/path_hug.hpp" | ||
|
|
||
| #include <vector> | ||
| #include <string> | ||
| #include <cmath> | ||
| #include <limits> | ||
|
|
||
| #include "pluginlib/class_list_macros.hpp" | ||
| #include "nav2_util/path_utils.hpp" | ||
| #include "nav2_util/geometry_utils.hpp" | ||
|
|
||
| namespace dwb_critics | ||
| { | ||
|
|
||
| void PathHugCritic::onInit() | ||
| { | ||
| auto node = node_.lock(); | ||
| if (!node) { | ||
| throw std::runtime_error{"Failed to lock node"}; | ||
| } | ||
|
|
||
| nav2::declare_parameter_if_not_declared( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we also set a max allowable left/right deviation to score as invalid if any path of the path exceeds? |
||
| node, dwb_plugin_name_ + "." + name_ + ".search_window", rclcpp::ParameterValue(3.0)); | ||
| node->get_parameter(dwb_plugin_name_ + "." + name_ + ".search_window", search_window_); | ||
|
|
||
| if (search_window_ <= 0.0) { | ||
| throw std::runtime_error{"search_window must be positive"}; | ||
| } | ||
| } | ||
|
|
||
| bool PathHugCritic::prepare( | ||
| const geometry_msgs::msg::Pose & /*pose*/, const nav_2d_msgs::msg::Twist2D & /*vel*/, | ||
| const geometry_msgs::msg::Pose & /*goal*/, | ||
| const nav_msgs::msg::Path & global_plan) | ||
| { | ||
| global_path_ = global_plan; | ||
| return true; | ||
| } | ||
|
|
||
| PathHugCritic::SegmentSearchResult PathHugCritic::findClosestSegmentWithLookback( | ||
| const nav_msgs::msg::Path & path, | ||
| const geometry_msgs::msg::Pose & pose, | ||
| size_t hint_index, | ||
| double search_window) const | ||
| { | ||
| if (path.poses.empty()) { | ||
| return {0, 0.0}; | ||
| } | ||
|
|
||
| const size_t path_size = path.poses.size(); | ||
|
|
||
| if (path_size == 1) { | ||
| double dist = nav2_util::geometry_utils::euclidean_distance( | ||
| pose.position, path.poses[0].pose.position); | ||
| return {0, dist}; | ||
| } | ||
|
|
||
| if (hint_index >= path_size) { | ||
| hint_index = path_size - 1; | ||
| } | ||
|
|
||
| double min_distance = std::numeric_limits<double>::max(); | ||
| size_t closest_index = hint_index; | ||
| const double half_window = search_window / 2.0; | ||
|
|
||
| // Search backward from hint_index | ||
| double distance_traversed = 0.0; | ||
| if (hint_index > 0) { | ||
| for (size_t i = hint_index; i > 0; --i) { | ||
| if (distance_traversed > half_window) { | ||
| break; | ||
| } | ||
|
|
||
| const double dist = nav2_util::geometry_utils::distance_to_path_segment( | ||
| pose.position, | ||
| path.poses[i - 1].pose, | ||
| path.poses[i].pose); | ||
|
|
||
| if (dist < min_distance) { | ||
| min_distance = dist; | ||
| closest_index = i - 1; | ||
| } | ||
|
|
||
| distance_traversed += nav2_util::geometry_utils::euclidean_distance( | ||
| path.poses[i - 1].pose.position, | ||
| path.poses[i].pose.position); | ||
| } | ||
| } | ||
|
|
||
| // Search forward from hint_index | ||
| distance_traversed = 0.0; | ||
| for (size_t i = hint_index; i < path_size - 1; ++i) { | ||
| if (distance_traversed > half_window) { | ||
| break; | ||
| } | ||
|
|
||
| const double dist = nav2_util::geometry_utils::distance_to_path_segment( | ||
| pose.position, | ||
| path.poses[i].pose, | ||
| path.poses[i + 1].pose); | ||
|
|
||
| if (dist < min_distance) { | ||
| min_distance = dist; | ||
| closest_index = i; | ||
| } | ||
|
|
||
| distance_traversed += nav2_util::geometry_utils::euclidean_distance( | ||
| path.poses[i].pose.position, | ||
| path.poses[i + 1].pose.position); | ||
| } | ||
|
|
||
| return {closest_index, min_distance}; | ||
| } | ||
|
|
||
| double PathHugCritic::scoreTrajectory(const dwb_msgs::msg::Trajectory2D & traj) | ||
| { | ||
| if (traj.poses.empty() || global_path_.poses.empty()) { | ||
| return 0.0; | ||
| } | ||
|
|
||
| // Find initial hint from first trajectory pose | ||
| const geometry_msgs::msg::Pose & current_pose = traj.poses[0]; | ||
| nav2_util::PathSearchResult search_result = nav2_util::distance_from_path( | ||
| global_path_, current_pose); | ||
|
|
||
| size_t start_index = search_result.closest_segment_index; | ||
| double total_distance = 0.0; | ||
|
|
||
| // Score each trajectory pose using bidirectional search | ||
| for (size_t traj_index = 0; traj_index < traj.poses.size(); traj_index++) { | ||
| SegmentSearchResult result = findClosestSegmentWithLookback( | ||
| global_path_, traj.poses[traj_index], start_index, search_window_); | ||
|
|
||
| total_distance += result.distance; | ||
| start_index = result.closest_index; // Update hint for next iteration | ||
| } | ||
|
|
||
| return total_distance / traj.poses.size(); | ||
| } | ||
|
|
||
| } // namespace dwb_critics | ||
|
|
||
| PLUGINLIB_EXPORT_CLASS(dwb_critics::PathHugCritic, dwb_core::TrajectoryCritic) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually rather like this name, but for the description docs / doxygen, can we mention cross tracking error (if not rename the critic to be more 'technical') so that this is clear the itention?