Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
regulation_functions.hpp
1 // Copyright (c) 2022 Samsung Research America
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef NAV2_REGULATED_PURE_PURSUIT_CONTROLLER__REGULATION_FUNCTIONS_HPP_
16 #define NAV2_REGULATED_PURE_PURSUIT_CONTROLLER__REGULATION_FUNCTIONS_HPP_
17 
18 #include <string>
19 #include <vector>
20 #include <memory>
21 #include <algorithm>
22 #include <mutex>
23 
24 #include "nav2_costmap_2d/costmap_2d_ros.hpp"
25 #include "nav2_util/geometry_utils.hpp"
26 #include "nav2_regulated_pure_pursuit_controller/parameter_handler.hpp"
27 
28 namespace nav2_regulated_pure_pursuit_controller
29 {
30 
31 namespace heuristics
32 {
33 
41 inline double curvatureConstraint(
42  const double raw_linear_vel, const double curvature, const double min_radius)
43 {
44  const double radius = fabs(1.0 / curvature);
45  if (radius < min_radius) {
46  return raw_linear_vel * (1.0 - (fabs(radius - min_radius) / min_radius));
47  } else {
48  return raw_linear_vel;
49  }
50 }
51 
60 inline double costConstraint(
61  const double raw_linear_vel,
62  const double pose_cost,
63  std::shared_ptr<nav2_costmap_2d::Costmap2DROS> costmap_ros,
64  Parameters * params)
65 {
66  using namespace nav2_costmap_2d; // NOLINT
67 
68  if (pose_cost != static_cast<double>(NO_INFORMATION) &&
69  pose_cost != static_cast<double>(FREE_SPACE))
70  {
71  const double & inscribed_radius = costmap_ros->getLayeredCostmap()->getInscribedRadius();
72 
73  const double min_distance_to_obstacle =
74  (params->inflation_cost_scaling_factor * inscribed_radius - log(pose_cost) + log(253.0f)) /
75  params->inflation_cost_scaling_factor;
76 
77  if (min_distance_to_obstacle < params->cost_scaling_dist) {
78  return raw_linear_vel *
79  (params->cost_scaling_gain * min_distance_to_obstacle / params->cost_scaling_dist);
80  }
81  }
82 
83  return raw_linear_vel;
84 }
85 
92 inline double approachVelocityScalingFactor(
93  const nav_msgs::msg::Path & transformed_path,
94  const double approach_velocity_scaling_dist)
95 {
96  using namespace nav2_util::geometry_utils; // NOLINT
97 
98  // Waiting to apply the threshold based on integrated distance ensures we don't
99  // erroneously apply approach scaling on curvy paths that are contained in a large local costmap.
100  const double remaining_distance = calculate_path_length(transformed_path);
101  if (remaining_distance < approach_velocity_scaling_dist) {
102  auto & last = transformed_path.poses.back();
103  // Here we will use a regular euclidean distance from the robot frame (origin)
104  // to get smooth scaling, regardless of path density.
105  return std::hypot(last.pose.position.x, last.pose.position.y) / approach_velocity_scaling_dist;
106  } else {
107  return 1.0;
108  }
109 }
110 
119 inline double approachVelocityConstraint(
120  const double constrained_linear_vel,
121  const nav_msgs::msg::Path & path,
122  const double min_approach_velocity,
123  const double approach_velocity_scaling_dist)
124 {
125  double velocity_scaling = approachVelocityScalingFactor(path, approach_velocity_scaling_dist);
126  double approach_vel = constrained_linear_vel * velocity_scaling;
127 
128  if (approach_vel < min_approach_velocity) {
129  approach_vel = min_approach_velocity;
130  }
131 
132  return std::min(constrained_linear_vel, approach_vel);
133 }
134 
135 } // namespace heuristics
136 
137 } // namespace nav2_regulated_pure_pursuit_controller
138 
139 #endif // NAV2_REGULATED_PURE_PURSUIT_CONTROLLER__REGULATION_FUNCTIONS_HPP_