15 #include "nav2_costmap_2d/plugin_container_layer.hpp"
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"
32 auto node = node_.lock();
35 throw std::runtime_error{
"Failed to lock node"};
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);
45 plugin_types_.resize(plugin_names_.size());
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]);
53 default_value_ = nav2_costmap_2d::NO_INFORMATION;
59 void PluginContainerLayer::addPlugin(std::shared_ptr<Layer> plugin, std::string layer_name)
61 plugins_.push_back(plugin);
62 auto node = node_.lock();
63 plugin->initialize(layered_costmap_, name_ +
"." + layer_name, tf_, node, callback_group_);
75 for (vector<std::shared_ptr<Layer>>::iterator plugin = plugins_.begin(); plugin != plugins_.end();
78 (*plugin)->updateBounds(robot_x, robot_y, robot_yaw, min_x, min_y, max_x, max_y);
89 std::lock_guard<Costmap2D::mutex_t> guard(*getMutex());
94 for (vector<std::shared_ptr<Layer>>::iterator plugin = plugins_.begin(); plugin != plugins_.end();
97 (*plugin)->updateCosts(*
this, min_i, min_j, max_i, max_j);
100 switch (combination_method_) {
102 updateWithOverwrite(master_grid, min_i, min_j, max_i, max_j);
105 updateWithMax(master_grid, min_i, min_j, max_i, max_j);
108 updateWithMaxWithoutUnknownOverwrite(master_grid, min_i, min_j, max_i, max_j);
119 auto node = node_.lock();
121 post_set_params_handler_ = node->add_post_set_parameters_callback(
124 this, std::placeholders::_1));
125 on_set_params_handler_ = node->add_on_set_parameters_callback(
128 this, std::placeholders::_1));
130 for (vector<std::shared_ptr<Layer>>::iterator plugin = plugins_.begin(); plugin != plugins_.end();
133 (*plugin)->activate();
139 auto node = node_.lock();
140 if (post_set_params_handler_ && node) {
141 node->remove_post_set_parameters_callback(post_set_params_handler_.get());
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());
147 on_set_params_handler_.reset();
148 for (vector<std::shared_ptr<Layer>>::iterator plugin = plugins_.begin(); plugin != plugins_.end();
151 (*plugin)->deactivate();
157 for (vector<std::shared_ptr<Layer>>::iterator plugin = plugins_.begin(); plugin != plugins_.end();
168 for (vector<std::shared_ptr<Layer>>::iterator plugin = plugins_.begin(); plugin != plugins_.end();
171 (*plugin)->onFootprintChanged();
177 std::lock_guard<Costmap2D::mutex_t> guard(*getMutex());
183 for (vector<std::shared_ptr<Layer>>::iterator plugin = plugins_.begin(); plugin != plugins_.end();
186 (*plugin)->matchSize();
192 for (vector<std::shared_ptr<Layer>>::iterator plugin = plugins_.begin(); plugin != plugins_.end();
195 if ((*plugin)->isClearable()) {
205 for (vector<std::shared_ptr<Layer>>::iterator plugin = plugins_.begin(); plugin != plugins_.end();
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);
216 const std::vector<rclcpp::Parameter> & )
218 rcl_interfaces::msg::SetParametersResult result;
219 result.successful =
true;
224 const std::vector<rclcpp::Parameter> & parameters)
226 std::lock_guard<Costmap2D::mutex_t> guard(*getMutex());
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) {
235 if (param_type == ParameterType::PARAMETER_INTEGER) {
236 if (param_name == name_ +
"." +
"combination_method") {
239 }
else if (param_type == ParameterType::PARAMETER_BOOL) {
240 if (param_name == name_ +
"." +
"enabled" && enabled_ != parameter.as_bool()) {
241 enabled_ = parameter.as_bool();
A 2D costmap provides a mapping between points in the world and their associated "costs".
void resizeMap(unsigned int size_x, unsigned int size_y, double resolution, double origin_x, double origin_y)
Resize the costmap.
double getResolution() const
Accessor for the resolution of the costmap.
unsigned int getSizeInCellsX() const
Accessor for the x size of the costmap in cells.
virtual void resetMaps()
Resets the costmap and static_map to be unknown space.
double getOriginY() const
Accessor for the y origin of the costmap.
unsigned int getSizeInCellsY() const
Accessor for the y size of the costmap in cells.
double getOriginX() const
Accessor for the x origin of the costmap.
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.
void setCurrent(bool current)
Set whether the data in the layer is up to date.
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 > ¶meters)
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 > ¶meters)
Apply parameter updates after validation This callback is executed when parameters have been successf...
void activate() override
Activate the layer.
@ MaxWithoutUnknownOverwrite