Nav2 Navigation Stack - kilted  kilted
ROS 2 Navigation Stack
path_angle_critic.cpp
1 // Copyright (c) 2022 Samsung Research America, @artofnothingness Alexey Budyakov
2 // Copyright (c) 2023 Open Navigation LLC
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.
15 
16 #include "nav2_mppi_controller/critics/path_angle_critic.hpp"
17 
18 #include <math.h>
19 
20 namespace mppi::critics
21 {
22 
24 {
25  auto getParentParam = parameters_handler_->getParamGetter(parent_name_);
26  getParentParam(enforce_path_inversion_, "enforce_path_inversion", false);
27  float vx_min;
28  getParentParam(vx_min, "vx_min", -0.35);
29  if (fabs(vx_min) < 1e-6f) { // zero
30  reversing_allowed_ = false;
31  } else if (vx_min < 0.0f) { // reversing possible
32  reversing_allowed_ = true;
33  }
34 
35  auto getParam = parameters_handler_->getParamGetter(name_);
36  getParam(offset_from_furthest_, "offset_from_furthest", 4);
37  getParam(power_, "cost_power", 1);
38  getParam(weight_, "cost_weight", 2.2f);
39  getParam(
40  threshold_to_consider_,
41  "threshold_to_consider", 0.5f);
42  getParam(
43  max_angle_to_furthest_,
44  "max_angle_to_furthest", 0.785398f);
45 
46  int mode = 0;
47  getParam(mode, "mode", mode);
48  mode_ = static_cast<PathAngleMode>(mode);
49  if (!reversing_allowed_ && mode_ == PathAngleMode::NO_DIRECTIONAL_PREFERENCE) {
50  mode_ = PathAngleMode::FORWARD_PREFERENCE;
51  RCLCPP_WARN(
52  logger_,
53  "Path angle mode set to no directional preference, but controller's settings "
54  "don't allow for reversing! Setting mode to forward preference.");
55  }
56 
57  RCLCPP_INFO(
58  logger_,
59  "PathAngleCritic instantiated with %d power and %f weight. Mode set to: %s",
60  power_, weight_, modeToStr(mode_).c_str());
61 }
62 
64 {
65  if (!enabled_) {
66  return;
67  }
68 
69  geometry_msgs::msg::Pose goal = utils::getCriticGoal(data, enforce_path_inversion_);
70 
71  if (utils::withinPositionGoalTolerance(
72  threshold_to_consider_, data.state.pose.pose, goal))
73  {
74  return;
75  }
76 
77  utils::setPathFurthestPointIfNotSet(data);
78  auto offsetted_idx = std::min(
79  *data.furthest_reached_path_point + offset_from_furthest_,
80  static_cast<size_t>(data.path.x.size()) - 1);
81 
82  const float goal_x = data.path.x(offsetted_idx);
83  const float goal_y = data.path.y(offsetted_idx);
84  const float goal_yaw = data.path.yaws(offsetted_idx);
85  const geometry_msgs::msg::Pose & pose = data.state.pose.pose;
86 
87  switch (mode_) {
88  case PathAngleMode::FORWARD_PREFERENCE:
89  if (utils::posePointAngle(pose, goal_x, goal_y, true) < max_angle_to_furthest_) {
90  return;
91  }
92  break;
93  case PathAngleMode::NO_DIRECTIONAL_PREFERENCE:
94  if (utils::posePointAngle(pose, goal_x, goal_y, false) < max_angle_to_furthest_) {
95  return;
96  }
97  break;
98  case PathAngleMode::CONSIDER_FEASIBLE_PATH_ORIENTATIONS:
99  if (utils::posePointAngle(pose, goal_x, goal_y, goal_yaw) < max_angle_to_furthest_) {
100  return;
101  }
102  break;
103  default:
104  throw nav2_core::ControllerException("Invalid path angle mode!");
105  }
106 
107  int last_idx = data.trajectories.y.cols() - 1;
108  auto diff_y = goal_y - data.trajectories.y.col(last_idx);
109  auto diff_x = goal_x - data.trajectories.x.col(last_idx);
110  auto yaws_between_points = diff_y.binaryExpr(
111  diff_x, [&](const float & y, const float & x){return atan2f(y, x);}).eval();
112 
113  switch (mode_) {
114  case PathAngleMode::FORWARD_PREFERENCE:
115  {
116  auto last_yaws = data.trajectories.yaws.col(last_idx);
117  auto yaws = utils::shortest_angular_distance(
118  last_yaws, yaws_between_points).abs();
119  if (power_ > 1u) {
120  data.costs += (yaws * weight_).pow(power_);
121  } else {
122  data.costs += yaws * weight_;
123  }
124  return;
125  }
126  case PathAngleMode::NO_DIRECTIONAL_PREFERENCE:
127  {
128  auto last_yaws = data.trajectories.yaws.col(last_idx);
129  auto yaws_between_points_corrected = utils::normalize_yaws_between_points(last_yaws,
130  yaws_between_points);
131  auto corrected_yaws = utils::shortest_angular_distance(
132  last_yaws, yaws_between_points_corrected).abs();
133  if (power_ > 1u) {
134  data.costs += (corrected_yaws * weight_).pow(power_);
135  } else {
136  data.costs += corrected_yaws * weight_;
137  }
138  return;
139  }
140  case PathAngleMode::CONSIDER_FEASIBLE_PATH_ORIENTATIONS:
141  {
142  auto last_yaws = data.trajectories.yaws.col(last_idx);
143  auto yaws_between_points_corrected = utils::normalize_yaws_between_points(goal_yaw,
144  yaws_between_points);
145  auto corrected_yaws = utils::shortest_angular_distance(
146  last_yaws, yaws_between_points_corrected).abs();
147  if (power_ > 1u) {
148  data.costs += (corrected_yaws * weight_).pow(power_);
149  } else {
150  data.costs += corrected_yaws * weight_;
151  }
152  return;
153  }
154  }
155 }
156 
157 } // namespace mppi::critics
158 
159 #include <pluginlib/class_list_macros.hpp>
160 
161 PLUGINLIB_EXPORT_CLASS(
auto getParamGetter(const std::string &ns)
Get an object to retrieve parameters.
Abstract critic objective function to score trajectories.
void initialize() override
Initialize critic.
void score(CriticData &data) override
Evaluate cost related to robot orientation at goal pose (considered only if robot near last goal in c...
Data to pass to critics for scoring, including state, trajectories, pruned path, global goal,...
Definition: critic_data.hpp:40