Nav2 Navigation Stack - jazzy  jazzy
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.0f);
29  getParam(threshold_to_consider_, "threshold_to_consider", 1.4f);
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.goal))
40  {
41  return;
42  }
43 
44  const auto & goal_x = data.goal.position.x;
45  const auto & goal_y = data.goal.position.y;
46 
47  const auto traj_x = xt::view(data.trajectories.x, xt::all(), xt::all());
48  const auto traj_y = xt::view(data.trajectories.y, xt::all(), xt::all());
49 
50  if (power_ > 1u) {
51  data.costs += xt::pow(
52  xt::mean(
53  xt::hypot(traj_x - goal_x, traj_y - goal_y),
54  {1}, immediate) * weight_, power_);
55  } else {
56  data.costs += xt::mean(
57  xt::hypot(traj_x - goal_x, traj_y - goal_y),
58  {1}, immediate) * weight_;
59  }
60 }
61 
62 } // namespace mppi::critics
63 
64 #include <pluginlib/class_list_macros.hpp>
65 
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, pruned path, global goal,...
Definition: critic_data.hpp:45