Nav2 Navigation Stack - jazzy  jazzy
ROS 2 Navigation Stack
smoother_cost_function.hpp
1 // Copyright (c) 2021 RoboTech Vision
2 // Copyright (c) 2020, Samsung Research America
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License. Reserved.
15 
16 #ifndef NAV2_CONSTRAINED_SMOOTHER__SMOOTHER_COST_FUNCTION_HPP_
17 #define NAV2_CONSTRAINED_SMOOTHER__SMOOTHER_COST_FUNCTION_HPP_
18 
19 #include <cmath>
20 #include <vector>
21 #include <iostream>
22 #include <unordered_map>
23 #include <memory>
24 #include <queue>
25 #include <utility>
26 
27 #include "ceres/ceres.h"
28 #include "ceres/cubic_interpolation.h"
29 #include "Eigen/Core"
30 #include "nav2_costmap_2d/costmap_2d.hpp"
31 #include "nav2_constrained_smoother/options.hpp"
32 #include "nav2_constrained_smoother/utils.hpp"
33 
34 namespace nav2_constrained_smoother
35 {
36 
43 {
44 public:
56  const Eigen::Vector2d & original_pos,
57  double next_to_last_length_ratio,
58  bool reversing,
59  const nav2_costmap_2d::Costmap2D * costmap,
60  const std::shared_ptr<ceres::BiCubicInterpolator<ceres::Grid2D<u_char>>> & costmap_interpolator,
61  const SmootherParams & params,
62  double costmap_weight)
63  : original_pos_(original_pos),
64  next_to_last_length_ratio_(next_to_last_length_ratio),
65  reversing_(reversing),
66  params_(params),
67  costmap_weight_(costmap_weight),
68  costmap_origin_(costmap->getOriginX(), costmap->getOriginY()),
69  costmap_resolution_(costmap->getResolution()),
70  costmap_interpolator_(costmap_interpolator)
71  {
72  }
73 
74  ceres::CostFunction * AutoDiff()
75  {
76  return new ceres::AutoDiffCostFunction<SmootherCostFunction, 4, 2, 2, 2>(this);
77  }
78 
79  void setCostmapWeight(double costmap_weight)
80  {
81  costmap_weight_ = costmap_weight;
82  }
83 
84  double getCostmapWeight()
85  {
86  return costmap_weight_;
87  }
88 
97  template<typename T>
98  bool operator()(
99  const T * const pt, const T * const pt_next, const T * const pt_prev,
100  T * pt_residual) const
101  {
102  Eigen::Map<const Eigen::Matrix<T, 2, 1>> xi(pt);
103  Eigen::Map<const Eigen::Matrix<T, 2, 1>> xi_next(pt_next);
104  Eigen::Map<const Eigen::Matrix<T, 2, 1>> xi_prev(pt_prev);
105  Eigen::Map<Eigen::Matrix<T, 4, 1>> residual(pt_residual);
106  residual.setZero();
107 
108  // compute cost
109  addSmoothingResidual<T>(params_.smooth_weight, xi, xi_next, xi_prev, residual[0]);
110  addCurvatureResidual<T>(params_.curvature_weight, xi, xi_next, xi_prev, residual[1]);
111  addDistanceResidual<T>(
112  params_.distance_weight, xi,
113  original_pos_.template cast<T>(), residual[2]);
114  addCostResidual<T>(costmap_weight_, xi, xi_next, xi_prev, residual[3]);
115 
116  return true;
117  }
118 
119 protected:
128  template<typename T>
129  inline void addSmoothingResidual(
130  const double & weight,
131  const Eigen::Matrix<T, 2, 1> & pt,
132  const Eigen::Matrix<T, 2, 1> & pt_next,
133  const Eigen::Matrix<T, 2, 1> & pt_prev,
134  T & r) const
135  {
136  Eigen::Matrix<T, 2, 1> d_next = pt_next - pt;
137  Eigen::Matrix<T, 2, 1> d_prev = pt - pt_prev;
138  Eigen::Matrix<T, 2, 1> d_diff = next_to_last_length_ratio_ * d_next - d_prev;
139  r += (T)weight * d_diff.dot(d_diff); // objective function value
140  }
141 
151  template<typename T>
152  inline void addCurvatureResidual(
153  const double & weight,
154  const Eigen::Matrix<T, 2, 1> & pt,
155  const Eigen::Matrix<T, 2, 1> & pt_next,
156  const Eigen::Matrix<T, 2, 1> & pt_prev,
157  T & r) const
158  {
159  Eigen::Matrix<T, 2, 1> center = arcCenter(
160  pt_prev, pt, pt_next,
161  next_to_last_length_ratio_ < 0);
162  if (CERES_ISINF(center[0])) {
163  return;
164  }
165  T turning_rad = (pt - center).norm();
166  T ki_minus_kmax = (T)1.0 / turning_rad - params_.max_curvature;
167 
168  if (ki_minus_kmax <= (T)EPSILON) {
169  return;
170  }
171 
172  r += (T)weight * ki_minus_kmax * ki_minus_kmax; // objective function value
173  }
174 
182  template<typename T>
183  inline void addDistanceResidual(
184  const double & weight,
185  const Eigen::Matrix<T, 2, 1> & xi,
186  const Eigen::Matrix<T, 2, 1> & xi_original,
187  T & r) const
188  {
189  r += (T)weight * (xi - xi_original).squaredNorm(); // objective function value
190  }
191 
199  template<typename T>
200  inline void addCostResidual(
201  const double & weight,
202  const Eigen::Matrix<T, 2, 1> & pt,
203  const Eigen::Matrix<T, 2, 1> & pt_next,
204  const Eigen::Matrix<T, 2, 1> & pt_prev,
205  T & r) const
206  {
207  if (params_.cost_check_points.empty()) {
208  Eigen::Matrix<T, 2, 1> interp_pos =
209  (pt - costmap_origin_.template cast<T>()) / (T)costmap_resolution_;
210  T value;
211  costmap_interpolator_->Evaluate(interp_pos[1] - (T)0.5, interp_pos[0] - (T)0.5, &value);
212  r += (T)weight * value * value; // objective function value
213  } else {
214  Eigen::Matrix<T, 2, 1> dir = tangentDir(
215  pt_prev, pt, pt_next,
216  next_to_last_length_ratio_ < 0);
217  dir.normalize();
218  if (((pt_next - pt).dot(dir) < (T)0) != reversing_) {
219  dir = -dir;
220  }
221  Eigen::Matrix<T, 3, 3> transform;
222  transform << dir[0], -dir[1], pt[0],
223  dir[1], dir[0], pt[1],
224  (T)0, (T)0, (T)1;
225  for (size_t i = 0; i < params_.cost_check_points.size(); i += 3) {
226  Eigen::Matrix<T, 3, 1> ccpt((T)params_.cost_check_points[i],
227  (T)params_.cost_check_points[i + 1], (T)1);
228  auto ccpt_world = (transform * ccpt).template block<2, 1>(0, 0);
229  Eigen::Matrix<T, 2,
230  1> interp_pos = (ccpt_world - costmap_origin_.template cast<T>()) /
231  (T)costmap_resolution_;
232  T value;
233  costmap_interpolator_->Evaluate(interp_pos[1] - (T)0.5, interp_pos[0] - (T)0.5, &value);
234 
235  r += (T)weight * (T)params_.cost_check_points[i + 2] * value * value;
236  }
237  }
238  }
239 
240  const Eigen::Vector2d original_pos_;
241  double next_to_last_length_ratio_;
242  bool reversing_;
243  SmootherParams params_;
244  double costmap_weight_;
245  Eigen::Vector2d costmap_origin_;
246  double costmap_resolution_;
247  std::shared_ptr<ceres::BiCubicInterpolator<ceres::Grid2D<u_char>>> costmap_interpolator_;
248 };
249 
250 } // namespace nav2_constrained_smoother
251 
252 #endif // NAV2_CONSTRAINED_SMOOTHER__SMOOTHER_COST_FUNCTION_HPP_
Cost function for path smoothing with multiple terms including curvature, smoothness,...
bool operator()(const T *const pt, const T *const pt_next, const T *const pt_prev, T *pt_residual) const
Smoother cost function evaluation.
SmootherCostFunction(const Eigen::Vector2d &original_pos, double next_to_last_length_ratio, bool reversing, const nav2_costmap_2d::Costmap2D *costmap, const std::shared_ptr< ceres::BiCubicInterpolator< ceres::Grid2D< u_char >>> &costmap_interpolator, const SmootherParams &params, double costmap_weight)
A constructor for nav2_constrained_smoother::SmootherCostFunction.
void addCostResidual(const double &weight, const Eigen::Matrix< T, 2, 1 > &pt, const Eigen::Matrix< T, 2, 1 > &pt_next, const Eigen::Matrix< T, 2, 1 > &pt_prev, T &r) const
Cost function term for steering away from costs.
void addCurvatureResidual(const double &weight, const Eigen::Matrix< T, 2, 1 > &pt, const Eigen::Matrix< T, 2, 1 > &pt_next, const Eigen::Matrix< T, 2, 1 > &pt_prev, T &r) const
Cost function term for maximum curved paths.
void addDistanceResidual(const double &weight, const Eigen::Matrix< T, 2, 1 > &xi, const Eigen::Matrix< T, 2, 1 > &xi_original, T &r) const
Cost function derivative term for steering away changes in pose.
void addSmoothingResidual(const double &weight, const Eigen::Matrix< T, 2, 1 > &pt, const Eigen::Matrix< T, 2, 1 > &pt_next, const Eigen::Matrix< T, 2, 1 > &pt_prev, T &r) const
Cost function term for smooth paths.
A 2D costmap provides a mapping between points in the world and their associated "costs".
Definition: costmap_2d.hpp:68