[60] | 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 | |
---|
[262] | 13 | |
---|
[60] | 14 | #include <estim/libKF.h> |
---|
| 15 | #include <estim/libPF.h> |
---|
| 16 | #include <stat/libFN.h> |
---|
| 17 | |
---|
[117] | 18 | #include <stat/loggers.h> |
---|
| 19 | |
---|
[60] | 20 | #include "pmsm.h" |
---|
| 21 | #include "simulator.h" |
---|
[117] | 22 | #include "sim_profiles.h" |
---|
[60] | 23 | |
---|
[254] | 24 | using namespace bdm; |
---|
[60] | 25 | //!Extended Kalman filter with unknown \c Q |
---|
| 26 | class EKF_unQ : public EKFCh , public BMcond { |
---|
| 27 | public: |
---|
| 28 | //! Default constructor |
---|
| 29 | void condition ( const vec &Q0 ) { |
---|
| 30 | Q.setD ( Q0,0 ); |
---|
| 31 | //from EKF |
---|
| 32 | preA.set_submatrix ( dimy+dimx,dimy,Q._Ch() ); |
---|
| 33 | }; |
---|
[63] | 34 | void bayes(const vec dt){ |
---|
| 35 | EKFCh::bayes(dt); |
---|
| 36 | |
---|
| 37 | vec xtrue(4); |
---|
| 38 | //UGLY HACK!!! reliance on a predictor!! |
---|
| 39 | xtrue(0)=x[0]; |
---|
| 40 | xtrue(1)=x[1]; |
---|
| 41 | xtrue(2)=x[2]; |
---|
| 42 | xtrue(3)=x[3]; |
---|
| 43 | |
---|
[81] | 44 | ll = -0.5* ( 4 * 1.83787706640935 +_P.logdet() +_P.invqform(xtrue)); |
---|
[63] | 45 | } |
---|
[60] | 46 | }; |
---|
[63] | 47 | class EKF_unQful : public EKFfull , public BMcond { |
---|
| 48 | public: |
---|
| 49 | void condition ( const vec &Q0 ) { |
---|
| 50 | Q=diag(Q0); |
---|
| 51 | }; |
---|
| 52 | void bayes(const vec dt){ |
---|
| 53 | EKFfull::bayes(dt); |
---|
| 54 | |
---|
| 55 | vec xtrue(4); |
---|
| 56 | //UGLY HACK!!! reliance on a predictor!! |
---|
| 57 | xtrue(0)=x[0]; |
---|
| 58 | xtrue(1)=x[1]; |
---|
| 59 | xtrue(2)=x[2]; |
---|
| 60 | xtrue(3)=x[3]; |
---|
| 61 | |
---|
| 62 | BM::ll = -0.5* ( 4 * 1.83787706640935 +log(det(P)) +xtrue* ( inv(P)*xtrue ) ); |
---|
| 63 | } |
---|
| 64 | }; |
---|
[60] | 65 | |
---|
| 66 | int main() { |
---|
| 67 | // Kalman filter |
---|
[62] | 68 | int Ndat = 90000; |
---|
[60] | 69 | double h = 1e-6; |
---|
| 70 | int Nsimstep = 125; |
---|
[63] | 71 | int Npart = 50; |
---|
[60] | 72 | |
---|
[117] | 73 | dirfilelog L("exp/pmsm_sim2",1000); |
---|
| 74 | |
---|
[60] | 75 | // internal model |
---|
| 76 | IMpmsm fxu; |
---|
[62] | 77 | // Rs Ls dt Fmag(Ypm) kp p J Bf(Mz) |
---|
[60] | 78 | fxu.set_parameters ( 0.28, 0.003465, Nsimstep*h, 0.1989, 1.5 ,4.0, 0.04, 0.0 ); |
---|
| 79 | // observation model |
---|
| 80 | OMpmsm hxu; |
---|
| 81 | |
---|
| 82 | vec mu0= "0.0 0.0 0.0 0.0"; |
---|
[62] | 83 | // vec Qdiag ( "0.01 0.01 0.01 0.0001" ); //zdenek: 0.01 0.01 0.0001 0.0001 |
---|
[63] | 84 | vec Qdiag ( "10 10 10 0.001" ); //zdenek: 0.01 0.01 0.0001 0.0001 |
---|
| 85 | vec Rdiag ( "100 100" ); //var(diff(xth)) = "0.034 0.034" |
---|
[60] | 86 | chmat Q ( Qdiag ); |
---|
| 87 | chmat R ( Rdiag ); |
---|
[279] | 88 | EKFCh KFE ; |
---|
[63] | 89 | KFE.set_est ( mu0, chmat( 1*eye ( 4 ) ) ); |
---|
| 90 | KFE.set_parameters ( &fxu,&hxu,Q,R); |
---|
[60] | 91 | |
---|
[162] | 92 | RV rQ ( "{Q}","4" ); |
---|
[279] | 93 | EKF_unQful KFEp ; |
---|
[63] | 94 | KFEp.set_est ( mu0, 1*ones ( 4 ) ); |
---|
| 95 | KFEp.set_parameters ( &fxu,&hxu,diag(Qdiag),diag(Rdiag) ); |
---|
[60] | 96 | |
---|
[279] | 97 | mgamma_fix evolQ ; |
---|
| 98 | MPF<EKF_unQful> M ( &evolQ,&evolQ,Npart,KFEp ); |
---|
[60] | 99 | // initialize |
---|
| 100 | evolQ.set_parameters ( 1000.0 ,Qdiag, 0.5); //sigma = 1/10 mu |
---|
| 101 | evolQ.condition ( Qdiag ); //Zdenek default |
---|
[279] | 102 | epdf& pfinit=evolQ._epdf(); |
---|
[60] | 103 | M.set_est ( pfinit ); |
---|
[63] | 104 | evolQ.set_parameters ( 100000.0, Qdiag, 0.9999 ); |
---|
[60] | 105 | // |
---|
[271] | 106 | const epdf& KFEep = KFE.posterior(); |
---|
| 107 | const epdf& Mep = M.posterior(); |
---|
[60] | 108 | |
---|
[117] | 109 | int X_log = L.add(rx,"X"); |
---|
| 110 | int Efix_log = L.add(rx,"XF"); |
---|
[162] | 111 | RV tmp=concat(rQ,rx); |
---|
| 112 | int M_log = L.add(tmp,"M"); |
---|
[117] | 113 | L.init(); |
---|
[60] | 114 | |
---|
| 115 | // SET SIMULATOR |
---|
| 116 | pmsmsim_set_parameters ( 0.28,0.003465,0.1989,0.0,4,1.5,0.04, 200., 3e-6, h ); |
---|
| 117 | double Ww=0.0; |
---|
| 118 | vec dt ( 2 ); |
---|
| 119 | vec ut ( 2 ); |
---|
| 120 | |
---|
| 121 | for ( int tK=1;tK<Ndat;tK++ ) { |
---|
| 122 | //Number of steps of a simulator for one step of Kalman |
---|
| 123 | for ( int ii=0; ii<Nsimstep;ii++ ) { |
---|
| 124 | //simulator |
---|
[117] | 125 | sim_profile_steps1(Ww); |
---|
[60] | 126 | pmsmsim_step ( Ww ); |
---|
| 127 | }; |
---|
| 128 | // collect data |
---|
| 129 | ut ( 0 ) = KalmanObs[0]; |
---|
| 130 | ut ( 1 ) = KalmanObs[1]; |
---|
| 131 | dt ( 0 ) = KalmanObs[2]; |
---|
| 132 | dt ( 1 ) = KalmanObs[3]; |
---|
| 133 | |
---|
| 134 | //estimator |
---|
| 135 | KFE.bayes ( concat ( dt,ut ) ); |
---|
| 136 | M.bayes ( concat ( dt,ut ) ); |
---|
| 137 | |
---|
[117] | 138 | L.logit(X_log, vec(x,4)); //vec from C-array |
---|
| 139 | L.logit(Efix_log, KFEep.mean() ); |
---|
| 140 | L.logit(M_log, Mep.mean() ); |
---|
| 141 | |
---|
[162] | 142 | L.step(); |
---|
[60] | 143 | } |
---|
| 144 | |
---|
[162] | 145 | L.finalize(); |
---|
[117] | 146 | |
---|
[60] | 147 | return 0; |
---|
| 148 | } |
---|