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