(十一)在ROS上发布点云 PointClouds

在ros中,对于3D点,可以直接给sensor_msgs/PointCloud 赋值并发布消息,比较方便。

1.创建工程

mkdir -p pointcloud/src
cd src
catkin_create_pkg pointcloud roscpp rospy sensor_msgs std_msgs 
cd ..
catkin_make

2.编辑主函数

pointcloud.cpp

#include <ros/ros.h>
#include <sensor_msgs/PointCloud.h>

int main(int argc, char** argv){
  ros::init(argc, argv, "point_cloud_publisher");
  ros::NodeHandle n;
  ros::Publisher cloud_pub = n.advertise<sensor_msgs::PointCloud>("cloud", 50);
  unsigned int num_points = 100;
  ros::Rate r(1.0);
  while(n.ok()){
    sensor_msgs::PointCloud cloud;
    cloud.header.stamp = ros::Time::now();
    cloud.header.frame_id = "sensor_frame";
    cloud.points.resize(num_points);
    //we'll also add an intensity channel to the cloud
    cloud.channels.resize(1);
    cloud.channels[0].name = "rgb";
    cloud.channels[0].values.resize(num_points);

    //generate some fake data for our point cloud
    for(unsigned int i = 0; i < num_points; ++i){
      cloud.points[i].x = 0.1*i;
      cloud.points[i].y = 0.1*i;
      cloud.points[i].z = 5;
      cloud.channels[0].values[i] = 255;
    }
    cloud_pub.publish(cloud);
    r.sleep();
  }
}

此程序对wiki上http://wiki.ros.org/cn/navigation/Tutorials/RobotSetup/Sensors的例子进行了修改。
cloud.channels[0].name = “rgb”,将名字改为rgb。另外固定点云位置为一条直线。

3.编辑CMakeLists.txt

在最后加入
add_executable(pointcloud src/pointcloud.cpp)
target_link_libraries(pointcloud ${catkin_LIBRARIES})

4.rviz显示

在rviz 中fix frame 设置为sensor_frame
其他设置及运行效果如图
这里写图片描述

参考文献
http://wiki.ros.org/cn/navigation/Tutorials/RobotSetup/Sensors
http://docs.ros.org/api/sensor_msgs/html/msg/PointCloud.html

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值