MOOS 0.2375
/home/toby/moos-ivp/MOOS-2375-Oct0611/NavigationAndControl/MOOSNavLib/MOOSNavVehicle.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 // MOOSNavVehicle.cpp: implementation of the CMOOSNavVehicle class.
00032 //
00034 #include "MOOSNavLibGlobalHelper.h"
00035 #include "MOOSNavVehicle.h"
00036 #include "MOOSNavLibDefs.h"
00037 #include "math.h"
00039 // Construction/Destruction
00041 
00042 CMOOSNavVehicle::CMOOSNavVehicle()
00043 {
00044     m_eType = POSE_ONLY;
00045     
00046 
00047     m_bDroppedState = false;
00048 
00049 }
00050 
00051 CMOOSNavVehicle::~CMOOSNavVehicle()
00052 {
00053 
00054 }
00055 
00056 bool CMOOSNavVehicle::GetFullState(Matrix &Result,Matrix * m_pXToUse,bool bUseEstimate)
00057 {
00058     if(Result.Nrows()!=FULL_STATES    || Result.Ncols()!=1)
00059     {
00060         Result.ReSize(FULL_STATES,1);
00061     }
00062 
00063     //have we been told to use a vector other than the global state vector?
00064     if(m_pXToUse==NULL)
00065     {
00066         //yes (this may happen during numerical evaluation of jacobians
00067         //for example
00068         m_pXToUse = m_pXhat;
00069     }
00070 
00071     //set all to zero..
00072     Result    =0;
00073     
00074     if(bUseEstimate)
00075     {
00076         I_X(Result,1)=I_X((*m_pXToUse),m_nStart);
00077         I_Y(Result,1)=I_Y((*m_pXToUse),m_nStart);
00078         I_Z(Result,1)=I_Z((*m_pXToUse),m_nStart);
00079         I_H(Result,1)=I_H((*m_pXToUse),m_nStart);
00080 
00081         if(m_eType==POSE_AND_RATE)
00082         {
00083             I_Xdot(Result,1)=I_Xdot((*m_pXToUse),m_nStart);
00084             I_Ydot(Result,1)=I_Ydot((*m_pXToUse),m_nStart);
00085             I_Zdot(Result,1)=I_Zdot((*m_pXToUse),m_nStart);
00086             I_Hdot(Result,1)=I_Hdot((*m_pXToUse),m_nStart);
00087         }
00088     }
00089     else
00090     {
00091         I_X(Result,1)=m_State.m_dfX;
00092         I_Y(Result,1)=m_State.m_dfY;
00093         I_Z(Result,1)=m_State.m_dfZ;
00094         I_H(Result,1)=m_State.m_dfH;
00095 
00096         if(m_eType==POSE_AND_RATE)
00097         {
00098             I_Xdot(Result,1)=m_State.m_dfXdot;
00099             I_Ydot(Result,1)=m_State.m_dfYdot;
00100             I_Zdot(Result,1)=m_State.m_dfZdot;
00101             I_Hdot(Result,1)=m_State.m_dfHdot;
00102         }
00103     }
00104 
00105     return true;
00106 
00107 
00108 
00109 }
00110 
00111 
00112 int CMOOSNavVehicle::GetStateSize()
00113 {
00114     switch(m_eType)
00115     {
00116         case POSE_ONLY:        return POSE_ONLY_STATES; break;
00117         case POSE_AND_RATE: return POSE_AND_RATE_STATES; break;
00118         default:
00119             return 0;
00120     }
00121 }
00122 
00123 bool CMOOSNavVehicle::RefreshState()
00124 {
00125 
00126 
00127 //    MOOSMatrixTrace(*m_pPhat,"Phat");
00128     m_State.m_dfX=I_X((*m_pXhat),m_nStart);
00129     m_State.m_dfY=I_Y((*m_pXhat),m_nStart);
00130     m_State.m_dfZ=I_Z((*m_pXhat),m_nStart);
00131     m_State.m_dfH=I_H((*m_pXhat),m_nStart);
00132 
00133     m_State.m_dfPX=(*m_pPhat)(m_nStart+iiX,m_nStart+iiX);
00134     m_State.m_dfPY=(*m_pPhat)(m_nStart+iiY,m_nStart+iiY);
00135     m_State.m_dfPZ=(*m_pPhat)(m_nStart+iiZ,m_nStart+iiZ);
00136     m_State.m_dfPH=(*m_pPhat)(m_nStart+iiH,m_nStart+iiH);
00137 
00138 
00139     if(m_eType==POSE_AND_RATE)
00140     {
00141         m_State.m_dfXdot=I_Xdot((*m_pXhat),m_nStart);
00142         m_State.m_dfYdot=I_Ydot((*m_pXhat),m_nStart);
00143         m_State.m_dfZdot=I_Zdot((*m_pXhat),m_nStart);
00144         m_State.m_dfHdot=I_Hdot((*m_pXhat),m_nStart);        
00145 
00146 
00147         m_State.m_dfPXdot=(*m_pPhat)(m_nStart+iiXdot,m_nStart+iiXdot);
00148         m_State.m_dfPYdot=(*m_pPhat)(m_nStart+iiYdot,m_nStart+iiYdot);
00149         m_State.m_dfPZdot=(*m_pPhat)(m_nStart+iiZdot,m_nStart+iiZdot);
00150         m_State.m_dfPHdot=(*m_pPhat)(m_nStart+iiHdot,m_nStart+iiHdot);
00151 
00152 
00153     }
00154 
00155     m_State.m_dfSpeed = sqrt(pow(m_State.m_dfXdot,2)+pow(m_State.m_dfYdot,2)+pow(m_State.m_dfZdot,2));
00156 
00157     m_State.m_dfDepth = (*m_pXhat)(TIDE_STATE_INDEX,1)-m_State.m_dfZ;
00158     return true;
00159 }
00160 
00161 bool CMOOSNavVehicle::FillModelMatrices(Matrix &F,Matrix &Q,Matrix & Xhat,double dfDeltaT)
00162 {
00163 
00164     switch(m_eType)
00165     {
00166     case POSE_AND_RATE:
00167         return FillPoseAndRateModelMatrices(F,Q,Xhat,dfDeltaT);
00168         break;    
00169     case POSE_ONLY:
00170         return FillPoseOnlyModelMatrices(F,Q,Xhat,dfDeltaT);
00171         break;    
00172     default:
00173         MOOSTrace("err what kind of vehicle is this?\n");
00174         return false;
00175     }
00176 
00177 }
00178 
00179 bool CMOOSNavVehicle::SetDynamics(double dfDynamicsXY, double dfDynamicsZ, double dfDynamicsYaw)
00180 {
00181     double dfSFX = dfDynamicsXY/10.0;
00182     double dfSFY = dfDynamicsXY/10.0;
00183     double dfSFZ = dfDynamicsZ/10.0;
00184     double dfSFH = dfDynamicsYaw/10.0;
00185 
00186     m_Noise.m_dfqXv = pow((dfSFX*FULL_SCALE_Q_VEL),2);
00187     m_Noise.m_dfqYv = pow((dfSFY*FULL_SCALE_Q_VEL),2);
00188     m_Noise.m_dfqZv = pow((dfSFZ*FULL_SCALE_Q_VEL),2);
00189     m_Noise.m_dfqHv = pow((dfSFH*FULL_SCALE_HEADING_VEL),2);
00190 
00191 
00192     m_Noise.m_dfqXa =dfSFX*FULL_SCALE_Q_ACC_XY;
00193     m_Noise.m_dfqYa =dfSFY*FULL_SCALE_Q_ACC_XY;
00194     m_Noise.m_dfqZa    =dfSFZ*FULL_SCALE_Q_ACC_Z;
00195     m_Noise.m_dfqHa    =dfSFH*FULL_SCALE_HEADING_ACC;
00196 
00197     return true;
00198 }
00199 
00200 
00201 
00202 bool CMOOSNavVehicle::FillPoseOnlyModelMatrices(Matrix &jF, Matrix &jQ, Matrix &Xhat, double dfDeltaT)
00203 {
00204 
00205     //static vehicles don't move!
00206 /*
00207     F = 1    0    0    0
00208         0    1    0    0    
00209         0    0    1    0
00210         0    0    0    1
00211 
00212     Q = v    0    0    0
00213         0    v    0    0
00214         0    0    v    0
00215         0    0    0    v
00216 
00217     where v = dT
00218     ( x= v * t -> x^2 = v^2 * dT^2 )
00219 
00220 */
00221 
00222     double dfT2 = pow(dfDeltaT,2);
00223     int iPx = m_nStart+iiX;
00224     int iPy    = m_nStart+iiY;
00225     int iPz = m_nStart+iiZ;
00226     int iPh = m_nStart+iiH;
00227 
00228 
00229     
00230     jQ(iPx,iPx) = m_Noise.m_dfqXv*dfT2;  //qv^2* dT^2
00231     jQ(iPy,iPy) = m_Noise.m_dfqYv*dfT2;
00232     jQ(iPz,iPz) = m_Noise.m_dfqZv*dfT2;
00233     jQ(iPh,iPh) = m_Noise.m_dfqHv*dfT2;
00234 
00235 
00236     jF(iPx,iPx) = 1.0;  
00237     jF(iPy,iPy) = 1.0;
00238     jF(iPz,iPz) = 1.0;
00239     jF(iPh,iPh) = 1.0;                
00240 
00241     return true;
00242 }
00243 
00244 bool CMOOSNavVehicle::FillPoseAndRateModelMatrices(Matrix &jF, Matrix &jQ, Matrix &Xhat, double dfDeltaT)
00245 {
00246 
00247 
00248 
00249 
00250 /*
00251         F =        |1    0    0    0    dT    0    0    0 |
00252                 |0    1    0    0    0    dT    0    0 |    
00253                 |0    0    1    0    0    0    dT    0 |    
00254                 |0    0    0    1    0    0    0    dT|    
00255                 |0    0    0    0    1    0    0    0 |    
00256                 |0    0    0    0    0    1    0    0 |    
00257                 |0    0    0    0    0    0    1    0 |    
00258                 |0    0    0    0    0    0    0    1 |    
00259     */
00260 
00261     
00262     int iPx = m_nStart+iiX;
00263     int iPy    = m_nStart+iiY;
00264     int iPz = m_nStart+iiZ;
00265     int iPh = m_nStart+iiH;
00266     int iVx = m_nStart+iiXdot;
00267     int iVy    = m_nStart+iiYdot;
00268     int iVz = m_nStart+iiZdot;
00269     int iVh = m_nStart+iiHdot;
00270 
00271     int i = 0;
00272     for(i = m_nStart; i<=m_nEnd;i++)
00273     {
00274         jF(i,i) = 1.0;
00275     }
00276 
00277 
00278     for(i = 0; i<4;i++)
00279     {
00280         jF(m_nStart+i,m_nStart+i+4) = dfDeltaT;
00281     }
00282 
00283     
00284 
00286     // now sort out Q
00287 
00288     double dfT4 = pow(dfDeltaT,4)/4;
00289     double dfT3 = pow(dfDeltaT,3)/2;
00290     double dfT2 = pow(dfDeltaT,2);
00291 
00292 
00293 
00294 
00295     //do the diagonals first 
00296     jQ(iPx,iPx) = m_Noise.m_dfqXa*dfT4;  //qa^2* dT^4/4
00297     jQ(iPy,iPy) = m_Noise.m_dfqYa*dfT4;
00298     jQ(iPz,iPz) = m_Noise.m_dfqZa*dfT4;
00299     jQ(iPh,iPh) = m_Noise.m_dfqHa*dfT4;
00300 
00301     jQ(iVx,iVx) = m_Noise.m_dfqXa*dfT2;  //qa^2* dT^2
00302     jQ(iVy,iVy) = m_Noise.m_dfqYa*dfT2;
00303     jQ(iVz,iVz) = m_Noise.m_dfqZa*dfT2;
00304     jQ(iVh,iVh) = m_Noise.m_dfqHa*dfT2;
00305         
00306 
00307     //cross correlation terms
00308     jQ(iPx,iVx) = m_Noise.m_dfqXa*dfT3;  //qa^2 * dT^3 /3
00309     jQ(iPy,iVy) = m_Noise.m_dfqYa*dfT3;
00310     jQ(iPz,iVz) = m_Noise.m_dfqZa*dfT3;
00311     jQ(iPh,iVh) = m_Noise.m_dfqHa*dfT3;
00312 
00313 
00314     jQ(iVx,iPx) = m_Noise.m_dfqXa*dfT3;
00315     jQ(iVy,iPy) = m_Noise.m_dfqYa*dfT3;
00316     jQ(iVz,iPz) = m_Noise.m_dfqZa*dfT3;
00317     jQ(iVh,iPh) = m_Noise.m_dfqHa*dfT3;
00318 
00319 //    MOOSMatrixTrace(jQ,"Q");
00320     
00321     return true;
00322 }
00323 
00324 //used to mark the vehicle as an 'old' vehicle location and
00325 //not used actively in tracking...
00326 bool CMOOSNavVehicle::SetDroppedState(bool bDropped)
00327 {
00328     m_bDroppedState = bDropped;
00329     return true;
00330 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines