在ROS中创建点云数据发布到话题并显示

我使用的是系统是Ubuntu18.04,ROS版本Melodic。

1.创建ROS包
cd catkin_ws/src
catkin_create_pkg point_cloud_processing pcl_ros roscpp rospy sensor_msgs std_msgs 
cd ..
catkin_make 
2.添加.cpp文件
#include 
#include 
#include  
#include  
#include 
#include  
#include  
#include  
using namespace std; 

int main(int argc,char ** argv)
{
    ROS_INFO("Log In Success!!!!!!!");
	ros::init (argc, argv, "pcdp"); 
	ros::NodeHandle n; 
	ros::Publisher pcl_pub;
	pcl_pub = n.advertise<sensor_msgs::PointCloud2> ("pcdp_output", 1);    
	pcl::PointCloud<pcl::PointXYZ> testcloud; //point cloud msg  
	sensor_msgs::PointCloud2 output; //PointCloud2 msg

	testcloud.width=50000;
	testcloud.height=2;
	testcloud.points.resize(testcloud.width*testcloud.height);

	while (ros::ok())
	{
		for(size_t i=0; i<testcloud.points.size(); ++i)
		{
				testcloud.points[i].x=1024*rand()/(RAND_MAX+1.0f);
				testcloud.points[i].y=1024*rand()/(RAND_MAX+1.0f);
				testcloud.points[i].z=1024*rand()/(RAND_MAX+1.0f);
		}
		pcl::toROSMsg(testcloud,output); //point cloud msg -> ROS msg
		output.header.frame_id="pcdp";
		pcl_pub.publish(output); //publish
	}
	ros::spin();
    return 0;
}
3.编辑CMakeLists.txt文件
cmake_minimum_required(VERSION 2.8.3)
project(point_cloud_processing)


find_package(catkin REQUIRED COMPONENTS
  pcl_ros
  roscpp
  rospy
  sensor_msgs
  std_msgs
)
find_package(PCL REQUIRED) 

catkin_package(
  CATKIN_DEPENDS pcl_ros roscpp rospy sensor_msgs std_msgs
)

include_directories(
  ${catkin_INCLUDE_DIRS}
	${PCL_INCLUDE_DIRS}
)

add_executable(pcdp src/pcdp.cpp)
target_link_libraries(pcdp ${catkin_LIBRARIES} ${PCL_LIBRARIES})
3.编译、并在RViz中显示点云数据
cd catkin_ws/
catkin_make 
roscore

重新打开一个终端,输入:

rosrun point_cloud_processing pcdp

打开RViz:
1.在左边的Displays中选择Global Options -> Fixed Frame,在后面输入pcdp。
2.点击左下角的Add按钮,选择PointCloud2d,在PointCloud2 -> Topic中选择/pcdp_output。

参考资料:
ROS创建点云数据并在rviz中显示
ROS-创建、发布和显示PCL点云

你可能感兴趣的:(Ubuntu,ROS,PCL,c++)