zoukankan      html  css  js  c++  java
  • sensor_msgs::laserscan 与pointcloud2、pointcloud 的转换

     sensor_msgs::laserscan 与pointcloud2、pointcloud 的转换 

     

    #include "My_Filter.h"
    
    My_Filter::My_Filter(){
        //scan_sub_ = node_.subscribe<sensor_msgs::LaserScan> ("/scan", 100, &My_Filter::scanCallback, this);
    
        //订阅 "/scan"
        scan_sub_ = node_.subscribe<sensor_msgs::LaserScan> ("/scan", 100, &My_Filter::scanCallback_2, this);
    
        //发布LaserScan转换为PointCloud2的后的数据
        point_cloud_publisher_ = node_.advertise<sensor_msgs::PointCloud2> ("/cloud2", 100, false);
    
        //此处的tf是 laser_geometry 要用到的
        tfListener_.setExtrapolationLimit(ros::Duration(0.1));
    
        //前面提到的通过订阅PointCloud2,直接转化为pcl::PointCloud的方式
        pclCloud_sub_ = node_.subscribe<pcl::PointCloud<pcl::PointXYZ> >("/cloud2", 10, &My_Filter::pclCloudCallback, this);
    
    }
    
    
    void My_Filter::scanCallback(const sensor_msgs::LaserScan::ConstPtr& scan){
        sensor_msgs::PointCloud2 cloud;
        projector_.transformLaserScanToPointCloud("laser", *scan, cloud, tfListener_);
        point_cloud_publisher_.publish(cloud);
    }
    
    
    void My_Filter::scanCallback_2(const sensor_msgs::LaserScan::ConstPtr& scan){
        sensor_msgs::PointCloud2 cloud;
    
        /*laser_geometry包中函数,将 sensor_msgs::LaserScan 转换为 sensor_msgs::PointCloud2 */
        //普通转换
        //projector_.projectLaser(*scan, cloud);        
        //使用tf的转换
        projector_.transformLaserScanToPointCloud("laser", *scan, cloud, tfListener_);
    
        int row_step = cloud.row_step;
        int height = cloud.height;
    
        /*将 sensor_msgs::PointCloud2 转换为 pcl::PointCloud<T> */
        //注意要用fromROSMsg函数需要引入pcl_versions(见头文件定义)
        pcl::PointCloud<pcl::PointXYZ> rawCloud;
        pcl::fromROSMsg(cloud, rawCloud);
    
        for(size_t i = 0; i < rawCloud.points.size(); i++){
            std::cout<<rawCloud.points[i].x<<"	"<<rawCloud.points[i].y<<"	"<<rawCloud.points[i].z<<std::endl;
        }
    
        point_cloud_publisher_.publish(cloud);
    
    
    }
    
    void My_Filter::pclCloudCallback(const pcl::PointCloud<pcl::PointXYZ>::ConstPtr& cloud){
        for(size_t i = 0; i < cloud->points.size(); i++){
            std::cout<<"direct_trans: "<<cloud->points[i].x<<"	"<<cloud->points[i].y<<"	"<<cloud->points[i].z<<std::endl;
        }
    }

    参考

    古月的博客

    https://www.guyuehome.com/12913

  • 相关阅读:
    zoj 1797 Least Common Multiple
    poj 3233 Matrix Power Series
    使用membership(System.Web.Security)来进行角色与权限管理 (转)
    flashplayer9的全屏模式
    AJAX1.0的UpdateProgress使用
    ASP.NET 2.0中的成员管理与角色管理 (续)
    ASP.net AJAX 调用PageMethods实例
    关于模式窗体的缓存问题的解决方案
    win2003服务器中:无法连接ACCESS数据库/sql数据库正常 && .net程序生成的dll文件拒绝访问问题
    js event 详解
  • 原文地址:https://www.cnblogs.com/lovebay/p/13915309.html
Copyright © 2011-2022 走看看