Nav2 Navigation Stack - humble  humble
ROS 2 Navigation Stack
path_align_legacy_critic.cpp
1 // Copyright (c) 2022 Samsung Research America, @artofnothingness Alexey Budyakov
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 #include "nav2_mppi_controller/critics/path_align_legacy_critic.hpp"
16 
17 #include <xtensor/xfixed.hpp>
18 #include <xtensor/xmath.hpp>
19 
20 namespace mppi::critics
21 {
22 
23 using namespace xt::placeholders; // NOLINT
24 using xt::evaluation_strategy::immediate;
25 
27 {
28  auto getParam = parameters_handler_->getParamGetter(name_);
29  getParam(power_, "cost_power", 1);
30  getParam(weight_, "cost_weight", 10.0);
31 
32  getParam(max_path_occupancy_ratio_, "max_path_occupancy_ratio", 0.07);
33  getParam(offset_from_furthest_, "offset_from_furthest", 20);
34  getParam(trajectory_point_step_, "trajectory_point_step", 4);
35  getParam(
36  threshold_to_consider_,
37  "threshold_to_consider", 0.5);
38  getParam(use_path_orientations_, "use_path_orientations", false);
39 
40  RCLCPP_INFO(
41  logger_,
42  "ReferenceTrajectoryCritic instantiated with %d power and %f weight",
43  power_, weight_);
44 }
45 
47 {
48  // Don't apply close to goal, let the goal critics take over
49  if (!enabled_ || utils::withinPositionGoalTolerance(
50  threshold_to_consider_, data.state.pose.pose, data.goal))
51  {
52  return;
53  }
54 
55  // Don't apply when first getting bearing w.r.t. the path
56  utils::setPathFurthestPointIfNotSet(data);
57  if (*data.furthest_reached_path_point < offset_from_furthest_) {
58  return;
59  }
60 
61  // Don't apply when dynamic obstacles are blocking significant proportions of the local path
62  utils::setPathCostsIfNotSet(data, costmap_ros_);
63  const size_t closest_initial_path_point = utils::findPathTrajectoryInitialPoint(data);
64  unsigned int invalid_ctr = 0;
65  const float range = *data.furthest_reached_path_point - closest_initial_path_point;
66  for (size_t i = closest_initial_path_point; i < *data.furthest_reached_path_point; i++) {
67  if (!(*data.path_pts_valid)[i]) {invalid_ctr++;}
68  if (static_cast<float>(invalid_ctr) / range > max_path_occupancy_ratio_ && invalid_ctr > 2) {
69  return;
70  }
71  }
72 
73  const auto & T_x = data.trajectories.x;
74  const auto & T_y = data.trajectories.y;
75  const auto & T_yaw = data.trajectories.yaws;
76 
77  const auto P_x = xt::view(data.path.x, xt::range(_, -1)); // path points
78  const auto P_y = xt::view(data.path.y, xt::range(_, -1)); // path points
79  const auto P_yaw = xt::view(data.path.yaws, xt::range(_, -1)); // path points
80 
81  const size_t batch_size = T_x.shape(0);
82  const size_t time_steps = T_x.shape(1);
83  const size_t traj_pts_eval = floor(time_steps / trajectory_point_step_);
84  const size_t path_segments_count = data.path.x.shape(0) - 1;
85  auto && cost = xt::xtensor<float, 1>::from_shape({data.costs.shape(0)});
86 
87  if (path_segments_count < 1) {
88  return;
89  }
90 
91  float dist_sq = 0.0f, dx = 0.0f, dy = 0.0f, dyaw = 0.0f, summed_dist = 0.0f;
92  float min_dist_sq = std::numeric_limits<float>::max();
93  size_t min_s = 0;
94 
95  for (size_t t = 0; t < batch_size; ++t) {
96  summed_dist = 0.0f;
97  for (size_t p = trajectory_point_step_; p < time_steps; p += trajectory_point_step_) {
98  min_dist_sq = std::numeric_limits<float>::max();
99  min_s = 0;
100 
101  // Find closest path segment to the trajectory point
102  for (size_t s = 0; s < path_segments_count - 1; s++) {
103  xt::xtensor_fixed<float, xt::xshape<2>> P;
104  dx = P_x(s) - T_x(t, p);
105  dy = P_y(s) - T_y(t, p);
106  if (use_path_orientations_) {
107  dyaw = angles::shortest_angular_distance(P_yaw(s), T_yaw(t, p));
108  dist_sq = dx * dx + dy * dy + dyaw * dyaw;
109  } else {
110  dist_sq = dx * dx + dy * dy;
111  }
112  if (dist_sq < min_dist_sq) {
113  min_dist_sq = dist_sq;
114  min_s = s;
115  }
116  }
117 
118  // The nearest path point to align to needs to be not in collision, else
119  // let the obstacle critic take over in this region due to dynamic obstacles
120  if (min_s != 0 && (*data.path_pts_valid)[min_s]) {
121  summed_dist += sqrtf(min_dist_sq);
122  }
123  }
124 
125  cost[t] = summed_dist / traj_pts_eval;
126  }
127 
128  data.costs += xt::pow(std::move(cost) * weight_, power_);
129 }
130 
131 } // namespace mppi::critics
132 
133 #include <pluginlib/class_list_macros.hpp>
134 
135 PLUGINLIB_EXPORT_CLASS(
Abstract critic objective function to score trajectories.
Critic objective function for aligning to the path. Note: High settings of this will follow the path ...
void score(CriticData &data) override
Evaluate cost related to trajectories path alignment.
void initialize() override
Initialize critic.
Data to pass to critics for scoring, including state, trajectories, pruned path, global goal,...
Definition: critic_data.hpp:39