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

Revision 1226, 8.2 kB (checked in by smidl, 14 years ago)

kalman in Ch

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