Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
inflation_layer.hpp
1 // Copyright (c) 2026, Dexory (Tony Najjar)
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 
16 #ifndef NAV2_COSTMAP_2D__INFLATION_LAYER_HPP_
17 #define NAV2_COSTMAP_2D__INFLATION_LAYER_HPP_
18 
19 #include <limits>
20 #include <map>
21 #include <memory>
22 #include <mutex>
23 #include <string>
24 #include <vector>
25 #ifdef _OPENMP
26 #include <omp.h>
27 #endif
28 #include <Eigen/Core>
29 
30 #include "rclcpp/node_interfaces/node_parameters_interface.hpp"
31 #include "rclcpp/parameter.hpp"
32 #include "nav2_costmap_2d/inflation_layer_interface.hpp"
33 #include "nav2_costmap_2d/layered_costmap.hpp"
34 #include "nav2_costmap_2d/distance_transform.hpp"
35 
36 namespace nav2_costmap_2d
37 {
38 
45 {
46 public:
51 
56 
60  void onInitialize() override;
61 
65  void deactivate() override;
66 
70  void activate() override;
71 
82  void updateBounds(
83  double robot_x, double robot_y, double robot_yaw, double * min_x,
84  double * min_y,
85  double * max_x,
86  double * max_y) override;
95  void updateCosts(
96  nav2_costmap_2d::Costmap2D & master_grid,
97  int min_i, int min_j, int max_i, int max_j) override;
98 
102  void matchSize() override;
103 
107  bool isClearable() override {return false;}
108 
112  void reset() override
113  {
114  matchSize();
115  setCurrent(false);
116  need_reinflation_ = true;
117  }
118 
122  inline unsigned char computeCost(double distance) const override
123  {
124  unsigned char cost = 0;
125  if (distance == 0) {
126  cost = LETHAL_OBSTACLE;
127  } else if (distance * resolution_ <= inscribed_radius_) {
128  cost = INSCRIBED_INFLATED_OBSTACLE;
129  } else {
130  // make sure cost falls off by Euclidean distance
131  double factor =
132  exp(-1.0 * cost_scaling_factor_ * (distance * resolution_ - inscribed_radius_));
133  cost = static_cast<unsigned char>((INSCRIBED_INFLATED_OBSTACLE - 1) * factor);
134  }
135  return cost;
136  }
137 
141  mutex_t * getMutex() override
142  {
143  return access_;
144  }
145 
146  double getCostScalingFactor() override
147  {
148  return cost_scaling_factor_;
149  }
150 
151  double getInflationRadius() override
152  {
153  return inflation_radius_;
154  }
155 
156 protected:
169  void applyInflation(
170  unsigned char * master_array,
171  const MatrixXfRM & distance_map,
172  int min_i, int min_j, int max_i, int max_j,
173  int roi_min_i, int roi_min_j,
174  unsigned int size_x);
175 
179  void onFootprintChanged() override;
180 
184  unsigned int cellDistance(double world_dist)
185  {
186  return layered_costmap_->getCostmap()->cellDistance(world_dist);
187  }
188 
192  void computeCaches();
193 
198  int getOptimalThreadCount();
199 
208  rcl_interfaces::msg::SetParametersResult validateParameterUpdatesCallback(
209  const std::vector<rclcpp::Parameter> & parameters);
210 
217  void updateParametersCallback(const std::vector<rclcpp::Parameter> & parameters);
218 
223  void updateInscribedRadius();
224 
225  double inflation_radius_, inscribed_radius_, custom_inscribed_radius_, cost_scaling_factor_;
226  bool inflate_unknown_, inflate_around_unknown_;
227  unsigned int cell_inflation_radius_;
228  int num_threads_; // Number of OpenMP threads (-1 = auto)
229  double resolution_;
230 
231  // Cost LUT precision: 100 samples per cell provides smooth gradients
232  static constexpr int COST_LUT_PRECISION = 100;
233  std::vector<unsigned char> cost_lut_;
234  double last_min_x_, last_min_y_, last_max_x_, last_max_y_;
235 
236  // Indicates that the entire costmap should be reinflated next time around.
237  bool need_reinflation_;
238  mutex_t * access_;
239  // Dynamic parameters handler
240  rclcpp::node_interfaces::PostSetParametersCallbackHandle::SharedPtr post_set_params_handler_;
241  rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr on_set_params_handler_;
242 };
243 
244 } // namespace nav2_costmap_2d
245 
246 #endif // NAV2_COSTMAP_2D__INFLATION_LAYER_HPP_
A 2D costmap provides a mapping between points in the world and their associated "costs".
Definition: costmap_2d.hpp:69
unsigned int cellDistance(double world_dist)
Given distance in the world... convert it to cells.
Definition: costmap_2d.cpp:254
Abstract interface for inflation layers, providing common methods needed by navigation components reg...
Layer to convolve costmap by robot's radius or footprint using Eigen-based Felzenszwalb-Huttenlocher ...
double getCostScalingFactor() override
Get the cost scaling factor.
void computeCaches()
Generate cost lookup table for distance to cost mapping.
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.
int getOptimalThreadCount()
Determine optimal thread count based on system resources.
unsigned int cellDistance(double world_dist)
Convert world distance to cell distance.
void deactivate() override
Deactivate the layer.
void onInitialize() override
Initialization process of layer on startup.
void reset() override
Reset this costmap.
double getInflationRadius() override
Get the inflation radius.
void matchSize() override
Match the size of the master costmap.
void onFootprintChanged() override
Process updates on footprint changes to the inflation layer.
bool isClearable() override
If clearing operations should be processed on this layer or not.
unsigned char computeCost(double distance) const override
Given a distance, compute a cost.
void updateParametersCallback(const std::vector< rclcpp::Parameter > &parameters)
Apply parameter updates after validation This callback is executed when parameters have been successf...
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.
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 activate() override
Activate the layer.
void applyInflation(unsigned char *master_array, const MatrixXfRM &distance_map, int min_i, int min_j, int max_i, int max_j, int roi_min_i, int roi_min_j, unsigned int size_x)
Apply inflation costs from distance map to costmap.
void updateInscribedRadius()
Update inscribed_radius_ from custom_inscribed_radius_ or the footprint, logging a warning when the c...
mutex_t * getMutex() override
Get the mutex of the inflation information.
void setCurrent(bool current)
Set whether the data in the layer is up to date.
Definition: layer.hpp:149
Costmap2D * getCostmap()
Get the costmap pointer to the master costmap.
Eigen::Matrix< float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > MatrixXfRM
Row-major float matrix type for efficient row-wise access.