[258] | 1 | /*! |
---|
| 2 | \file |
---|
| 3 | \brief DataSource for experiments with realistic simulator of the PMSM model |
---|
| 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 <stat/loggers.h> |
---|
[260] | 14 | #include "simulator.h" |
---|
[258] | 15 | #include "pmsm.h" |
---|
| 16 | |
---|
| 17 | //! Simulator of PMSM machine with predefined profile on omega |
---|
| 18 | class pmsmDS : public DS { |
---|
| 19 | |
---|
| 20 | protected: |
---|
| 21 | //! indeces of logged variables |
---|
[260] | 22 | int L_x, L_ou, L_oy, L_iu, L_optu; |
---|
[258] | 23 | //! Setpoints of omega in timespans given by dt_prof |
---|
| 24 | vec profileWw; |
---|
| 25 | //! time-step for profiles |
---|
| 26 | double dt_prof; |
---|
| 27 | //! Number of miliseconds per discrete time step |
---|
| 28 | int Dt; |
---|
[260] | 29 | //! options for logging, string in which each character has a meaning: |
---|
| 30 | //! \c u - log predictions of 'true' voltage |
---|
| 31 | string opt; |
---|
[258] | 32 | public: |
---|
[260] | 33 | //! Constructor with fixed sampling period |
---|
| 34 | pmsmDS (string opt0="" ) : Dt ( 125 ), opt(opt0) {} |
---|
[258] | 35 | void set_parameters ( double Rs0, double Ls0, double Fmag0, double Bf0, double p0, double kp0, double J0, double Uc0, double DT0, double dt0 ) { |
---|
| 36 | pmsmsim_set_parameters ( Rs0, Ls0, Fmag0, Bf0, p0, kp0, J0, Uc0, DT0, dt0 ); |
---|
| 37 | } |
---|
| 38 | void getdata ( vec &dt ) {dt=vec ( KalmanObs,6 );} |
---|
| 39 | void write ( vec &ut ) {} |
---|
| 40 | |
---|
| 41 | void step() { |
---|
| 42 | static int ind=0; |
---|
| 43 | static double dW; // increase of W |
---|
| 44 | static double Ww; // W |
---|
| 45 | if ( t>=dt_prof*ind ) { |
---|
[260] | 46 | ind++; |
---|
[258] | 47 | if ( ind<profileWw.length() ) { |
---|
| 48 | //linear increase |
---|
[260] | 49 | if ( profileWw.length() ==1 ) { |
---|
| 50 | Ww=profileWw ( 0 ); dW=0.0;} |
---|
| 51 | else { |
---|
| 52 | dW = profileWw ( ind )-profileWw ( ind-1 ); |
---|
| 53 | dW *=125e-6/dt_prof; |
---|
| 54 | } |
---|
[258] | 55 | } |
---|
| 56 | else { |
---|
| 57 | dW = 0; |
---|
| 58 | } |
---|
| 59 | } |
---|
| 60 | Ww += dW; |
---|
| 61 | //Simulate Dt seconds! |
---|
| 62 | for ( int i=0;i<Dt;i++ ) { pmsmsim_step ( Ww );} |
---|
| 63 | }; |
---|
| 64 | |
---|
| 65 | void log_add ( logger &L ) { |
---|
| 66 | L_x = L.add ( rx, "x" ); |
---|
[260] | 67 | L_oy = L.add ( ry, "obs" ); |
---|
| 68 | L_ou = L.add ( ru, "obs" ); |
---|
| 69 | L_iu = L.add ( ru, "true" ); |
---|
| 70 | // log differences |
---|
| 71 | if (opt.find("u")==string::npos){ |
---|
| 72 | L_optu = L.add(ru, "model"); |
---|
| 73 | } |
---|
[258] | 74 | } |
---|
| 75 | |
---|
| 76 | void logit ( logger &L ) { |
---|
| 77 | L.logit ( L_x, vec ( x,4 ) ); |
---|
| 78 | L.logit ( L_oy, vec_2 ( KalmanObs[2],KalmanObs[3] ) ); |
---|
| 79 | L.logit ( L_ou, vec_2 ( KalmanObs[0],KalmanObs[1] ) ); |
---|
| 80 | L.logit ( L_iu, vec_2 ( KalmanObs[4],KalmanObs[5] ) ); |
---|
[260] | 81 | if (opt.find("u")==string::npos){ |
---|
| 82 | double sq3=sqrt(3.0); |
---|
| 83 | double ua,ub; |
---|
| 84 | double i1=x[0]; |
---|
| 85 | double i2=0.5*(-i1+sq3*x[1]); |
---|
| 86 | double i3=0.5*(-i1-sq3*x[1]); |
---|
| 87 | double u1=KalmanObs[0]; |
---|
| 88 | double u2=0.5*(-u1+sq3*KalmanObs[1]); |
---|
| 89 | double u3=0.5*(-u1-sq3*KalmanObs[1]); |
---|
| 90 | |
---|
| 91 | double du1=0.7*(double(i1>0.1) - double(i1<-0.1))+0.05*i1; |
---|
| 92 | double du2=0.7*(double(i2>0.1) - double(i2<-0.1))+0.05*i2; |
---|
| 93 | double du3=0.7*(double(i3>0.1) - double(i3<-0.1))+0.05*i3; |
---|
| 94 | ua = (2.0*(u1-du1)-(u2-du2)-(u3-du3))/3.0; |
---|
| 95 | ub = ((u2-du2)-(u3-du3))/sq3; |
---|
| 96 | L.logit( L_optu , vec_2 (ua,ub)); |
---|
| 97 | } |
---|
| 98 | } |
---|
[258] | 99 | |
---|
| 100 | void set_profile ( double dt, const vec &Ww ) {dt_prof=dt; profileWw=Ww;} |
---|
| 101 | }; |
---|