Nav2 Navigation Stack - humble  humble
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 <xtensor/xmath.hpp>
18 #include <xtensor/xsort.hpp>
19 
20 namespace mppi::critics
21 {
22 
24 {
25  auto getParam = parameters_handler_->getParamGetter(name_);
26 
27  getParam(
28  threshold_to_consider_,
29  "threshold_to_consider", 1.4);
30  getParam(offset_from_furthest_, "offset_from_furthest", 6);
31  getParam(power_, "cost_power", 1);
32  getParam(weight_, "cost_weight", 5.0);
33 }
34 
36 {
37  if (!enabled_ || data.path.x.shape(0) < 2 ||
38  utils::withinPositionGoalTolerance(threshold_to_consider_, data.state.pose.pose, data.goal))
39  {
40  return;
41  }
42 
43  utils::setPathFurthestPointIfNotSet(data);
44  utils::setPathCostsIfNotSet(data, costmap_ros_);
45  const size_t path_size = data.path.x.shape(0) - 1;
46 
47  auto offseted_idx = std::min(
48  *data.furthest_reached_path_point + offset_from_furthest_, path_size);
49 
50  // Drive to the first valid path point, in case of dynamic obstacles on path
51  // we want to drive past it, not through it
52  bool valid = false;
53  while (!valid && offseted_idx < path_size - 1) {
54  valid = (*data.path_pts_valid)[offseted_idx];
55  if (!valid) {
56  offseted_idx++;
57  }
58  }
59 
60  const auto path_x = data.path.x(offseted_idx);
61  const auto path_y = data.path.y(offseted_idx);
62 
63  const auto last_x = xt::view(data.trajectories.x, xt::all(), -1);
64  const auto last_y = xt::view(data.trajectories.y, xt::all(), -1);
65 
66  auto dists = xt::sqrt(
67  xt::pow(last_x - path_x, 2) +
68  xt::pow(last_y - path_y, 2));
69 
70  data.costs += xt::pow(weight_ * std::move(dists), power_);
71 }
72 
73 } // namespace mppi::critics
74 
75 #include <pluginlib/class_list_macros.hpp>
76 
77 PLUGINLIB_EXPORT_CLASS(
auto getParamGetter(const std::string &ns)
Get an object to retreive 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:39