(八)ROS创建点云数据并在rviz中显示

示例选自ROS机器人程序设计(原书第2版)indigo源码
在书籍的第6章

1.新建工程

mkdir -p chapter6_tutorials/src
cd src
catkin_create_pkg chapter6_tutorials pcl_ros roscpp rospy sensor_msgs std_msgs 
cd ..
catkin_make  

2.编辑主函数pcl_create.cpp

内容如下

#include 
#include 
#include 
#include 

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

    ros::NodeHandle nh;
    ros::Publisher pcl_pub = nh.advertise ("pcl_output", 1);
    pcl::PointCloud 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;
}

3.编辑CMakeLists.txt

编辑/chapter6_tutorials/src/chapter6_tutorials路径下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})

4.编译和在rviz中显示

编译

catkin_make 

配置环境变量

source ./devel/setup.bash

运行

rosrun chapter6_tutorials pcl_create

打开rviz

rosrun rviz rviz

在rviz中增加PointCloud2d
topic 选 /pcl_output
fixed Frame 输入odom
如图
(八)ROS创建点云数据并在rviz中显示_第1张图片

你可能感兴趣的:(ROS,SLAM基础工具及算法)