Nav2 Navigation Stack - kilted  kilted
ROS 2 Navigation Stack
critic_manager.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/critic_manager.hpp"
16 
17 namespace mppi
18 {
19 
21  rclcpp_lifecycle::LifecycleNode::WeakPtr parent, const std::string & name,
22  std::shared_ptr<nav2_costmap_2d::Costmap2DROS> costmap_ros, ParametersHandler * param_handler)
23 {
24  parent_ = parent;
25  costmap_ros_ = costmap_ros;
26  name_ = name;
27  auto node = parent_.lock();
28  logger_ = node->get_logger();
29  parameters_handler_ = param_handler;
30 
31  getParams();
32  loadCritics();
33 }
34 
36 {
37  auto node = parent_.lock();
38  auto getParam = parameters_handler_->getParamGetter(name_);
39  getParam(critic_names_, "critics", std::vector<std::string>{}, ParameterType::Static);
40 }
41 
43 {
44  if (!loader_) {
45  loader_ = std::make_unique<pluginlib::ClassLoader<critics::CriticFunction>>(
46  "nav2_mppi_controller", "mppi::critics::CriticFunction");
47  }
48 
49  critics_.clear();
50  for (auto name : critic_names_) {
51  std::string fullname = getFullName(name);
52  auto instance = std::unique_ptr<critics::CriticFunction>(
53  loader_->createUnmanagedInstance(fullname));
54  critics_.push_back(std::move(instance));
55  critics_.back()->on_configure(
56  parent_, name_, name_ + "." + name, costmap_ros_,
57  parameters_handler_);
58  RCLCPP_INFO(logger_, "Critic loaded : %s", fullname.c_str());
59  }
60 }
61 
62 std::string CriticManager::getFullName(const std::string & name)
63 {
64  return "mppi::critics::" + name;
65 }
66 
68  CriticData & data) const
69 {
70  for (const auto & critic : critics_) {
71  if (data.fail_flag) {
72  break;
73  }
74  critic->score(data);
75  }
76 }
77 
78 } // namespace mppi
void on_configure(rclcpp_lifecycle::LifecycleNode::WeakPtr parent, const std::string &name, std::shared_ptr< nav2_costmap_2d::Costmap2DROS >, ParametersHandler *)
Configure critic manager on bringup and load plugins.
void getParams()
Get parameters (critics to load)
void evalTrajectoriesScores(CriticData &data) const
Score trajectories by the set of loaded critic functions.
virtual void loadCritics()
Load the critic plugins.
std::string getFullName(const std::string &name)
Get full-name namespaced critic IDs.
Handles getting parameters and dynamic parameter changes.
auto getParamGetter(const std::string &ns)
Get an object to retrieve parameters.
Data to pass to critics for scoring, including state, trajectories, pruned path, global goal,...
Definition: critic_data.hpp:40