[105] | 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 <stat/emix.h> |
---|
| 16 | #include <estim/libKF.h> |
---|
| 17 | #include <estim/libPF.h> |
---|
| 18 | |
---|
| 19 | #include "pmsm.h" |
---|
| 20 | #include "simulator.h" |
---|
| 21 | |
---|
| 22 | #include <stat/loggers.h> |
---|
| 23 | |
---|
| 24 | using namespace itpp; |
---|
| 25 | //!Extended Kalman filter with unknown \c Q |
---|
| 26 | |
---|
| 27 | void set_simulator_t ( double &Ww ) { |
---|
| 28 | |
---|
| 29 | if ( t>0.0002 ) x[8]=1.2; // 1A //0.2ZP |
---|
| 30 | if ( t>0.4 ) x[8]=10.8; // 9A |
---|
| 31 | if ( t>0.6 ) x[8]=25.2; // 21A |
---|
| 32 | |
---|
| 33 | if ( t>0.7 ) Ww=2.*M_PI*10.; |
---|
| 34 | if ( t>1.0 ) x[8]=1.2; // 1A |
---|
| 35 | if ( t>1.2 ) x[8]=10.8; // 9A |
---|
| 36 | if ( t>1.4 ) x[8]=25.2; // 21A |
---|
| 37 | |
---|
| 38 | if ( t>1.6 ) Ww=2.*M_PI*50.; |
---|
| 39 | if ( t>1.9 ) x[8]=1.2; // 1A |
---|
| 40 | if ( t>2.1 ) x[8]=10.8; // 9A |
---|
| 41 | if ( t>2.3 ) x[8]=25.2; // 21A |
---|
| 42 | |
---|
| 43 | if ( t>2.5 ) Ww=2.*M_PI*100; |
---|
| 44 | if ( t>2.8 ) x[8]=1.2; // 1A |
---|
| 45 | if ( t>3.0 ) x[8]=10.8; // 9A |
---|
| 46 | if ( t>3.2 ) x[8]=25.2; // 21A |
---|
| 47 | |
---|
| 48 | if ( t>3.4 ) Ww=2.*M_PI*150; |
---|
| 49 | if ( t>3.7 ) x[8]=1.2; // 1A |
---|
| 50 | if ( t>3.9 ) x[8]=10.8; // 9A |
---|
| 51 | if ( t>4.1 ) x[8]=25.2; // 21A |
---|
| 52 | |
---|
| 53 | if ( t>4.3 ) Ww=2.*M_PI*0; |
---|
| 54 | if ( t>4.8 ) x[8]=-1.2; // 1A |
---|
| 55 | if ( t>5.0 ) x[8]=-10.8; // 9A |
---|
| 56 | if ( t>5.2 ) x[8]=-25.2; // 21A |
---|
| 57 | |
---|
| 58 | if ( t>5.4 ) Ww=2.*M_PI* ( -10. ); |
---|
| 59 | if ( t>5.7 ) x[8]=-1.2; // 1A |
---|
| 60 | if ( t>5.9 ) x[8]=-10.8; // 9A |
---|
| 61 | if ( t>6.1 ) x[8]=-25.2; // 21A |
---|
| 62 | |
---|
| 63 | if ( t>6.3 ) Ww=2.*M_PI* ( -50. ); |
---|
| 64 | if ( t>6.7 ) x[8]=-1.2; // 1A |
---|
| 65 | if ( t>6.9 ) x[8]=-10.8; // 9A |
---|
| 66 | if ( t>7.1 ) x[8]=-25.2; // 21A |
---|
| 67 | |
---|
| 68 | if ( t>7.3 ) Ww=2.*M_PI* ( -100. ); |
---|
| 69 | if ( t>7.7 ) x[8]=-1.2; // 1A |
---|
| 70 | if ( t>7.9 ) x[8]=-10.8; // 9A |
---|
| 71 | if ( t>8.1 ) x[8]=-25.2; // 21A |
---|
| 72 | if ( t>8.3 ) x[8]=10.8; // 9A |
---|
| 73 | if ( t>8.5 ) x[8]=25.2; // 21A |
---|
| 74 | |
---|
| 75 | x[8]=0.0; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | //!Extended Kalman filter with unknown \c Q |
---|
| 79 | class EKFful_unQR : public EKFfull , public BMcond { |
---|
| 80 | public: |
---|
| 81 | //! Default constructor |
---|
| 82 | EKFful_unQR ( RV rx, RV ry,RV ru,RV rQR ) :EKFfull ( rx,ry,ru ),BMcond ( rQR ) {}; |
---|
| 83 | void condition ( const vec &Q0 ) { |
---|
| 84 | Q=diag(Q0(0,3)); |
---|
| 85 | R=diag(Q0(4,5)); |
---|
| 86 | }; |
---|
| 87 | /* void bayes(const vec dt){ |
---|
| 88 | EKFfull::bayes(dt); |
---|
| 89 | |
---|
| 90 | vec xtrue(4); |
---|
| 91 | //UGLY HACK!!! reliance on a predictor!! |
---|
| 92 | xtrue(0)=x[0]; |
---|
| 93 | xtrue(1)=x[1]; |
---|
| 94 | xtrue(2)=x[2]; |
---|
| 95 | xtrue(3)=x[3]; |
---|
| 96 | |
---|
| 97 | BM::ll = -0.5* ( 4 * 1.83787706640935 +log(det(P)) +xtrue* ( inv(P)*xtrue ) ); |
---|
| 98 | }*/ |
---|
| 99 | }; |
---|
| 100 | |
---|
| 101 | int main() { |
---|
| 102 | // Kalman filter |
---|
| 103 | int Ndat = 90000; |
---|
| 104 | double h = 1e-6; |
---|
| 105 | int Nsimstep = 125; |
---|
| 106 | int Npar = 100; |
---|
| 107 | |
---|
| 108 | dirfilelog L("exp/pmsm_mix",1000); |
---|
| 109 | //memlog L(Ndat); |
---|
| 110 | |
---|
| 111 | // SET SIMULATOR |
---|
| 112 | pmsmsim_set_parameters ( 0.28,0.003465,0.1989,0.0,4,1.5,0.04, 200., 3e-6, h ); |
---|
| 113 | double Ww = 0.0; |
---|
| 114 | vec dt ( 2 ); |
---|
| 115 | vec ut ( 2 ); |
---|
| 116 | vec xtm=zeros ( 4 ); |
---|
| 117 | vec xdif=zeros ( 4 ); |
---|
| 118 | vec xt ( 4 ); |
---|
| 119 | vec ddif=zeros(2); |
---|
| 120 | IMpmsm fxu; |
---|
| 121 | // Rs Ls dt Fmag(Ypm) kp p J Bf(Mz) |
---|
| 122 | fxu.set_parameters ( 0.28, 0.003465, Nsimstep*h, 0.1989, 1.5 ,4.0, 0.04, 0.0 ); |
---|
| 123 | OMpmsm hxu; |
---|
| 124 | mat Qt=zeros ( 4,4 ); |
---|
| 125 | mat Rt=zeros ( 2,2 ); |
---|
| 126 | |
---|
| 127 | // ESTIMATORS |
---|
| 128 | vec mu0= "0.0 0.0 0.0 0.0"; |
---|
| 129 | vec Qdiag ( "62 66 454 0.03" ); //zdenek: 0.01 0.01 0.0001 0.0001 |
---|
| 130 | vec Rdiag ( "100 100" ); //var(diff(xth)) = "0.034 0.034" |
---|
| 131 | mat Q =diag( Qdiag ); |
---|
| 132 | mat R =diag ( Rdiag ); |
---|
| 133 | EKFfull Efix ( rx,ry,ru ); |
---|
| 134 | Efix.set_est ( mu0, 1*eye ( 4 ) ); |
---|
| 135 | Efix.set_parameters ( &fxu,&hxu,Q,R); |
---|
| 136 | |
---|
| 137 | RV rQR("10 11", "{Q R }", "4 2 ","0 0"); |
---|
| 138 | EKFful_unQR EKU (rx,ry,ru,rQR); |
---|
| 139 | EKU.set_est ( mu0, 1*ones ( 4 ) ); |
---|
| 140 | EKU.set_parameters ( &fxu,&hxu,diag(Qdiag),diag(Rdiag) ); |
---|
| 141 | |
---|
| 142 | //QU model |
---|
| 143 | egamma Gcom(rQR);Gcom.set_parameters(ones(6),vec("1 1 1e4 1e10 1 1")); |
---|
| 144 | cout << Gcom.mean() <<endl; |
---|
| 145 | cout << Gcom.sample() <<endl; |
---|
| 146 | euni Ucom(rQR); Ucom.set_parameters(zeros(6),vec("60 60 0.03 1e-8 100 100")); |
---|
| 147 | cout << Ucom.mean() <<endl; |
---|
| 148 | cout << Ucom.sample() <<endl; |
---|
| 149 | Array<epdf*> Coms(2); |
---|
| 150 | Coms(0) = &Gcom; |
---|
| 151 | Coms(1) = &Ucom; |
---|
| 152 | emix Eevol(rQR); Eevol.set_parameters("0.5 0.5", Coms); |
---|
| 153 | cout << Eevol.sample() <<endl; |
---|
| 154 | |
---|
| 155 | mmix_triv evolQR(rQR,rQR,&Eevol); |
---|
| 156 | MPF<EKFful_unQR> M ( rx,rQR, evolQR, evolQR, Npar, EKU ); |
---|
| 157 | |
---|
| 158 | epdf& Efix_ep = Efix._epdf(); |
---|
| 159 | epdf& M_ep = M._epdf(); |
---|
| 160 | |
---|
| 161 | //LOG |
---|
| 162 | RV rUD("12 13 14 15", "{u_isa u_isb i_isa i_isb }", ones_i(4),zeros_i(4)); |
---|
| 163 | int X_log = L.add(rx,"X"); |
---|
| 164 | int Efix_log = L.add(rx,"XF"); |
---|
| 165 | int M_log = L.add(concat(rx,rQR),"M"); |
---|
| 166 | L.init(); |
---|
| 167 | |
---|
| 168 | for ( int tK=1;tK<Ndat;tK++ ) { |
---|
| 169 | //Number of steps of a simulator for one step of Kalman |
---|
| 170 | for ( int ii=0; ii<Nsimstep;ii++ ) { |
---|
| 171 | set_simulator_t ( Ww ); |
---|
| 172 | pmsmsim_step ( Ww ); |
---|
| 173 | }; |
---|
| 174 | // simulation via deterministic model |
---|
| 175 | ut ( 0 ) = KalmanObs[0]; |
---|
| 176 | ut ( 1 ) = KalmanObs[1]; |
---|
| 177 | dt ( 0 ) = KalmanObs[2]; |
---|
| 178 | dt ( 1 ) = KalmanObs[3]; |
---|
| 179 | |
---|
| 180 | //ESTIMATE |
---|
| 181 | Efix.bayes(concat(dt,ut)); |
---|
| 182 | // |
---|
| 183 | M.bayes(concat(dt,ut)); |
---|
| 184 | |
---|
| 185 | //LOG |
---|
| 186 | L.logit(X_log, vec(x,4)); //vec from C-array |
---|
| 187 | L.logit(Efix_log, Efix_ep.mean() ); |
---|
| 188 | L.logit(M_log, M_ep.mean() ); |
---|
| 189 | |
---|
| 190 | L.step(false); |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | L.step(true); |
---|
| 194 | //L.itsave("sim_var.it"); |
---|
| 195 | |
---|
| 196 | |
---|
| 197 | return 0; |
---|
| 198 | } |
---|