Skip to content

Commit aeccbe7

Browse files
authored
Replaced sensor_msgs/PointCloud to sensor_msgs/PointCloud2 (#2263)
* converted PointCloud -> PointCloud2 adn checked linting. WIP Signed-off-by: Sachin <[email protected]> * modified base_obstacke_test Signed-off-by: Sachin <[email protected]> * Changed to PointCloudModifier WIP * formating fix Signed-off-by: Sachin <[email protected]> * formating fix2 Signed-off-by: Sachin <[email protected]> * added tests for clearing_endpoints Signed-off-by: Sachin <[email protected]> * removing print statements Signed-off-by: Sachin <[email protected]> * added subscribers for voxel marked and cost_cloud Signed-off-by: Sachin <[email protected]> * modified test cases to use single wait for all pointcloud2 subscribers Signed-off-by: Sachin <[email protected]> * removed unnecessary comments and publish_cost_grid_pc from yaml file Signed-off-by: Sachin <[email protected]> * Fix ament_flake issues Signed-off-by: Sachin <[email protected]>
1 parent ed6a8de commit aeccbe7

16 files changed

Lines changed: 301 additions & 142 deletions

File tree

nav2_bringup/bringup/params/nav2_params.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ controller_server:
152152
RotateToGoal.scale: 32.0
153153
RotateToGoal.slowing_factor: 5.0
154154
RotateToGoal.lookahead_time: -1.0
155-
155+
156156
controller_server_rclcpp_node:
157157
ros__parameters:
158158
use_sim_time: True

nav2_bringup/bringup/rviz/nav2_default_view.rviz

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ Visualization Manager:
293293
Value: true
294294
Axis: Z
295295
Channel Name: intensity
296-
Class: rviz_default_plugins/PointCloud
296+
Class: rviz_default_plugins/PointCloud2
297297
Color: 125; 125; 125
298298
Color Transformer: FlatColor
299299
Decay Time: 0
@@ -407,7 +407,7 @@ Visualization Manager:
407407
Value: true
408408
Axis: Z
409409
Channel Name: intensity
410-
Class: rviz_default_plugins/PointCloud
410+
Class: rviz_default_plugins/PointCloud2
411411
Color: 255; 255; 255
412412
Color Transformer: RGB8
413413
Decay Time: 0

nav2_costmap_2d/include/nav2_costmap_2d/voxel_layer.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ class VoxelLayer : public ObstacleLayer
9999
nav2_voxel_grid::VoxelGrid voxel_grid_;
100100
double z_resolution_, origin_z_;
101101
int unknown_threshold_, mark_threshold_, size_z_;
102-
rclcpp::Publisher<sensor_msgs::msg::PointCloud>::SharedPtr clearing_endpoints_pub_;
102+
rclcpp_lifecycle::LifecyclePublisher<sensor_msgs::msg::PointCloud2>::SharedPtr
103+
clearing_endpoints_pub_;
103104

104105
inline bool worldToMap3DFloat(
105106
double wx, double wy, double wz, double & mx, double & my,

nav2_costmap_2d/plugins/voxel_layer.cpp

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,9 @@ void VoxelLayer::onInitialize()
9696
voxel_pub_->on_activate();
9797
}
9898

99-
clearing_endpoints_pub_ = node->create_publisher<sensor_msgs::msg::PointCloud>(
99+
clearing_endpoints_pub_ = node->create_publisher<sensor_msgs::msg::PointCloud2>(
100100
"clearing_endpoints", custom_qos);
101+
clearing_endpoints_pub_->on_activate();
101102

102103
unknown_threshold_ += (VOXEL_BITS - size_z_);
103104
matchSize();
@@ -302,11 +303,9 @@ void VoxelLayer::raytraceFreespace(
302303
double * max_x,
303304
double * max_y)
304305
{
305-
auto clearing_endpoints_ = std::make_unique<sensor_msgs::msg::PointCloud>();
306+
auto clearing_endpoints_ = std::make_unique<sensor_msgs::msg::PointCloud2>();
306307

307-
size_t clearing_observation_cloud_size = clearing_observation.cloud_->height *
308-
clearing_observation.cloud_->width;
309-
if (clearing_observation_cloud_size == 0) {
308+
if (clearing_observation.cloud_->height == 0 || clearing_observation.cloud_->width == 0) {
310309
return;
311310
}
312311

@@ -334,10 +333,23 @@ void VoxelLayer::raytraceFreespace(
334333
}
335334

336335
if (publish_clearing_points) {
337-
clearing_endpoints_->points.clear();
338-
clearing_endpoints_->points.reserve(clearing_observation_cloud_size);
336+
clearing_endpoints_->data.clear();
337+
clearing_endpoints_->width = clearing_observation.cloud_->width;
338+
clearing_endpoints_->height = clearing_observation.cloud_->height;
339+
clearing_endpoints_->is_dense = true;
340+
clearing_endpoints_->is_bigendian = false;
339341
}
340342

343+
sensor_msgs::PointCloud2Modifier modifier(*clearing_endpoints_);
344+
modifier.setPointCloud2Fields(
345+
3, "x", 1, sensor_msgs::msg::PointField::FLOAT32,
346+
"y", 1, sensor_msgs::msg::PointField::FLOAT32,
347+
"z", 1, sensor_msgs::msg::PointField::FLOAT32);
348+
349+
sensor_msgs::PointCloud2Iterator<float> clearing_endpoints_iter_x(*clearing_endpoints_, "x");
350+
sensor_msgs::PointCloud2Iterator<float> clearing_endpoints_iter_y(*clearing_endpoints_, "y");
351+
sensor_msgs::PointCloud2Iterator<float> clearing_endpoints_iter_z(*clearing_endpoints_, "z");
352+
341353
// we can pre-compute the enpoints of the map outside of the inner loop... we'll need these later
342354
double map_end_x = origin_x_ + getSizeInMetersX();
343355
double map_end_y = origin_y_ + getSizeInMetersY();
@@ -414,11 +426,13 @@ void VoxelLayer::raytraceFreespace(
414426
max_y);
415427

416428
if (publish_clearing_points) {
417-
geometry_msgs::msg::Point32 point;
418-
point.x = wpx;
419-
point.y = wpy;
420-
point.z = wpz;
421-
clearing_endpoints_->points.push_back(point);
429+
*clearing_endpoints_iter_x = wpx;
430+
*clearing_endpoints_iter_y = wpy;
431+
*clearing_endpoints_iter_z = wpz;
432+
433+
++clearing_endpoints_iter_x;
434+
++clearing_endpoints_iter_y;
435+
++clearing_endpoints_iter_z;
422436
}
423437
}
424438
}

nav2_costmap_2d/src/costmap_2d_cloud.cpp

Lines changed: 69 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
#include <utility>
3232

3333
#include "rclcpp/rclcpp.hpp"
34-
#include "sensor_msgs/msg/point_cloud.hpp"
35-
#include "sensor_msgs/msg/channel_float32.hpp"
34+
#include "sensor_msgs/msg/point_cloud2.hpp"
35+
#include "sensor_msgs/point_cloud2_iterator.hpp"
3636
#include "nav2_voxel_grid/voxel_grid.hpp"
3737
#include "nav2_msgs/msg/voxel_grid.hpp"
3838
#include "nav2_util/execution_timer.hpp"
@@ -70,8 +70,63 @@ V_Cell g_unknown;
7070

7171
rclcpp::Node::SharedPtr g_node;
7272

73-
rclcpp::Publisher<sensor_msgs::msg::PointCloud>::SharedPtr pub_marked;
74-
rclcpp::Publisher<sensor_msgs::msg::PointCloud>::SharedPtr pub_unknown;
73+
rclcpp::Publisher<sensor_msgs::msg::PointCloud2>::SharedPtr pub_marked;
74+
rclcpp::Publisher<sensor_msgs::msg::PointCloud2>::SharedPtr pub_unknown;
75+
76+
/**
77+
* @brief An helper function to fill pointcloud2 of both the marked and unknown points from voxel_grid
78+
* @param cloud PointCloud2 Ptr which needs to be filled
79+
* @param num_channels Represents the total number of points that are going to be filled
80+
* @param header Carries the header information that needs to be assigned to PointCloud2 header
81+
* @param g_cells contains the x, y, z values that needs to be added to the PointCloud2
82+
*/
83+
void pointCloud2Helper(
84+
std::unique_ptr<sensor_msgs::msg::PointCloud2> & cloud,
85+
uint32_t num_channels,
86+
std_msgs::msg::Header header,
87+
V_Cell & g_cells)
88+
{
89+
cloud->header = header;
90+
cloud->width = num_channels;
91+
cloud->height = 1;
92+
cloud->is_dense = true;
93+
cloud->is_bigendian = false;
94+
sensor_msgs::PointCloud2Modifier modifier(*cloud);
95+
96+
modifier.setPointCloud2Fields(
97+
6, "x", 1, sensor_msgs::msg::PointField::FLOAT32,
98+
"y", 1, sensor_msgs::msg::PointField::FLOAT32,
99+
"z", 1, sensor_msgs::msg::PointField::FLOAT32,
100+
"r", 1, sensor_msgs::msg::PointField::UINT8,
101+
"g", 1, sensor_msgs::msg::PointField::UINT8,
102+
"b", 1, sensor_msgs::msg::PointField::UINT8);
103+
104+
sensor_msgs::PointCloud2Iterator<float> iter_x(*cloud, "x");
105+
sensor_msgs::PointCloud2Iterator<float> iter_y(*cloud, "y");
106+
sensor_msgs::PointCloud2Iterator<float> iter_z(*cloud, "z");
107+
108+
sensor_msgs::PointCloud2Iterator<uint8_t> iter_r(*cloud, "r");
109+
sensor_msgs::PointCloud2Iterator<uint8_t> iter_g(*cloud, "g");
110+
sensor_msgs::PointCloud2Iterator<uint8_t> iter_b(*cloud, "b");
111+
112+
for (uint32_t i = 0; i < num_channels; ++i) {
113+
Cell & c = g_cells[i];
114+
// assigning value to the point cloud2's iterator
115+
*iter_x = c.x;
116+
*iter_y = c.y;
117+
*iter_z = c.z;
118+
*iter_r = g_colors_r[c.status] * 255.0;
119+
*iter_g = g_colors_g[c.status] * 255.0;
120+
*iter_b = g_colors_b[c.status] * 255.0;
121+
122+
++iter_x;
123+
++iter_y;
124+
++iter_z;
125+
++iter_r;
126+
++iter_g;
127+
++iter_b;
128+
}
129+
}
75130

76131
void voxelCallback(const nav2_msgs::msg::VoxelGrid::ConstSharedPtr grid)
77132
{
@@ -133,65 +188,19 @@ void voxelCallback(const nav2_msgs::msg::VoxelGrid::ConstSharedPtr grid)
133188
}
134189
}
135190

136-
{
137-
auto cloud = std::make_unique<sensor_msgs::msg::PointCloud>();
138-
cloud->points.resize(num_marked);
139-
cloud->channels.resize(1);
140-
cloud->channels[0].values.resize(num_marked);
141-
cloud->channels[0].name = "rgb";
142-
cloud->header.frame_id = frame_id;
143-
cloud->header.stamp = stamp;
144-
145-
sensor_msgs::msg::ChannelFloat32 & chan = cloud->channels[0];
146-
for (uint32_t i = 0; i < num_marked; ++i) {
147-
geometry_msgs::msg::Point32 & p = cloud->points[i];
148-
float & cval = chan.values[i];
149-
Cell & c = g_marked[i];
150-
151-
p.x = c.x;
152-
p.y = c.y;
153-
p.z = c.z;
154-
155-
uint32_t r = g_colors_r[c.status] * 255.0;
156-
uint32_t g = g_colors_g[c.status] * 255.0;
157-
uint32_t b = g_colors_b[c.status] * 255.0;
158-
// uint32_t a = g_colors_a[c.status] * 255.0;
159-
160-
uint32_t col = (r << 16) | (g << 8) | b;
161-
memcpy(&cval, &col, sizeof col);
162-
}
191+
std_msgs::msg::Header pcl_header;
192+
pcl_header.frame_id = frame_id;
193+
pcl_header.stamp = stamp;
163194

195+
{
196+
auto cloud = std::make_unique<sensor_msgs::msg::PointCloud2>();
197+
pointCloud2Helper(cloud, num_marked, pcl_header, g_marked);
164198
pub_marked->publish(std::move(cloud));
165199
}
166200

167201
{
168-
auto cloud = std::make_unique<sensor_msgs::msg::PointCloud>();
169-
cloud->points.resize(num_unknown);
170-
cloud->channels.resize(1);
171-
cloud->channels[0].values.resize(num_unknown);
172-
cloud->channels[0].name = "rgb";
173-
cloud->header.frame_id = frame_id;
174-
cloud->header.stamp = stamp;
175-
176-
sensor_msgs::msg::ChannelFloat32 & chan = cloud->channels[0];
177-
for (uint32_t i = 0; i < num_unknown; ++i) {
178-
geometry_msgs::msg::Point32 & p = cloud->points[i];
179-
float & cval = chan.values[i];
180-
Cell & c = g_unknown[i];
181-
182-
p.x = c.x;
183-
p.y = c.y;
184-
p.z = c.z;
185-
186-
uint32_t r = g_colors_r[c.status] * 255.0;
187-
uint32_t g = g_colors_g[c.status] * 255.0;
188-
uint32_t b = g_colors_b[c.status] * 255.0;
189-
// uint32_t a = g_colors_a[c.status] * 255.0;
190-
191-
uint32_t col = (r << 16) | (g << 8) | b;
192-
memcpy(&cval, &col, sizeof col);
193-
}
194-
202+
auto cloud = std::make_unique<sensor_msgs::msg::PointCloud2>();
203+
pointCloud2Helper(cloud, num_unknown, pcl_header, g_unknown);
195204
pub_unknown->publish(std::move(cloud));
196205
}
197206

@@ -208,9 +217,9 @@ int main(int argc, char ** argv)
208217

209218
RCLCPP_DEBUG(g_node->get_logger(), "Starting up costmap_2d_cloud");
210219

211-
pub_marked = g_node->create_publisher<sensor_msgs::msg::PointCloud>(
220+
pub_marked = g_node->create_publisher<sensor_msgs::msg::PointCloud2>(
212221
"voxel_marked_cloud", 1);
213-
pub_unknown = g_node->create_publisher<sensor_msgs::msg::PointCloud>(
222+
pub_unknown = g_node->create_publisher<sensor_msgs::msg::PointCloud2>(
214223
"voxel_unknown_cloud", 1);
215224
auto sub = g_node->create_subscription<nav2_msgs::msg::VoxelGrid>(
216225
"voxel_grid", rclcpp::SystemDefaultsQoS(), voxelCallback);

nav2_dwb_controller/dwb_core/include/dwb_core/publisher.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class DWBPublisher
124124
std::shared_ptr<LifecyclePublisher<nav_msgs::msg::Path>> transformed_pub_;
125125
std::shared_ptr<LifecyclePublisher<nav_msgs::msg::Path>> local_pub_;
126126
std::shared_ptr<LifecyclePublisher<visualization_msgs::msg::MarkerArray>> marker_pub_;
127-
std::shared_ptr<LifecyclePublisher<sensor_msgs::msg::PointCloud>> cost_grid_pc_pub_;
127+
std::shared_ptr<LifecyclePublisher<sensor_msgs::msg::PointCloud2>> cost_grid_pc_pub_;
128128

129129
rclcpp_lifecycle::LifecycleNode::WeakPtr node_;
130130
rclcpp::Clock::SharedPtr clock_;

nav2_dwb_controller/dwb_core/include/dwb_core/trajectory_critic.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include <string>
3939
#include <vector>
4040
#include <memory>
41+
#include <utility>
4142

4243
#include "rclcpp/rclcpp.hpp"
4344
#include "nav2_costmap_2d/costmap_2d_ros.hpp"
@@ -166,7 +167,7 @@ class TrajectoryCritic
166167
*
167168
* @param pc PointCloud to add channels to
168169
*/
169-
virtual void addCriticVisualization(sensor_msgs::msg::PointCloud &) {}
170+
virtual void addCriticVisualization(std::vector<std::pair<std::string, std::vector<float>>> &) {}
170171

171172
std::string getName()
172173
{

0 commit comments

Comments
 (0)