00001 #ifndef NDT_MCL_HPP_
00002 #define NDT_MCL_HPP_
00003 #include <pcl/ros/conversions.h>
00004 #include <pcl/point_cloud.h>
00005 #include <pcl/io/pcd_io.h>
00006 #include <pcl/point_types.h>
00007 #include <ndt_map.h>
00008 #include <ndt_cell.h>
00009 #include <pointcloud_utils.h>
00010 #include <tf_conversions/tf_eigen.h>
00011 #include "CParticleFilter.h"
00012 #include <pointcloud_utils.h>
00013 #include <cstdio>
00014 #include <Eigen/Eigen>
00015 #include <Eigen/Geometry>
00016 #include <fstream>
00017
00021 template <typename PointT>
00022 class NDTMCL{
00023 public:
00024 lslgeneric::NDTMap<PointT> map;
00025 mcl::CParticleFilter pf;
00026 double resolution;
00027 int counter;
00028 double zfilt_min;
00029 bool forceSIR;
00033 NDTMCL(double map_resolution, lslgeneric::NDTMap<PointT> &nd_map, double zfilter):
00034 map(new lslgeneric::LazyGrid<PointT>(map_resolution))
00035 {
00036 isInit = false;
00037 forceSIR = false;
00038 resolution=map_resolution;
00039 counter = 0;
00040 zfilt_min = zfilter;
00041 sinceSIR = 0;
00044 double cx,cy,cz;
00045 if(!nd_map.getCentroid(cx, cy, cz)){
00046 fprintf(stderr,"Centroid NOT Given-abort!\n");
00047 exit(1);
00048 }else{
00049 fprintf(stderr,"Centroid(%lf,%lf,%lf)\n",cx,cy,cz);
00050 }
00051
00052 double wx,wy,wz;
00053
00054 if(!nd_map.getGridSizeInMeters(wx, wy, wz)){
00055 fprintf(stderr,"Grid size NOT Given-abort!\n");
00056 exit(1);
00057 }else{
00058 fprintf(stderr,"GridSize(%lf,%lf,%lf)\n",wx,wy,wz);
00059 }
00060
00061 map.initialize(cx,cy,cz,wx,wy,wz);
00062
00063 std::vector<lslgeneric::NDTCell<PointT>*> ndts;
00064 ndts = nd_map.getAllCells();
00065 fprintf(stderr,"NDT MAP with %d components",ndts.size());
00066 for(unsigned int i=0;i<ndts.size();i++){
00067 Eigen::Vector3d m = ndts[i]->getMean();
00068 if(m[2]>zfilter){
00069 Eigen::Matrix3d cov = ndts[i]->getCov();
00070 unsigned int nump = ndts[i]->getN();
00071
00072 map.addDistributionToCell(cov, m,nump);
00073 }
00074 }
00075 }
00076
00081 void initializeFilter(double x, double y, double yaw,double x_e, double y_e, double yaw_e, int numParticles){
00082 pf.initializeNormalRandom(mcl::pose(x,y,yaw), mcl::pose(x_e,y_e,yaw_e),numParticles);
00083 }
00084
00085 void updateAndPredict(Eigen::Affine3d Tmotion, pcl::PointCloud<PointT> &cloud){
00086 Eigen::Vector3d tr = Tmotion.translation();
00087 Eigen::Vector3d rot = Tmotion.rotation().eulerAngles(0,1,2);
00088
00089
00099 pf.predict(mcl::pose(tr[0],tr[1],rot[2]), mcl::pose(tr[0]*0.1 + 0.005,tr[1]*0.1+ 0.005,rot[2]*0.1+0.001));
00100
00101 lslgeneric::NDTMap<PointT> local_map(new lslgeneric::LazyGrid<PointT>(resolution));
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116 local_map.addPointCloudSimple(cloud);
00117
00118 local_map.computeNDTCells(CELL_UPDATE_MODE_STUDENT_T);
00119 int Nn = 0;
00120
00121 for(int i=0;i<pf.NumOfParticles;i++){
00122 Eigen::Affine3d T = getAsAffine(i);
00123
00124 std::vector<lslgeneric::NDTCell<PointT>*> ndts;
00125 ndts = local_map.pseudoTransformNDT(T);
00126 double score=1;
00127
00128 if(ndts.size()==0) fprintf(stderr,"ERROR no gaussians in measurement!!!\n");
00129 Nn = ndts.size();
00130 for(int n=0;n<ndts.size();n++){
00131 Eigen::Vector3d m = ndts[n]->getMean();
00132 if(m[2]<zfilt_min) continue;
00133
00134 lslgeneric::NDTCell<PointT> *cell;
00135 PointT p;
00136 p.x = m[0];p.y=m[1];p.z=m[2];
00137
00138
00139 if(map.getCellForPoint(p,cell)){
00140 if(cell == NULL) continue;
00141 if(cell->hasGaussian_){
00142 Eigen::Matrix3d covCombined = cell->getCov() + ndts[n]->getCov();
00143 Eigen::Matrix3d icov;
00144 bool exists;
00145 double det = 0;
00146 covCombined.computeInverseAndDetWithCheck(icov,det,exists);
00147 if(!exists) continue;
00148 double l = (cell->getMean() - m).dot(icov*(cell->getMean() - m));
00149 if(l*0 != 0) continue;
00150
00151 score += 0.1 + 0.9 * exp(-0.05*l/2.0);
00152 }else{
00153 }
00154 }
00155 }
00156
00157
00158
00159 pf.Particles[i].lik = score;
00160 for(unsigned int j=0;j<ndts.size();j++){
00161 delete ndts[j];
00162 }
00163
00164 }
00165
00166 pf.normalize();
00167
00168
00169 if(forceSIR){
00170
00171 fprintf(stderr, "forceSIR(%d) ",forceSIR);
00172 pf.SIRUpdate();
00173 }else{
00174
00175 double varP=0;
00176 for(int i = 0; i<pf.NumOfParticles;i++){
00177 varP += (pf.Particles[i].p - 1.0/pf.NumOfParticles)*(pf.Particles[i].p - 1.0/pf.NumOfParticles);
00178 }
00179 varP /= pf.NumOfParticles;
00180 varP = sqrt(varP);
00181 fprintf(stderr,"Var P=%lf (Npf=%d, Nm=%d)",varP,pf.NumOfParticles, Nn);
00182 if(varP > 0.006 || sinceSIR >25){
00183 fprintf(stderr,"-SIR- ");
00184 sinceSIR = 0;
00185 pf.SIRUpdate();
00186 }else{
00187 sinceSIR++;
00188 }
00189
00190 }
00191 }
00192
00193 Eigen::Vector3d getMean(){
00194 mcl::pose m = pf.getDistributionMean(true);
00195 Eigen::Vector3d mm; mm<<m.x,m.y,m.a;
00196 return mm;
00197 }
00198
00199
00200 private:
00201 bool isInit;
00202 int sinceSIR;
00203 Eigen::Affine3d getAsAffine(int i){
00204 Eigen::Matrix3d m;
00205 m = Eigen::AngleAxisd(0, Eigen::Vector3d::UnitX())
00206 * Eigen::AngleAxisd(0, Eigen::Vector3d::UnitY())
00207 * Eigen::AngleAxisd(pf.Particles[i].a, Eigen::Vector3d::UnitZ());
00208 Eigen::Translation3d v(pf.Particles[i].x,pf.Particles[i].y,0);
00209 Eigen::Affine3d T = v*m;
00210 return T;
00211 }
00212
00213 };
00214
00215 #endif
00216