Nav2 Navigation Stack - humble  humble
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-6) { // zero
29  reversing_allowed_ = false;
30  } else if (vx_min < 0.0) { // 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.0);
38  getParam(
39  threshold_to_consider_,
40  "threshold_to_consider", 0.5);
41  getParam(
42  max_angle_to_furthest_,
43  "max_angle_to_furthest", 1.2);
44  getParam(
45  forward_preference_,
46  "forward_preference", true);
47 
48  if (!reversing_allowed_) {
49  forward_preference_ = true;
50  }
51 
52  RCLCPP_INFO(
53  logger_,
54  "PathAngleCritic instantiated with %d power and %f weight. Reversing %s",
55  power_, weight_, reversing_allowed_ ? "allowed." : "not allowed.");
56 }
57 
59 {
60  using xt::evaluation_strategy::immediate;
61  if (!enabled_) {
62  return;
63  }
64 
65  if (utils::withinPositionGoalTolerance(
66  threshold_to_consider_, data.state.pose.pose, data.goal))
67  {
68  return;
69  }
70 
71  utils::setPathFurthestPointIfNotSet(data);
72 
73  auto offseted_idx = std::min(
74  *data.furthest_reached_path_point + offset_from_furthest_, data.path.x.shape(0) - 1);
75 
76  const float goal_x = xt::view(data.path.x, offseted_idx);
77  const float goal_y = xt::view(data.path.y, offseted_idx);
78 
79  if (utils::posePointAngle(
80  data.state.pose.pose, goal_x, goal_y, forward_preference_) < max_angle_to_furthest_)
81  {
82  return;
83  }
84 
85  auto yaws_between_points = xt::atan2(
86  goal_y - data.trajectories.y,
87  goal_x - data.trajectories.x);
88 
89  auto yaws =
90  xt::abs(utils::shortest_angular_distance(data.trajectories.yaws, yaws_between_points));
91 
92  if (reversing_allowed_ && !forward_preference_) {
93  const auto yaws_between_points_corrected = xt::where(
94  yaws < M_PI_2, yaws_between_points, utils::normalize_angles(yaws_between_points + M_PI));
95  const auto corrected_yaws = xt::abs(
96  utils::shortest_angular_distance(data.trajectories.yaws, yaws_between_points_corrected));
97  data.costs += xt::pow(xt::mean(corrected_yaws, {1}, immediate) * weight_, power_);
98  } else {
99  data.costs += xt::pow(xt::mean(yaws, {1}, immediate) * weight_, power_);
100  }
101 }
102 
103 } // namespace mppi::critics
104 
105 #include <pluginlib/class_list_macros.hpp>
106 
107 PLUGINLIB_EXPORT_CLASS(
auto getParamGetter(const std::string &ns)
Get an object to retreive 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:39