main.cpp
Go to the documentation of this file.
00001 #include<iostream>
00002 #include"ros/ros.h"
00003 #include "nav_msgs/Odometry.h"
00004 //用于打开pcd
00005 #include <pcl/io/io.h>
00006 #include <pcl/io/pcd_io.h>
00007 #include <pcl_ros/point_cloud.h>
00008 #include <pcl/point_types.h>
00009 #include <pcl/filters/voxel_grid.h>
00010 #include <pcl/filters/filter.h>
00011 #include <pcl_conversions/pcl_conversions.h>
00012 #include <pcl_ros/point_cloud.h>
00013 #include "pcl_ros/transforms.h"
00014 
00015 
00016 #include <tf/tf.h>
00017 #include <tf/transform_listener.h>
00018 
00019 #include <message_filters/subscriber.h>
00020 #include <message_filters/synchronizer.h>
00021 #include <message_filters/sync_policies/approximate_time.h>
00022 
00023 namespace testPointCloud
00024 {
00025     using namespace message_filters;
00026     typedef sync_policies::ApproximateTime<sensor_msgs::PointCloud2, nav_msgs::Odometry> MySyncPolicy;
00027     class tp
00028     {
00029         public:
00030             tp(void ):
00031                 cloud_sum(new pcl::PointCloud<pcl::PointXYZ>),
00032                 count_point(0)
00033             {
00034                 point_sub_.subscribe(n,"/multisense/organized_image_points2",5);
00035                 vo_sub_.subscribe(n,"/stereo_odometer/odometry",1000);
00036 
00037                 my_sync_.reset(new AppSync(MySyncPolicy(5),point_sub_, vo_sub_));
00038                 my_sync_->registerCallback(boost::bind(&tp::callback, this, _1, _2));
00039             }
00040         private:
00041             void callback(const sensor_msgs::PointCloud2ConstPtr& msg,const nav_msgs::OdometryConstPtr& vo);
00042 
00043             ros::NodeHandle n;
00044             ros::Subscriber m_subscriber_;
00045             tf::TransformListener mylistener;
00046             tf::StampedTransform transform;
00047             tf::Transform vo_meas_;
00048 
00049             message_filters::Subscriber<sensor_msgs::PointCloud2> point_sub_;
00050             message_filters::Subscriber<nav_msgs::Odometry> vo_sub_;
00051             typedef message_filters::Synchronizer<MySyncPolicy> AppSync;
00052             boost::shared_ptr<AppSync> my_sync_;
00053             pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_sum;
00054 
00055             pcl::PCDWriter writer;
00056             int count_point;
00057     };
00058 
00059     void tp::callback(const sensor_msgs::PointCloud2ConstPtr& msg, const nav_msgs::OdometryConstPtr& vo)
00060     {
00061         ROS_INFO("Callback %s",vo->header.frame_id.c_str());
00062 
00063         sensor_msgs::PointCloud2 transformed_PointCloud2;
00064         poseMsgToTF(vo->pose.pose, vo_meas_);
00065         tf::Transform vo_meas_tran;
00066 
00067 
00068         //坐标轴变化
00069         double Rx, Ry, Rz, tx, ty, tz;
00070         vo_meas_.getBasis().getEulerYPR(Rz, Ry, Rx);
00071         ROS_INFO("%f,%f,%f",Rx,Ry,Rz);
00072         tx = vo_meas_.getOrigin().x();
00073         ty = vo_meas_.getOrigin().y();
00074         tz = vo_meas_.getOrigin().z();
00075 
00076         tf::Quaternion q(Rz,Ry,Rx);
00077         vo_meas_tran.setOrigin(tf::Vector3(tx,ty,tz));
00078         vo_meas_tran.setRotation(q);
00079 
00080         pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
00081         pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_fil (new pcl::PointCloud<pcl::PointXYZ>);
00082 
00083 
00084         pcl_ros::transformPointCloud("odom", vo_meas_tran.inverse(), *msg, transformed_PointCloud2);
00085         pcl::fromROSMsg(transformed_PointCloud2, *cloud);
00086         /*
00087          *Eigen::Matrix4f transformation_matrix = Eigen::Matrix4f::Identity();
00088          *transformation_matrix (0,3) = tx; transformation_matrix (1,3) = ty;transformation_matrix (2,3) = tz;
00089          *pcl::transformPointCloud (*cloud, *cloud, transformation_matrix);
00090          */
00091 
00092         pcl::VoxelGrid<pcl::PointXYZ> sor;
00093         sor.setInputCloud(cloud);
00094         sor.setLeafSize(0.01f,0.01f,0.01f);
00095         sor.filter(*cloud_fil);
00096         *cloud_sum += *cloud_fil;
00097 
00098         if(count_point == 6)
00099             writer.write("sum.pcd",*cloud_sum);
00100 
00101         count_point++;
00102     }
00103 };
00104 int main(int argc,char** argv)
00105 {
00106     ros::init(argc,argv,"test_point");
00107     testPointCloud::tp tp;
00108     ros::spin();
00109 }


test_point
Author(s):
autogenerated on Thu Jun 6 2019 20:03:39