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