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/rclcpp.hpp"
31 #include "nav2_costmap_2d/inflation_layer_interface.hpp"
32 #include "nav2_costmap_2d/layered_costmap.hpp"
33 #include "nav2_costmap_2d/distance_transform.hpp"
34 
35 namespace nav2_costmap_2d
36 {
37 
44 {
45 public:
50 
55 
59  void onInitialize() override;
60 
64  void deactivate() override;
65 
69  void activate() override;
70 
81  void updateBounds(
82  double robot_x, double robot_y, double robot_yaw, double * min_x,
83  double * min_y,
84  double * max_x,
85  double * max_y) override;
94  void updateCosts(
95  nav2_costmap_2d::Costmap2D & master_grid,
96  int min_i, int min_j, int max_i, int max_j) override;
97 
101  void matchSize() override;
102 
106  bool isClearable() override {return false;}
107 
111  void reset() override
112  {
113  matchSize();
114  setCurrent(false);
115  need_reinflation_ = true;
116  }
117 
121  inline unsigned char computeCost(double distance) const override
122  {
123  unsigned char cost = 0;
124  if (distance == 0) {
125  cost = LETHAL_OBSTACLE;
126  } else if (distance * resolution_ <= inscribed_radius_) {
127  cost = INSCRIBED_INFLATED_OBSTACLE;
128  } else {
129  // make sure cost falls off by Euclidean distance
130  double factor =
131  exp(-1.0 * cost_scaling_factor_ * (distance * resolution_ - inscribed_radius_));
132  cost = static_cast<unsigned char>((INSCRIBED_INFLATED_OBSTACLE - 1) * factor);
133  }
134  return cost;
135  }
136 
140  mutex_t * getMutex() override
141  {
142  return access_;
143  }
144 
145  double getCostScalingFactor() override
146  {
147  return cost_scaling_factor_;
148  }
149 
150  double getInflationRadius() override
151  {
152  return inflation_radius_;
153  }
154 
155 protected:
168  void applyInflation(
169  unsigned char * master_array,
170  const MatrixXfRM & distance_map,
171  int min_i, int min_j, int max_i, int max_j,
172  int roi_min_i, int roi_min_j,
173  unsigned int size_x);
174 
178  void onFootprintChanged() override;
179 
183  unsigned int cellDistance(double world_dist)
184  {
185  return layered_costmap_->getCostmap()->cellDistance(world_dist);
186  }
187 
191  void computeCaches();
192 
197  int getOptimalThreadCount();
198 
207  rcl_interfaces::msg::SetParametersResult validateParameterUpdatesCallback(
208  const std::vector<rclcpp::Parameter> & parameters);
209 
216  void updateParametersCallback(const std::vector<rclcpp::Parameter> & parameters);
217 
222  void updateInscribedRadius();
223 
224  double inflation_radius_, inscribed_radius_, custom_inscribed_radius_, cost_scaling_factor_;
225  bool inflate_unknown_, inflate_around_unknown_;
226  unsigned int cell_inflation_radius_;
227  int num_threads_; // Number of OpenMP threads (-1 = auto)
228  double resolution_;
229 
230  // Cost LUT precision: 100 samples per cell provides smooth gradients
231  static constexpr int COST_LUT_PRECISION = 100;
232  std::vector<unsigned char> cost_lut_;
233  double last_min_x_, last_min_y_, last_max_x_, last_max_y_;
234 
235  // Indicates that the entire costmap should be reinflated next time around.
236  bool need_reinflation_;
237  mutex_t * access_;
238  // Dynamic parameters handler
239  rclcpp::node_interfaces::PostSetParametersCallbackHandle::SharedPtr post_set_params_handler_;
240  rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr on_set_params_handler_;
241 };
242 
243 } // namespace nav2_costmap_2d
244 
245 #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:147
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.