15 #include <ompl/base/ScopedState.h>
16 #include <ompl/base/spaces/DubinsStateSpace.h>
19 #include "nav2_smac_planner/smoother.hpp"
21 namespace nav2_smac_planner
23 using namespace nav2_util::geometry_utils;
24 using namespace std::chrono;
28 tolerance_ = params.tolerance_;
29 max_its_ = params.max_its_;
30 data_w_ = params.w_data_;
31 smooth_w_ = params.w_smooth_;
32 is_holonomic_ = params.holonomic_;
33 do_refinement_ = params.do_refinement_;
38 min_turning_rad_ = min_turning_radius;
39 state_space_ = std::make_unique<ompl::base::DubinsStateSpace>(min_turning_rad_);
43 nav_msgs::msg::Path & path,
45 const double & max_time)
53 steady_clock::time_point start = steady_clock::now();
54 double time_remaining = max_time;
55 bool success =
true, reversing_segment;
56 nav_msgs::msg::Path curr_path_segment;
57 curr_path_segment.header = path.header;
58 std::vector<PathSegment> path_segments = findDirectionalPathSegments(path);
60 for (
unsigned int i = 0; i != path_segments.size(); i++) {
61 if (path_segments[i].end - path_segments[i].start > 10) {
63 curr_path_segment.poses.clear();
65 path.poses.begin() + path_segments[i].start,
66 path.poses.begin() + path_segments[i].end + 1,
67 std::back_inserter(curr_path_segment.poses));
70 steady_clock::time_point now = steady_clock::now();
71 time_remaining = max_time - duration_cast<duration<double>>(now - start).count();
74 const geometry_msgs::msg::Pose start_pose = curr_path_segment.poses.front().pose;
75 const geometry_msgs::msg::Pose goal_pose = curr_path_segment.poses.back().pose;
77 smoothImpl(curr_path_segment, reversing_segment, costmap, time_remaining);
78 success = success && local_success;
81 if (!is_holonomic_ && local_success) {
82 enforceStartBoundaryConditions(start_pose, curr_path_segment, costmap, reversing_segment);
83 enforceEndBoundaryConditions(goal_pose, curr_path_segment, costmap, reversing_segment);
88 curr_path_segment.poses.begin(),
89 curr_path_segment.poses.end(),
90 path.poses.begin() + path_segments[i].start);
98 nav_msgs::msg::Path & path,
99 bool & reversing_segment,
101 const double & max_time)
103 steady_clock::time_point a = steady_clock::now();
104 rclcpp::Duration max_dur = rclcpp::Duration::from_seconds(max_time);
107 double change = tolerance_;
108 const unsigned int & path_size = path.poses.size();
109 double x_i, y_i, y_m1, y_ip1, y_i_org;
112 nav_msgs::msg::Path new_path = path;
113 nav_msgs::msg::Path last_path = path;
115 while (change >= tolerance_) {
120 if (its >= max_its_) {
122 rclcpp::get_logger(
"SmacPlannerSmoother"),
123 "Number of iterations has exceeded limit of %i.", max_its_);
125 updateApproximatePathOrientations(path, reversing_segment);
130 steady_clock::time_point b = steady_clock::now();
131 rclcpp::Duration timespan(duration_cast<duration<double>>(b - a));
132 if (timespan > max_dur) {
134 rclcpp::get_logger(
"SmacPlannerSmoother"),
135 "Smoothing time exceeded allowed duration of %0.2f.", max_time);
137 updateApproximatePathOrientations(path, reversing_segment);
141 for (
unsigned int i = 1; i != path_size - 1; i++) {
142 for (
unsigned int j = 0; j != 2; j++) {
143 x_i = getFieldByDim(path.poses[i], j);
144 y_i = getFieldByDim(new_path.poses[i], j);
145 y_m1 = getFieldByDim(new_path.poses[i - 1], j);
146 y_ip1 = getFieldByDim(new_path.poses[i + 1], j);
150 y_i += data_w_ * (x_i - y_i) + smooth_w_ * (y_ip1 + y_m1 - (2.0 * y_i));
151 setFieldByDim(new_path.poses[i], j, y_i);
152 change += abs(y_i - y_i_org);
159 getFieldByDim(new_path.poses[i], 0),
160 getFieldByDim(new_path.poses[i], 1),
162 cost =
static_cast<float>(costmap->
getCost(mx, my));
165 if (cost > MAX_NON_OBSTACLE && cost != UNKNOWN) {
167 rclcpp::get_logger(
"SmacPlannerSmoother"),
168 "Smoothing process resulted in an infeasible collision. "
169 "Returning the last path before the infeasibility was introduced.");
171 updateApproximatePathOrientations(path, reversing_segment);
176 last_path = new_path;
181 if (do_refinement_ && refinement_ctr_ < 4) {
183 smoothImpl(new_path, reversing_segment, costmap, max_time);
186 updateApproximatePathOrientations(new_path, reversing_segment);
192 const geometry_msgs::msg::PoseStamped & msg,
const unsigned int & dim)
195 return msg.pose.position.x;
196 }
else if (dim == 1) {
197 return msg.pose.position.y;
199 return msg.pose.position.z;
204 geometry_msgs::msg::PoseStamped & msg,
const unsigned int dim,
205 const double & value)
208 msg.pose.position.x = value;
209 }
else if (dim == 1) {
210 msg.pose.position.y = value;
212 msg.pose.position.z = value;
218 std::vector<PathSegment> segments;
220 curr_segment.start = 0;
225 curr_segment.end = path.poses.size() - 1;
226 segments.push_back(curr_segment);
231 for (
unsigned int idx = 1; idx < path.poses.size() - 1; ++idx) {
233 double oa_x = path.poses[idx].pose.position.x -
234 path.poses[idx - 1].pose.position.x;
235 double oa_y = path.poses[idx].pose.position.y -
236 path.poses[idx - 1].pose.position.y;
237 double ab_x = path.poses[idx + 1].pose.position.x -
238 path.poses[idx].pose.position.x;
239 double ab_y = path.poses[idx + 1].pose.position.y -
240 path.poses[idx].pose.position.y;
243 double dot_product = (oa_x * ab_x) + (oa_y * ab_y);
244 if (dot_product < 0.0) {
245 curr_segment.end = idx;
246 segments.push_back(curr_segment);
247 curr_segment.start = idx;
251 double cur_theta = tf2::getYaw(path.poses[idx].pose.orientation);
252 double next_theta = tf2::getYaw(path.poses[idx + 1].pose.orientation);
253 double dtheta = angles::shortest_angular_distance(cur_theta, next_theta);
254 if (fabs(ab_x) < 1e-4 && fabs(ab_y) < 1e-4 && fabs(dtheta) > 1e-4) {
255 curr_segment.end = idx;
256 segments.push_back(curr_segment);
257 curr_segment.start = idx;
261 curr_segment.end = path.poses.size() - 1;
262 segments.push_back(curr_segment);
267 nav_msgs::msg::Path & path,
268 bool & reversing_segment)
270 double dx, dy, theta, pt_yaw;
271 reversing_segment =
false;
274 dx = path.poses[2].pose.position.x - path.poses[1].pose.position.x;
275 dy = path.poses[2].pose.position.y - path.poses[1].pose.position.y;
276 theta = atan2(dy, dx);
277 pt_yaw = tf2::getYaw(path.poses[1].pose.orientation);
278 if (!is_holonomic_ && fabs(angles::shortest_angular_distance(pt_yaw, theta)) > M_PI_2) {
279 reversing_segment =
true;
283 for (
unsigned int i = 0; i != path.poses.size() - 1; i++) {
284 dx = path.poses[i + 1].pose.position.x - path.poses[i].pose.position.x;
285 dy = path.poses[i + 1].pose.position.y - path.poses[i].pose.position.y;
286 theta = atan2(dy, dx);
289 if (fabs(dx) < 1e-4 && fabs(dy) < 1e-4) {
294 if (reversing_segment) {
298 path.poses[i].pose.orientation = orientationAroundZAxis(theta);
303 const BoundaryExpansions & boundary_expansions)
308 double min_length = 1e9;
309 int shortest_boundary_expansion_idx = 1e9;
310 for (
unsigned int idx = 0; idx != boundary_expansions.size(); idx++) {
311 if (boundary_expansions[idx].expansion_path_length<min_length &&
312 !boundary_expansions[idx].in_collision &&
313 boundary_expansions[idx].path_end_idx>0.0 &&
314 boundary_expansions[idx].expansion_path_length > 0.0)
316 min_length = boundary_expansions[idx].expansion_path_length;
317 shortest_boundary_expansion_idx = idx;
321 return shortest_boundary_expansion_idx;
325 const geometry_msgs::msg::Pose & start,
326 const geometry_msgs::msg::Pose & end,
330 static ompl::base::ScopedState<> from(state_space_), to(state_space_), s(state_space_);
332 from[0] = start.position.x;
333 from[1] = start.position.y;
334 from[2] = tf2::getYaw(start.orientation);
335 to[0] = end.position.x;
336 to[1] = end.position.y;
337 to[2] = tf2::getYaw(end.orientation);
339 double d = state_space_->distance(from(), to());
347 if (d > 2.0 * expansion.original_path_length) {
351 std::vector<double> reals;
352 double theta(0.0), x(0.0), y(0.0);
353 double x_m = start.position.x;
354 double y_m = start.position.y;
357 for (
double i = 0; i <= expansion.path_end_idx; i++) {
358 state_space_->interpolate(from(), to(), i / expansion.path_end_idx, s());
361 theta = (reals[2] < 0.0) ? (reals[2] + 2.0 * M_PI) : reals[2];
362 theta = (theta > 2.0 * M_PI) ? (theta - 2.0 * M_PI) : theta;
369 if (
static_cast<float>(costmap->
getCost(mx, my)) >= INSCRIBED) {
370 expansion.in_collision =
true;
374 expansion.expansion_path_length += hypot(x - x_m, y - y_m);
379 expansion.pts.emplace_back(x, y, theta);
383 template<
typename IteratorT>
386 std::vector<double> distances = {
388 2.0 * min_turning_rad_,
389 M_PI * min_turning_rad_,
390 2.0 * M_PI * min_turning_rad_
393 BoundaryExpansions boundary_expansions;
394 boundary_expansions.resize(distances.size());
395 double curr_dist = 0.0;
396 double x_last = start->pose.position.x;
397 double y_last = start->pose.position.y;
398 geometry_msgs::msg::Point pt;
399 unsigned int curr_dist_idx = 0;
401 for (IteratorT iter = start; iter != end; iter++) {
402 pt = iter->pose.position;
403 curr_dist += hypot(pt.x - x_last, pt.y - y_last);
407 if (curr_dist >= distances[curr_dist_idx]) {
408 boundary_expansions[curr_dist_idx].path_end_idx = iter - start;
409 boundary_expansions[curr_dist_idx].original_path_length = curr_dist;
413 if (curr_dist_idx == boundary_expansions.size()) {
418 return boundary_expansions;
422 const geometry_msgs::msg::Pose & start_pose,
423 nav_msgs::msg::Path & path,
425 const bool & reversing_segment)
428 BoundaryExpansions boundary_expansions =
429 generateBoundaryExpansionPoints<PathIterator>(path.poses.begin(), path.poses.end());
432 for (
unsigned int i = 0; i != boundary_expansions.size(); i++) {
434 if (expansion.path_end_idx == 0.0) {
438 if (!reversing_segment) {
439 findBoundaryExpansion(
440 start_pose, path.poses[expansion.path_end_idx].pose, expansion,
443 findBoundaryExpansion(
444 path.poses[expansion.path_end_idx].pose, start_pose, expansion,
450 unsigned int best_expansion_idx = findShortestBoundaryExpansionIdx(boundary_expansions);
451 if (best_expansion_idx > boundary_expansions.size()) {
457 if (reversing_segment) {
458 std::reverse(best_expansion.pts.begin(), best_expansion.pts.end());
460 for (
unsigned int i = 0; i != best_expansion.pts.size(); i++) {
461 path.poses[i].pose.position.x = best_expansion.pts[i].x;
462 path.poses[i].pose.position.y = best_expansion.pts[i].y;
463 path.poses[i].pose.orientation = orientationAroundZAxis(best_expansion.pts[i].theta);
468 const geometry_msgs::msg::Pose & end_pose,
469 nav_msgs::msg::Path & path,
471 const bool & reversing_segment)
474 BoundaryExpansions boundary_expansions =
475 generateBoundaryExpansionPoints<ReversePathIterator>(path.poses.rbegin(), path.poses.rend());
478 unsigned int expansion_starting_idx;
479 for (
unsigned int i = 0; i != boundary_expansions.size(); i++) {
481 if (expansion.path_end_idx == 0.0) {
484 expansion_starting_idx = path.poses.size() - expansion.path_end_idx - 1;
485 if (!reversing_segment) {
486 findBoundaryExpansion(path.poses[expansion_starting_idx].pose, end_pose, expansion, costmap);
488 findBoundaryExpansion(end_pose, path.poses[expansion_starting_idx].pose, expansion, costmap);
493 unsigned int best_expansion_idx = findShortestBoundaryExpansionIdx(boundary_expansions);
494 if (best_expansion_idx > boundary_expansions.size()) {
500 if (reversing_segment) {
501 std::reverse(best_expansion.pts.begin(), best_expansion.pts.end());
503 expansion_starting_idx = path.poses.size() - best_expansion.path_end_idx - 1;
504 for (
unsigned int i = 0; i != best_expansion.pts.size(); i++) {
505 path.poses[expansion_starting_idx + i].pose.position.x = best_expansion.pts[i].x;
506 path.poses[expansion_starting_idx + i].pose.position.y = best_expansion.pts[i].y;
507 path.poses[expansion_starting_idx + i].pose.orientation = orientationAroundZAxis(
508 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 smooth(nav_msgs::msg::Path &path, const nav2_costmap_2d::Costmap2D *costmap, const double &max_time)
Smoother API method.
bool smoothImpl(nav_msgs::msg::Path &path, bool &reversing_segment, const nav2_costmap_2d::Costmap2D *costmap, const double &max_time)
Smoother method - does the smoothing on a segment.
void updateApproximatePathOrientations(nav_msgs::msg::Path &path, bool &reversing_segment)
For a given path, update the path point orientations based on smoothing.
std::vector< PathSegment > findDirectionalPathSegments(const nav_msgs::msg::Path &path)
Finds the starting and end indices of path segments where the robot is traveling in the same directio...
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...
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.
A segment of a path in start/end indices.
Parameters for the smoother cost function.