Nav2 Navigation Stack - humble  humble
ROS 2 Navigation Stack
costmap_2d_markers.cpp
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  * Steve Macenski
38  *********************************************************************/
39 #include <string>
40 #include <vector>
41 #include <memory>
42 #include <utility>
43 
44 #include "rclcpp/rclcpp.hpp"
45 #include "visualization_msgs/msg/marker.hpp"
46 #include "nav2_msgs/msg/voxel_grid.hpp"
47 #include "nav2_voxel_grid/voxel_grid.hpp"
48 #include "nav2_util/execution_timer.hpp"
49 
50 struct Cell
51 {
52  double x;
53  double y;
54  double z;
55  nav2_voxel_grid::VoxelStatus status;
56 };
57 typedef std::vector<Cell> V_Cell;
58 
59 float g_colors_r[] = {0.0f, 0.0f, 1.0f};
60 float g_colors_g[] = {0.0f, 0.0f, 0.0f};
61 float g_colors_b[] = {0.0f, 1.0f, 0.0f};
62 float g_colors_a[] = {0.0f, 0.5f, 1.0f};
63 
64 V_Cell g_cells;
65 rclcpp::Node::SharedPtr g_node;
66 rclcpp::Publisher<visualization_msgs::msg::Marker>::SharedPtr pub;
67 
68 void voxelCallback(const nav2_msgs::msg::VoxelGrid::ConstSharedPtr grid)
69 {
70  if (grid->data.empty()) {
71  RCLCPP_ERROR(g_node->get_logger(), "Received voxel grid");
72  return;
73  }
74 
76  timer.start();
77 
78  RCLCPP_DEBUG(g_node->get_logger(), "Received voxel grid");
79 
80  const std::string frame_id = grid->header.frame_id;
81  const rclcpp::Time stamp = grid->header.stamp;
82  const uint32_t * data = &grid->data.front();
83  const double x_origin = grid->origin.x;
84  const double y_origin = grid->origin.y;
85  const double z_origin = grid->origin.z;
86  const double x_res = grid->resolutions.x;
87  const double y_res = grid->resolutions.y;
88  const double z_res = grid->resolutions.z;
89  const uint32_t x_size = grid->size_x;
90  const uint32_t y_size = grid->size_y;
91  const uint32_t z_size = grid->size_z;
92 
93  g_cells.clear();
94  uint32_t num_markers = 0;
95  for (uint32_t y_grid = 0; y_grid < y_size; ++y_grid) {
96  for (uint32_t x_grid = 0; x_grid < x_size; ++x_grid) {
97  for (uint32_t z_grid = 0; z_grid < z_size; ++z_grid) {
98  nav2_voxel_grid::VoxelStatus status =
99  nav2_voxel_grid::VoxelGrid::getVoxel(
100  x_grid, y_grid,
101  z_grid, x_size, y_size, z_size, data);
102  if (status == nav2_voxel_grid::MARKED) {
103  Cell c;
104  c.status = status;
105  c.x = x_origin + (x_grid + 0.5) * x_res;
106  c.y = y_origin + (y_grid + 0.5) * y_res;
107  c.z = z_origin + (z_grid + 0.5) * z_res;
108  g_cells.push_back(c);
109 
110  ++num_markers;
111  }
112  }
113  }
114  }
115 
116  auto m = std::make_unique<visualization_msgs::msg::Marker>();
117  m->header.frame_id = frame_id;
118  m->header.stamp = stamp;
119  m->ns = g_node->get_namespace();
120  m->id = 0;
121  m->type = visualization_msgs::msg::Marker::CUBE_LIST;
122  m->action = visualization_msgs::msg::Marker::ADD;
123  m->pose.orientation.w = 1.0;
124  m->scale.x = x_res;
125  m->scale.y = y_res;
126  m->scale.z = z_res;
127  m->color.r = g_colors_r[nav2_voxel_grid::MARKED];
128  m->color.g = g_colors_g[nav2_voxel_grid::MARKED];
129  m->color.b = g_colors_b[nav2_voxel_grid::MARKED];
130  m->color.a = g_colors_a[nav2_voxel_grid::MARKED];
131  m->points.resize(num_markers);
132  for (uint32_t i = 0; i < num_markers; ++i) {
133  Cell & c = g_cells[i];
134  geometry_msgs::msg::Point & p = m->points[i];
135  p.x = c.x;
136  p.y = c.y;
137  p.z = c.z;
138  }
139 
140  pub->publish(std::move(m));
141 
142  timer.end();
143  RCLCPP_INFO(
144  g_node->get_logger(), "Published %d markers in %f seconds",
145  num_markers, timer.elapsed_time_in_seconds());
146 }
147 
148 int main(int argc, char ** argv)
149 {
150  rclcpp::init(argc, argv);
151  g_node = rclcpp::Node::make_shared("costmap_2d_marker");
152 
153  RCLCPP_DEBUG(g_node->get_logger(), "Starting costmap_2d_marker");
154 
155  pub = g_node->create_publisher<visualization_msgs::msg::Marker>(
156  "visualization_marker", 1);
157 
158  auto sub = g_node->create_subscription<nav2_msgs::msg::VoxelGrid>(
159  "voxel_grid", rclcpp::SystemDefaultsQoS(), voxelCallback);
160 
161  rclcpp::spin(g_node->get_node_base_interface());
162 }
Measures execution time of code between calls to start and end.
void start()
Call just prior to code you want to measure.
double elapsed_time_in_seconds()
Extract the measured time as a floating point number of seconds.
void end()
Call just after the code you want to measure.