Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
parameters_handler.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/tools/parameters_handler.hpp"
16 
17 namespace mppi
18 {
19 
21  const nav2::LifecycleNode::WeakPtr & parent, std::string & name)
22 {
23  node_ = parent;
24  auto node = node_.lock();
25  node_name_ = node->get_name();
26  logger_ = node->get_logger();
27  name_ = name;
28 }
29 
31 {
32  auto node = node_.lock();
33  if (on_set_param_handler_ && node) {
34  node->remove_on_set_parameters_callback(on_set_param_handler_.get());
35  }
36  on_set_param_handler_.reset();
37 }
38 
40 {
41  auto node = node_.lock();
42 
43  auto get_param = getParamGetter(node_name_);
44  get_param(verbose_, "verbose", false);
45 
46  on_set_param_handler_ = node->add_on_set_parameters_callback(
47  std::bind(
49  std::placeholders::_1));
50 }
51 
52 rcl_interfaces::msg::SetParametersResult
54  std::vector<rclcpp::Parameter> parameters)
55 {
56  rcl_interfaces::msg::SetParametersResult result;
57  result.successful = true;
58  result.reason = "";
59 
60  std::lock_guard<std::mutex> lock(parameters_change_mutex_);
61  std::vector<rclcpp::Parameter> plugin_params;
62  for (auto & param : parameters) {
63  const std::string & param_name = param.get_name();
64  if (param_name.find(name_ + ".") != 0) {
65  continue;
66  }
67  plugin_params.push_back(param);
68  }
69 
70  if (!plugin_params.empty()) {
71  for (auto & pre_cb : pre_callbacks_) {
72  pre_cb();
73  }
74 
75  for (auto & param : plugin_params) {
76  const std::string & param_name = param.get_name();
77  if (auto callback = get_param_callbacks_.find(param_name);
78  callback != get_param_callbacks_.end())
79  {
80  callback->second(param, result);
81  }
82  }
83 
84  for (auto & post_cb : post_callbacks_) {
85  post_cb();
86  }
87  }
88 
89  if (!result.successful) {
90  RCLCPP_ERROR(logger_, result.reason.c_str());
91  }
92  return result;
93 }
94 
95 } // namespace mppi
rcl_interfaces::msg::SetParametersResult dynamicParamsCallback(std::vector< rclcpp::Parameter > parameters)
Dynamic parameter callback.
auto getParamGetter(const std::string &ns)
Get an object to retrieve parameters.
void start()
Starts processing dynamic parameter changes.
ParametersHandler()=default
Constructor for mppi::ParametersHandler.
~ParametersHandler()
Destructor for mppi::ParametersHandler.