15 #include <ompl/base/ScopedState.h>
16 #include <ompl/base/spaces/DubinsStateSpace.h>
22 #include "angles/angles.h"
24 #include "tf2/utils.hpp"
26 #include "nav2_smac_planner/smoother.hpp"
27 #include "nav2_util/smoother_utils.hpp"
29 namespace nav2_smac_planner
31 using namespace nav2_util::geometry_utils;
32 using namespace std::chrono;
37 tolerance_ = params.tolerance_;
38 max_its_ = params.max_its_;
39 data_w_ = params.w_data_;
40 smooth_w_ = params.w_smooth_;
41 is_holonomic_ = params.holonomic_;
42 do_refinement_ = params.do_refinement_;
43 refinement_num_ = params.refinement_num_;
48 min_turning_rad_ = min_turning_radius;
49 state_space_ = std::make_unique<ompl::base::DubinsStateSpace>(min_turning_rad_);
53 nav_msgs::msg::Path & path,
55 const double & max_time,
56 const std::vector<geometry_msgs::msg::Point> & footprint)
63 steady_clock::time_point start = steady_clock::now();
64 double time_remaining = max_time;
65 bool success =
true, reversing_segment;
66 nav_msgs::msg::Path curr_path_segment;
67 curr_path_segment.header = path.header;
68 std::vector<PathSegment> path_segments = nav2_util::findDirectionalPathSegments(
72 for (
unsigned int i = 0; i != path_segments.size(); i++) {
73 if (path_segments[i].end - path_segments[i].start > 10) {
75 curr_path_segment.poses.clear();
77 path.poses.begin() + path_segments[i].start,
78 path.poses.begin() + path_segments[i].end + 1,
79 std::back_inserter(curr_path_segment.poses));
82 steady_clock::time_point now = steady_clock::now();
83 time_remaining = max_time - duration_cast<duration<double>>(now - start).count();
87 const geometry_msgs::msg::Pose start_pose = curr_path_segment.poses.front().pose;
88 const geometry_msgs::msg::Pose goal_pose = curr_path_segment.poses.back().pose;
91 curr_path_segment, reversing_segment, costmap, time_remaining,
93 success = success && local_success;
96 if (!is_holonomic_ && local_success) {
97 enforceStartBoundaryConditions(start_pose, curr_path_segment, costmap, reversing_segment);
98 enforceEndBoundaryConditions(goal_pose, curr_path_segment, costmap, reversing_segment);
103 curr_path_segment.poses.begin(),
104 curr_path_segment.poses.end(),
105 path.poses.begin() + path_segments[i].start);
113 nav_msgs::msg::Path & path,
114 bool & reversing_segment,
116 const double & max_time,
117 const std::vector<geometry_msgs::msg::Point> & footprint)
119 steady_clock::time_point a = steady_clock::now();
120 rclcpp::Duration max_dur = rclcpp::Duration::from_seconds(max_time);
123 double change = tolerance_;
124 const unsigned int & path_size = path.poses.size();
125 double x_i, y_i, y_m1, y_ip1, y_i_org;
128 nav_msgs::msg::Path new_path = path;
129 nav_msgs::msg::Path last_path = path;
131 while (change >= tolerance_) {
136 if (its >= max_its_) {
138 rclcpp::get_logger(
"SmacPlannerSmoother"),
139 "Number of iterations has exceeded limit of %i.", max_its_);
141 nav2_util::updateApproximatePathOrientations(path, reversing_segment, is_holonomic_);
146 steady_clock::time_point b = steady_clock::now();
147 rclcpp::Duration timespan(duration_cast<duration<double>>(b - a));
148 if (timespan > max_dur) {
150 rclcpp::get_logger(
"SmacPlannerSmoother"),
151 "Smoothing time exceeded allowed duration of %0.2f.", max_time);
153 nav2_util::updateApproximatePathOrientations(path, reversing_segment, is_holonomic_);
157 for (
unsigned int i = 1; i != path_size - 1; i++) {
158 for (
unsigned int j = 0; j != 2; j++) {
159 x_i = getFieldByDim(path.poses[i], j);
160 y_i = getFieldByDim(new_path.poses[i], j);
161 y_m1 = getFieldByDim(new_path.poses[i - 1], j);
162 y_ip1 = getFieldByDim(new_path.poses[i + 1], j);
166 y_i += data_w_ * (x_i - y_i) + smooth_w_ * (y_ip1 + y_m1 - (2.0 * y_i));
167 setFieldByDim(new_path.poses[i], j, y_i);
168 change += abs(y_i - y_i_org);
172 nav2_util::updateApproximatePathOrientations(
173 new_path, reversing_segment, is_holonomic_);
177 if (!footprint.empty()) {
178 footprint_checker_.setCostmap(
182 for (
unsigned int i = 1; i != path_size - 1; i++) {
185 if (footprint.empty()) {
187 getFieldByDim(new_path.poses[i], 0),
188 getFieldByDim(new_path.poses[i], 1),
190 cost =
static_cast<float>(costmap->
getCost(mx, my));
192 cost =
static_cast<float>(footprint_checker_.footprintCostAtPose(
193 new_path.poses[i].pose.position.x,
194 new_path.poses[i].pose.position.y,
195 tf2::getYaw(new_path.poses[i].pose.orientation),
199 if (cost > MAX_NON_OBSTACLE_COST && cost != UNKNOWN_COST) {
201 rclcpp::get_logger(
"SmacPlannerSmoother"),
202 "Smoothing process resulted in an infeasible collision. "
203 "Returning the last path before the infeasibility was introduced.");
210 last_path = new_path;
215 if (do_refinement_ && refinement_ctr_ < refinement_num_) {
217 smoothImpl(new_path, reversing_segment, costmap, max_time, footprint);
225 const geometry_msgs::msg::PoseStamped & msg,
const unsigned int & dim)
228 return msg.pose.position.x;
229 }
else if (dim == 1) {
230 return msg.pose.position.y;
232 return msg.pose.position.z;
237 geometry_msgs::msg::PoseStamped & msg,
const unsigned int dim,
238 const double & value)
241 msg.pose.position.x = value;
242 }
else if (dim == 1) {
243 msg.pose.position.y = value;
245 msg.pose.position.z = value;
250 const BoundaryExpansions & boundary_expansions)
255 double min_length = 1e9;
256 int shortest_boundary_expansion_idx = 1e9;
257 for (
unsigned int idx = 0; idx != boundary_expansions.size(); idx++) {
258 if (boundary_expansions[idx].expansion_path_length<min_length &&
259 !boundary_expansions[idx].in_collision &&
260 boundary_expansions[idx].path_end_idx>0.0 &&
261 boundary_expansions[idx].expansion_path_length > 0.0)
263 min_length = boundary_expansions[idx].expansion_path_length;
264 shortest_boundary_expansion_idx = idx;
268 return shortest_boundary_expansion_idx;
272 const geometry_msgs::msg::Pose & start,
273 const geometry_msgs::msg::Pose & end,
277 ompl::base::ScopedState<> from(state_space_), to(state_space_), s(state_space_);
279 from[0] = start.position.x;
280 from[1] = start.position.y;
281 from[2] = tf2::getYaw(start.orientation);
282 to[0] = end.position.x;
283 to[1] = end.position.y;
284 to[2] = tf2::getYaw(end.orientation);
286 double d = state_space_->distance(from(), to());
294 if (d > 2.0 * expansion.original_path_length) {
298 std::vector<double> reals;
299 double theta(0.0), x(0.0), y(0.0);
300 double x_m = start.position.x;
301 double y_m = start.position.y;
304 for (
double i = 0; i <= expansion.path_end_idx; i++) {
305 state_space_->interpolate(from(), to(), i / expansion.path_end_idx, s());
308 theta = (reals[2] < 0.0) ? (reals[2] + 2.0 * M_PI) : reals[2];
309 theta = (theta > 2.0 * M_PI) ? (theta - 2.0 * M_PI) : theta;
316 if (
static_cast<float>(costmap->
getCost(mx, my)) >= INSCRIBED_COST) {
317 expansion.in_collision =
true;
321 expansion.expansion_path_length += hypot(x - x_m, y - y_m);
326 expansion.pts.emplace_back(x, y, theta);
330 template<
typename IteratorT>
333 std::vector<double> distances = {
335 2.0 * min_turning_rad_,
336 M_PI * min_turning_rad_,
337 2.0 * M_PI * min_turning_rad_
340 BoundaryExpansions boundary_expansions;
341 boundary_expansions.resize(distances.size());
342 double curr_dist = 0.0;
343 double x_last = start->pose.position.x;
344 double y_last = start->pose.position.y;
345 geometry_msgs::msg::Point pt;
346 unsigned int curr_dist_idx = 0;
348 for (IteratorT iter = start; iter != end; iter++) {
349 pt = iter->pose.position;
350 curr_dist += hypot(pt.x - x_last, pt.y - y_last);
354 if (curr_dist >= distances[curr_dist_idx]) {
355 boundary_expansions[curr_dist_idx].path_end_idx = iter - start;
356 boundary_expansions[curr_dist_idx].original_path_length = curr_dist;
360 if (curr_dist_idx == boundary_expansions.size()) {
365 return boundary_expansions;
369 const geometry_msgs::msg::Pose & start_pose,
370 nav_msgs::msg::Path & path,
372 const bool & reversing_segment)
375 BoundaryExpansions boundary_expansions =
376 generateBoundaryExpansionPoints<PathIterator>(path.poses.begin(), path.poses.end());
379 for (
unsigned int i = 0; i != boundary_expansions.size(); i++) {
381 if (expansion.path_end_idx == 0.0) {
385 if (!reversing_segment) {
386 findBoundaryExpansion(
387 start_pose, path.poses[expansion.path_end_idx].pose, expansion,
390 findBoundaryExpansion(
391 path.poses[expansion.path_end_idx].pose, start_pose, expansion,
397 unsigned int best_expansion_idx = findShortestBoundaryExpansionIdx(boundary_expansions);
398 if (best_expansion_idx > boundary_expansions.size()) {
404 if (reversing_segment) {
405 std::reverse(best_expansion.pts.begin(), best_expansion.pts.end());
407 for (
unsigned int i = 0; i != best_expansion.pts.size(); i++) {
408 path.poses[i].pose.position.x = best_expansion.pts[i].x;
409 path.poses[i].pose.position.y = best_expansion.pts[i].y;
410 path.poses[i].pose.orientation = orientationAroundZAxis(best_expansion.pts[i].theta);
415 const geometry_msgs::msg::Pose & end_pose,
416 nav_msgs::msg::Path & path,
418 const bool & reversing_segment)
421 BoundaryExpansions boundary_expansions =
422 generateBoundaryExpansionPoints<ReversePathIterator>(path.poses.rbegin(), path.poses.rend());
425 unsigned int expansion_starting_idx;
426 for (
unsigned int i = 0; i != boundary_expansions.size(); i++) {
428 if (expansion.path_end_idx == 0.0) {
431 expansion_starting_idx = path.poses.size() - expansion.path_end_idx - 1;
432 if (!reversing_segment) {
433 findBoundaryExpansion(path.poses[expansion_starting_idx].pose, end_pose, expansion, costmap);
435 findBoundaryExpansion(end_pose, path.poses[expansion_starting_idx].pose, expansion, costmap);
440 unsigned int best_expansion_idx = findShortestBoundaryExpansionIdx(boundary_expansions);
441 if (best_expansion_idx > boundary_expansions.size()) {
447 if (reversing_segment) {
448 std::reverse(best_expansion.pts.begin(), best_expansion.pts.end());
450 expansion_starting_idx = path.poses.size() - best_expansion.path_end_idx - 1;
451 for (
unsigned int i = 0; i != best_expansion.pts.size(); i++) {
452 path.poses[expansion_starting_idx + i].pose.position.x = best_expansion.pts[i].x;
453 path.poses[expansion_starting_idx + i].pose.position.y = best_expansion.pts[i].y;
454 path.poses[expansion_starting_idx + i].pose.orientation = orientationAroundZAxis(
455 best_expansion.pts[i].theta);
A 2D costmap provides a mapping between points in the world and their associated "costs".
unsigned char getCost(unsigned int mx, unsigned int my) const
Get the cost of a cell in the costmap.
bool worldToMap(double wx, double wy, unsigned int &mx, unsigned int &my) const
Convert from world coordinates to map coordinates.
void initialize(const double &min_turning_radius)
Initialization of the smoother.
bool smoothImpl(nav_msgs::msg::Path &path, bool &reversing_segment, const nav2_costmap_2d::Costmap2D *costmap, const double &max_time, const std::vector< geometry_msgs::msg::Point > &footprint)
Smoother method - does the smoothing on a segment.
void enforceStartBoundaryConditions(const geometry_msgs::msg::Pose &start_pose, nav_msgs::msg::Path &path, const nav2_costmap_2d::Costmap2D *costmap, const bool &reversing_segment)
Enforced minimum curvature boundary conditions on plan output the robot is traveling in the same dire...
bool smooth(nav_msgs::msg::Path &path, const nav2_costmap_2d::Costmap2D *costmap, const double &max_time, const std::vector< geometry_msgs::msg::Point > &footprint=std::vector< geometry_msgs::msg::Point >())
Smoother API method.
double getFieldByDim(const geometry_msgs::msg::PoseStamped &msg, const unsigned int &dim)
Get the field value for a given dimension.
void enforceEndBoundaryConditions(const geometry_msgs::msg::Pose &end_pose, nav_msgs::msg::Path &path, const nav2_costmap_2d::Costmap2D *costmap, const bool &reversing_segment)
Enforced minimum curvature boundary conditions on plan output the robot is traveling in the same dire...
void setFieldByDim(geometry_msgs::msg::PoseStamped &msg, const unsigned int dim, const double &value)
Set the field value for a given dimension.
Smoother(const SmootherParams ¶ms)
A constructor for nav2_smac_planner::Smoother.
BoundaryExpansions generateBoundaryExpansionPoints(IteratorT start, IteratorT end)
Generates boundary expansions with end idx at least strategic distances away, using either Reverse or...
unsigned int findShortestBoundaryExpansionIdx(const BoundaryExpansions &boundary_expansions)
Given a set of boundary expansion, find the one which is shortest such that it is least likely to con...
void findBoundaryExpansion(const geometry_msgs::msg::Pose &start, const geometry_msgs::msg::Pose &end, BoundaryExpansion &expansion, const nav2_costmap_2d::Costmap2D *costmap)
Populate a motion model expansion from start->end into expansion.
Boundary expansion state.
Parameters for the smoother cost function.
A segment of a path in start/end indices.