ROS 创建点云数据,使用rviz显示

环境:ubuntu16.04、ros(kinetic)、pcl

新建工程

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

建主函数 chapter6_tutorials/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; 
}

修改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})

编译/刷新环境

cd ~/catkin_ws
catkin_make
source devel/setup.bash

运行roscore (新终端)

roscore

运行代码 (catkin_make目录下)

rosrun chapter6_tutorials pcl_create

打开rviz (新终端)

rviz

add一个PointCloud2

ROS 创建点云数据,使用rviz显示_第1张图片

topic 选择/pcl_output

fixed Frame输入odom

如图所示:

ROS 创建点云数据,使用rviz显示_第2张图片

OK.

 

 

你可能感兴趣的:(pcl,ros)