EKF.h

Go to the documentation of this file.
00001 //
00002 // Copyright 2008, 2009, 2010, 2011, 2012  Antonio Franchi and Paolo Stegagno
00003 //
00004 // This file is part of MIP.
00005 //
00006 // MIP is free software: you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License as published by
00008 // the Free Software Foundation, either version 3 of the License, or
00009 // (at your option) any later version.
00010 //
00011 // MIP is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU General Public License
00017 // along with MIP. If not, see <http://www.gnu.org/licenses/>.
00018 //
00019 // Contact info: antonio.franchi@tuebingen.mpg.de stegagno@diag.uniroma1.it
00020 //
00021 // ----------------------------------------------------------------------------
00022 
00023 /* Caso EKF */
00024       // x+ : stima a posteriori
00025       // x- : stima a priori
00026       // w : rumore di processo
00027       // v : rumore di misura
00028       // f(x+,u,0) -> per la proiezione in avanti dello stato (stima a priori) <- funzione
00029       // h(x-,0) -> modello per la misura <- funzione
00030       // A = df/dx -> calcolata per (x+,u,0) <- funzione (se variabile), matrice (se costante)
00031       // W = df/dw -> calcolata per (x+,u,0) <- funzione (se variabile), matrice (se costante)
00032       // H = dh/dx -> calcolata per (x-,u,0) <- funzione (se variabile), matrice (se costante)
00033       // V = dh/dv -> calcolata per (x-,u,0) <- funzione (se variabile), matrice (se costante)
00034 
00035 #include <bfl/filter/extendedkalmanfilter.h>
00036 #include <bfl/model/linearanalyticsystemmodel_gaussianuncertainty.h>
00037 #include <bfl/model/linearanalyticmeasurementmodel_gaussianuncertainty.h>
00038 #include <bfl/pdf/analyticconditionalgaussian.h>
00039 #include <bfl/pdf/linearanalyticconditionalgaussian.h>
00040 #include <bfl/pdf/analyticconditionalgaussian_additivenoise.h>
00041 
00042 #include <iostream>
00043 #include <fstream>
00044 
00045 #include <baselib.h>
00046 #include <bfl/filter/kalmanfilter.h>
00047 #include <bfl/pdf/conditionalpdf.h>
00048 #include <bfl/pdf/gaussian.h>
00049 # include <map>
00050 
00051 //#include <time.h>
00052 #include <bfl/wrappers/rng/rng.h>
00053 #include <LogTrace.h>
00054 
00055 #ifndef _EKF_H
00056 #define _EKF_H
00057 
00061 
00062 using namespace MipBaselib;
00063 using namespace std;
00064 
00065 namespace MipAlgorithms{
00066  
00068  /* @{ */
00069   
00073     class processModel : public BFL::AnalyticConditionalGaussianAdditiveNoise
00074     {
00075     private:
00076       MatrixWrapper::Matrix A;     // jacobian matrix (df/dx) <- an external callback function will compute this
00077       MatrixWrapper::Matrix W;     // jacobian matrix (df/dw) <- an external callback function will compute this
00078       MatrixWrapper::ColumnVector preStateEst;   // state projection (a priori) <- an external callback function will compute this
00079     public:
00080       processModel(const BFL::Gaussian& additiveNoise,int ConditionalArguments);
00081       // default costructor copy
00082       // The redefinition of these methods is mandatory
00083       virtual MatrixWrapper::ColumnVector    ExpectedValueGet() const; //returns x-
00084       virtual MatrixWrapper::Matrix dfGet(unsigned int i) const;                 //returns A
00085 
00086       // This method must be redefined to get W*Q*W' instead of Q
00087       virtual MatrixWrapper::SymmetricMatrix CovarianceGet   () const;
00088       // brief get/set methods for internal members
00089       inline void set_A(MatrixWrapper::Matrix df_dx) {A = df_dx;};
00090       inline MatrixWrapper::Matrix get_A() {return A;};
00091       inline void set_W(MatrixWrapper::Matrix df_dw) {W = df_dw;};
00092       inline MatrixWrapper::Matrix get_W() {return W;};
00093       inline void set_preStateEst(MatrixWrapper::ColumnVector preEst) {preStateEst = preEst;};
00094       inline MatrixWrapper::ColumnVector get_preStateEst() {return preStateEst;};
00095     };
00096 
00099     // TODO - this class has to be checked!!! By now this doesn't work!!!
00100     class measModel : public BFL::AnalyticConditionalGaussianAdditiveNoise
00101     {
00102     private:
00103       BFL::Matrix H;          // jacobian matrix (df/dx) <- an external callback function will compute this
00104       BFL::ColumnVector zEst; // stima della misura
00105       BFL::ColumnVector stateEst; // stima dello stato (serve per calcolare la previsione della misura)
00106       int rows,columns; // righe/colonne di H
00107     public:
00108       measModel(const BFL::Matrix& ratio, const BFL::Gaussian& additiveNoise);
00109 
00110       virtual BFL::ColumnVector    ExpectedValueGet() ;
00111       virtual BFL::Matrix          dfGet(unsigned int i)       const;
00112       // brief get/set methods for internal members
00113       inline BFL::Matrix get_H() {return H;};
00114       inline void set_H(BFL::Matrix dh_dx) {H = dh_dx;};
00115       inline BFL::ColumnVector get_stateEst() {return stateEst;};
00116       inline void set_stateEst(BFL::ColumnVector state){printf("prova\n"); stateEst = state;};
00117     };
00118 
00120     class EKF : public BFL::ExtendedKalmanFilter
00121     {
00122     public:
00123         EKF(BFL::Gaussian* prior);
00124         void SystemUpdate(BFL::SystemModel<MatrixWrapper::ColumnVector>* const sysmodel,
00125                          const MatrixWrapper::ColumnVector& u);
00126         void MeasurementUpdate(BFL::MeasurementModel<MatrixWrapper::ColumnVector,MatrixWrapper::ColumnVector>* const measmodel,
00127                           const MatrixWrapper::ColumnVector& z,
00128      const MatrixWrapper::ColumnVector& s);
00129     };
00130 
00135     enum filterTypes{
00136   NLIN_PROCESS_LIN_MEASURE,
00137   LIN_PROCESS_NLIN_MEASURE,
00138                 NLIN_PROCESS_NLIN_MEASURE,
00139                 LIN_PROCESS_LIN_MEASURE
00140     };
00141     class genericKF
00142     {
00143     private:
00144         filterTypes   filterType;   // The filter type
00145         MatrixWrapper::ColumnVector preStateEst;  // current state estimation ("a priori")
00146         MatrixWrapper::ColumnVector postStateEst; // current state estimation ("a posteriori")
00147         int num_conditional_arguments; // conditional arguments of the conditional pdf
00148         // NON LINEAR MODELS
00149         processModel* process;      // Non linear process model
00150         measModel*    meas;         // Non linear measurement model
00151         // LINEAR MODELS
00152         BFL::LinearAnalyticConditionalGaussian * linear_process; // Linear process model
00153         BFL::LinearAnalyticConditionalGaussian * linear_meas;    // Linear measurement model
00154 
00155         BFL::AnalyticSystemModelGaussianUncertainty* _processModelNonLin;       // arguments of the filter process non lin
00156         BFL::AnalyticMeasurementModelGaussianUncertainty* _measModelNonLin;     // arguments of the filter meas non lin
00157         BFL::LinearAnalyticSystemModelGaussianUncertainty* _processModelLin;    // arguments of the filter meas lin
00158         BFL::LinearAnalyticMeasurementModelGaussianUncertainty* _measModelLin;  // arguments of the filter process lin
00159 
00160         BFL::Gaussian* _priorProb;          // Gaussian associated with prior state knowledge
00161         MatrixWrapper::ColumnVector s;  // Internal orocos implementation argument
00162         // External callback functions to update internal Matrices (A = df/dx, W = df/dw)
00163         MatrixWrapper::Matrix (*func_W)(MatrixWrapper::ColumnVector, MatrixWrapper::ColumnVector, double);
00164         MatrixWrapper::Matrix (*func_A)(MatrixWrapper::ColumnVector, MatrixWrapper::ColumnVector, double);
00165         MatrixWrapper::ColumnVector (*projState)(MatrixWrapper::ColumnVector,MatrixWrapper::ColumnVector,double);
00166     public:
00168         genericKF(){};
00169         // Non linear process model, linear measure model, non additive process noise (EKF)
00170         genericKF(MatrixWrapper::ColumnVector prior_mu,MatrixWrapper::SymmetricMatrix prior_sigma,
00171                 MatrixWrapper::ColumnVector Pro_mu,MatrixWrapper::SymmetricMatrix Pro_sigma,
00172                 MatrixWrapper::ColumnVector Meas_mu,MatrixWrapper::SymmetricMatrix Meas_sigma,
00173                 int numConditionalArguments,
00174                 MatrixWrapper::Matrix (*eval_A)(MatrixWrapper::ColumnVector , MatrixWrapper::ColumnVector , double ),
00175                 MatrixWrapper::Matrix (*eval_W)(MatrixWrapper::ColumnVector , MatrixWrapper::ColumnVector , double ),
00176                 MatrixWrapper::Matrix dh_dx,
00177                 MatrixWrapper::ColumnVector (*funcState)(MatrixWrapper::ColumnVector , MatrixWrapper::ColumnVector , double ));
00178         // Linear process model, linear measure model, additive process noise (KF)
00179         genericKF(MatrixWrapper::ColumnVector prior_mu,MatrixWrapper::SymmetricMatrix prior_sigma,
00180                 MatrixWrapper::ColumnVector Pro_mu,MatrixWrapper::SymmetricMatrix Pro_sigma,
00181                 MatrixWrapper::ColumnVector Meas_mu,MatrixWrapper::SymmetricMatrix Meas_sigma,
00182                 vector<MatrixWrapper::Matrix> AB,
00183                 MatrixWrapper::Matrix dh_dx);
00184         // TODO - constructors for the other types
00185 
00186         // Filter main loop
00187         bool Update(MatrixWrapper::ColumnVector u, MatrixWrapper::ColumnVector z, double T_sample);      // Time update --> Measurement update
00188         bool revUpdate(MatrixWrapper::ColumnVector u, MatrixWrapper::ColumnVector z, double T_sample);   // Measurement update --> Time update
00189 
00190         EKF* _EKF; // A simple wrapper of EKF to get acces to protected members
00191         BFL::KalmanFilter* _KF;
00192         // brief get/set methods for internal members
00193         inline BFL::AnalyticSystemModelGaussianUncertainty* getProcessModel(){return _processModelNonLin;};
00194         inline BFL::AnalyticMeasurementModelGaussianUncertainty* getMeasModel(){return _measModelNonLin;};
00195         inline BFL::LinearAnalyticSystemModelGaussianUncertainty* getProcessModelLin(){return _processModelLin;};
00196         inline BFL::LinearAnalyticMeasurementModelGaussianUncertainty* getMeasModelLin(){return _measModelLin;};
00197         inline MatrixWrapper::ColumnVector getPreStateEst(){return preStateEst;};
00198         inline void setPreStateEst(MatrixWrapper::ColumnVector input){preStateEst = input;};
00199         inline MatrixWrapper::ColumnVector getPostStateEst(){return postStateEst;};
00200         inline void setPostStateEst(MatrixWrapper::ColumnVector input){postStateEst = input;};
00201         //tramite selector scelgo cosa modificare:
00202         //1)sel=0 model uncertainties
00203         //2)sel=1 meas uncertainties
00204         //3)sel=2 prior
00205         static void update_uncertainties(MatrixWrapper::ColumnVector in_mu,MatrixWrapper::SymmetricMatrix in_Cov,
00206                                         std::vector<MatrixWrapper::ColumnVector> & mu,std::vector<MatrixWrapper::SymmetricMatrix> & sigma,int selctor){};
00207         static void update_uncertainties(MatrixWrapper::ColumnVector in_mu,std::vector<MatrixWrapper::ColumnVector> & mu,int selctor){};
00208         static void update_uncertainties(MatrixWrapper::SymmetricMatrix in_Cov,std::vector<MatrixWrapper::SymmetricMatrix> & sigma,int selctor){};
00209 
00210          genericKF & operator = (const genericKF & copy)
00211 {
00212      printf("sono dentro l operatore =\n");
00213      if (this != &copy)
00214    {
00215      switch(copy.filterType)
00216     {
00217      case NLIN_PROCESS_LIN_MEASURE :
00218      delete process;
00219      delete linear_meas;
00220      delete _processModelNonLin;
00221      delete _measModelLin;
00222      delete _priorProb;
00223      delete _EKF;
00224      
00225 
00226      num_conditional_arguments=copy.num_conditional_arguments;
00227      process = new processModel(*(copy.process));
00228      _processModelNonLin = new BFL::AnalyticSystemModelGaussianUncertainty(process);
00229      linear_meas = new BFL::LinearAnalyticConditionalGaussian(*(copy.linear_meas));
00230      _measModelLin = new BFL::LinearAnalyticMeasurementModelGaussianUncertainty(linear_meas);
00231      _priorProb = new BFL::Gaussian(*(copy._priorProb));
00232      _EKF = new EKF(_priorProb);
00233      func_W=copy.func_W;
00234      func_A=copy.func_A;
00235      projState=copy.projState;
00236      break;
00237      case LIN_PROCESS_LIN_MEASURE :
00238      delete linear_process;
00239      delete linear_meas;
00240      delete _processModelLin;
00241      delete _measModelLin;
00242      delete _priorProb;
00243      delete _EKF;
00244      num_conditional_arguments=copy.num_conditional_arguments;
00245      linear_process = new BFL::LinearAnalyticConditionalGaussian(*(copy.linear_process));
00246      _processModelNonLin = new BFL::LinearAnalyticSystemModelGaussianUncertainty(linear_process);
00247      linear_meas = new BFL::LinearAnalyticConditionalGaussian(*(copy.linear_meas));
00248      _measModelLin = new BFL::LinearAnalyticMeasurementModelGaussianUncertainty(linear_meas);
00249      _priorProb = new BFL::Gaussian(*(copy._priorProb));
00250      _EKF = new EKF(_priorProb);
00251      break;
00252 
00253      case LIN_PROCESS_NLIN_MEASURE :
00254 
00255      break;
00256 
00257      case NLIN_PROCESS_NLIN_MEASURE :
00258 
00259      break;
00260     }
00261 
00262    }
00263     return  *this;
00264 }
00265     };
00266 
00267     
00268 
00269 
00270  /* @} */
00271 }; //end namespace MipAlgorithms
00272 
00273 #endif

Generated on Mon Feb 20 07:01:06 2017 for MIP by  doxygen 1.5.6