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

Revision 1294, 8.5 kB (checked in by smidl, 13 years ago)

zmeny v experimentech

  • 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                        E._mu()=zeros(4);
168                        E._R()=zeros(4,4);
169                        init_ekf(0.000125);
170                };
171                //! Here dt = [yt;ut] of appropriate dimensions
172                void bayes ( const vec &yt, const vec &ut );
173                //!dummy!
174                const epdf& posterior() const {return E;};
175                void log_register(logger &L, const string &prefix){
176                        BM::log_register ( L, prefix );
177                       
178                                L.add_vector ( log_level, logG, RV("G",16), prefix );
179                                L.add_vector ( log_level, logU, RV ("U", 16 ), prefix );
180                                L.add_vector ( log_level, logD, RV ("D", 4 ), prefix );
181                                L.add_vector ( log_level, logA, RV ("A", 16 ), prefix );
182                                L.add_vector ( log_level, logP, RV ("P", 16 ), prefix );
183                               
184                };
185                //void from_setting();
186};
187
188UIREGISTER(EKFfixedUD);
189
190/*!
191 * \brief Extended Kalman Filter with Chol matrices in fixed point16 arithmetic
192 *
193 * An approximation of the exact Bayesian filter with Gaussian noices and non-linear evolutions of their mean.
194 */
195class EKFfixedCh : public BM {
196public:
197        LOG_LEVEL(EKFfixedCh,logCh, logA, logP);
198       
199        void init_ekf(double Tv);
200        void ekf(double ux, double uy, double isxd, double isyd);
201       
202        /* Constants - definovat jako konstanty ?? ?kde je vyhodnejsi aby v pameti byli?*/
203        int16 Q[16]; /* matrix [4,4] */
204        int16 R[4]; /* matrix [2,2] */
205       
206        int16 x_est[4]; /* estimate and prediction */
207       
208        int16 PSI[16]; /* matrix [4,4] */
209        int16 PSICh[16]; /* matrix PIS*U, [4,4] */
210       
211        int16 Chf[16]; // upper triangular of covariance (inplace)
212       
213        int16 cA, cB, cC, cG, cH;  // cD, cE, cF, cI ... nepouzivane
214       
215        enorm<chmat> E;
216        mat Ry;
217       
218public:
219        //! Default constructor
220        EKFfixedCh ():BM(),E(),Ry(2,2){
221                int16 i;
222                for(i=0;i<16;i++){Q[i]=0;}
223                for(i=0;i<4;i++){R[i]=0;}
224               
225                for(i=0;i<4;i++){x_est[i]=0;}
226                for(i=0;i<16;i++){Chf[i]=0;}
227               
228                for(i=0;i<16;i++){PSI[i]=0;}
229               
230                set_dim(4);
231                E._mu()=zeros(4);
232                E._R()=zeros(4,4);
233                init_ekf(0.000125);
234        };
235        //! Here dt = [yt;ut] of appropriate dimensions
236        void bayes ( const vec &yt, const vec &ut );
237        //!dummy!
238        const epdf& posterior() const {return E;};
239        void log_register(logger &L, const string &prefix){
240                BM::log_register ( L, prefix );
241               
242                L.add_vector ( log_level, logCh, RV ("Ch", 16 ), prefix );
243                L.add_vector ( log_level, logA, RV ("A", 16 ), prefix );
244                L.add_vector ( log_level, logP, RV ("P", 16 ), prefix );
245               
246        };
247        //void from_setting();
248};
249
250UIREGISTER(EKFfixedCh);
251
252
253//! EKF for comparison of EKF_UD with its fixed-point16 implementation
254class EKF_UDfix : public BM {
255        protected:
256                //! logger
257                LOG_LEVEL(EKF_UDfix,logU, logG);
258                //! Internal Model f(x,u)
259                shared_ptr<diffbifn> pfxu;
260               
261                //! Observation Model h(x,u)
262                shared_ptr<diffbifn> phxu;
263               
264                //! U part
265                mat U;
266                //! D part
267                vec D;
268                               
269                mat A;
270                mat C;
271                mat Q;
272                vec R;
273               
274                enorm<ldmat> est;
275               
276               
277        public:
278               
279                //! copy constructor duplicated
280                EKF_UDfix* _copy() const {
281                        return new EKF_UDfix(*this);
282                }
283               
284                const enorm<ldmat>& posterior()const{return est;};
285               
286                enorm<ldmat>& prior() {
287                        return const_cast<enorm<ldmat>&>(posterior());
288                }
289               
290                EKF_UDfix(){}
291               
292               
293                EKF_UDfix(const EKF_UDfix &E0): pfxu(E0.pfxu),phxu(E0.phxu), U(E0.U), D(E0.D){}
294               
295                //! Set nonlinear functions for mean values and covariance matrices.
296                void set_parameters ( const shared_ptr<diffbifn> &pfxu, const shared_ptr<diffbifn> &phxu, const mat Q0, const vec R0 );
297               
298                //! Here dt = [yt;ut] of appropriate dimensions
299                void bayes ( const vec &yt, const vec &cond = empty_vec );
300               
301                void log_register ( bdm::logger& L, const string& prefix ){
302                        BM::log_register ( L, prefix );
303                       
304                        if ( log_level[logU] )
305                                L.add_vector ( log_level, logU, RV ( dimension()*dimension() ), prefix );
306                        if ( log_level[logG] )
307                                L.add_vector ( log_level, logG, RV ( dimension()*dimension() ), prefix );
308                       
309                }
310                /*! Create object from the following structure
311               
312                \code
313                class = 'EKF_UD';
314                OM = configuration of bdm::diffbifn;    % any offspring of diffbifn, bdm::diffbifn::from_setting
315                IM = configuration of bdm::diffbifn;    % any offspring of diffbifn, bdm::diffbifn::from_setting
316                dQ = [...];                             % vector containing diagonal of Q
317                dR = [...];                             % vector containing diagonal of R
318                --- optional fields ---
319                mu0 = [...];                            % vector of statistics mu0
320                dP0 = [...];                            % vector containing diagonal of P0
321                -- or --
322                P0 = [...];                             % full matrix P0
323                --- inherited fields ---
324                bdm::BM::from_setting
325                \endcode
326                If the optional fields are not given, they will be filled as follows:
327                \code
328                mu0 = [0,0,0,....];                     % empty statistics
329                P0 = eye( dim );             
330                \endcode
331                */
332                void from_setting ( const Setting &set );
333               
334                void validate() {};
335                // TODO dodelat void to_setting( Setting &set ) const;
336               
337};
338UIREGISTER(EKF_UDfix);
339
340
341
342
343#endif // KF_H
344
Note: See TracBrowser for help on using the browser.