MOOS 0.2375
/home/toby/moos-ivp/MOOS-2375-Oct0611/NavigationAndControl/MOOSNavLib/CMOOSNavLibGlobalHelper.cpp
Go to the documentation of this file.
00001 
00002 //
00003 //   MOOS - Mission Oriented Operating Suite 
00004 //  
00005 //   A suit of Applications and Libraries for Mobile Robotics Research 
00006 //   Copyright (C) 2001-2005 Massachusetts Institute of Technology and 
00007 //   Oxford University. 
00008 //    
00009 //   This software was written by Paul Newman and others
00010 //   at MIT 2001-2002 and Oxford University 2003-2005.
00011 //   email: pnewman@robots.ox.ac.uk. 
00012 //      
00013 //   This file is part of a  MOOS Basic (Common) Application. 
00014 //        
00015 //   This program is free software; you can redistribute it and/or 
00016 //   modify it under the terms of the GNU General Public License as 
00017 //   published by the Free Software Foundation; either version 2 of the 
00018 //   License, or (at your option) any later version. 
00019 //          
00020 //   This program is distributed in the hope that it will be useful, 
00021 //   but WITHOUT ANY WARRANTY; without even the implied warranty of 
00022 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
00023 //   General Public License for more details. 
00024 //            
00025 //   You should have received a copy of the GNU General Public License 
00026 //   along with this program; if not, write to the Free Software 
00027 //   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 
00028 //   02111-1307, USA. 
00029 //
00031 
00032 #include <MOOSGenLib/MOOSGenLibGlobalHelper.h>
00033 #include "MOOSNavLibGlobalHelper.h"
00034 #include <sstream>
00035 #include <iomanip>
00036 #include <stdio.h>
00037 
00038 using namespace std;
00039 
00040 void   MOOSMatrixTrace(const Matrix & Mat,const string & sStr)
00041 {
00042     int nRows = Mat.Nrows();
00043     int nCols = Mat.Ncols();
00044 
00045     ostringstream os;
00046 
00047     os<<"Matrix "<<sStr.c_str()<<"["<<nRows<<" x "<<nCols<<"]"<<endl;
00048 
00049     int nWidth = 10;
00050     os.setf(ios::left);
00051 
00052     for(int nRow = 1; nRow<=Mat.Nrows();nRow++)
00053     {
00054         for(int nCol = 1; nCol<=Mat.Ncols();nCol++)
00055         {
00056             os<<setw(nWidth)<<setprecision(3);
00057             os<<Mat(nRow,nCol)<<" ";
00058         }
00059         os<<endl;
00060     }
00061     
00062     os<<endl<<ends;
00063     string sTxt = os.str();
00064 
00065 //    os.rdbuf()->freeze(0);
00066 
00067     MOOSTrace(sTxt);
00068 }
00069 
00070 
00071 
00072 
00073                  
00074 double MOOS_WRAP_ANGLE(double d, bool Radians /* true */)
00075 {
00076     double FullScale;
00077     if (Radians)
00078     {
00079         FullScale=2*PI;
00080     }
00081     else
00082     {
00083         FullScale=360.0;
00084     }
00085 
00086     d = fmod(d,FullScale);
00087 
00088     if( d >= (FullScale/2))
00089     {
00090       d-=FullScale;
00091     }
00092     else if (d<=-(FullScale/2))
00093     {
00094       d+=FullScale;
00095     }
00096 
00097     return d;
00098 }
00099 
00100 bool MOOSGetJacobian(Matrix & J,const Matrix & XAbout,
00101                  int nOut,
00102                  int nIn,
00103                  bool (*pfn)(Matrix & XOut,Matrix & XIn,void *pParam),
00104                  void * pParam)
00105 {
00106     
00107     if(J.Nrows()!=nOut || J.Ncols()!=nIn)
00108     {
00109         J.ReSize(nOut,nIn);
00110     }
00111     
00112     if(XAbout.Nrows()!=nIn)
00113     {
00114         printf("cannot linearise about XAbout (wrong size)\n");
00115         return false;
00116     }
00117             
00118     Matrix XOutNominal(nOut,1);
00119     
00120     Matrix XPerturbed = XAbout;
00121 
00122     //calculate the nominal projected
00123     //vector XPreturbed hasn't yet been peturbed!
00124     if((*pfn)(XOutNominal,XPerturbed,pParam))
00125     {
00126 
00127         double dfPerturbation=1e-7;
00128     
00129         Matrix XOutPeturbed(nOut,1);
00130         
00131         for(int nVar = 1; nVar<= nIn;nVar++)
00132         {
00133             //perturb the set point
00134             XPerturbed(nVar,1)+=dfPerturbation;
00135             
00136             //project with perturbation
00137             if((*pfn)(XOutPeturbed,XPerturbed,pParam))
00138             {
00139                 J.SubMatrix(1,nOut,nVar,nVar) = (XOutPeturbed-XOutNominal)/dfPerturbation;
00140             }
00141 
00142             XPerturbed(nVar,1)-=dfPerturbation;        
00143         }
00144     }
00145         
00146     return true;
00147 }
00148 
00149 
00152 bool MOOSChiSquaredTest(Matrix &Innovation, Matrix &Cov, bool bAlreadyInverted)
00153 {
00154     Matrix mEpsilon;
00155     if(!bAlreadyInverted)
00156     {
00157         mEpsilon = (Innovation.t() * Cov.i()* Innovation);
00158     }
00159     else
00160     {
00161         mEpsilon = (Innovation.t() * Cov* Innovation);
00162     }
00163 
00164     double dfEpsilon = mEpsilon(1,1);
00165 
00166 
00167     double ChiSquared[] = {    12.12,    //1
00168                             15.20,    //2
00169                             17.73,    //3
00170                             20.00,    //4
00171                             22.11,    //5
00172                             24.10,    //6
00173                             26.02,    //7
00174                             27.87,    //8
00175                             29.76,    //9
00176                             31.42,    //10
00177                             33.14,    //11
00178                             34.82,    //12
00179                             36.48,    //13
00180                             38.11,    //14
00181                             39.72,    //15
00182                             41.31,    //16
00183                             42.88,    //17
00184                             44.43,    //18
00185                             45.97,    //19
00186                             47.50,    //20
00187                             };
00188 
00189     int MaxDOF =sizeof(ChiSquared)/sizeof(double);
00190 
00191     int DOF = Innovation.Nrows();
00192 
00193     if(DOF>MaxDOF)
00194     {
00195         MOOSTrace("MOOS data validation...Chi Squared  data for %d DOF  unavailable!\n",DOF);
00196         return false;
00197     }
00198     else
00199     {
00200 
00201 //        MOOSTrace("df%d v'Sv = %e, Chi = %e \n", DOF, dfEpsilon,ChiSquared[DOF-1]);
00202 
00203         if(dfEpsilon<=ChiSquared[DOF-1])
00204         {
00205             return     true;
00206         }
00207         else
00208         {
00209 //            MOOSTrace("df%d v'Sv = %e, Chi = %e \n", DOF, dfEpsilon,ChiSquared[DOF-1]);
00210             return false;
00211         }
00212     }                                        
00213     return false;
00214 }
00215 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines