Nav2 Navigation Stack - humble  humble
ROS 2 Navigation Stack
goal_critic.cpp
1 // Copyright (c) 2022 Samsung Research America, @artofnothingness Alexey Budyakov
2 // Copyright (c) 2023 Open Navigation LLC
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #include "nav2_mppi_controller/critics/goal_critic.hpp"
17 
18 namespace mppi::critics
19 {
20 
21 using xt::evaluation_strategy::immediate;
22 
24 {
25  auto getParam = parameters_handler_->getParamGetter(name_);
26 
27  getParam(power_, "cost_power", 1);
28  getParam(weight_, "cost_weight", 5.0);
29  getParam(threshold_to_consider_, "threshold_to_consider", 1.4);
30 
31  RCLCPP_INFO(
32  logger_, "GoalCritic instantiated with %d power and %f weight.",
33  power_, weight_);
34 }
35 
37 {
38  if (!enabled_ || !utils::withinPositionGoalTolerance(
39  threshold_to_consider_, data.state.pose.pose, data.path))
40  {
41  return;
42  }
43 
44  const auto goal_idx = data.path.x.shape(0) - 1;
45 
46  const auto goal_x = data.path.x(goal_idx);
47  const auto goal_y = data.path.y(goal_idx);
48 
49  const auto traj_x = xt::view(data.trajectories.x, xt::all(), xt::all());
50  const auto traj_y = xt::view(data.trajectories.y, xt::all(), xt::all());
51 
52  auto dists = xt::sqrt(
53  xt::pow(traj_x - goal_x, 2) +
54  xt::pow(traj_y - goal_y, 2));
55 
56  data.costs += xt::pow(xt::mean(dists, {1}, immediate) * weight_, power_);
57 }
58 
59 } // namespace mppi::critics
60 
61 #include <pluginlib/class_list_macros.hpp>
62 
auto getParamGetter(const std::string &ns)
Get an object to retreive parameters.
Abstract critic objective function to score trajectories.
void initialize() override
Initialize critic.
Definition: goal_critic.cpp:23
void score(CriticData &data) override
Evaluate cost related to goal following.
Definition: goal_critic.cpp:36
Data to pass to critics for scoring, including state, trajectories, path, costs, and important parame...
Definition: critic_data.hpp:39