ROS 可视化: 发布PointCloud2点云数据到Rviz

Posted lvchaoshun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ROS 可视化: 发布PointCloud2点云数据到Rviz相关的知识,希望对你有一定的参考价值。

1. 相关依赖
package.xml 需要添加对 pcl_ros 包的依赖

2. CMakeLists.txt

find_package(PCL REQUIRED)
include_directories(include${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_executable(pcl_create src/pcl_create.cpp)
target_link_libraries(pcl_create ${catkin_LIBRARIES} ${PCL_LIBRARIES}) 

 2. 测试代码

#include <ros/ros.h>
#include <pcl/point_cloud.h>
#include <pcl_conversions/pcl_conversions.h>
#include <sensor_msgs/PointCloud2.h>

main (int argc, char **argv)
{
    ros::init (argc, argv, "pcl_create");

    ros::NodeHandle nh;
    ros::Publisher pcl_pub = nh.advertise<sensor_msgs::PointCloud2> ("pcl_output", 1);
    pcl::PointCloud<pcl::PointXYZ> cloud;
    sensor_msgs::PointCloud2 output;

    // Fill in the cloud data
    cloud.width  = 100;
    cloud.height = 1;
    cloud.points.resize(cloud.width * cloud.height);

    for (size_t i = 0; i < cloud.points.size (); ++i)
    {
        cloud.points[i].x = 1024 * rand () / (RAND_MAX + 1.0f);
        cloud.points[i].y = 1024 * rand () / (RAND_MAX + 1.0f);
        cloud.points[i].z = 1024 * rand () / (RAND_MAX + 1.0f);
    }

    //Convert the cloud to ROS message
    pcl::toROSMsg(cloud, output);
    output.header.frame_id = "odom";

    ros::Rate loop_rate(1);
    while (ros::ok())
    {
        pcl_pub.publish(output);
        ros::spinOnce();
        loop_rate.sleep();
    }

    return 0;
}

 


以上是关于ROS 可视化: 发布PointCloud2点云数据到Rviz的主要内容,如果未能解决你的问题,请参考以下文章

Ros发布点云文件

kitti之ros可视化_学习笔记--第3课:点云资料发布

将 ROS PointCloud2 消息转换为 PCL PointCloud 后出现奇怪的分段错误

ROS2学习笔记18-velodyne 16雷达点云在ros2中可视化案例参考

如何在ROS中使用PCL—数据格式

PCL数据类型和ROS数据类型的转换