ROS中使用rviz读取pcd点云文件

本文档详细介绍了如何在ROS环境下创建功能包,处理cmakelists.txt和package.xml,以便读取和在rviz中显示pcd点云文件。首先创建名为pcd_to_rviz的功能包,接着修改cmakelists.txt和package.xml添加必要依赖。然后编译功能包,并通过source更新环境变量。在rviz中配置点云显示,设置Fixed Frame为odom1,Topic为pcl_output1。在遇到错误如pcl_conversions缺失和libvtkproj4-6.2.so.6.2.0链接问题时,提供了解决方案,包括安装缺失库和重新建立链接。


本质就是在工作空间下编译一个功能包实现功能。

创建功能包

打开之前创建好的工作空间的src目录下,创建功能包package,命名为pcd_to_rviz。

cd catkin_ws/src;
catkin_create_pkg pcd_to_rviz pcd_conversions pcl_ros roscpp sensor_msgs;

此时就会在catkin_ws/src下生成功能包文件夹pcd_to_rviz,在里面的src目录下新建一个pcd_to_rviz.cpp,将下面代码复制进去。
此外功能包文件夹下还包含cmakelists.txt和package.xml文件。

//pcd_to_rviz.cpp
#include<ros/ros.h>
#include<pcl/point_cloud.h>
#include<pcl_conversions/pcl_conversions.h>
#include<sensor_msgs/PointCloud2.h>
#include<pcl/io/pcd_io.h>
//which contains the required definitions to load and store point clouds to PCD and other file formats.
 
main (int argc, char **argv)
{
  ros::init (argc, argv, "UandBdetect");
  ros::NodeHandle nh;
  ros::Publisher pcl_pub = nh.advertise<sensor_msgs::PointCloud2> ("pcl_output1", 1);
  pcl::PointCloud<pcl::PointXYZ> cloud;
  sensor_msgs::PointCloud2 output;
  pcl::io::loadPCDFile ("/home/***/catkin_ws/src/pcd_to_rviz/src/test.pcd", cloud);//更换为自己的pcd文件路径
  //Convert the cloud to ROS message
  pcl::toROSMsg(cloud, output);
  output.header.frame_id = "odom1";//this has been done in order to be able to visualize our PointCloud2 message on the RViz visualizer,这里时fix frame的名字
  ros::Rate loop_rate(1);
  while (ros::ok())
  {
    pcl_pub.publish(output);
    ros::spinOnce();
    loop_rate.sleep();
  }
  return 0;
}

修改cmakelists.txt和package.xml

在pcd_to_rviz功能包里修改cmakelists.txt
在其中的代码后添加以下两行:

add_executable(pcd_to_rviz src/pcd_to_rviz.cpp)
target_link_libraries(pcd_to_rviz ${catkin_LIBRARIES})

在pcd_to_rviz功能包里修改package.xml
在其中的代码后添加以下两行:

<build_depend>libpcl-all-dev</build_depend>
<exec_depend>libpcl-all</exec_depend>

编译功能包

编译指定功能包

catkin_make -DCATKIN_WHITELIST_PACKAGES="pcd_to_rviz"//只编译该功能包

catkin_make -DCATKIN_WHITELIST_PACKAGES=""//单独编译一个包之后,想要再编译所有的包,需使用该命令

全编译

catkin_make //编译整个工作空间

编译成功的界面如下:
在这里插入图片描述
再source一下环境变量

source devel/setup.bash

Rviz查看pcd

运行ROS,新建一个终端:

roscoe

在终端中切换到可执行文件pcd_to_rviz所在目录,执行:

./pcd_to_rviz

此时打开 rviz, add 点云对应的topic——/pcl_output1

rosrun rviz rviz

rviz打开后,手动加载PointCloud2
Global Options中的Fixed Frame改为odom1
PointCloud2中的Topic改为pcl_output1
即可看到pcd文件中的点云

在这里插入图片描述

报错信息error

1. pcl_conversions文件缺失

CMake Warning at /opt/ros/kinetic/share/catkin/cmake/catkinConfig.cmake:76 (find_package):
  Could not find a package configuration file provided by "pcd_conversions"
  with any of the following names:

    pcd_conversionsConfig.cmake
    pcd_conversions-config.cmake

  Add the installation prefix of "pcd_conversions" to CMAKE_PREFIX_PATH or
  set "pcd_conversions_DIR" to a directory containing one of the above files.
  If "pcd_conversions" provides a separate development package or SDK, be
  sure it has been installed.
Call Stack (most recent call first):
  pcd_to_rviz/CMakeLists.txt:10 (find_package)


-- Could not find the required component 'pcd_conversions'. The following CMake error indicates that you either need to install the package with the same name or change your environment so that it can be found.

主要原因是在PCL1.8中缺少pcl_ros 库和pcl_conversions库,此处用到需要自己下载并进行安装,新建一个终端,执行:

sudo apt-get install ros-kinetic-pcl-conversions

2. libvtkproj4-6.2.so.6.2.0报错

make[2]: *** No rule to make target '/usr/lib/x86_64-linux-gnu/libvtkproj4-6.2.so.6.2.0', needed by 'pcd_to_rviz/pcd_to_rviz'。 停止。
make[2]: *** 正在等待未完成的任务....
[ 83%] Building CXX object pcd_to_rviz/CMakeFiles/pcd_to_rviz.dir/src/pcd_to_rviz.cpp.o
CMakeFiles/Makefile2:490: recipe for target 'pcd_to_rviz/CMakeFiles/pcd_to_rviz.dir/all' failed
make[1]: *** [pcd_to_rviz/CMakeFiles/pcd_to_rviz.dir/all] Error 2
Makefile:138: recipe for target 'all' failed
make: *** [all] Error 2
Invoking "make -j4 -l4" failed

应该是libvtkproj4-6.2.so.6.2.0没找到目标链接库,重新建立链接即可,新建一个终端,执行:

sudo ln -s /usr/lib/x86_64-linux-gnu/libvtkCommonCore-6.2.so /usr/lib/x86_64-linux-gnu/libvtkproj4-6.2.so.6.2.0
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值