| 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 | //!Extended Kalman filter with unknown \c Q |
|---|
| 23 | class EKF_unQ : public EKFCh , public BMcond { |
|---|
| 24 | public: |
|---|
| 25 | //! Default constructor |
|---|
| 26 | EKF_unQ ( RV rx, RV ry,RV ru,RV rQ ) :EKFCh ( rx,ry,ru ),BMcond ( rQ ) {}; |
|---|
| 27 | void condition ( const vec &Q0 ) { |
|---|
| 28 | Q.setD ( Q0,0 ); |
|---|
| 29 | //from EKF |
|---|
| 30 | preA.set_submatrix ( dimy+dimx,dimy,Q._Ch() ); |
|---|
| 31 | }; |
|---|
| 32 | }; |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | int main() { |
|---|
| 36 | // Kalman filter |
|---|
| 37 | int Ndat = 30000; |
|---|
| 38 | double h = 1e-6; |
|---|
| 39 | int Nsimstep = 125; |
|---|
| 40 | int Npart = 1000; |
|---|
| 41 | |
|---|
| 42 | // internal model |
|---|
| 43 | IMpmsm fxu; |
|---|
| 44 | // Rs Ls dt Fmag(Ypm) kp p J Bf(Mz) |
|---|
| 45 | fxu.set_parameters ( 0.28, 0.003465, Nsimstep*h, 0.1989, 1.5 ,4.0, 0.04, 0.0 ); |
|---|
| 46 | // observation model |
|---|
| 47 | OMpmsm hxu; |
|---|
| 48 | |
|---|
| 49 | vec mu0= "0.0 0.0 0.0 0.0"; |
|---|
| 50 | vec Qdiag ( "0.01 0.01 0.00001 0.00001" ); //zdenek: 0.01 0.01 0.0001 0.0001 |
|---|
| 51 | vec Rdiag ( "0.0005 0.0005" ); //var(diff(xth)) = "0.034 0.034" |
|---|
| 52 | chmat Q ( Qdiag ); |
|---|
| 53 | chmat R ( Rdiag ); |
|---|
| 54 | EKFCh KFE ( rx,ry,ru ); |
|---|
| 55 | KFE.set_parameters ( &fxu,&hxu,Q,R ); |
|---|
| 56 | KFE.set_est ( mu0, chmat ( 1*ones ( 4 ) ) ); |
|---|
| 57 | |
|---|
| 58 | RV rQ ( "100","{Q}","2","0" ); |
|---|
| 59 | EKF_unQ KFEp ( rx,ry,ru,rQ ); |
|---|
| 60 | KFEp.set_parameters ( &fxu,&hxu,Q,R ); |
|---|
| 61 | KFEp.set_est ( mu0, chmat ( 1*ones ( 4 ) ) ); |
|---|
| 62 | |
|---|
| 63 | mgamma evolQ ( rQ,rQ ); |
|---|
| 64 | MPF<EKF_unQ> M ( rx,rQ,evolQ,evolQ,Npart,KFEp ); |
|---|
| 65 | // initialize |
|---|
| 66 | evolQ.set_parameters ( 100.0 ); //sigma = 1/10 mu |
|---|
| 67 | evolQ.condition ( "0.01 0.01" ); //Zdenek default |
|---|
| 68 | epdf& pfinit=evolQ._epdf(); |
|---|
| 69 | M.set_est ( pfinit ); |
|---|
| 70 | evolQ.set_parameters ( 1000.0 ); |
|---|
| 71 | |
|---|
| 72 | // |
|---|
| 73 | |
|---|
| 74 | epdf& KFEep = KFE._epdf(); |
|---|
| 75 | epdf& Mep = M._epdf(); |
|---|
| 76 | |
|---|
| 77 | mat Xt=zeros ( Ndat ,9 ); //true state from simulator |
|---|
| 78 | mat Dt=zeros ( Ndat,4+2 ); //observation |
|---|
| 79 | mat XtE=zeros ( Ndat, 4 ); |
|---|
| 80 | mat XtM=zeros ( Ndat,6 ); //Q + x |
|---|
| 81 | |
|---|
| 82 | // SET SIMULATOR |
|---|
| 83 | pmsmsim_set_parameters ( 0.28,0.003465,0.1989,0.0,4,1.5,0.04, 200., 3e-6, h ); |
|---|
| 84 | double Ww=0.0; |
|---|
| 85 | static int k_rampa=1; |
|---|
| 86 | static long k_rampa_tmp=0; |
|---|
| 87 | vec dt ( 2 ); |
|---|
| 88 | vec ut ( 2 ); |
|---|
| 89 | |
|---|
| 90 | for ( int tK=1;tK<Ndat;tK++ ) { |
|---|
| 91 | //Number of steps of a simulator for one step of Kalman |
|---|
| 92 | for ( int ii=0; ii<Nsimstep;ii++ ) { |
|---|
| 93 | //simulator |
|---|
| 94 | Ww+=k_rampa*2.*M_PI*2e-4; //1000Hz/s |
|---|
| 95 | if ( Ww>2.*M_PI*150. ) { |
|---|
| 96 | Ww=2.*M_PI*150.; |
|---|
| 97 | if ( k_rampa_tmp<500000 ) k_rampa_tmp++; |
|---|
| 98 | else {k_rampa=-1;k_rampa_tmp=0;} |
|---|
| 99 | }; |
|---|
| 100 | if ( Ww<-2.*M_PI*150. ) Ww=-2.*M_PI*150.; /* */ |
|---|
| 101 | |
|---|
| 102 | pmsmsim_step ( Ww ); |
|---|
| 103 | }; |
|---|
| 104 | // collect data |
|---|
| 105 | ut ( 0 ) = KalmanObs[0]; |
|---|
| 106 | ut ( 1 ) = KalmanObs[1]; |
|---|
| 107 | dt ( 0 ) = KalmanObs[2]; |
|---|
| 108 | dt ( 1 ) = KalmanObs[3]; |
|---|
| 109 | |
|---|
| 110 | //estimator |
|---|
| 111 | KFE.bayes ( concat ( dt,ut ) ); |
|---|
| 112 | M.bayes ( concat ( dt,ut ) ); |
|---|
| 113 | |
|---|
| 114 | Xt.set_row ( tK,vec ( x,9 ) ); //vec from C-array |
|---|
| 115 | Dt.set_row ( tK, concat ( dt,ut,vec_1(sqrt(pow(ut(0),2)+pow(ut(1),2))), vec_1(sqrt(pow(dt(0),2)+pow(dt(1),2))) ) ); |
|---|
| 116 | XtE.set_row ( tK,KFEep.mean() ); |
|---|
| 117 | XtM.set_row ( tK,Mep.mean() ); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | it_file fou ( "pmsm_sim.it" ); |
|---|
| 121 | |
|---|
| 122 | fou << Name ( "xth" ) << Xt; |
|---|
| 123 | fou << Name ( "Dt" ) << Dt; |
|---|
| 124 | fou << Name ( "xthE" ) << XtE; |
|---|
| 125 | fou << Name ( "xthM" ) << XtM; |
|---|
| 126 | //Exit program: |
|---|
| 127 | |
|---|
| 128 | return 0; |
|---|
| 129 | } |
|---|