Nav2 Navigation Stack - jazzy  jazzy
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 "rclcpp/rclcpp.hpp"
25 #include "nav2_costmap_2d/costmap_2d_ros.hpp"
26 #include "nav2_util/geometry_utils.hpp"
27 #include "nav2_regulated_pure_pursuit_controller/parameter_handler.hpp"
28 
29 namespace nav2_regulated_pure_pursuit_controller
30 {
31 
32 namespace heuristics
33 {
34 
42 inline double curvatureConstraint(
43  const double raw_linear_vel, const double curvature, const double min_radius)
44 {
45  const double radius = fabs(1.0 / curvature);
46  if (radius < min_radius) {
47  return raw_linear_vel * (1.0 - (fabs(radius - min_radius) / min_radius));
48  } else {
49  return raw_linear_vel;
50  }
51 }
52 
61 inline double costConstraint(
62  const double raw_linear_vel,
63  const double pose_cost,
64  std::shared_ptr<nav2_costmap_2d::Costmap2DROS> costmap_ros,
65  Parameters * params)
66 {
67  using namespace nav2_costmap_2d; // NOLINT
68 
69  if (pose_cost != static_cast<double>(NO_INFORMATION) &&
70  pose_cost != static_cast<double>(FREE_SPACE))
71  {
72  const double & inscribed_radius = costmap_ros->getLayeredCostmap()->getInscribedRadius();
73 
74  const double min_distance_to_obstacle =
75  (params->inflation_cost_scaling_factor * inscribed_radius - log(pose_cost) + log(253.0f)) /
76  params->inflation_cost_scaling_factor;
77 
78  if (min_distance_to_obstacle < params->cost_scaling_dist) {
79  return raw_linear_vel *
80  (params->cost_scaling_gain * min_distance_to_obstacle / params->cost_scaling_dist);
81  }
82  }
83 
84  return raw_linear_vel;
85 }
86 
93 inline double approachVelocityScalingFactor(
94  const nav_msgs::msg::Path & transformed_path,
95  const double approach_velocity_scaling_dist)
96 {
97  using namespace nav2_util::geometry_utils; // NOLINT
98 
99  // Waiting to apply the threshold based on integrated distance ensures we don't
100  // erroneously apply approach scaling on curvy paths that are contained in a large local costmap.
101  const double remaining_distance = calculate_path_length(transformed_path);
102  if (remaining_distance < approach_velocity_scaling_dist) {
103  auto & last = transformed_path.poses.back();
104  // Here we will use a regular euclidean distance from the robot frame (origin)
105  // to get smooth scaling, regardless of path density.
106  return std::hypot(last.pose.position.x, last.pose.position.y) / approach_velocity_scaling_dist;
107  } else {
108  return 1.0;
109  }
110 }
111 
120 inline double approachVelocityConstraint(
121  const double constrained_linear_vel,
122  const nav_msgs::msg::Path & path,
123  const double min_approach_velocity,
124  const double approach_velocity_scaling_dist)
125 {
126  double velocity_scaling = approachVelocityScalingFactor(path, approach_velocity_scaling_dist);
127  double approach_vel = constrained_linear_vel * velocity_scaling;
128 
129  if (approach_vel < min_approach_velocity) {
130  approach_vel = min_approach_velocity;
131  }
132 
133  return std::min(constrained_linear_vel, approach_vel);
134 }
135 
136 } // namespace heuristics
137 
138 } // namespace nav2_regulated_pure_pursuit_controller
139 
140 #endif // NAV2_REGULATED_PURE_PURSUIT_CONTROLLER__REGULATION_FUNCTIONS_HPP_