[81] | 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 <stat/libFN.h> |
---|
| 15 | #include <estim/libKF.h> |
---|
| 16 | #include <estim/libPF.h> |
---|
| 17 | |
---|
| 18 | #include "pmsm.h" |
---|
| 19 | #include "simulator.h" |
---|
| 20 | |
---|
[94] | 21 | #include <stat/loggers.h> |
---|
[81] | 22 | |
---|
| 23 | using namespace itpp; |
---|
| 24 | //!Extended Kalman filter with unknown \c Q |
---|
| 25 | |
---|
| 26 | void set_simulator_t ( double &Ww ) { |
---|
| 27 | |
---|
| 28 | if ( t>0.0002 ) x[8]=1.2; // 1A //0.2ZP |
---|
| 29 | if ( t>0.4 ) x[8]=10.8; // 9A |
---|
| 30 | if ( t>0.6 ) x[8]=25.2; // 21A |
---|
| 31 | |
---|
| 32 | if ( t>0.7 ) Ww=2.*M_PI*10.; |
---|
| 33 | if ( t>1.0 ) x[8]=1.2; // 1A |
---|
| 34 | if ( t>1.2 ) x[8]=10.8; // 9A |
---|
| 35 | if ( t>1.4 ) x[8]=25.2; // 21A |
---|
| 36 | |
---|
| 37 | if ( t>1.6 ) Ww=2.*M_PI*50.; |
---|
| 38 | if ( t>1.9 ) x[8]=1.2; // 1A |
---|
| 39 | if ( t>2.1 ) x[8]=10.8; // 9A |
---|
| 40 | if ( t>2.3 ) x[8]=25.2; // 21A |
---|
| 41 | |
---|
| 42 | if ( t>2.5 ) Ww=2.*M_PI*100; |
---|
| 43 | if ( t>2.8 ) x[8]=1.2; // 1A |
---|
| 44 | if ( t>3.0 ) x[8]=10.8; // 9A |
---|
| 45 | if ( t>3.2 ) x[8]=25.2; // 21A |
---|
| 46 | |
---|
| 47 | if ( t>3.4 ) Ww=2.*M_PI*150; |
---|
| 48 | if ( t>3.7 ) x[8]=1.2; // 1A |
---|
| 49 | if ( t>3.9 ) x[8]=10.8; // 9A |
---|
| 50 | if ( t>4.1 ) x[8]=25.2; // 21A |
---|
| 51 | |
---|
| 52 | if ( t>4.3 ) Ww=2.*M_PI*0; |
---|
| 53 | if ( t>4.8 ) x[8]=-1.2; // 1A |
---|
| 54 | if ( t>5.0 ) x[8]=-10.8; // 9A |
---|
| 55 | if ( t>5.2 ) x[8]=-25.2; // 21A |
---|
| 56 | |
---|
| 57 | if ( t>5.4 ) Ww=2.*M_PI* ( -10. ); |
---|
| 58 | if ( t>5.7 ) x[8]=-1.2; // 1A |
---|
| 59 | if ( t>5.9 ) x[8]=-10.8; // 9A |
---|
| 60 | if ( t>6.1 ) x[8]=-25.2; // 21A |
---|
| 61 | |
---|
| 62 | if ( t>6.3 ) Ww=2.*M_PI* ( -50. ); |
---|
| 63 | if ( t>6.7 ) x[8]=-1.2; // 1A |
---|
| 64 | if ( t>6.9 ) x[8]=-10.8; // 9A |
---|
| 65 | if ( t>7.1 ) x[8]=-25.2; // 21A |
---|
| 66 | |
---|
| 67 | if ( t>7.3 ) Ww=2.*M_PI* ( -100. ); |
---|
| 68 | if ( t>7.7 ) x[8]=-1.2; // 1A |
---|
| 69 | if ( t>7.9 ) x[8]=-10.8; // 9A |
---|
| 70 | if ( t>8.1 ) x[8]=-25.2; // 21A |
---|
| 71 | if ( t>8.3 ) x[8]=10.8; // 9A |
---|
| 72 | if ( t>8.5 ) x[8]=25.2; // 21A |
---|
| 73 | |
---|
| 74 | x[8]=0.0; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | int main() { |
---|
| 78 | // Kalman filter |
---|
| 79 | int Ndat = 90000; |
---|
| 80 | double h = 1e-6; |
---|
| 81 | int Nsimstep = 125; |
---|
| 82 | |
---|
[94] | 83 | dirfilelog L("exp/sim_var",1000); |
---|
| 84 | |
---|
[81] | 85 | // SET SIMULATOR |
---|
| 86 | pmsmsim_set_parameters ( 0.28,0.003465,0.1989,0.0,4,1.5,0.04, 200., 3e-6, h ); |
---|
| 87 | double Ww = 0.0; |
---|
| 88 | vec dt ( 2 ); |
---|
| 89 | vec ut ( 2 ); |
---|
| 90 | vec xtm=zeros ( 4 ); |
---|
| 91 | vec xdif=zeros ( 4 ); |
---|
| 92 | vec xt ( 4 ); |
---|
[94] | 93 | vec ddif=zeros(2); |
---|
[81] | 94 | IMpmsm fxu; |
---|
| 95 | // Rs Ls dt Fmag(Ypm) kp p J Bf(Mz) |
---|
| 96 | fxu.set_parameters ( 0.28, 0.003465, Nsimstep*h, 0.1989, 1.5 ,4.0, 0.04, 0.0 ); |
---|
| 97 | OMpmsm hxu; |
---|
| 98 | mat Qt=zeros ( 4,4 ); |
---|
[94] | 99 | mat Rt=zeros ( 2,2 ); |
---|
[81] | 100 | |
---|
| 101 | // ESTIMATORS |
---|
| 102 | vec mu0= "0.0 0.0 0.0 0.0"; |
---|
| 103 | vec Qdiag ( "62 66 454 0.03" ); //zdenek: 0.01 0.01 0.0001 0.0001 |
---|
| 104 | vec Rdiag ( "100 100" ); //var(diff(xth)) = "0.034 0.034" |
---|
| 105 | mat Q =diag( Qdiag ); |
---|
| 106 | mat R =diag ( Rdiag ); |
---|
| 107 | EKFfull Efix ( rx,ry,ru ); |
---|
| 108 | Efix.set_est ( mu0, 1*eye ( 4 ) ); |
---|
| 109 | Efix.set_parameters ( &fxu,&hxu,Q,R); |
---|
| 110 | |
---|
| 111 | EKFfull Eop ( rx,ry,ru ); |
---|
| 112 | Eop.set_est ( mu0, 1*eye ( 4 ) ); |
---|
| 113 | Eop.set_parameters ( &fxu,&hxu,Q,R); |
---|
| 114 | |
---|
[94] | 115 | EKFfull Edi ( rx,ry,ru ); |
---|
| 116 | Edi.set_est ( mu0, 1*eye ( 4 ) ); |
---|
| 117 | Edi.set_parameters ( &fxu,&hxu,Q,R); |
---|
| 118 | |
---|
[81] | 119 | epdf& Efix_ep = Efix._epdf(); |
---|
| 120 | epdf& Eop_ep = Eop._epdf(); |
---|
[94] | 121 | epdf& Edi_ep = Edi._epdf(); |
---|
[81] | 122 | |
---|
[94] | 123 | //LOG |
---|
| 124 | RV rQ("10", "{Q }", "16","0"); |
---|
| 125 | RV rR("11", "{R }", "4","0"); |
---|
| 126 | int X_log = L.add(rx,"X"); |
---|
| 127 | int Efix_log = L.add(rx,"XF"); |
---|
| 128 | int Eop_log = L.add(rx,"XO"); |
---|
| 129 | int Edi_log = L.add(rx,"XD"); |
---|
| 130 | int Q_log = L.add(rQ,""); |
---|
| 131 | int R_log = L.add(rR,""); |
---|
| 132 | L.init(); |
---|
[81] | 133 | |
---|
| 134 | for ( int tK=1;tK<Ndat;tK++ ) { |
---|
| 135 | //Number of steps of a simulator for one step of Kalman |
---|
| 136 | for ( int ii=0; ii<Nsimstep;ii++ ) { |
---|
| 137 | set_simulator_t ( Ww ); |
---|
| 138 | pmsmsim_step ( Ww ); |
---|
| 139 | }; |
---|
| 140 | // simulation via deterministic model |
---|
| 141 | ut ( 0 ) = KalmanObs[0]; |
---|
| 142 | ut ( 1 ) = KalmanObs[1]; |
---|
| 143 | dt ( 0 ) = KalmanObs[2]; |
---|
| 144 | dt ( 1 ) = KalmanObs[3]; |
---|
| 145 | |
---|
| 146 | xt = fxu.eval ( xtm,ut ); |
---|
| 147 | //Results: |
---|
| 148 | // in xt we have simulaton according to the model |
---|
| 149 | // in x we have "reality" |
---|
| 150 | xtm ( 0 ) =x[0];xtm ( 1 ) =x[1];xtm ( 2 ) =x[2];xtm ( 3 ) =x[3]; |
---|
[94] | 151 | xdif = xtm-xt; |
---|
[81] | 152 | if ( xdif ( 3 ) >pi ) xdif ( 3 )-=2*pi; |
---|
| 153 | if ( xdif ( 3 ) <-pi ) xdif ( 3 ) +=2*pi; |
---|
[94] | 154 | |
---|
| 155 | ddif = hxu.eval(xtm,ut) - dt; |
---|
[81] | 156 | |
---|
| 157 | //Rt = 0.9*Rt + xdif^2 |
---|
[94] | 158 | Qt*=0.01; |
---|
[81] | 159 | Qt += outer_product ( xdif,xdif ); //(x-xt)^2 |
---|
[94] | 160 | Rt*=0.01; |
---|
| 161 | Rt += outer_product ( ddif,ddif ); //(x-xt)^2 |
---|
[81] | 162 | |
---|
| 163 | //ESTIMATE |
---|
| 164 | Efix.bayes(concat(dt,ut)); |
---|
| 165 | // |
---|
[94] | 166 | Eop.set_parameters ( &fxu,&hxu,(Qt+1e-16*eye(4)),(Rt+1e-3*eye(2))); |
---|
[81] | 167 | Eop.bayes(concat(dt,ut)); |
---|
[94] | 168 | // |
---|
| 169 | Edi.set_parameters ( &fxu,&hxu,(diag(diag(Qt))+1e-16*eye(4)),(diag(diag(Rt))+1e-3*eye(2))); |
---|
| 170 | Edi.bayes(concat(dt,ut)); |
---|
[81] | 171 | |
---|
[94] | 172 | //LOG |
---|
| 173 | L.logit(X_log, vec(x,4)); //vec from C-array |
---|
| 174 | L.logit(Efix_log, Efix_ep.mean() ); |
---|
| 175 | L.logit(Eop_log, Eop_ep.mean() ); |
---|
| 176 | L.logit(Edi_log, Edi_ep.mean() ); |
---|
| 177 | L.logit(Q_log, vec(Qt._data(),16) ); |
---|
| 178 | L.logit(R_log, vec(Rt._data(),4) ); |
---|
| 179 | |
---|
| 180 | L.step(false); |
---|
[81] | 181 | } |
---|
| 182 | |
---|
[94] | 183 | L.step(true); |
---|
[81] | 184 | |
---|
| 185 | return 0; |
---|
| 186 | } |
---|