Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
voxel_layer.hpp
1 /*********************************************************************
2  *
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2008, 2013, Willow Garage, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of Willow Garage, Inc. nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * Author: Eitan Marder-Eppstein
36  * David V. Lu!!
37  *********************************************************************/
38 #ifndef NAV2_COSTMAP_2D__VOXEL_LAYER_HPP_
39 #define NAV2_COSTMAP_2D__VOXEL_LAYER_HPP_
40 
41 #include <vector>
42 
43 #include "rclcpp/node_interfaces/node_parameters_interface.hpp"
44 #include "rclcpp/parameter.hpp"
45 #include <nav2_costmap_2d/layer.hpp>
46 #include <nav2_costmap_2d/layered_costmap.hpp>
47 #include <nav2_costmap_2d/observation_buffer.hpp>
48 #include <nav_msgs/msg/occupancy_grid.hpp>
49 #include <nav2_msgs/msg/voxel_grid.hpp>
50 #include <sensor_msgs/msg/laser_scan.hpp>
51 #include <laser_geometry/laser_geometry.hpp>
52 #include <sensor_msgs/msg/point_cloud.hpp>
53 #include <sensor_msgs/msg/point_cloud2.hpp>
54 #include <nav2_costmap_2d/obstacle_layer.hpp>
55 #include <nav2_voxel_grid/voxel_grid.hpp>
56 
57 namespace nav2_costmap_2d
58 {
59 
64 class VoxelLayer : public ObstacleLayer
65 {
66 public:
71  : voxel_grid_(0, 0, 0)
72  {
73  costmap_ = NULL; // this is the unsigned char* member of parent class's parent class Costmap2D
74  }
75 
79  virtual ~VoxelLayer();
80 
84  virtual void onInitialize();
85 
89  virtual void deactivate();
90 
94  virtual void activate();
95 
106  virtual void updateBounds(
107  double robot_x, double robot_y, double robot_yaw, double * min_x,
108  double * min_y,
109  double * max_x,
110  double * max_y);
111 
115  void updateOrigin(double new_origin_x, double new_origin_y);
116 
121  {
122  return true;
123  }
124 
128  virtual void matchSize();
129 
133  virtual void reset();
134 
138  virtual bool isClearable() {return true;}
139 
140 protected:
144  virtual void resetMaps();
145 
149  virtual void raytraceFreespace(
150  const nav2_costmap_2d::Observation & clearing_observation,
151  double * min_x, double * min_y,
152  double * max_x,
153  double * max_y);
154 
155  bool publish_voxel_;
156  nav2::Publisher<nav2_msgs::msg::VoxelGrid>::SharedPtr voxel_pub_;
157  nav2_voxel_grid::VoxelGrid voxel_grid_;
158  double z_resolution_, origin_z_;
159  int unknown_threshold_, mark_threshold_, size_z_;
160  nav2::Publisher<sensor_msgs::msg::PointCloud2>::SharedPtr
161  clearing_endpoints_pub_;
162 
166  inline bool worldToMap3DFloat(
167  double wx, double wy, double wz, double & mx, double & my,
168  double & mz)
169  {
170  if (wx < origin_x_ || wy < origin_y_ || wz < origin_z_) {
171  return false;
172  }
173  mx = ((wx - origin_x_) / resolution_);
174  my = ((wy - origin_y_) / resolution_);
175  mz = ((wz - origin_z_) / z_resolution_);
176  if (mx < size_x_ && my < size_y_ && mz < size_z_) {
177  return true;
178  }
179 
180  return false;
181  }
182 
186  inline bool worldToMap3D(
187  double wx, double wy, double wz, unsigned int & mx, unsigned int & my,
188  unsigned int & mz)
189  {
190  if (wx < origin_x_ || wy < origin_y_ || wz < origin_z_) {
191  return false;
192  }
193 
194  mx = static_cast<unsigned int>((wx - origin_x_) / resolution_);
195  my = static_cast<unsigned int>((wy - origin_y_) / resolution_);
196  mz = static_cast<unsigned int>((wz - origin_z_) / z_resolution_);
197 
198  if (mx < size_x_ && my < size_y_ && mz < (unsigned int)size_z_) {
199  return true;
200  }
201 
202  return false;
203  }
204 
208  inline void mapToWorld3D(
209  unsigned int mx, unsigned int my, unsigned int mz, double & wx,
210  double & wy,
211  double & wz)
212  {
213  // returns the center point of the cell
214  wx = origin_x_ + (mx + 0.5) * resolution_;
215  wy = origin_y_ + (my + 0.5) * resolution_;
216  wz = origin_z_ + (mz + 0.5) * z_resolution_;
217  }
218 
222  inline double dist(double x0, double y0, double z0, double x1, double y1, double z1)
223  {
224  return sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0) + (z1 - z0) * (z1 - z0));
225  }
226 
230  double getSizeInMetersZ() const
231  {
232  return size_z_ * z_resolution_;
233  }
234 
243  rcl_interfaces::msg::SetParametersResult validateParameterUpdatesCallback(
244  const std::vector<rclcpp::Parameter> & parameters);
245 
252  void updateParametersCallback(const std::vector<rclcpp::Parameter> & parameters);
253 
254  // Dynamic parameters handler
255  rclcpp::node_interfaces::PostSetParametersCallbackHandle::SharedPtr post_set_params_handler_;
256  rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr on_set_params_handler_;
257 };
258 
259 } // namespace nav2_costmap_2d
260 
261 #endif // NAV2_COSTMAP_2D__VOXEL_LAYER_HPP_
Stores an observation in terms of a point cloud and the origin of the source.
Definition: observation.hpp:47
Takes in laser and pointcloud data to populate into 2D costmap.
Takes laser and pointcloud data to populate a 3D voxel representation of the environment.
Definition: voxel_layer.hpp:65
void mapToWorld3D(unsigned int mx, unsigned int my, unsigned int mz, double &wx, double &wy, double &wz)
Convert map coordinates into world coordinates.
virtual void onInitialize()
Initialization process of layer on startup.
Definition: voxel_layer.cpp:61
virtual void activate()
Activate the layer.
virtual ~VoxelLayer()
Voxel Layer destructor.
double getSizeInMetersZ() const
Get the height of the voxel sizes in meters.
virtual bool isClearable()
If clearing operations should be processed on this layer or not.
virtual void updateBounds(double robot_x, double robot_y, double robot_yaw, double *min_x, double *min_y, double *max_x, double *max_y)
Update the bounds of the master costmap by this layer's update dimensions.
void updateOrigin(double new_origin_x, double new_origin_y)
Update the layer's origin to a new pose, often when in a rolling costmap.
virtual void deactivate()
Deactivate the layer.
bool worldToMap3DFloat(double wx, double wy, double wz, double &mx, double &my, double &mz)
Convert world coordinates into map coordinates.
bool worldToMap3D(double wx, double wy, double wz, unsigned int &mx, unsigned int &my, unsigned int &mz)
Convert world coordinates into map coordinates.
virtual void resetMaps()
Reset internal maps.
virtual void matchSize()
Match the size of the master costmap.
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 updateParametersCallback(const std::vector< rclcpp::Parameter > &parameters)
Apply parameter updates after validation This callback is executed when parameters have been successf...
virtual void reset()
Reset this costmap.
double dist(double x0, double y0, double z0, double x1, double y1, double z1)
Find L2 norm distance in 3D.
VoxelLayer()
Voxel Layer constructor.
Definition: voxel_layer.hpp:70
virtual void raytraceFreespace(const nav2_costmap_2d::Observation &clearing_observation, double *min_x, double *min_y, double *max_x, double *max_y)
Use raycasting between 2 points to clear freespace.
bool isDiscretized()
If layer is discretely populated.