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

Revision 1240, 8.3 kB (checked in by smidl, 14 years ago)

prechod na int16

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