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

Revision 1311, 10.7 kB (checked in by smidl, 13 years ago)

ekf2

  • 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 Uncertaint16y
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_Q15.h"
22#include "parametry_motoru.h"
23
24using namespace bdm;
25
26double minQ(double Q);
27
28void mat_to_int16(const imat &M, int16 *I);
29void vec_to_int16(const ivec &v, int16 *I);
30void UDtof(const mat &U, const vec &D, imat &Uf, ivec &Df, const vec &xref);
31
32#ifdef XXX
33/*!
34\brief Extended Kalman Filter with full matrices in fixed point16 arithmetic
35
36An approximation of the exact Bayesian filter with Gaussian noices and non-linear evolutions of their mean.
37*/
38class EKFfixed : public BM {
39public:
40void init_ekf(double Tv);
41void ekf(double ux, double uy, double isxd, double isyd);
42
43/* Declaration of local functions */
44void prediction(int16 *ux);
45void correction(void);
46void update_psi(void);
47
48/* Constants - definovat jako konstanty ?? ?kde je vyhodnejsi aby v pameti byli?*/
49 int16 Q[16]; /* matrix [4,4] */
50 int16 R[4]; /* matrix [2,2] */
51
52 int16 x_est[4];
53 int16 x_pred[4];
54 int16 P_pred[16]; /* matrix [4,4] */
55 int16 P_est[16]; /* matrix [4,4] */
56 int16 Y_mes[2];
57 int16 ukalm[2];
58 int16 Kalm[8]; /* matrix [5,2] */
59
60 int16 PSI[16]; /* matrix [4,4] */
61
62 int16 temp15a[16];
63
64 int16 cA, cB, cC, cG, cH;  // cD, cE, cF, cI ... nepouzivane
65
66 int32 temp30a[4]; /* matrix [2,2] - temporary matrix for inversion */
67 enorm<fsqmat> E;
68 mat Ry;
69 
70public:
71        //! Default constructor
72        EKFfixed ():BM(),E(),Ry(2,2){
73                int16 i;
74                for(i=0;i<16;i++){Q[i]=0;}
75                for(i=0;i<4;i++){R[i]=0;}
76
77                for(i=0;i<4;i++){x_est[i]=0;}
78                for(i=0;i<4;i++){x_pred[i]=0;}
79                for(i=0;i<16;i++){P_pred[i]=0;}
80                for(i=0;i<16;i++){P_est[i]=0;}
81                P_est[0]=0x7FFF;
82                P_est[5]=0x7FFF;
83                P_est[10]=0x7FFF;
84                P_est[15]=0x7FFF;
85                for(i=0;i<2;i++){Y_mes[i]=0;}
86                for(i=0;i<2;i++){ukalm[i]=0;}
87                for(i=0;i<8;i++){Kalm[i]=0;}
88
89                for(i=0;i<16;i++){PSI[i]=0;}
90
91                set_dim(4);
92                E._mu()=zeros(4);
93                E._R()=zeros(4,4);
94                init_ekf(0.000125);
95        };
96        //! Here dt = [yt;ut] of appropriate dimensions
97        void bayes ( const vec &yt, const vec &ut );
98        //!dummy!
99        const epdf& posterior() const {return E;};
100       
101};
102
103UIREGISTER(EKFfixed);
104
105#endif
106
107//! EKF for testing q44
108class EKFtest: public EKF_UD{
109        void bayes ( const vec &yt, const vec &cond ) {
110                EKF_UD::bayes(yt,cond);
111                vec D =  prior()._R()._D();
112               
113                if (D(3)>10) D(3) = 10;
114               
115                prior()._R().__D()=D;
116        }
117};
118UIREGISTER(EKFtest);
119
120/*!
121\brief Extended Kalman Filter with UD matrices in fixed point16 arithmetic
122
123An approximation of the exact Bayesian filter with Gaussian noices and non-linear evolutions of their mean.
124*/
125class EKFfixedUD : public BM {
126        public:
127                LOG_LEVEL(EKFfixedUD,logU, logG, logD, logA, logP);
128               
129                void init_ekf(double Tv);
130                void ekf(double ux, double uy, double isxd, double isyd);
131                               
132                /* Constants - definovat jako konstanty ?? ?kde je vyhodnejsi aby v pameti byli?*/
133                int16 Q[16]; /* matrix [4,4] */
134                int16 R[4]; /* matrix [2,2] */
135               
136                int16 x_est[4]; /* estimate and prediction */
137               
138                int16 PSI[16]; /* matrix [4,4] */
139                int16 PSIU[16]; /* matrix PIS*U, [4,4] */
140               
141                int16 Uf[16]; // upper triangular of covariance (inplace)
142                int16 Df[4];  // diagonal covariance
143                int16 Dfold[4]; // temp of D
144                int16 G[16];  // temp for bierman
145               
146                int16 cA, cB, cC, cG, cH;  // cD, cE, cF, cI ... nepouzivane
147               
148                enorm<fsqmat> E;
149                mat Ry;
150               
151        public:
152                //! Default constructor
153                EKFfixedUD ():BM(),E(),Ry(2,2){
154                        int16 i;
155                        for(i=0;i<16;i++){Q[i]=0;}
156                        for(i=0;i<4;i++){R[i]=0;}
157                       
158                        for(i=0;i<4;i++){x_est[i]=0;}
159                        for(i=0;i<16;i++){Uf[i]=0;}
160                        for(i=0;i<4;i++){Df[i]=0;}
161                        for(i=0;i<16;i++){G[i]=0;}
162                        for(i=0;i<4;i++){Dfold[i]=0;}
163                       
164                        for(i=0;i<16;i++){PSI[i]=0;}
165                       
166                        set_dim(4);
167                        dimy = 2;
168                        dimc = 2;
169                        E._mu()=zeros(4);
170                        E._R()=zeros(4,4);
171                        init_ekf(0.000125);
172                };
173                //! Here dt = [yt;ut] of appropriate dimensions
174                void bayes ( const vec &yt, const vec &ut );
175                //!dummy!
176                const epdf& posterior() const {return E;};
177                void log_register(logger &L, const string &prefix){
178                        BM::log_register ( L, prefix );
179                       
180                                L.add_vector ( log_level, logG, RV("G",16), prefix );
181                                L.add_vector ( log_level, logU, RV ("U", 16 ), prefix );
182                                L.add_vector ( log_level, logD, RV ("D", 4 ), prefix );
183                                L.add_vector ( log_level, logA, RV ("A", 16 ), prefix );
184                                L.add_vector ( log_level, logP, RV ("P", 16 ), prefix );
185                               
186                };
187                //void from_setting();
188};
189
190UIREGISTER(EKFfixedUD);
191
192/*!
193 * \brief Extended Kalman Filter with UD matrices in fixed point16 arithmetic
194 *
195 * An approximation of the exact Bayesian filter with Gaussian noices and non-linear evolutions of their mean.
196 */
197class EKFfixedUD2 : public BM {
198public:
199        LOG_LEVEL(EKFfixedUD2,logU, logG, logD, logA, logC, logP);
200       
201        void init_ekf2(double Tv);
202        void ekf2(double ux, double uy, double isxd, double isyd);
203       
204        /* Constants - definovat jako konstanty ?? ?kde je vyhodnejsi aby v pameti byli?*/
205        int16 Q[4]; /* matrix [4,4] */
206        int16 R[4]; /* matrix [2,2] */
207       
208        int16 x_est[2]; /* estimate and prediction */
209        int16 y_est[2]; /* estimate and prediction */
210        int16 y_old[2]; /* estimate and prediction */
211       
212        int16 PSI[4]; /* matrix [4,4] */
213        int16 PSIU[4]; /* matrix PIS*U, [4,4] */
214        int16 C[4]; /* matrix [4,4] */
215       
216        int16 Uf[4]; // upper triangular of covariance (inplace)
217        int16 Df[2];  // diagonal covariance
218        int16 Dfold[2]; // temp of D
219        int16 G[4];  // temp for bierman
220       
221        int16 cA, cB, cC, cG, cH;  // cD, cE, cF, cI ... nepouzivane
222       
223        enorm<fsqmat> E;
224        mat Ry;
225       
226public:
227        //! Default constructor
228        EKFfixedUD2 ():BM(),E(),Ry(2,2){
229                int16 i;
230                for(i=0;i<4;i++){Q[i]=0;}
231                for(i=0;i<4;i++){R[i]=0;}
232               
233                for(i=0;i<2;i++){x_est[i]=0;}
234                for(i=0;i<2;i++){y_est[i]=0;}
235                for(i=0;i<2;i++){y_old[i]=0;}
236                for(i=0;i<4;i++){Uf[i]=0;}
237                for(i=0;i<2;i++){Df[i]=0;}
238                for(i=0;i<4;i++){G[i]=0;}
239                for(i=0;i<2;i++){Dfold[i]=0;}
240               
241                for(i=0;i<4;i++){PSI[i]=0;}
242                for(i=0;i<4;i++){C[i]=0;}
243               
244                set_dim(2);
245                dimc = 2;
246                dimy = 2;
247                E._mu()=zeros(2);
248                E._R()=zeros(2,2);
249                init_ekf2(0.000125);
250        };
251        //! Here dt = [yt;ut] of appropriate dimensions
252        void bayes ( const vec &yt, const vec &ut );
253        //!dummy!
254        const epdf& posterior() const {return E;};
255        void log_register(logger &L, const string &prefix){
256                BM::log_register ( L, prefix );
257               
258                L.add_vector ( log_level, logG, RV("G2",4), prefix );
259                L.add_vector ( log_level, logU, RV ("U2", 4 ), prefix );
260                L.add_vector ( log_level, logD, RV ("D2", 2 ), prefix );
261                L.add_vector ( log_level, logA, RV ("A2", 4 ), prefix );
262                L.add_vector ( log_level, logC, RV ("C2", 4 ), prefix );
263                L.add_vector ( log_level, logP, RV ("P2", 4 ), prefix );
264               
265        };
266        //void from_setting();
267};
268
269UIREGISTER(EKFfixedUD2);
270
271/*!
272 * \brief Extended Kalman Filter with Chol matrices in fixed point16 arithmetic
273 *
274 * An approximation of the exact Bayesian filter with Gaussian noices and non-linear evolutions of their mean.
275 */
276class EKFfixedCh : public BM {
277public:
278        LOG_LEVEL(EKFfixedCh,logCh, logA, logP);
279       
280        void init_ekf(double Tv);
281        void ekf(double ux, double uy, double isxd, double isyd);
282       
283        /* Constants - definovat jako konstanty ?? ?kde je vyhodnejsi aby v pameti byli?*/
284        int16 Q[16]; /* matrix [4,4] */
285        int16 R[4]; /* matrix [2,2] */
286       
287        int16 x_est[4]; /* estimate and prediction */
288       
289        int16 PSI[16]; /* matrix [4,4] */
290        int16 PSICh[16]; /* matrix PIS*U, [4,4] */
291       
292        int16 Chf[16]; // upper triangular of covariance (inplace)
293       
294        int16 cA, cB, cC, cG, cH;  // cD, cE, cF, cI ... nepouzivane
295       
296        enorm<chmat> E;
297        mat Ry;
298       
299public:
300        //! Default constructor
301        EKFfixedCh ():BM(),E(),Ry(2,2){
302                int16 i;
303                for(i=0;i<16;i++){Q[i]=0;}
304                for(i=0;i<4;i++){R[i]=0;}
305               
306                for(i=0;i<4;i++){x_est[i]=0;}
307                for(i=0;i<16;i++){Chf[i]=0;}
308               
309                for(i=0;i<16;i++){PSI[i]=0;}
310               
311                set_dim(4);
312                E._mu()=zeros(4);
313                E._R()=zeros(4,4);
314                init_ekf(0.000125);
315        };
316        //! Here dt = [yt;ut] of appropriate dimensions
317        void bayes ( const vec &yt, const vec &ut );
318        //!dummy!
319        const epdf& posterior() const {return E;};
320        void log_register(logger &L, const string &prefix){
321                BM::log_register ( L, prefix );
322               
323                L.add_vector ( log_level, logCh, RV ("Ch", 16 ), prefix );
324                L.add_vector ( log_level, logA, RV ("A", 16 ), prefix );
325                L.add_vector ( log_level, logP, RV ("P", 16 ), prefix );
326               
327        };
328        //void from_setting();
329};
330
331UIREGISTER(EKFfixedCh);
332
333
334//! EKF for comparison of EKF_UD with its fixed-point16 implementation
335class EKF_UDfix : public BM {
336        protected:
337                //! logger
338                LOG_LEVEL(EKF_UDfix,logU, logG);
339                //! Internal Model f(x,u)
340                shared_ptr<diffbifn> pfxu;
341               
342                //! Observation Model h(x,u)
343                shared_ptr<diffbifn> phxu;
344               
345                //! U part
346                mat U;
347                //! D part
348                vec D;
349                               
350                mat A;
351                mat C;
352                mat Q;
353                vec R;
354               
355                enorm<ldmat> est;
356               
357               
358        public:
359               
360                //! copy constructor duplicated
361                EKF_UDfix* _copy() const {
362                        return new EKF_UDfix(*this);
363                }
364               
365                const enorm<ldmat>& posterior()const{return est;};
366               
367                enorm<ldmat>& prior() {
368                        return const_cast<enorm<ldmat>&>(posterior());
369                }
370               
371                EKF_UDfix(){}
372               
373               
374                EKF_UDfix(const EKF_UDfix &E0): pfxu(E0.pfxu),phxu(E0.phxu), U(E0.U), D(E0.D){}
375               
376                //! Set nonlinear functions for mean values and covariance matrices.
377                void set_parameters ( const shared_ptr<diffbifn> &pfxu, const shared_ptr<diffbifn> &phxu, const mat Q0, const vec R0 );
378               
379                //! Here dt = [yt;ut] of appropriate dimensions
380                void bayes ( const vec &yt, const vec &cond = empty_vec );
381               
382                void log_register ( bdm::logger& L, const string& prefix ){
383                        BM::log_register ( L, prefix );
384                       
385                        if ( log_level[logU] )
386                                L.add_vector ( log_level, logU, RV ( dimension()*dimension() ), prefix );
387                        if ( log_level[logG] )
388                                L.add_vector ( log_level, logG, RV ( dimension()*dimension() ), prefix );
389                       
390                }
391                /*! Create object from the following structure
392               
393                \code
394                class = 'EKF_UD';
395                OM = configuration of bdm::diffbifn;    % any offspring of diffbifn, bdm::diffbifn::from_setting
396                IM = configuration of bdm::diffbifn;    % any offspring of diffbifn, bdm::diffbifn::from_setting
397                dQ = [...];                             % vector containing diagonal of Q
398                dR = [...];                             % vector containing diagonal of R
399                --- optional fields ---
400                mu0 = [...];                            % vector of statistics mu0
401                dP0 = [...];                            % vector containing diagonal of P0
402                -- or --
403                P0 = [...];                             % full matrix P0
404                --- inherited fields ---
405                bdm::BM::from_setting
406                \endcode
407                If the optional fields are not given, they will be filled as follows:
408                \code
409                mu0 = [0,0,0,....];                     % empty statistics
410                P0 = eye( dim );             
411                \endcode
412                */
413                void from_setting ( const Setting &set );
414               
415                void validate() {};
416                // TODO dodelat void to_setting( Setting &set ) const;
417               
418};
419UIREGISTER(EKF_UDfix);
420
421
422
423
424#endif // KF_H
425
Note: See TracBrowser for help on using the browser.