Nav2 Navigation Stack - rolling  main
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 
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(power_, "cost_power", 1);
30  getParam(weight_, "cost_weight", 5.0f);
31  getParam(threshold_to_consider_, "threshold_to_consider", 1.4f);
32 
33  RCLCPP_INFO(
34  logger_, "GoalCritic instantiated with %d power and %f weight.",
35  power_, weight_);
36 }
37 
39 {
40  if (!enabled_) {
41  return;
42  }
43 
44  geometry_msgs::msg::Pose goal = utils::getCriticGoal(data, enforce_path_inversion_);
45 
46  if (!utils::withinPositionGoalTolerance(
47  threshold_to_consider_, data.state.pose.pose, goal))
48  {
49  return;
50  }
51 
52  auto goal_x = goal.position.x;
53  auto goal_y = goal.position.y;
54 
55  const auto delta_x = data.trajectories.x - goal_x;
56  const auto delta_y = data.trajectories.y - goal_y;
57 
58  if(power_ > 1u) {
59  data.costs += (((delta_x.square() + delta_y.square()).sqrt()).rowwise().mean() *
60  weight_).pow(power_);
61  } else {
62  data.costs += (((delta_x.square() + delta_y.square()).sqrt()).rowwise().mean() *
63  weight_).eval();
64  }
65 }
66 
67 } // namespace mppi::critics
68 
69 #include <pluginlib/class_list_macros.hpp>
70 
auto getParamGetter(const std::string &ns)
Get an object to retrieve parameters.
Abstract critic objective function to score trajectories.
void initialize() override
Initialize critic.
Definition: goal_critic.cpp:22
void score(CriticData &data) override
Evaluate cost related to goal following.
Definition: goal_critic.cpp:38
Data to pass to critics for scoring, including state, trajectories, pruned path, global goal,...
Definition: critic_data.hpp:40