[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 | |
---|
| 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 | #include <netcdfcpp.h> |
---|
| 22 | void write_to_nc ( NcFile &nc, mat &X, std::string Xn, Array<std::string> A ) { |
---|
| 23 | char tmpstr[200]; |
---|
| 24 | int Len = X.rows(); |
---|
| 25 | |
---|
| 26 | sprintf ( tmpstr,"%s.length",Xn.c_str() ); |
---|
| 27 | NcDim* lengt = nc.add_dim ( tmpstr, ( long ) Len ); |
---|
| 28 | for ( int j=0; j<X.cols(); j++ ) { |
---|
| 29 | if ( j<A.length() ) |
---|
| 30 | sprintf ( tmpstr,"%s_%s",Xn.c_str(), ( A ( j ) ).c_str() ); |
---|
| 31 | else |
---|
| 32 | sprintf ( tmpstr,"%s_%d",Xn.c_str(),j ); |
---|
| 33 | // Create variables and their attributes |
---|
| 34 | NcVar* P = nc.add_var ( tmpstr, ncDouble, lengt ); |
---|
| 35 | const double* Dp = X._data(); |
---|
| 36 | P->put ( &Dp[j*Len],Len ); |
---|
| 37 | } |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | |
---|
| 41 | using namespace itpp; |
---|
| 42 | //!Extended Kalman filter with unknown \c Q |
---|
| 43 | class EKF_unQ : public EKFCh , public BMcond { |
---|
| 44 | public: |
---|
| 45 | //! Default constructor |
---|
| 46 | EKF_unQ ( RV rx, RV ry,RV ru,RV rQ ) :EKFCh ( rx,ry,ru ),BMcond ( rQ ) {}; |
---|
| 47 | void condition ( const vec &Q0 ) { |
---|
| 48 | Q.setD ( Q0,0 ); |
---|
| 49 | //from EKF |
---|
| 50 | preA.set_submatrix ( dimy+dimx,dimy,Q._Ch() ); |
---|
| 51 | }; |
---|
| 52 | }; |
---|
| 53 | |
---|
| 54 | void set_simulator_t(double &Ww) { |
---|
| 55 | |
---|
| 56 | if (t>0.0) x[8]=1.2; // 1A //0.2ZP |
---|
| 57 | if (t>0.4) x[8]=10.8; // 9A |
---|
| 58 | if (t>0.6) x[8]=25.2; // 21A |
---|
| 59 | |
---|
| 60 | if (t>0.7) Ww=2.*M_PI*10.; |
---|
| 61 | if (t>1.0) x[8]=1.2; // 1A |
---|
| 62 | if (t>1.2) x[8]=10.8; // 9A |
---|
| 63 | if (t>1.4) x[8]=25.2; // 21A |
---|
| 64 | |
---|
| 65 | if (t>1.6) Ww=2.*M_PI*50.; |
---|
| 66 | if (t>1.9) x[8]=1.2; // 1A |
---|
| 67 | if (t>2.1) x[8]=10.8; // 9A |
---|
| 68 | if (t>2.3) x[8]=25.2; // 21A |
---|
| 69 | |
---|
| 70 | if (t>2.5) Ww=2.*M_PI*100; |
---|
| 71 | if (t>2.8) x[8]=1.2; // 1A |
---|
| 72 | if (t>3.0) x[8]=10.8; // 9A |
---|
| 73 | if (t>3.2) x[8]=25.2; // 21A |
---|
| 74 | |
---|
| 75 | if (t>3.4) Ww=2.*M_PI*150; |
---|
| 76 | if (t>3.7) x[8]=1.2; // 1A |
---|
| 77 | if (t>3.9) x[8]=10.8; // 9A |
---|
| 78 | if (t>4.1) x[8]=25.2; // 21A |
---|
| 79 | |
---|
| 80 | if (t>4.3) Ww=2.*M_PI*0; |
---|
| 81 | if (t>4.8) x[8]=-1.2; // 1A |
---|
| 82 | if (t>5.0) x[8]=-10.8; // 9A |
---|
| 83 | if (t>5.2) x[8]=-25.2; // 21A |
---|
| 84 | |
---|
| 85 | if (t>5.4) Ww=2.*M_PI*(-10.); |
---|
| 86 | if (t>5.7) x[8]=-1.2; // 1A |
---|
| 87 | if (t>5.9) x[8]=-10.8; // 9A |
---|
| 88 | if (t>6.1) x[8]=-25.2; // 21A |
---|
| 89 | |
---|
| 90 | if (t>6.3) Ww=2.*M_PI*(-50.); |
---|
| 91 | if (t>6.7) x[8]=-1.2; // 1A |
---|
| 92 | if (t>6.9) x[8]=-10.8; // 9A |
---|
| 93 | if (t>7.1) x[8]=-25.2; // 21A |
---|
| 94 | |
---|
| 95 | if (t>7.3) Ww=2.*M_PI*(-100.); |
---|
| 96 | if (t>7.7) x[8]=-1.2; // 1A |
---|
| 97 | if (t>7.9) x[8]=-10.8; // 9A |
---|
| 98 | if (t>8.1) x[8]=-25.2; // 21A |
---|
| 99 | if (t>8.3) x[8]=10.8; // 9A |
---|
| 100 | if (t>8.5) x[8]=25.2; // 21A |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | int main() { |
---|
| 104 | // Kalman filter |
---|
| 105 | int Ndat = 30000; |
---|
| 106 | double h = 1e-6; |
---|
| 107 | int Nsimstep = 125; |
---|
| 108 | int Npart = 100; |
---|
| 109 | |
---|
| 110 | //StrSim:06: |
---|
| 111 | vec SSAT(Ndat); |
---|
| 112 | |
---|
| 113 | // internal model |
---|
| 114 | IMpmsm fxu; |
---|
| 115 | // Rs Ls dt Fmag(Ypm) kp p J Bf(Mz) |
---|
| 116 | fxu.set_parameters ( 0.28, 0.003465, Nsimstep*h, 0.1989, 1.5 ,4.0, 0.04, 0.0 ); |
---|
| 117 | // observation model |
---|
| 118 | OMpmsm hxu; |
---|
| 119 | |
---|
| 120 | vec mu0= "0.0 0.0 0.0 0.0"; |
---|
| 121 | vec Qdiag ( "0.01 0.01 0.0001 0.0001" ); //zdenek: 0.01 0.01 0.0001 0.0001 |
---|
| 122 | vec Rdiag ( "0.005 0.005" ); //var(diff(xth)) = "0.034 0.034" |
---|
| 123 | chmat Q ( Qdiag ); |
---|
| 124 | chmat R ( Rdiag ); |
---|
| 125 | EKFCh KFE ( rx,ry,ru ); |
---|
| 126 | KFE.set_parameters ( &fxu,&hxu,Q,R ); |
---|
| 127 | KFE.set_est ( mu0, chmat ( 1*ones ( 4 ) ) ); |
---|
| 128 | |
---|
| 129 | RV rQ ( "100","{Q}","4","0" ); |
---|
| 130 | EKF_unQ KFEp ( rx,ry,ru,rQ ); |
---|
| 131 | KFEp.set_parameters ( &fxu,&hxu,Q,R ); |
---|
| 132 | KFEp.set_est ( mu0, chmat ( 1*ones ( 4 ) ) ); |
---|
| 133 | |
---|
| 134 | mgamma_fix evolQ ( rQ,rQ ); |
---|
| 135 | MPF<EKF_unQ> M ( rx,rQ,evolQ,evolQ,Npart,KFEp ); |
---|
| 136 | // initialize |
---|
| 137 | evolQ.set_parameters ( 1000.0 ,Qdiag, 0.5); //sigma = 1/10 mu |
---|
| 138 | evolQ.condition ( Qdiag ); //Zdenek default |
---|
| 139 | epdf& pfinit=evolQ._epdf(); |
---|
| 140 | M.set_est ( pfinit ); |
---|
| 141 | evolQ.set_parameters ( 100000.0, Qdiag, 0.5 ); |
---|
| 142 | |
---|
| 143 | // |
---|
| 144 | |
---|
| 145 | epdf& KFEep = KFE._epdf(); |
---|
| 146 | epdf& Mep = M._epdf(); |
---|
| 147 | |
---|
| 148 | mat Xt=zeros ( Ndat ,9 ); //true state from simulator |
---|
| 149 | mat Dt=zeros ( Ndat,4+2 ); //observation |
---|
| 150 | mat XtE=zeros ( Ndat, 4 ); |
---|
| 151 | mat XtM=zeros ( Ndat,4+4 ); //Q + x |
---|
| 152 | |
---|
| 153 | // SET SIMULATOR |
---|
| 154 | pmsmsim_set_parameters ( 0.28,0.003465,0.1989,0.0,4,1.5,0.04, 200., 3e-6, h ); |
---|
| 155 | double Ww=0.0; |
---|
| 156 | vec dt ( 2 ); |
---|
| 157 | vec ut ( 2 ); |
---|
| 158 | |
---|
| 159 | for ( int tK=1;tK<Ndat;tK++ ) { |
---|
| 160 | //Number of steps of a simulator for one step of Kalman |
---|
| 161 | for ( int ii=0; ii<Nsimstep;ii++ ) { |
---|
| 162 | //simulator |
---|
| 163 | set_simulator_t(Ww); |
---|
| 164 | pmsmsim_step ( Ww ); |
---|
| 165 | }; |
---|
| 166 | // collect data |
---|
| 167 | ut ( 0 ) = KalmanObs[0]; |
---|
| 168 | ut ( 1 ) = KalmanObs[1]; |
---|
| 169 | dt ( 0 ) = KalmanObs[2]; |
---|
| 170 | dt ( 1 ) = KalmanObs[3]; |
---|
| 171 | |
---|
| 172 | //estimator |
---|
| 173 | KFE.bayes ( concat ( dt,ut ) ); |
---|
| 174 | M.bayes ( concat ( dt,ut ) ); |
---|
| 175 | SSAT(tK) = M.SSAT; |
---|
| 176 | |
---|
| 177 | Xt.set_row ( tK,vec ( x,9 ) ); //vec from C-array |
---|
| 178 | 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))) ) ); |
---|
| 179 | XtE.set_row ( tK,KFEep.mean() ); |
---|
| 180 | XtM.set_row ( tK,Mep.mean() ); |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | it_file fou ( "pmsm_sim.it" ); |
---|
| 184 | |
---|
| 185 | fou << Name ( "xth" ) << Xt; |
---|
| 186 | fou << Name ( "Dt" ) << Dt; |
---|
| 187 | fou << Name ( "xthE" ) << XtE; |
---|
| 188 | fou << Name ( "xthM" ) << XtM; |
---|
| 189 | fou << Name ( "SSAT" ) << SSAT; |
---|
| 190 | //Exit program: |
---|
| 191 | |
---|
| 192 | //////////////// |
---|
| 193 | // Just Testing |
---|
| 194 | NcFile nc ( "pmsm_sim.nc", NcFile::Replace ); // Create and leave in define mode |
---|
| 195 | if ( ! nc.is_valid() ) { std::cerr << "creation of NCFile failed."<<endl;} |
---|
| 196 | |
---|
| 197 | write_to_nc ( nc,Xt,"X","{isa isb om th }" ); |
---|
| 198 | write_to_nc ( nc,XtM,"XtM","{q1 q2 isa isb om th }" ); |
---|
| 199 | write_to_nc ( nc,XtE,"XE","{isa isb om th }" ); |
---|
| 200 | write_to_nc ( nc,Dt,"Dt","{isa isb ua ub }" ); |
---|
| 201 | return 0; |
---|
| 202 | } |
---|