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"
61 nav2_voxel_grid::VoxelStatus status;
63 typedef std::vector<Cell> V_Cell;
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};
71 rclcpp::Node::SharedPtr g_node;
72 rclcpp::Publisher<visualization_msgs::msg::Marker>::SharedPtr pub;
74 void voxelCallback(
const nav2_msgs::msg::VoxelGrid::ConstSharedPtr grid)
76 if (grid->data.empty()) {
77 RCLCPP_ERROR(g_node->get_logger(),
"Received voxel grid");
84 RCLCPP_DEBUG(g_node->get_logger(),
"Received voxel grid");
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;
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(
107 z_grid, x_size, y_size, z_size, data);
108 if (status == nav2_voxel_grid::MARKED) {
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);
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();
127 m->type = visualization_msgs::msg::Marker::CUBE_LIST;
128 m->action = visualization_msgs::msg::Marker::ADD;
129 m->pose.orientation.w = 1.0;
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];
146 pub->publish(std::move(m));
150 g_node->get_logger(),
"Published %d markers in %f seconds",
154 int main(
int argc,
char ** argv)
156 rclcpp::init(argc, argv);
157 g_node = rclcpp::Node::make_shared(
"costmap_2d_marker");
159 RCLCPP_DEBUG(g_node->get_logger(),
"Starting costmap_2d_marker");
161 pub = g_node->create_publisher<visualization_msgs::msg::Marker>(
164 auto sub = g_node->create_subscription<nav2_msgs::msg::VoxelGrid>(
167 rclcpp::spin(g_node->get_node_base_interface());
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.