ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
A PointCloud2
is simply a raw memory buffer, to interpret it, you need to use a PointCloud2Iterator
which can be found here.
#include <sensor_msgs/msg/point_cloud2.hpp>
#include <sensor_msgs/point_cloud2_iterator.hpp>
void topic_callback(const sensor_msgs::msg::PointCloud2::SharedPtr msg)
{
sensor_msgs::PointCloud2Iterator<float> iter_x(*msg, "x");
sensor_msgs::PointCloud2Iterator<float> iter_y(*msg, "y");
sensor_msgs::PointCloud2Iterator<float> iter_z(*msg, "z");
for(size_t i = 0; i < msg->height * msg->width; ++i, ++iter_x, ++iter_y, ++iter_z) {
std::cout << "coords: " << *iter_x << ", " << *iter_y << ", " << *iter_z << '\n';
}
}
That being said, printing to console is a terrible way of debugging. I highly recommend you learn to use a debugger.