Nav2 Navigation Stack - rolling  main
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>>(data.trajectories.x.data(),
102  strided_traj_rows, strided_traj_cols, Eigen::Stride<-1, -1>(outer_stride, 1));
103  const auto T_y = Eigen::Map<const Eigen::ArrayXXf, 0,
104  Eigen::Stride<-1, -1>>(data.trajectories.y.data(),
105  strided_traj_rows, strided_traj_cols, Eigen::Stride<-1, -1>(outer_stride, 1));
106  const auto T_yaw = Eigen::Map<const Eigen::ArrayXXf, 0,
107  Eigen::Stride<-1, -1>>(data.trajectories.yaws.data(), strided_traj_rows, strided_traj_cols,
108  Eigen::Stride<-1, -1>(outer_stride, 1));
109  const auto traj_sampled_size = T_x.cols();
110 
111  for (size_t t = 0; t < batch_size; ++t) {
112  summed_path_dist = 0.0f;
113  num_samples = 0u;
114  traj_integrated_distance = 0.0f;
115  path_pt = 0u;
116  float Tx_m1 = T_x(t, 0);
117  float Ty_m1 = T_y(t, 0);
118  for (int p = 1; p < traj_sampled_size; p++) {
119  const float Tx = T_x(t, p);
120  const float Ty = T_y(t, p);
121  dx = Tx - Tx_m1;
122  dy = Ty - Ty_m1;
123  Tx_m1 = Tx;
124  Ty_m1 = Ty;
125  traj_integrated_distance += sqrtf(dx * dx + dy * dy);
126  path_pt = utils::findClosestPathPt(
127  path_integrated_distances, traj_integrated_distance, path_pt);
128 
129  // The nearest path point to align to needs to be not in collision, else
130  // let the obstacle critic take over in this region due to dynamic obstacles
131  if (path_pts_valid[path_pt]) {
132  const auto & pose = path[path_pt];
133  dx = pose.x - Tx;
134  dy = pose.y - Ty;
135  num_samples++;
136  if (use_path_orientations_) {
137  dyaw = angles::shortest_angular_distance(pose.theta, T_yaw(t, p));
138  summed_path_dist += sqrtf(dx * dx + dy * dy + dyaw * dyaw);
139  } else {
140  summed_path_dist += sqrtf(dx * dx + dy * dy);
141  }
142  }
143  }
144  if (num_samples > 0u) {
145  cost(t) = summed_path_dist / static_cast<float>(num_samples);
146  } else {
147  cost(t) = 0.0f;
148  }
149  }
150 
151  if (power_ > 1u) {
152  data.costs += (cost * weight_).pow(power_).eval();
153  } else {
154  data.costs += (cost * weight_).eval();
155  }
156 }
157 
158 } // namespace mppi::critics
159 
160 #include <pluginlib/class_list_macros.hpp>
161 
162 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