Nav2 Navigation Stack - lyrical  lyrical
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  float vx_min;
27  getParentParam(vx_min, "vx_min", -0.35);
28  if (fabs(vx_min) < 1e-6f) { // zero
29  reversing_allowed_ = false;
30  } else if (vx_min < 0.0f) { // reversing possible
31  reversing_allowed_ = true;
32  }
33 
34  auto getParam = parameters_handler_->getParamGetter(name_);
35  getParam(offset_from_furthest_, "offset_from_furthest", 4);
36  getParam(power_, "cost_power", 1);
37  getParam(weight_, "cost_weight", 2.2f);
38  getParam(
39  threshold_to_consider_,
40  "threshold_to_consider", 0.5f);
41  getParam(
42  max_angle_to_furthest_,
43  "max_angle_to_furthest", 0.785398f);
44 
45  int mode = 0;
46  getParam(mode, "mode", mode);
47  mode_ = static_cast<PathAngleMode>(mode);
48  if (!reversing_allowed_ && mode_ == PathAngleMode::NO_DIRECTIONAL_PREFERENCE) {
49  mode_ = PathAngleMode::FORWARD_PREFERENCE;
50  RCLCPP_WARN(
51  logger_,
52  "Path angle mode set to no directional preference, but controller's settings "
53  "don't allow for reversing! Setting mode to forward preference.");
54  }
55 
56  RCLCPP_INFO(
57  logger_,
58  "PathAngleCritic instantiated with %d power and %f weight. Mode set to: %s",
59  power_, weight_, modeToStr(mode_).c_str());
60 }
61 
63 {
64  if (!enabled_ || data.state.local_path_length < threshold_to_consider_) {
65  return;
66  }
67 
68  utils::setPathFurthestPointIfNotSet(data);
69  auto offsetted_idx = std::min(
70  *data.furthest_reached_path_point + offset_from_furthest_,
71  static_cast<size_t>(data.path.x.size()) - 1);
72 
73  const float goal_x = data.path.x(offsetted_idx);
74  const float goal_y = data.path.y(offsetted_idx);
75  const float goal_yaw = data.path.yaws(offsetted_idx);
76  const geometry_msgs::msg::Pose & pose = data.state.pose.pose;
77 
78  switch (mode_) {
79  case PathAngleMode::FORWARD_PREFERENCE:
80  if (utils::posePointAngle(pose, goal_x, goal_y, true) < max_angle_to_furthest_) {
81  return;
82  }
83  break;
84  case PathAngleMode::NO_DIRECTIONAL_PREFERENCE:
85  if (utils::posePointAngle(pose, goal_x, goal_y, false) < max_angle_to_furthest_) {
86  return;
87  }
88  break;
89  case PathAngleMode::CONSIDER_FEASIBLE_PATH_ORIENTATIONS:
90  if (utils::posePointAngle(pose, goal_x, goal_y, goal_yaw) < max_angle_to_furthest_) {
91  return;
92  }
93  break;
94  default:
95  throw nav2_core::ControllerException("Invalid path angle mode!");
96  }
97 
98  int last_idx = data.trajectories.y.cols() - 1;
99  auto diff_y = goal_y - data.trajectories.y.col(last_idx);
100  auto diff_x = goal_x - data.trajectories.x.col(last_idx);
101  auto yaws_between_points = diff_y.binaryExpr(
102  diff_x, [&](const float & y, const float & x) {return atan2f(y, x);}).eval();
103 
104  switch (mode_) {
105  case PathAngleMode::FORWARD_PREFERENCE:
106  {
107  auto last_yaws = data.trajectories.yaws.col(last_idx);
108  auto yaws = utils::shortest_angular_distance(
109  last_yaws, yaws_between_points).abs();
110  if (power_ > 1u) {
111  data.costs += (yaws * weight_).pow(power_);
112  } else {
113  data.costs += yaws * weight_;
114  }
115  return;
116  }
117  case PathAngleMode::NO_DIRECTIONAL_PREFERENCE:
118  {
119  auto last_yaws = data.trajectories.yaws.col(last_idx);
120  auto yaws_between_points_corrected = utils::normalize_yaws_between_points(
121  last_yaws,
122  yaws_between_points);
123  auto corrected_yaws = utils::shortest_angular_distance(
124  last_yaws, yaws_between_points_corrected).abs();
125  if (power_ > 1u) {
126  data.costs += (corrected_yaws * weight_).pow(power_);
127  } else {
128  data.costs += corrected_yaws * weight_;
129  }
130  return;
131  }
132  case PathAngleMode::CONSIDER_FEASIBLE_PATH_ORIENTATIONS:
133  {
134  auto last_yaws = data.trajectories.yaws.col(last_idx);
135  auto yaws_between_points_corrected = utils::normalize_yaws_between_points(
136  goal_yaw,
137  yaws_between_points);
138  auto corrected_yaws = utils::shortest_angular_distance(
139  last_yaws, yaws_between_points_corrected).abs();
140  if (power_ > 1u) {
141  data.costs += (corrected_yaws * weight_).pow(power_);
142  } else {
143  data.costs += corrected_yaws * weight_;
144  }
145  return;
146  }
147  }
148 }
149 
150 } // namespace mppi::critics
151 
152 #include <pluginlib/class_list_macros.hpp>
153 
154 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