Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
path_follow_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_follow_critic.hpp"
16 
17 #include <Eigen/Dense>
18 
19 namespace mppi::critics
20 {
21 
23 {
24  auto getParentParam = parameters_handler_->getParamGetter(parent_name_);
25  getParentParam(enforce_path_inversion_, "enforce_path_inversion", false);
26 
27  auto getParam = parameters_handler_->getParamGetter(name_);
28 
29  getParam(
30  threshold_to_consider_,
31  "threshold_to_consider", 1.4f);
32  getParam(offset_from_furthest_, "offset_from_furthest", 6);
33  getParam(power_, "cost_power", 1);
34  getParam(weight_, "cost_weight", 5.0f);
35 }
36 
38 {
39  if (!enabled_) {
40  return;
41  }
42 
43  geometry_msgs::msg::Pose goal = utils::getCriticGoal(data, enforce_path_inversion_);
44 
45  if (data.path.x.size() < 2 ||
46  utils::withinPositionGoalTolerance(
47  threshold_to_consider_, data.state.pose.pose, goal))
48  {
49  return;
50  }
51 
52  utils::setPathFurthestPointIfNotSet(data);
53  utils::setPathCostsIfNotSet(data, costmap_ros_);
54  const size_t path_size = data.path.x.size() - 1;
55 
56  auto offsetted_idx = std::min(
57  *data.furthest_reached_path_point + offset_from_furthest_, path_size);
58 
59  // Drive to the first valid path point, in case of dynamic obstacles on path
60  // we want to drive past it, not through it
61  bool valid = false;
62  while (!valid && offsetted_idx < path_size - 1) {
63  valid = (*data.path_pts_valid)[offsetted_idx];
64  if (!valid) {
65  offsetted_idx++;
66  }
67  }
68 
69  const auto path_x = data.path.x(offsetted_idx);
70  const auto path_y = data.path.y(offsetted_idx);
71 
72  const int && rightmost_idx = data.trajectories.x.cols() - 1;
73  const auto last_x = data.trajectories.x.col(rightmost_idx);
74  const auto last_y = data.trajectories.y.col(rightmost_idx);
75 
76  const auto delta_x = last_x - path_x;
77  const auto delta_y = last_y - path_y;
78  if (power_ > 1u) {
79  data.costs += (((delta_x.square() + delta_y.square()).sqrt()) * weight_).pow(power_);
80  } else {
81  data.costs += ((delta_x.square() + delta_y.square()).sqrt()) * weight_;
82  }
83 }
84 
85 } // namespace mppi::critics
86 
87 #include <pluginlib/class_list_macros.hpp>
88 
89 PLUGINLIB_EXPORT_CLASS(
auto getParamGetter(const std::string &ns)
Get an object to retrieve 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:40