pointcloud_writer_node.cc
Go to the documentation of this file.
00001 #include<pcl/ros/conversions.h>
00002 #include<iostream>
00003 #include<fstream>
00004 #include"sensor_msgs/PointCloud2.h"
00005 #include"sensor_msgs/point_cloud_conversion.h"
00006 #include<cstring>
00007 #include<Eigen/Eigen>
00008 #include<pointcloud_utils.h>
00009 #include "ros/ros.h"
00010 #include "pcl/point_cloud.h"
00011 #include "sensor_msgs/PointCloud2.h"
00012 #include "nav_msgs/Odometry.h"
00013 #include "pcl/io/pcd_io.h"
00014 #include "message_filters/subscriber.h"
00015 #include "tf/message_filter.h"
00016 #include <tf/transform_broadcaster.h>
00017 
00018 using namespace std;
00019 
00020 class WriterNode
00021 {
00022 protected:
00023     // Our NodeHandle
00024     ros::NodeHandle nh_;
00025     ros::Subscriber pc_;
00026     ros::Subscriber pose_;
00027     // Use the vector as a cyclic buffer (increment with std::rotate).
00028     pcl::PointCloud<pcl::PointXYZ> sensor_pc;
00029     int dumpNumber;
00030     const char* dumpPath;
00031 
00032     Eigen::Transform<double,3,Eigen::Affine,Eigen::ColMajor> Tinit, T;
00033     bool inited;
00034 
00035     boost::mutex data_mutex;
00036 
00037     FILE *fout;
00038 public:
00039     // Constructor
00040     WriterNode(const char* _dumpPath) : dumpNumber(0)
00041     {
00042         dumpPath = _dumpPath;
00043         pc_ = nh_.subscribe("/camera/rgb/points", 10, &WriterNode::log, this);
00044         pose_ = nh_.subscribe("/pose", 10, &WriterNode::updatePose, this);
00045         char fn[500];
00046 
00047         inited = false;
00048         snprintf (fn,499,"%s/pose.dat",dumpPath);
00049         fout = fopen(fn,"w");
00050     }
00051 
00052     ~WriterNode()
00053     {
00054         fclose(fout);
00055     }
00056 
00057     // Callback
00058     void log(const sensor_msgs::PointCloud2::ConstPtr& msg_in)
00059     {
00060         char fname [500];
00061         snprintf (fname,499,"%s/cloud%03d.wrl",dumpPath,dumpNumber);
00062         dumpNumber ++;
00063 
00064         pcl::fromROSMsg (*msg_in, sensor_pc);
00065         lslgeneric::writeToVRML<pcl::PointXYZ>(fname,sensor_pc);
00066         printf ("Dumped %s\n",fname);
00067 
00068         Eigen::Quaterniond qlocal;
00069         Eigen::Vector3d vlocal;
00070         Eigen::Transform<double,3,Eigen::Affine,Eigen::ColMajor> Tlocal;
00071 
00072         data_mutex.lock();
00073         if(!inited)
00074         {
00075             inited = true;
00076             Tinit = T;
00077             cout<<"Tinit:: "<<Tinit.translation().transpose()<<" r "<<Tinit.rotation().eulerAngles(0,1,2).transpose()<<endl;
00078         }
00079         Tlocal = Tinit.inverse()*T;
00080         data_mutex.unlock();
00081 
00082         vlocal = Tlocal.translation();
00083         qlocal = Tlocal.rotation();
00084         fprintf(fout,"0 %lf %lf %lf %lf %lf %lf %lf\n",
00085                 vlocal(0),vlocal(1),vlocal(2),qlocal.x(),qlocal.y(),qlocal.z(),qlocal.w());
00086 
00087     }
00088 
00089     void updatePose(const nav_msgs::Odometry::ConstPtr& msg_in)
00090     {
00091         data_mutex.lock();
00092         Eigen::Vector3d v;
00093         v<<msg_in->pose.pose.position.x,msg_in->pose.pose.position.y,msg_in->pose.pose.position.z;
00094         Eigen::Quaterniond q = Eigen::Quaterniond(msg_in->pose.pose.orientation.w,msg_in->pose.pose.orientation.x,
00095                                msg_in->pose.pose.orientation.y,msg_in->pose.pose.orientation.z);
00096         T = Eigen::Translation<double,3>(v)*q;
00097 
00098         data_mutex.unlock();
00099 
00100     }
00101 
00102 public:
00103     EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
00104 };
00105 
00106 
00107 
00108 int main(int argc, char**argv)
00109 {
00110 
00111     if(argc!=2)
00112     {
00113         cout<<"Usage: "<<argv[0]<<" PATH_TO_POINTCLOUDS \n";
00114         return -1;
00115     }
00116     ros::init(argc, argv, "pointcloud_writer");
00117 
00118     WriterNode nd(argv[1]);
00119     ros::spin();
00120 
00121 }


pointcloud_vrml
Author(s): Dev
autogenerated on Mon Jan 6 2014 11:31:55