MOOS 0.2375
/home/toby/moos-ivp/MOOS-2375-Oct0611/NavigationAndControl/MOOSNavLib/MOOSNavLogger.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 // MOOSNavLogger.cpp: implementation of the CMOOSNavLogger class.
00032 //
00034 #include <MOOSLIB/MOOSLib.h>
00035 #include <newmat.h>
00036 using namespace NEWMAT;
00037 #include <time.h>
00038 #include "MOOSNavLibDefs.h"
00039 #include "MOOSObservation.h"
00040 #include "math.h"
00041 #include <iomanip>
00042 #include "MOOSNavLogger.h"
00043 
00044 
00046 // Construction/Destruction
00048 
00049 CMOOSNavLogger::CMOOSNavLogger()
00050 {
00051 
00052 }
00053 
00054 CMOOSNavLogger::~CMOOSNavLogger()
00055 {
00056 
00057 }
00058 
00059 bool CMOOSNavLogger::LogState(double dfTimeNow, Matrix &Xhat, Matrix &Phat)
00060 {
00061     m_StateLogFile.setf(ios::left);
00062     int nStates = Xhat.Nrows();
00063     int nWidth = 14;
00064 
00065 
00066 
00067     if(m_StateLogFile.is_open())
00068     {
00069         //write time
00070         m_StateLogFile<<setw(20)<<setprecision(12)<<dfTimeNow;
00071         m_StateLogFile<<setprecision(5);
00072         int i =0;
00073         for(i = 1;i<=nStates;i++)
00074         {
00075             m_StateLogFile<<setw(nWidth)<<Xhat(i,1)<<" ";
00076         }
00077         
00078         for(i = 1;i<=nStates;i++)
00079         {
00080             m_StateLogFile<<setw(nWidth)<<sqrt(Phat(i,i))<<" ";
00081         }
00082 
00083         m_StateLogFile<<endl;
00084 
00085         return true;
00086 
00087     }
00088     else
00089     {
00090         return false;
00091     }
00092 }
00093 
00094 bool CMOOSNavLogger::Initialise(const string &sFileName,
00095                                 const string &sPath,
00096                                 bool bTimeStamp)
00097 {
00098     m_sStateFileName = MakeFileName(sFileName,"xlog",sPath,bTimeStamp);
00099     m_sObsFileName = MakeFileName(sFileName,"olog",sPath,bTimeStamp);
00100 
00101     m_StateLogFile.open(m_sStateFileName.c_str());
00102     m_ObsLogFile.open(m_sObsFileName.c_str());
00103 
00104     m_StateLogFile<<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
00105     m_StateLogFile<<"%% FILTER STATE LOG FILE:       "<<m_sStateFileName.c_str()<<endl;
00106     m_StateLogFile<<"%% FILE OPENED ON               "<<MOOSGetDate().c_str();
00107     m_StateLogFile<<"%% LOGSTART                     "<<setw(20)<<setprecision(12)<<MOOSTime()<<endl;
00108     m_StateLogFile<<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
00109 
00110     m_ObsLogFile<<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
00111     m_ObsLogFile<<"%% FILTER OBS LOG FILE:       "<<m_sObsFileName.c_str()<<endl;
00112     m_ObsLogFile<<"%% FILE OPENED ON               "<<MOOSGetDate().c_str();
00113     m_ObsLogFile<<"%% LOGSTART                     "<<setw(20)<<setprecision(12)<<MOOSTime()<<endl;
00114     m_ObsLogFile<<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
00115     
00116     m_nObsLogged = 0;
00117 
00118     return m_StateLogFile.is_open() && m_ObsLogFile.is_open();
00119 }
00120 
00121 
00122 string CMOOSNavLogger::MakeFileName(string sStem,
00123                                     const string & sExtension,
00124                                     string sPath,
00125                                     bool bTimeStamp)
00126 {
00127     struct tm *Now;
00128     time_t aclock;
00129     time( &aclock );                 
00130     Now = localtime( &aclock );  
00131 
00132     char sTmp[1000];
00133 
00134     if(bTimeStamp)
00135     {
00136         // Print local time as a string 
00137 
00138         //ODYSSEYLOG_14_5_1993_____9_30.log
00139         sprintf(sTmp, "%s%s_%d_%d_%d_____%.2d_%.2d.%s",
00140                 sPath.c_str(),
00141                 sStem.c_str(),
00142                 Now->tm_mday,
00143                 Now->tm_mon+1,
00144                 Now->tm_year+1900,
00145                 Now->tm_hour,
00146                 Now->tm_min,
00147                 sExtension.c_str());
00148     }
00149     else
00150     {
00151         sprintf(sTmp,"%s%s.%s",sPath.c_str(),sStem.c_str(),sExtension.c_str());
00152     }
00153 
00154      return string(sTmp);
00155 
00156 }
00157 
00158 
00159 
00160 bool CMOOSNavLogger::LogObservation(double dfTimeNow,
00161                                     CMOOSObservation &rObs,                              
00162                                     int nUpdate,
00163                                     bool bNaN)
00164 {
00165 
00166     if(!m_ObsLogFile.is_open())
00167         return false;
00168     
00169     if(m_nObsLogged++==0)
00170     {
00171         m_ObsLogFile<<"%% 1. UpdateTime"<<endl;
00172         m_ObsLogFile<<"%% 2. Update Number"<<endl;
00173         m_ObsLogFile<<"%% 3. ObsTime"<<endl;
00174         m_ObsLogFile<<"%% 4. ObsID"<<endl;
00175         m_ObsLogFile<<"%% 5. Type"<<endl;
00176         m_ObsLogFile<<"%% 6. Data"<<endl;
00177         m_ObsLogFile<<"%% 7. LBL Chan"<<endl;
00178         m_ObsLogFile<<"%% 8. Innov"<<endl;
00179         m_ObsLogFile<<"%% 9. Innov Std"<<endl;
00180         m_ObsLogFile<<"%% 10.DA Good"<<endl;
00181         
00182         m_ObsLogFile<<"%% ObsTypes:"<<endl;
00183         m_ObsLogFile<<"%%     X=1"<<endl;
00184         m_ObsLogFile<<"%%     Y=2 "<<endl;
00185         m_ObsLogFile<<"%%     YAW=3 "<<endl;
00186         m_ObsLogFile<<"%%     LBL_BEACON_2WR=4 "<<endl;
00187         m_ObsLogFile<<"%%     DEPTH=5"<<endl;
00188         m_ObsLogFile<<"%%     ALTITUDE=6"<<endl;
00189         m_ObsLogFile<<"%%     SPEED=7"<<endl;
00190         m_ObsLogFile<<"%%     THRUST=8"<<endl;
00191         m_ObsLogFile<<"%%     BODY_VEL_Y=9"<<endl;
00192         m_ObsLogFile<<"%%     BODY_VEL_X=10"<<endl;
00193         m_ObsLogFile<<"%%     BODY_VEL_Z=11"<<endl;
00194         m_ObsLogFile<<"%%     TIDE=12"<<endl;
00195         m_ObsLogFile<<"%%     RUDDER=13"<<endl;
00196         m_ObsLogFile<<"%%     ELEVATOR=14"<<endl;
00197 
00198     }
00199 
00200 
00201     m_ObsLogFile.setf(ios::left);
00202     int nWidth = 14;
00203     if(m_ObsLogFile.is_open())
00204     {
00205         //write update time
00206         m_ObsLogFile<<setw(20)<<setprecision(12)<<dfTimeNow;
00207 
00208         //write the nth update
00209         m_ObsLogFile<<setw(10)<<nUpdate;
00210 
00211         //write observation valid time
00212         m_ObsLogFile<<setw(20)<<setprecision(12)<<rObs.m_dfTime;
00213         
00214 
00215         //write obs ID
00216         m_ObsLogFile<<setw(6)<<setprecision(5)<<rObs.GetID()<<" ";
00217         
00218         //write obs type
00219         m_ObsLogFile<<setw(4)<<rObs.m_eType<<" ";
00220 
00221         //write data
00222         if(bNaN)
00223         {
00224             m_ObsLogFile<<setw(15)<<"NaN"<<" ";
00225         }
00226         else
00227         {
00228             m_ObsLogFile<<setw(15)<<rObs.m_dfData<<" ";
00229         }
00230 
00231         //write channel (only really useful for LBL...
00232         m_ObsLogFile<<setw(4)<<rObs.m_nChan<<" ";
00233 
00234         //write innovation
00235         m_ObsLogFile<<setw(15)<<rObs.m_dfInnov<<" ";
00236 
00237         //write covariance
00238         double dfSii = 0;
00239         if(rObs.m_dfInnovStd<0)
00240         {
00241             m_ObsLogFile<<setw(15)<<"NaN"<<" ";
00242         }
00243         else
00244         {
00245             m_ObsLogFile<<setw(15)<<rObs.m_dfInnovStd<<" ";
00246         }
00247 
00248 
00249         //write DA success
00250         m_ObsLogFile<<setw(4)<<rObs.m_bGoodDA<<" ";
00251 
00252         m_ObsLogFile<<endl;
00253 
00254 
00255         return true;
00256 
00257     }
00258     else
00259     {
00260         return false;
00261     }
00262 
00263 }
00264 
00265 bool CMOOSNavLogger::Comment(const string &sComment, double dfTime)
00266 {
00267     
00268     m_ObsLogFile<<"%%  ";
00269     m_ObsLogFile<<setw(20)<<setprecision(12)<<dfTime;
00270     m_ObsLogFile<<sComment.c_str()<<endl;
00271 
00272     m_StateLogFile<<"%%  ";
00273     m_StateLogFile<<setw(20)<<setprecision(12)<<dfTime;
00274     m_StateLogFile<<sComment.c_str()<<endl;
00275   
00276     return true;
00277 
00278 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines