Nav2 Navigation Stack - jazzy  jazzy
ROS 2 Navigation Stack
path_align_critic.cpp
1 // Copyright (c) 2023 Open Navigation LLC
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_critic.hpp"
16 
17 namespace mppi::critics
18 {
19 
20 using namespace xt::placeholders; // NOLINT
21 using xt::evaluation_strategy::immediate;
22 
24 {
25  auto getParam = parameters_handler_->getParamGetter(name_);
26  getParam(power_, "cost_power", 1);
27  getParam(weight_, "cost_weight", 10.0f);
28 
29  getParam(max_path_occupancy_ratio_, "max_path_occupancy_ratio", 0.07f);
30  getParam(offset_from_furthest_, "offset_from_furthest", 20);
31  getParam(trajectory_point_step_, "trajectory_point_step", 4);
32  getParam(
33  threshold_to_consider_,
34  "threshold_to_consider", 0.5f);
35  getParam(use_path_orientations_, "use_path_orientations", false);
36 
37  RCLCPP_INFO(
38  logger_,
39  "ReferenceTrajectoryCritic instantiated with %d power and %f weight",
40  power_, weight_);
41 }
42 
44 {
45  // Don't apply close to goal, let the goal critics take over
46  if (!enabled_ || utils::withinPositionGoalTolerance(
47  threshold_to_consider_, data.state.pose.pose, data.goal))
48  {
49  return;
50  }
51 
52  // Don't apply when first getting bearing w.r.t. the path
53  utils::setPathFurthestPointIfNotSet(data);
54  // Up to furthest only, closest path point is always 0 from path handler
55  const size_t path_segments_count = *data.furthest_reached_path_point;
56  float path_segments_flt = static_cast<float>(path_segments_count);
57  if (path_segments_count < 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  std::vector<bool> & path_pts_valid = *data.path_pts_valid;
64  float invalid_ctr = 0.0f;
65  for (size_t i = 0; i < path_segments_count; i++) {
66  if (!path_pts_valid[i]) {invalid_ctr += 1.0f;}
67  if (invalid_ctr / path_segments_flt > max_path_occupancy_ratio_ && invalid_ctr > 2.0f) {
68  return;
69  }
70  }
71 
72  const size_t batch_size = data.trajectories.x.shape(0);
73  auto && cost = xt::xtensor<float, 1>::from_shape({data.costs.shape(0)});
74 
75  // Find integrated distance in the path
76  std::vector<float> path_integrated_distances(path_segments_count, 0.0f);
77  std::vector<utils::Pose2D> path(path_segments_count);
78  float dx = 0.0f, dy = 0.0f;
79  for (unsigned int i = 1; i != path_segments_count; i++) {
80  auto & pose = path[i - 1];
81  pose.x = data.path.x(i - 1);
82  pose.y = data.path.y(i - 1);
83  pose.theta = data.path.yaws(i - 1);
84 
85  dx = data.path.x(i) - pose.x;
86  dy = data.path.y(i) - pose.y;
87  path_integrated_distances[i] = path_integrated_distances[i - 1] + sqrtf(dx * dx + dy * dy);
88  }
89 
90  // Finish populating the path vector
91  auto & final_pose = path[path_segments_count - 1];
92  final_pose.x = data.path.x(path_segments_count - 1);
93  final_pose.y = data.path.y(path_segments_count - 1);
94  final_pose.theta = data.path.yaws(path_segments_count - 1);
95 
96  float summed_path_dist = 0.0f, dyaw = 0.0f;
97  unsigned int num_samples = 0u;
98  unsigned int path_pt = 0u;
99  float traj_integrated_distance = 0.0f;
100 
101  // Get strided trajectory information
102  const auto T_x = xt::view(
103  data.trajectories.x, xt::all(),
104  xt::range(0, _, trajectory_point_step_));
105  const auto T_y = xt::view(
106  data.trajectories.y, xt::all(),
107  xt::range(0, _, trajectory_point_step_));
108  const auto T_yaw = xt::view(
109  data.trajectories.yaws, xt::all(),
110  xt::range(0, _, trajectory_point_step_));
111  const auto traj_sampled_size = T_x.shape(1);
112 
113  for (size_t t = 0; t < batch_size; ++t) {
114  summed_path_dist = 0.0f;
115  num_samples = 0u;
116  traj_integrated_distance = 0.0f;
117  path_pt = 0u;
118  float Tx_m1 = T_x(t, 0);
119  float Ty_m1 = T_y(t, 0);
120  for (size_t p = 1; p < traj_sampled_size; p++) {
121  const float Tx = T_x(t, p);
122  const float Ty = T_y(t, p);
123  dx = Tx - Tx_m1;
124  dy = Ty - Ty_m1;
125  Tx_m1 = Tx;
126  Ty_m1 = Ty;
127  traj_integrated_distance += sqrtf(dx * dx + dy * dy);
128  path_pt = utils::findClosestPathPt(
129  path_integrated_distances, traj_integrated_distance, path_pt);
130 
131  // The nearest path point to align to needs to be not in collision, else
132  // let the obstacle critic take over in this region due to dynamic obstacles
133  if (path_pts_valid[path_pt]) {
134  const auto & pose = path[path_pt];
135  dx = pose.x - Tx;
136  dy = pose.y - Ty;
137  num_samples++;
138  if (use_path_orientations_) {
139  dyaw = angles::shortest_angular_distance(pose.theta, T_yaw(t, p));
140  summed_path_dist += sqrtf(dx * dx + dy * dy + dyaw * dyaw);
141  } else {
142  summed_path_dist += sqrtf(dx * dx + dy * dy);
143  }
144  }
145  }
146  if (num_samples > 0u) {
147  cost[t] = summed_path_dist / static_cast<float>(num_samples);
148  } else {
149  cost[t] = 0.0f;
150  }
151  }
152 
153  if (power_ > 1u) {
154  data.costs += xt::pow(std::move(cost) * weight_, power_);
155  } else {
156  data.costs += std::move(cost) * weight_;
157  }
158 }
159 
160 } // namespace mppi::critics
161 
162 #include <pluginlib/class_list_macros.hpp>
163 
164 PLUGINLIB_EXPORT_CLASS(
Abstract critic objective function to score trajectories.
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:45