Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
options.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__OPTIONS_HPP_
17 #define NAV2_CONSTRAINED_SMOOTHER__OPTIONS_HPP_
18 
19 #include <map>
20 #include <string>
21 #include <vector>
22 #include "nav2_ros_common/lifecycle_node.hpp"
23 #include "nav2_ros_common/node_utils.hpp"
24 #include "ceres/ceres.h"
25 
26 namespace nav2_constrained_smoother
27 {
28 
34 {
39  {
40  }
41 
47  void get(nav2::LifecycleNode * node, const std::string & name)
48  {
49  std::string local_name = name + std::string(".");
50 
51  // Smoother params
52  double minimum_turning_radius = node->declare_or_get_parameter(
53  name + ".minimum_turning_radius", 0.4);
54  max_curvature = 1.0f / minimum_turning_radius;
55  curvature_weight_sqrt = std::sqrt(node->declare_or_get_parameter(local_name + "w_curve", 30.0));
56  costmap_weight_sqrt = std::sqrt(node->declare_or_get_parameter(local_name + "w_cost", 0.015));
57  double cost_cusp_multiplier_sqrt = std::sqrt(
59  local_name + "w_cost_cusp_multiplier", 3.0));
60  cusp_costmap_weight_sqrt = costmap_weight_sqrt * cost_cusp_multiplier_sqrt;
61  cusp_zone_length = node->declare_or_get_parameter(local_name + "cusp_zone_length", 2.5);
62  distance_weight_sqrt = std::sqrt(node->declare_or_get_parameter(local_name + "w_dist", 0.0));
63  smooth_weight_sqrt = std::sqrt(
65  local_name + "w_smooth",
66  2000000.0));
67  cost_check_points = node->declare_or_get_parameter(
68  local_name + "cost_check_points", std::vector<double>());
69  if (cost_check_points.size() % 3 != 0) {
70  RCLCPP_ERROR(
71  rclcpp::get_logger(
72  "constrained_smoother"),
73  "cost_check_points parameter must contain values as follows: "
74  "[x1, y1, weight1, x2, y2, weight2, ...]");
75  throw std::runtime_error("Invalid parameter: cost_check_points");
76  }
77  // sqrt the costs to account for ceres solver's internal squaring
78  for (size_t i = 2u; i < cost_check_points.size(); i += 3) {
79  cost_check_points[i] = std::sqrt(cost_check_points[i]);
80  }
81  // normalize check point weights so that their sum == 1.0
82  double check_point_weights_sum = 0.0;
83  for (size_t i = 2u; i < cost_check_points.size(); i += 3) {
84  check_point_weights_sum += cost_check_points[i];
85  }
86  for (size_t i = 2u; i < cost_check_points.size(); i += 3) {
87  cost_check_points[i] /= check_point_weights_sum;
88  }
89  path_downsampling_factor = node->declare_or_get_parameter(
90  local_name + "path_downsampling_factor", 1);
91  path_upsampling_factor = node->declare_or_get_parameter(
92  local_name + "path_upsampling_factor", 1);
93  reversing_enabled = node->declare_or_get_parameter(local_name + "reversing_enabled", true);
94  keep_goal_orientation = node->declare_or_get_parameter(
95  local_name + "keep_goal_orientation", true);
96  keep_start_orientation = node->declare_or_get_parameter(
97  local_name + "keep_start_orientation", true);
98  }
99 
100  double smooth_weight_sqrt{0.0};
101  double costmap_weight_sqrt{0.0};
102  double cusp_costmap_weight_sqrt{0.0};
103  double cusp_zone_length{0.0};
104  double distance_weight_sqrt{0.0};
105  double curvature_weight_sqrt{0.0};
106  double max_curvature{0.0};
107  double max_time{10.0}; // adjusted by action goal, not by parameters
108  int path_downsampling_factor{1};
109  int path_upsampling_factor{1};
110  bool reversing_enabled{true};
111  bool keep_goal_orientation{true};
112  bool keep_start_orientation{true};
113  std::vector<double> cost_check_points{};
114 };
115 
121 {
123  : debug(false),
124  max_iterations(50),
125  param_tol(1e-8),
126  fn_tol(1e-6),
127  gradient_tol(1e-10)
128  {
129  }
130 
136  void get(nav2::LifecycleNode * node, const std::string & name)
137  {
138  std::string local_name = name + std::string(".optimizer.");
139 
140  // Optimizer params
141  linear_solver_type = node->declare_or_get_parameter(
142  local_name + "linear_solver_type", std::string("SPARSE_NORMAL_CHOLESKY"));
143  if (solver_types.find(linear_solver_type) == solver_types.end()) {
144  std::stringstream valid_types_str;
145  for (auto type = solver_types.begin(); type != solver_types.end(); type++) {
146  if (type != solver_types.begin()) {
147  valid_types_str << ", ";
148  }
149  valid_types_str << type->first;
150  }
151  RCLCPP_ERROR(
152  rclcpp::get_logger("constrained_smoother"),
153  "Invalid linear_solver_type. Valid values are %s", valid_types_str.str().c_str());
154  throw std::runtime_error("Invalid parameter: linear_solver_type");
155  }
156  param_tol = node->declare_or_get_parameter(local_name + "param_tol", 1e-15);
157  fn_tol = node->declare_or_get_parameter(local_name + "fn_tol", 1e-7);
158  gradient_tol = node->declare_or_get_parameter(local_name + "gradient_tol", 1e-10);
159  max_iterations = node->declare_or_get_parameter(local_name + "max_iterations", 100);
160  debug = node->declare_or_get_parameter(local_name + "debug_optimizer", false);
161  }
162 
163  const std::map<std::string, ceres::LinearSolverType> solver_types = {
164  {"DENSE_QR", ceres::DENSE_QR},
165  {"SPARSE_NORMAL_CHOLESKY", ceres::SPARSE_NORMAL_CHOLESKY}};
166 
167  bool debug;
168  std::string linear_solver_type;
169  int max_iterations; // Ceres default: 50
170 
171  double param_tol; // Ceres default: 1e-8
172  double fn_tol; // Ceres default: 1e-6
173  double gradient_tol; // Ceres default: 1e-10
174 };
175 
176 } // namespace nav2_constrained_smoother
177 
178 #endif // NAV2_CONSTRAINED_SMOOTHER__OPTIONS_HPP_
A lifecycle node wrapper to enable common Nav2 needs such as manipulating parameters.
ParameterT declare_or_get_parameter(const std::string &parameter_name, const ParameterDescriptor &parameter_descriptor=ParameterDescriptor())
Declares or gets a parameter with specified type (not value). If the parameter is already declared,...
void get(nav2::LifecycleNode *node, const std::string &name)
Get params from ROS parameter.
Definition: options.hpp:136
void get(nav2::LifecycleNode *node, const std::string &name)
Get params from ROS parameter.
Definition: options.hpp:47
SmootherParams()
A constructor for nav2_smac_planner::SmootherParams.
Definition: options.hpp:38