root/applications/pmsm/simulator_zdenek/ekf_example/ekf_obj.h @ 1174

Revision 1174, 4.8 kB (checked in by smidl, 14 years ago)

development of fixed Bierman code

  • Property svn:eol-style set to native
Line 
1/*!
2  \file
3  \brief Bayesian Filtering for linear Gaussian models (Kalman Filter) and extensions
4  \author Vaclav Smidl.
5
6  -----------------------------------
7  BDM++ - C++ library for Bayesian Decision Making under Uncertainty
8
9  Using IT++ for numerical operations
10  -----------------------------------
11*/
12
13#ifndef EKFfix_H
14#define EKFfix_H
15
16
17#include <estim/kalman.h>
18#include "fixed.h"
19#include "matrix.h"
20#include "matrix_vs.h"
21#include "reference.h"
22#include "parametry_motoru.h"
23
24using namespace bdm;
25
26double minQ(double Q);
27
28void mat_to_int(const imat &M, int *I);
29void vec_to_int(const ivec &v, int *I);
30
31/*!
32\brief Extended Kalman Filter with full matrices in fixed point arithmetic
33
34An approximation of the exact Bayesian filter with Gaussian noices and non-linear evolutions of their mean.
35*/
36class EKFfixed : public BM {
37public:
38void init_ekf(double Tv);
39void ekf(double ux, double uy, double isxd, double isyd);
40
41/* Declaration of local functions */
42void prediction(int *ux);
43void correction(void);
44void update_psi(void);
45
46/* Constants - definovat jako konstanty ?? ?kde je vyhodnejsi aby v pameti byli?*/
47 int Q[16]; /* matrix [4,4] */
48 int R[4]; /* matrix [2,2] */
49
50 int x_est[4];
51 int x_pred[4];
52 int P_pred[16]; /* matrix [4,4] */
53 int P_est[16]; /* matrix [4,4] */
54 int Y_mes[2];
55 int ukalm[2];
56 int Kalm[8]; /* matrix [5,2] */
57
58 int PSI[16]; /* matrix [4,4] */
59
60 int temp15a[16];
61
62 int cA, cB, cC, cG, cH;  // cD, cE, cF, cI ... nepouzivane
63
64 long temp30a[4]; /* matrix [2,2] - temporary matrix for inversion */
65 enorm<fsqmat> E;
66 mat Ry;
67 
68public:
69        //! Default constructor
70        EKFfixed ():BM(),E(),Ry(2,2){
71                int i;
72                for(i=0;i<16;i++){Q[i]=0;}
73                for(i=0;i<4;i++){R[i]=0;}
74
75                for(i=0;i<4;i++){x_est[i]=0;}
76                for(i=0;i<4;i++){x_pred[i]=0;}
77                for(i=0;i<16;i++){P_pred[i]=0;}
78                for(i=0;i<16;i++){P_est[i]=0;}
79                P_est[0]=0x7FFF;
80                P_est[5]=0x7FFF;
81                P_est[10]=0x7FFF;
82                P_est[15]=0x7FFF;
83                for(i=0;i<2;i++){Y_mes[i]=0;}
84                for(i=0;i<2;i++){ukalm[i]=0;}
85                for(i=0;i<8;i++){Kalm[i]=0;}
86
87                for(i=0;i<16;i++){PSI[i]=0;}
88
89                set_dim(4);
90                E._mu()=zeros(4);
91                E._R()=zeros(4,4);
92                init_ekf(0.000125);
93        };
94        //! Here dt = [yt;ut] of appropriate dimensions
95        void bayes ( const vec &yt, const vec &ut );
96        //!dummy!
97        const epdf& posterior() const {return E;};
98       
99};
100
101UIREGISTER(EKFfixed);
102
103//! EKF for comparison of EKF_UD with its fixed-point implementation
104class EKF_UDfix : public BM {
105        protected:
106                //! logger
107                LOG_LEVEL(EKF_UDfix,logU, logG);
108                //! Internal Model f(x,u)
109                shared_ptr<diffbifn> pfxu;
110               
111                //! Observation Model h(x,u)
112                shared_ptr<diffbifn> phxu;
113               
114                //! U part
115                mat U;
116                //! D part
117                vec D;
118               
119                int Uf[25];
120                int Df[5];
121                int Dfold[5];
122               
123                mat A;
124                mat C;
125                mat Q;
126                vec R;
127               
128                int PSI[25];
129                int PSIU[25];
130                int Gf[25];
131
132                enorm<ldmat> est;
133               
134               
135        public:
136               
137                //! copy constructor duplicated
138                EKF_UDfix* _copy() const {
139                        return new EKF_UDfix(*this);
140                }
141               
142                const enorm<ldmat>& posterior()const{return est;};
143               
144                enorm<ldmat>& prior() {
145                        return const_cast<enorm<ldmat>&>(posterior());
146                }
147               
148                EKF_UDfix(){}
149               
150               
151                EKF_UDfix(const EKF_UDfix &E0): pfxu(E0.pfxu),phxu(E0.phxu), U(E0.U), D(E0.D){}
152               
153                //! Set nonlinear functions for mean values and covariance matrices.
154                void set_parameters ( const shared_ptr<diffbifn> &pfxu, const shared_ptr<diffbifn> &phxu, const mat Q0, const vec R0 );
155               
156                //! Here dt = [yt;ut] of appropriate dimensions
157                void bayes ( const vec &yt, const vec &cond = empty_vec );
158               
159                void log_register ( bdm::logger& L, const string& prefix ){
160                        BM::log_register ( L, prefix );
161                       
162                        if ( log_level[logU] )
163                                L.add_vector ( log_level, logU, RV ( dimension()*dimension() ), prefix );
164                        if ( log_level[logG] )
165                                L.add_vector ( log_level, logG, RV ( dimension()*dimension() ), prefix );
166                       
167                }
168                /*! Create object from the following structure
169               
170                \code
171                class = 'EKF_UD';
172                OM = configuration of bdm::diffbifn;    % any offspring of diffbifn, bdm::diffbifn::from_setting
173                IM = configuration of bdm::diffbifn;    % any offspring of diffbifn, bdm::diffbifn::from_setting
174                dQ = [...];                             % vector containing diagonal of Q
175                dR = [...];                             % vector containing diagonal of R
176                --- optional fields ---
177                mu0 = [...];                            % vector of statistics mu0
178                dP0 = [...];                            % vector containing diagonal of P0
179                -- or --
180                P0 = [...];                             % full matrix P0
181                --- inherited fields ---
182                bdm::BM::from_setting
183                \endcode
184                If the optional fields are not given, they will be filled as follows:
185                \code
186                mu0 = [0,0,0,....];                     % empty statistics
187                P0 = eye( dim );             
188                \endcode
189                */
190                void from_setting ( const Setting &set );
191               
192                void validate() {};
193                // TODO dodelat void to_setting( Setting &set ) const;
194               
195};
196UIREGISTER(EKF_UDfix);
197
198
199#endif // KF_H
200
Note: See TracBrowser for help on using the browser.