1 | /* |
---|
2 | \file |
---|
3 | \brief Models for synchronous electric drive using IT++ and BDM |
---|
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 | #include <itpp/itbase.h> |
---|
14 | #include <estim/libKF.h> |
---|
15 | #include <estim/libPF.h> |
---|
16 | #include <stat/libFN.h> |
---|
17 | |
---|
18 | #include "pmsm.h" |
---|
19 | #include "simulator.h" |
---|
20 | |
---|
21 | using namespace itpp; |
---|
22 | /* |
---|
23 | // PMSM with Q on Ia and Ib given externally |
---|
24 | class EKF_unQ : public EKF<chmat> , public BMcond { |
---|
25 | public: |
---|
26 | EKF_unQ( rx,ry,ru,rQ):EKF<chmat>(rx,ry,ru),BMcond(rQ){}; |
---|
27 | void condition(const vec &Q0){}; |
---|
28 | };*/ |
---|
29 | |
---|
30 | |
---|
31 | int main() { |
---|
32 | // Kalman filter |
---|
33 | int Ndat = 10000; |
---|
34 | double h = 1e-6; |
---|
35 | int Nsimstep = 20; |
---|
36 | |
---|
37 | const it Nll = 10; |
---|
38 | |
---|
39 | // cout << KF; |
---|
40 | // internal model |
---|
41 | IMpmsm fxu; |
---|
42 | // Rs Ls dt Fmag(Ypm) kp p J Bf(Mz) |
---|
43 | fxu.set_parameters ( 0.28, 0.003465, Nsimstep*h, 0.1989, 1.5 ,4.0, 0.04, 0.0 ); |
---|
44 | // observation model |
---|
45 | OMpmsm hxu; |
---|
46 | |
---|
47 | vec mu0= "0.0 0.0 0.0 0.0"; |
---|
48 | vec Qdiag ( "0.03 0.03 0.001 0.00001" ); |
---|
49 | vec Rdiag ( "0.000001 0.000001" ); //var(diff(xth)) = "0.034 0.034" |
---|
50 | vec vQ = "0.01:0.01:0.1"; |
---|
51 | chmat Q ( Qdiag ); |
---|
52 | chmat R ( Rdiag ); |
---|
53 | EKFCh KFE ( rx,ry,ru ); |
---|
54 | KFE.set_est ( mu0, chmat ( 1000*ones ( 4 ) ) ); |
---|
55 | KFE.set_parameters ( &fxu,&hxu,Q,R ); |
---|
56 | |
---|
57 | mat ll ( Nll,Ndat ); |
---|
58 | |
---|
59 | EKFCh* kfArray[Nll]; |
---|
60 | |
---|
61 | |
---|
62 | for ( int i=0;i<Nll;i++ ) { |
---|
63 | vec Qid ( Qdiag ); |
---|
64 | Qid ( 0 ) = vQ ( i ); Qid ( 1 ) = vQ ( i ); |
---|
65 | kfArray[i]= new EKFCh ( rx,ry,ru ); |
---|
66 | kfArray[i]->set_est ( mu0, chmat ( 100*ones ( 4 ) ) ); |
---|
67 | kfArray[i]->set_parameters ( &fxu,&hxu,chmat ( Qid ),R ); |
---|
68 | } |
---|
69 | |
---|
70 | epdf& KFEep = KFE._epdf(); |
---|
71 | |
---|
72 | mat Xt=zeros ( 9,Ndat ); //true state from simulator |
---|
73 | mat Dt=zeros ( 4,Ndat ); //observation |
---|
74 | mat XtE=zeros ( 4,Ndat ); |
---|
75 | |
---|
76 | // SET SIMULATOR |
---|
77 | pmsmsim_set_parameters ( 0.28,0.003465,0.1989,0.0,4,1.5,0.04, 200., 3e-6, h ); |
---|
78 | double Ww=0.0; |
---|
79 | static int k_rampa=1; |
---|
80 | static long k_rampa_tmp=0; |
---|
81 | vec dt ( 2 ); |
---|
82 | vec ut ( 2 ); |
---|
83 | |
---|
84 | for ( int tK=1;tK<Ndat;tK++ ) { |
---|
85 | //Number of steps of a simulator for one step of Kalman |
---|
86 | for ( int ii=0; ii<Nsimstep;ii++ ) { |
---|
87 | //simulator |
---|
88 | Ww+=k_rampa*2.*M_PI*2e-4; //1000Hz/s |
---|
89 | if ( Ww>2.*M_PI*150. ) { |
---|
90 | Ww=2.*M_PI*150.; |
---|
91 | if ( k_rampa_tmp<500000 ) k_rampa_tmp++; |
---|
92 | else {k_rampa=-1;k_rampa_tmp=0;} |
---|
93 | }; |
---|
94 | if ( Ww<-2.*M_PI*150. ) Ww=-2.*M_PI*150.; /* */ |
---|
95 | |
---|
96 | pmsmsim_step ( Ww ); |
---|
97 | }; |
---|
98 | // collect data |
---|
99 | ut ( 0 ) = KalmanObs[0]; |
---|
100 | ut ( 1 ) = KalmanObs[1]; |
---|
101 | dt ( 0 ) = KalmanObs[2]; |
---|
102 | dt ( 1 ) = KalmanObs[3]; |
---|
103 | //estimator |
---|
104 | KFE.bayes ( concat ( dt,ut ) ); |
---|
105 | for ( int i=0;i<Nll;i++ ) { |
---|
106 | kfArray[i]->bayes ( concat ( dt,ut ) ); |
---|
107 | ll ( i,tK ) =ll ( i,tK-1 ) + kfArray[i]->_ll(); |
---|
108 | } |
---|
109 | |
---|
110 | Xt.set_col ( tK,vec ( x,9 ) ); |
---|
111 | Dt.set_col ( tK, concat ( dt,ut ) ); |
---|
112 | XtE.set_col ( tK,KFEep.mean() ); |
---|
113 | } |
---|
114 | |
---|
115 | it_file fou ( "pmsm_sim.it" ); |
---|
116 | |
---|
117 | fou << Name ( "xth" ) << Xt; |
---|
118 | fou << Name ( "Dt" ) << Dt; |
---|
119 | fou << Name ( "xthE" ) << XtE; |
---|
120 | fou << Name ( "ll" ) << ll; |
---|
121 | fou << Name ( "llgrid" ) << vQ; |
---|
122 | //Exit program: |
---|
123 | return 0; |
---|
124 | |
---|
125 | } |
---|