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

Revision 1305, 10.4 kB (checked in by smidl, 13 years ago)

object for reduced order filter

  • 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, 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       
210        int16 PSI[4]; /* matrix [4,4] */
211        int16 PSIU[4]; /* matrix PIS*U, [4,4] */
212       
213        int16 Uf[4]; // upper triangular of covariance (inplace)
214        int16 Df[2];  // diagonal covariance
215        int16 Dfold[2]; // temp of D
216        int16 G[4];  // temp for bierman
217       
218        int16 cA, cB, cC, cG, cH;  // cD, cE, cF, cI ... nepouzivane
219       
220        enorm<fsqmat> E;
221        mat Ry;
222       
223public:
224        //! Default constructor
225        EKFfixedUD2 ():BM(),E(),Ry(2,2){
226                int16 i;
227                for(i=0;i<4;i++){Q[i]=0;}
228                for(i=0;i<4;i++){R[i]=0;}
229               
230                for(i=0;i<2;i++){x_est[i]=0;}
231                for(i=0;i<4;i++){Uf[i]=0;}
232                for(i=0;i<2;i++){Df[i]=0;}
233                for(i=0;i<4;i++){G[i]=0;}
234                for(i=0;i<2;i++){Dfold[i]=0;}
235               
236                for(i=0;i<4;i++){PSI[i]=0;}
237               
238                set_dim(2);
239                E._mu()=zeros(2);
240                E._R()=zeros(2,2);
241                init_ekf2(0.000125);
242        };
243        //! Here dt = [yt;ut] of appropriate dimensions
244        void bayes ( const vec &yt, const vec &ut );
245        //!dummy!
246        const epdf& posterior() const {return E;};
247        void log_register(logger &L, const string &prefix){
248                BM::log_register ( L, prefix );
249               
250                L.add_vector ( log_level, logG, RV("G",4), prefix );
251                L.add_vector ( log_level, logU, RV ("U", 4 ), prefix );
252                L.add_vector ( log_level, logD, RV ("D", 2 ), prefix );
253                L.add_vector ( log_level, logA, RV ("A", 4 ), prefix );
254                L.add_vector ( log_level, logP, RV ("P", 4 ), prefix );
255               
256        };
257        //void from_setting();
258};
259
260UIREGISTER(EKFfixedUD2);
261
262/*!
263 * \brief Extended Kalman Filter with Chol matrices in fixed point16 arithmetic
264 *
265 * An approximation of the exact Bayesian filter with Gaussian noices and non-linear evolutions of their mean.
266 */
267class EKFfixedCh : public BM {
268public:
269        LOG_LEVEL(EKFfixedCh,logCh, logA, logP);
270       
271        void init_ekf(double Tv);
272        void ekf(double ux, double uy, double isxd, double isyd);
273       
274        /* Constants - definovat jako konstanty ?? ?kde je vyhodnejsi aby v pameti byli?*/
275        int16 Q[16]; /* matrix [4,4] */
276        int16 R[4]; /* matrix [2,2] */
277       
278        int16 x_est[4]; /* estimate and prediction */
279       
280        int16 PSI[16]; /* matrix [4,4] */
281        int16 PSICh[16]; /* matrix PIS*U, [4,4] */
282       
283        int16 Chf[16]; // upper triangular of covariance (inplace)
284       
285        int16 cA, cB, cC, cG, cH;  // cD, cE, cF, cI ... nepouzivane
286       
287        enorm<chmat> E;
288        mat Ry;
289       
290public:
291        //! Default constructor
292        EKFfixedCh ():BM(),E(),Ry(2,2){
293                int16 i;
294                for(i=0;i<16;i++){Q[i]=0;}
295                for(i=0;i<4;i++){R[i]=0;}
296               
297                for(i=0;i<4;i++){x_est[i]=0;}
298                for(i=0;i<16;i++){Chf[i]=0;}
299               
300                for(i=0;i<16;i++){PSI[i]=0;}
301               
302                set_dim(4);
303                E._mu()=zeros(4);
304                E._R()=zeros(4,4);
305                init_ekf(0.000125);
306        };
307        //! Here dt = [yt;ut] of appropriate dimensions
308        void bayes ( const vec &yt, const vec &ut );
309        //!dummy!
310        const epdf& posterior() const {return E;};
311        void log_register(logger &L, const string &prefix){
312                BM::log_register ( L, prefix );
313               
314                L.add_vector ( log_level, logCh, RV ("Ch", 16 ), prefix );
315                L.add_vector ( log_level, logA, RV ("A", 16 ), prefix );
316                L.add_vector ( log_level, logP, RV ("P", 16 ), prefix );
317               
318        };
319        //void from_setting();
320};
321
322UIREGISTER(EKFfixedCh);
323
324
325//! EKF for comparison of EKF_UD with its fixed-point16 implementation
326class EKF_UDfix : public BM {
327        protected:
328                //! logger
329                LOG_LEVEL(EKF_UDfix,logU, logG);
330                //! Internal Model f(x,u)
331                shared_ptr<diffbifn> pfxu;
332               
333                //! Observation Model h(x,u)
334                shared_ptr<diffbifn> phxu;
335               
336                //! U part
337                mat U;
338                //! D part
339                vec D;
340                               
341                mat A;
342                mat C;
343                mat Q;
344                vec R;
345               
346                enorm<ldmat> est;
347               
348               
349        public:
350               
351                //! copy constructor duplicated
352                EKF_UDfix* _copy() const {
353                        return new EKF_UDfix(*this);
354                }
355               
356                const enorm<ldmat>& posterior()const{return est;};
357               
358                enorm<ldmat>& prior() {
359                        return const_cast<enorm<ldmat>&>(posterior());
360                }
361               
362                EKF_UDfix(){}
363               
364               
365                EKF_UDfix(const EKF_UDfix &E0): pfxu(E0.pfxu),phxu(E0.phxu), U(E0.U), D(E0.D){}
366               
367                //! Set nonlinear functions for mean values and covariance matrices.
368                void set_parameters ( const shared_ptr<diffbifn> &pfxu, const shared_ptr<diffbifn> &phxu, const mat Q0, const vec R0 );
369               
370                //! Here dt = [yt;ut] of appropriate dimensions
371                void bayes ( const vec &yt, const vec &cond = empty_vec );
372               
373                void log_register ( bdm::logger& L, const string& prefix ){
374                        BM::log_register ( L, prefix );
375                       
376                        if ( log_level[logU] )
377                                L.add_vector ( log_level, logU, RV ( dimension()*dimension() ), prefix );
378                        if ( log_level[logG] )
379                                L.add_vector ( log_level, logG, RV ( dimension()*dimension() ), prefix );
380                       
381                }
382                /*! Create object from the following structure
383               
384                \code
385                class = 'EKF_UD';
386                OM = configuration of bdm::diffbifn;    % any offspring of diffbifn, bdm::diffbifn::from_setting
387                IM = configuration of bdm::diffbifn;    % any offspring of diffbifn, bdm::diffbifn::from_setting
388                dQ = [...];                             % vector containing diagonal of Q
389                dR = [...];                             % vector containing diagonal of R
390                --- optional fields ---
391                mu0 = [...];                            % vector of statistics mu0
392                dP0 = [...];                            % vector containing diagonal of P0
393                -- or --
394                P0 = [...];                             % full matrix P0
395                --- inherited fields ---
396                bdm::BM::from_setting
397                \endcode
398                If the optional fields are not given, they will be filled as follows:
399                \code
400                mu0 = [0,0,0,....];                     % empty statistics
401                P0 = eye( dim );             
402                \endcode
403                */
404                void from_setting ( const Setting &set );
405               
406                void validate() {};
407                // TODO dodelat void to_setting( Setting &set ) const;
408               
409};
410UIREGISTER(EKF_UDfix);
411
412
413
414
415#endif // KF_H
416
Note: See TracBrowser for help on using the browser.