在ROS中创建点云并可视化
Posted KAI-yq
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在ROS中创建点云并可视化相关的知识,希望对你有一定的参考价值。
文章目录
1.创建功能包
$ catkin_create_pkg pcl_viewer pcl_conversions pcl_ros roscpp sensor_msgs
修改package.xml
<build_depend>libpcl-all-dev</build_depend>
<exec_depend>libpcl-all</exec_depend>
修改CMakeLists.txt(系统里多个版本pcl,需单独设置)
set(PCL_DIR "/usr/local/pcl18/share/pcl-1.8")
find_package(PCL 1.8 REQUIRED)
link_directories($PCL_LIBRARY_DIRS)
add_definitions($PCL_DEFINITIONS)
include_directories(
# include
$catkin_INCLUDE_DIRS
$PCL_INCLUDE_DIRS
)
add_executable(kdtree_search_node src/kdtree_search.cpp)
target_link_libraries(kdtree_search_node $catkin_LIBRARIES $PCL_LIBRARIES )
2.代码
#include <ros/ros.h>
#include <sensor_msgs/PointCloud2.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl_ros/point_cloud.h>//若写成#include <pcl/point_cloud.h>就报错
#include <pcl/point_types.h>
#include <pcl/kdtree/kdtree_flann.h>
#include <iostream>
#include <vector>
#include <ctime>
int main (int argc, char**argv)
ros::init(argc, argv, "kdtree_search_node");
ros::NodeHandle n;
//typedef pcl::PointXYZ PointT;
//typedef pcl::PointCloud<PointT> PointCloudT;
ros::Publisher pointcloud_pub = n.advertise<sensor_msgs::PointCloud2>("pointcloud", 1);
pcl::PointCloud<pcl::PointXYZ> cloud;
sensor_msgs::PointCloud2 output;
srand (time (NULL));
cloud.width =50000;
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);
//此处若写成1024.0f,则点云不显示,原因未知
pcl::toROSMsg(cloud, output);
output.header.frame_id = "odom";
ros::Rate loop_rate(1);
while (ros::ok())
pointcloud_pub.publish(output);
ros::spinOnce();
loop_rate.sleep();
return 0;
3.rviz
改坐标系odom
add话题
4.特别注意
#include <pcl_ros/point_cloud.h>//若写成#include <pcl/point_cloud.h>就报错
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);
//此处若写成1024.0f,则点云不显示,原因未知
pcl::PointCloud<pcl::PointXYZ> cloud;
sensor_msgs::PointCloud2 output;
srand (time (NULL));
cloud.width =50000;
cloud.height =1;
cloud.points.resize (cloud.width * cloud.height);
//必须是cloud.width,不能是cloud->width,因为定义的不是指针
以上是关于在ROS中创建点云并可视化的主要内容,如果未能解决你的问题,请参考以下文章
ROS2学习笔记18-velodyne 16雷达点云在ros2中可视化案例参考
ROS 可视化: 发布PointCloud2点云数据到Rviz