您的位置:首页 > 其它

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

2017-04-15 17:19 706 查看
示例选自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 <ros/ros.h>
#include <pcl/point_cloud.h>
#include <pcl_conversions/pcl_conversions.h>
#include <sensor_msgs/PointCloud2.h>

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

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

如图

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息