Nav2 Navigation Stack - jazzy  jazzy
ROS 2 Navigation Stack
critic_function.hpp
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 #ifndef NAV2_MPPI_CONTROLLER__CRITIC_FUNCTION_HPP_
16 #define NAV2_MPPI_CONTROLLER__CRITIC_FUNCTION_HPP_
17 
18 #include <string>
19 #include <memory>
20 
21 #include "rclcpp_lifecycle/lifecycle_node.hpp"
22 #include "nav2_costmap_2d/costmap_2d_ros.hpp"
23 
24 #include "nav2_mppi_controller/tools/parameters_handler.hpp"
25 #include "nav2_mppi_controller/critic_data.hpp"
26 
27 namespace mppi::critics
28 {
29 
35 {
36  float cost{0};
37  bool using_footprint{false};
38 };
39 
45 {
46 public:
50  CriticFunction() = default;
51 
55  virtual ~CriticFunction() = default;
56 
66  rclcpp_lifecycle::LifecycleNode::WeakPtr parent,
67  const std::string & parent_name,
68  const std::string & name,
69  std::shared_ptr<nav2_costmap_2d::Costmap2DROS> costmap_ros,
70  ParametersHandler * param_handler)
71  {
72  parent_ = parent;
73  logger_ = parent_.lock()->get_logger();
74  name_ = name;
75  parent_name_ = parent_name;
76  costmap_ros_ = costmap_ros;
77  costmap_ = costmap_ros_->getCostmap();
78  parameters_handler_ = param_handler;
79 
80  auto getParam = parameters_handler_->getParamGetter(name_);
81  getParam(enabled_, "enabled", true);
82 
83  initialize();
84  }
85 
90  virtual void score(CriticData & data) = 0;
91 
95  virtual void initialize() = 0;
96 
100  std::string getName()
101  {
102  return name_;
103  }
104 
105 protected:
106  bool enabled_;
107  std::string name_, parent_name_;
108  rclcpp_lifecycle::LifecycleNode::WeakPtr parent_;
109  std::shared_ptr<nav2_costmap_2d::Costmap2DROS> costmap_ros_;
110  nav2_costmap_2d::Costmap2D * costmap_{nullptr};
111 
112  ParametersHandler * parameters_handler_;
113  rclcpp::Logger logger_{rclcpp::get_logger("MPPIController")};
114 };
115 
116 } // namespace mppi::critics
117 
118 #endif // NAV2_MPPI_CONTROLLER__CRITIC_FUNCTION_HPP_
Handles getting parameters and dynamic parmaeter changes.
auto getParamGetter(const std::string &ns)
Get an object to retreive parameters.
Abstract critic objective function to score trajectories.
std::string getName()
Get name of critic.
virtual void score(CriticData &data)=0
Main function to score trajectory.
CriticFunction()=default
Constructor for mppi::critics::CriticFunction.
virtual ~CriticFunction()=default
Destructor for mppi::critics::CriticFunction.
virtual void initialize()=0
Initialize critic.
void on_configure(rclcpp_lifecycle::LifecycleNode::WeakPtr parent, const std::string &parent_name, const std::string &name, std::shared_ptr< nav2_costmap_2d::Costmap2DROS > costmap_ros, ParametersHandler *param_handler)
Configure critic on bringup.
A 2D costmap provides a mapping between points in the world and their associated "costs".
Definition: costmap_2d.hpp:68
Data to pass to critics for scoring, including state, trajectories, pruned path, global goal,...
Definition: critic_data.hpp:45
Utility for storing cost information.