17 #include "nav2_smoother/simple_smoother.hpp"
18 #include "nav2_core/smoother_exceptions.hpp"
20 namespace nav2_smoother
22 using namespace nav2_util::geometry_utils;
23 using namespace std::chrono;
26 void SimpleSmoother::configure(
27 const nav2::LifecycleNode::WeakPtr & parent,
28 std::string name, nav2::TransformBuffer::SharedPtr,
29 std::shared_ptr<nav2_costmap_2d::CostmapSubscriber> costmap_sub,
30 std::shared_ptr<nav2_costmap_2d::FootprintSubscriber>)
32 costmap_sub_ = costmap_sub;
34 auto node = parent.lock();
35 logger_ = node->get_logger();
37 tolerance_ = node->declare_or_get_parameter(
38 name +
".tolerance", 1e-10);
39 max_its_ = node->declare_or_get_parameter(
40 name +
".max_its", 1000);
41 data_w_ = node->declare_or_get_parameter(
42 name +
".w_data", 0.2);
43 smooth_w_ = node->declare_or_get_parameter(
44 name +
".w_smooth", 0.3);
45 do_refinement_ = node->declare_or_get_parameter(
46 name +
".do_refinement",
true);
47 refinement_num_ = node->declare_or_get_parameter(
48 name +
".refinement_num", 2);
49 enforce_path_inversion_ = node->declare_or_get_parameter(
50 name +
".enforce_path_inversion",
true);
54 nav_msgs::msg::Path & path,
55 const rclcpp::Duration & max_time)
57 auto costmap = costmap_sub_->getCostmap();
59 steady_clock::time_point start = steady_clock::now();
60 double time_remaining = max_time.seconds();
62 bool reversing_segment;
63 nav_msgs::msg::Path curr_path_segment;
64 curr_path_segment.header = path.header;
66 std::vector<nav2_util::PathSegment> path_segments{
PathSegment{
67 0u,
static_cast<unsigned int>(path.poses.size() - 1)}};
68 if (enforce_path_inversion_) {
69 path_segments = nav2_util::findDirectionalPathSegments(path);
72 std::lock_guard<nav2_costmap_2d::Costmap2D::mutex_t> lock(*(costmap->getMutex()));
74 for (
unsigned int i = 0; i != path_segments.size(); i++) {
75 if (path_segments[i].end - path_segments[i].start > 3) {
77 curr_path_segment.poses.clear();
79 path.poses.begin() + path_segments[i].start,
80 path.poses.begin() + path_segments[i].end + 1,
81 std::back_inserter(curr_path_segment.poses));
84 steady_clock::time_point now = steady_clock::now();
85 time_remaining = max_time.seconds() - duration_cast<duration<double>>(now - start).count();
90 smoothImpl(curr_path_segment, reversing_segment, costmap.get(), time_remaining);
94 curr_path_segment.poses.begin(),
95 curr_path_segment.poses.end(),
96 path.poses.begin() + path_segments[i].start);
104 nav_msgs::msg::Path & path,
105 bool & reversing_segment,
107 const double & max_time)
109 steady_clock::time_point a = steady_clock::now();
110 rclcpp::Duration max_dur = rclcpp::Duration::from_seconds(max_time);
113 double change = tolerance_;
114 const unsigned int & path_size = path.poses.size();
115 double x_i, y_i, y_m1, y_ip1, y_i_org;
118 nav_msgs::msg::Path new_path = path;
119 nav_msgs::msg::Path last_path = path;
121 while (change >= tolerance_) {
126 if (its >= max_its_) {
129 "Number of iterations has exceeded limit of %i.", max_its_);
131 nav2_util::updateApproximatePathOrientations(path, reversing_segment);
136 steady_clock::time_point b = steady_clock::now();
137 rclcpp::Duration timespan(duration_cast<duration<double>>(b - a));
138 if (timespan > max_dur) {
141 "Smoothing time exceeded allowed duration of %0.2f.", max_time);
143 nav2_util::updateApproximatePathOrientations(path, reversing_segment);
147 for (
unsigned int i = 1; i != path_size - 1; i++) {
148 for (
unsigned int j = 0; j != 2; j++) {
149 x_i = getFieldByDim(path.poses[i], j);
150 y_i = getFieldByDim(new_path.poses[i], j);
151 y_m1 = getFieldByDim(new_path.poses[i - 1], j);
152 y_ip1 = getFieldByDim(new_path.poses[i + 1], j);
156 y_i += data_w_ * (x_i - y_i) + smooth_w_ * (y_ip1 + y_m1 - (2.0 * y_i));
157 setFieldByDim(new_path.poses[i], j, y_i);
158 change += abs(y_i - y_i_org);
165 getFieldByDim(new_path.poses[i], 0),
166 getFieldByDim(new_path.poses[i], 1),
168 cost =
static_cast<float>(costmap->
getCost(mx, my));
171 if (cost > nav2_costmap_2d::MAX_NON_OBSTACLE && cost != nav2_costmap_2d::NO_INFORMATION) {
173 rclcpp::get_logger(
"SmacPlannerSmoother"),
174 "Smoothing process resulted in an infeasible collision. "
175 "Returning the last path before the infeasibility was introduced.");
177 nav2_util::updateApproximatePathOrientations(path, reversing_segment);
182 last_path = new_path;
187 if (do_refinement_ && refinement_ctr_ < refinement_num_) {
189 smoothImpl(new_path, reversing_segment, costmap, max_time);
192 nav2_util::updateApproximatePathOrientations(new_path, reversing_segment);
197 const geometry_msgs::msg::PoseStamped & msg,
const unsigned int & dim)
200 return msg.pose.position.x;
201 }
else if (dim == 1) {
202 return msg.pose.position.y;
204 return msg.pose.position.z;
209 geometry_msgs::msg::PoseStamped & msg,
const unsigned int dim,
210 const double & value)
213 msg.pose.position.x = value;
214 }
else if (dim == 1) {
215 msg.pose.position.y = value;
217 msg.pose.position.z = value;
223 #include "pluginlib/class_list_macros.hpp"
224 #include "nav2_ros_common/tf2_factories.hpp"
smoother interface that acts as a virtual base class for all smoother plugins
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.
A path smoother implementation.
void setFieldByDim(geometry_msgs::msg::PoseStamped &msg, const unsigned int dim, const double &value)
Set the field value for a given dimension.
bool smooth(nav_msgs::msg::Path &path, const rclcpp::Duration &max_time) override
Method to smooth given path.
void 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.
double getFieldByDim(const geometry_msgs::msg::PoseStamped &msg, const unsigned int &dim)
Get the field value for a given dimension.
A segment of a path in start/end indices.