Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
plugin_container_layer.cpp
1 // Copyright (c) 2024 Polymath Robotics, Inc.
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_costmap_2d/plugin_container_layer.hpp"
16 
17 #include "nav2_costmap_2d/costmap_math.hpp"
18 #include "nav2_costmap_2d/footprint.hpp"
19 #include "nav2_ros_common/node_utils.hpp"
20 #include "rclcpp/parameter_events_filter.hpp"
21 #include "pluginlib/class_list_macros.hpp"
22 
24 
25 using std::vector;
26 
27 namespace nav2_costmap_2d
28 {
29 
31 {
32  auto node = node_.lock();
33 
34  if (!node) {
35  throw std::runtime_error{"Failed to lock node"};
36  }
37 
38  enabled_ = node->declare_or_get_parameter(name_ + "." + "enabled", true);
39  plugin_names_ = node->declare_or_get_parameter(
40  name_ + "." + "plugins", std::vector<std::string>{});
41  int combination_method_param = node->declare_or_get_parameter(
42  name_ + "." + "combination_method", 1);
43  combination_method_ = combination_method_from_int(combination_method_param);
44 
45  plugin_types_.resize(plugin_names_.size());
46 
47  for (unsigned int i = 0; i < plugin_names_.size(); ++i) {
48  plugin_types_[i] = nav2::get_plugin_type_param(node, name_ + "." + plugin_names_[i]);
49  std::shared_ptr<Layer> plugin = plugin_loader_.createSharedInstance(plugin_types_[i]);
50  addPlugin(plugin, plugin_names_[i]);
51  }
52 
53  default_value_ = nav2_costmap_2d::NO_INFORMATION;
54 
56  setCurrent(true);
57 }
58 
59 void PluginContainerLayer::addPlugin(std::shared_ptr<Layer> plugin, std::string layer_name)
60 {
61  plugins_.push_back(plugin);
62  auto node = node_.lock();
63  plugin->initialize(layered_costmap_, name_ + "." + layer_name, tf_, node, callback_group_);
64 }
65 
67  double robot_x,
68  double robot_y,
69  double robot_yaw,
70  double * min_x,
71  double * min_y,
72  double * max_x,
73  double * max_y)
74 {
75  for (vector<std::shared_ptr<Layer>>::iterator plugin = plugins_.begin(); plugin != plugins_.end();
76  ++plugin)
77  {
78  (*plugin)->updateBounds(robot_x, robot_y, robot_yaw, min_x, min_y, max_x, max_y);
79  }
80 }
81 
83  nav2_costmap_2d::Costmap2D & master_grid,
84  int min_i,
85  int min_j,
86  int max_i,
87  int max_j)
88 {
89  std::lock_guard<Costmap2D::mutex_t> guard(*getMutex());
90  if (!enabled_) {
91  return;
92  }
93 
94  for (vector<std::shared_ptr<Layer>>::iterator plugin = plugins_.begin(); plugin != plugins_.end();
95  ++plugin)
96  {
97  (*plugin)->updateCosts(*this, min_i, min_j, max_i, max_j);
98  }
99 
100  switch (combination_method_) {
102  updateWithOverwrite(master_grid, min_i, min_j, max_i, max_j);
103  break;
105  updateWithMax(master_grid, min_i, min_j, max_i, max_j);
106  break;
108  updateWithMaxWithoutUnknownOverwrite(master_grid, min_i, min_j, max_i, max_j);
109  break;
110  default: // Nothing
111  break;
112  }
113 
114  setCurrent(true);
115 }
116 
118 {
119  auto node = node_.lock();
120  // Add callback for dynamic parameters
121  post_set_params_handler_ = node->add_post_set_parameters_callback(
122  std::bind(
124  this, std::placeholders::_1));
125  on_set_params_handler_ = node->add_on_set_parameters_callback(
126  std::bind(
128  this, std::placeholders::_1));
129 
130  for (vector<std::shared_ptr<Layer>>::iterator plugin = plugins_.begin(); plugin != plugins_.end();
131  ++plugin)
132  {
133  (*plugin)->activate();
134  }
135 }
136 
138 {
139  auto node = node_.lock();
140  if (post_set_params_handler_ && node) {
141  node->remove_post_set_parameters_callback(post_set_params_handler_.get());
142  }
143  post_set_params_handler_.reset();
144  if (on_set_params_handler_ && node) {
145  node->remove_on_set_parameters_callback(on_set_params_handler_.get());
146  }
147  on_set_params_handler_.reset();
148  for (vector<std::shared_ptr<Layer>>::iterator plugin = plugins_.begin(); plugin != plugins_.end();
149  ++plugin)
150  {
151  (*plugin)->deactivate();
152  }
153 }
154 
156 {
157  for (vector<std::shared_ptr<Layer>>::iterator plugin = plugins_.begin(); plugin != plugins_.end();
158  ++plugin)
159  {
160  (*plugin)->reset();
161  }
162  resetMaps();
163  setCurrent(false);
164 }
165 
167 {
168  for (vector<std::shared_ptr<Layer>>::iterator plugin = plugins_.begin(); plugin != plugins_.end();
169  ++plugin)
170  {
171  (*plugin)->onFootprintChanged();
172  }
173 }
174 
176 {
177  std::lock_guard<Costmap2D::mutex_t> guard(*getMutex());
178  Costmap2D * master = layered_costmap_->getCostmap();
179  resizeMap(
180  master->getSizeInCellsX(), master->getSizeInCellsY(),
181  master->getResolution(), master->getOriginX(), master->getOriginY());
182 
183  for (vector<std::shared_ptr<Layer>>::iterator plugin = plugins_.begin(); plugin != plugins_.end();
184  ++plugin)
185  {
186  (*plugin)->matchSize();
187  }
188 }
189 
191 {
192  for (vector<std::shared_ptr<Layer>>::iterator plugin = plugins_.begin(); plugin != plugins_.end();
193  ++plugin)
194  {
195  if ((*plugin)->isClearable()) {
196  return true;
197  }
198  }
199  return false;
200 }
201 
202 void PluginContainerLayer::clearArea(int start_x, int start_y, int end_x, int end_y, bool invert)
203 {
204  CostmapLayer::clearArea(start_x, start_y, end_x, end_y, invert);
205  for (vector<std::shared_ptr<Layer>>::iterator plugin = plugins_.begin(); plugin != plugins_.end();
206  ++plugin)
207  {
208  auto costmap_layer = std::dynamic_pointer_cast<nav2_costmap_2d::CostmapLayer>(*plugin);
209  if ((*plugin)->isClearable() && costmap_layer != nullptr) {
210  costmap_layer->clearArea(start_x, start_y, end_x, end_y, invert);
211  }
212  }
213 }
214 
215 rcl_interfaces::msg::SetParametersResult PluginContainerLayer::validateParameterUpdatesCallback(
216  const std::vector<rclcpp::Parameter> & /*parameters*/)
217 {
218  rcl_interfaces::msg::SetParametersResult result;
219  result.successful = true;
220  return result;
221 }
222 
224  const std::vector<rclcpp::Parameter> & parameters)
225 {
226  std::lock_guard<Costmap2D::mutex_t> guard(*getMutex());
227 
228  for (const auto & parameter : parameters) {
229  const auto & param_type = parameter.get_type();
230  const auto & param_name = parameter.get_name();
231  if (param_name.find(name_ + ".") != 0) {
232  continue;
233  }
234 
235  if (param_type == ParameterType::PARAMETER_INTEGER) {
236  if (param_name == name_ + "." + "combination_method") {
237  combination_method_ = combination_method_from_int(parameter.as_int());
238  }
239  } else if (param_type == ParameterType::PARAMETER_BOOL) {
240  if (param_name == name_ + "." + "enabled" && enabled_ != parameter.as_bool()) {
241  enabled_ = parameter.as_bool();
242  setCurrent(false);
243  }
244  }
245  }
246 }
247 
248 } // namespace nav2_costmap_2d
A 2D costmap provides a mapping between points in the world and their associated "costs".
Definition: costmap_2d.hpp:69
void resizeMap(unsigned int size_x, unsigned int size_y, double resolution, double origin_x, double origin_y)
Resize the costmap.
Definition: costmap_2d.cpp:111
double getResolution() const
Accessor for the resolution of the costmap.
Definition: costmap_2d.cpp:578
unsigned int getSizeInCellsX() const
Accessor for the x size of the costmap in cells.
Definition: costmap_2d.cpp:548
virtual void resetMaps()
Resets the costmap and static_map to be unknown space.
Definition: costmap_2d.cpp:125
double getOriginY() const
Accessor for the y origin of the costmap.
Definition: costmap_2d.cpp:573
unsigned int getSizeInCellsY() const
Accessor for the y size of the costmap in cells.
Definition: costmap_2d.cpp:553
double getOriginX() const
Accessor for the x origin of the costmap.
Definition: costmap_2d.cpp:568
virtual void clearArea(int start_x, int start_y, int end_x, int end_y, bool invert)
Clear an are in the costmap with the given dimension if invert, then clear everything except these di...
CombinationMethod combination_method_from_int(const int value)
Converts an integer to a CombinationMethod enum and logs on failure.
Abstract class for layered costmap plugin implementations.
Definition: layer.hpp:60
void setCurrent(bool current)
Set whether the data in the layer is up to date.
Definition: layer.hpp:147
Costmap2D * getCostmap()
Get the costmap pointer to the master costmap.
Holds a list of plugins and applies them only to the specific layer.
bool isClearable() override
If clearing operations should be processed on this layer or not.
void onInitialize() override
Initialization process of layer on startup.
void onFootprintChanged() override
LayeredCostmap calls this whenever the footprint there changes (via LayeredCostmap::setFootprint())....
void reset() override
Reset this costmap.
void matchSize() override
Update the footprint to match size of the parent costmap.
void updateBounds(double robot_x, double robot_y, double robot_yaw, double *min_x, double *min_y, double *max_x, double *max_y) override
Update the bounds of the master costmap by this layer's update dimensions.
void deactivate() override
Deactivate the layer.
void clearArea(int start_x, int start_y, int end_x, int end_y, bool invert) override
Clear an area in the constituent costmaps with the given dimension if invert, then clear everything e...
rcl_interfaces::msg::SetParametersResult validateParameterUpdatesCallback(const std::vector< rclcpp::Parameter > &parameters)
Validate incoming parameter updates before applying them. This callback is triggered when one or more...
void updateCosts(nav2_costmap_2d::Costmap2D &master_grid, int min_i, int min_j, int max_i, int max_j) override
Update the costs in the master costmap in the window.
void updateParametersCallback(const std::vector< rclcpp::Parameter > &parameters)
Apply parameter updates after validation This callback is executed when parameters have been successf...
void activate() override
Activate the layer.