Nav2 Navigation Stack - rolling  main
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 = node->declare_or_get_parameter(local_name + "w_curve", 30.0);
56  costmap_weight = node->declare_or_get_parameter(local_name + "w_cost", 0.015);
57  double cost_cusp_multiplier = node->declare_or_get_parameter(
58  local_name + "w_cost_cusp_multiplier", 3.0);
59  cusp_costmap_weight = costmap_weight * cost_cusp_multiplier;
60  cusp_zone_length = node->declare_or_get_parameter(local_name + "cusp_zone_length", 2.5);
61  distance_weight = node->declare_or_get_parameter(local_name + "w_dist", 0.0);
62  smooth_weight = node->declare_or_get_parameter(local_name + "w_smooth", 2000000.0);
63  cost_check_points = node->declare_or_get_parameter(
64  local_name + "cost_check_points", std::vector<double>());
65  if (cost_check_points.size() % 3 != 0) {
66  RCLCPP_ERROR(
67  rclcpp::get_logger(
68  "constrained_smoother"),
69  "cost_check_points parameter must contain values as follows: "
70  "[x1, y1, weight1, x2, y2, weight2, ...]");
71  throw std::runtime_error("Invalid parameter: cost_check_points");
72  }
73  // normalize check point weights so that their sum == 1.0
74  double check_point_weights_sum = 0.0;
75  for (size_t i = 2u; i < cost_check_points.size(); i += 3) {
76  check_point_weights_sum += cost_check_points[i];
77  }
78  for (size_t i = 2u; i < cost_check_points.size(); i += 3) {
79  cost_check_points[i] /= check_point_weights_sum;
80  }
81  path_downsampling_factor = node->declare_or_get_parameter(
82  local_name + "path_downsampling_factor", 1);
83  path_upsampling_factor = node->declare_or_get_parameter(
84  local_name + "path_upsampling_factor", 1);
85  reversing_enabled = node->declare_or_get_parameter(local_name + "reversing_enabled", true);
86  keep_goal_orientation = node->declare_or_get_parameter(
87  local_name + "keep_goal_orientation", true);
88  keep_start_orientation = node->declare_or_get_parameter(
89  local_name + "keep_start_orientation", true);
90  }
91 
92  double smooth_weight{0.0};
93  double costmap_weight{0.0};
94  double cusp_costmap_weight{0.0};
95  double cusp_zone_length{0.0};
96  double distance_weight{0.0};
97  double curvature_weight{0.0};
98  double max_curvature{0.0};
99  double max_time{10.0}; // adjusted by action goal, not by parameters
100  int path_downsampling_factor{1};
101  int path_upsampling_factor{1};
102  bool reversing_enabled{true};
103  bool keep_goal_orientation{true};
104  bool keep_start_orientation{true};
105  std::vector<double> cost_check_points{};
106 };
107 
113 {
115  : debug(false),
116  max_iterations(50),
117  param_tol(1e-8),
118  fn_tol(1e-6),
119  gradient_tol(1e-10)
120  {
121  }
122 
128  void get(nav2::LifecycleNode * node, const std::string & name)
129  {
130  std::string local_name = name + std::string(".optimizer.");
131 
132  // Optimizer params
133  linear_solver_type = node->declare_or_get_parameter(
134  local_name + "linear_solver_type", std::string("SPARSE_NORMAL_CHOLESKY"));
135  if (solver_types.find(linear_solver_type) == solver_types.end()) {
136  std::stringstream valid_types_str;
137  for (auto type = solver_types.begin(); type != solver_types.end(); type++) {
138  if (type != solver_types.begin()) {
139  valid_types_str << ", ";
140  }
141  valid_types_str << type->first;
142  }
143  RCLCPP_ERROR(
144  rclcpp::get_logger("constrained_smoother"),
145  "Invalid linear_solver_type. Valid values are %s", valid_types_str.str().c_str());
146  throw std::runtime_error("Invalid parameter: linear_solver_type");
147  }
148  param_tol = node->declare_or_get_parameter(local_name + "param_tol", 1e-15);
149  fn_tol = node->declare_or_get_parameter(local_name + "fn_tol", 1e-7);
150  gradient_tol = node->declare_or_get_parameter(local_name + "gradient_tol", 1e-10);
151  max_iterations = node->declare_or_get_parameter(local_name + "max_iterations", 100);
152  debug = node->declare_or_get_parameter(local_name + "debug_optimizer", false);
153  }
154 
155  const std::map<std::string, ceres::LinearSolverType> solver_types = {
156  {"DENSE_QR", ceres::DENSE_QR},
157  {"SPARSE_NORMAL_CHOLESKY", ceres::SPARSE_NORMAL_CHOLESKY}};
158 
159  bool debug;
160  std::string linear_solver_type;
161  int max_iterations; // Ceres default: 50
162 
163  double param_tol; // Ceres default: 1e-8
164  double fn_tol; // Ceres default: 1e-6
165  double gradient_tol; // Ceres default: 1e-10
166 };
167 
168 } // namespace nav2_constrained_smoother
169 
170 #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:128
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