Changeset 63
- Timestamp:
- 04/09/08 15:29:53 (17 years ago)
- Location:
- tests
- Files:
-
- 4 modified
Legend:
- Unmodified
- Added
- Removed
-
tests/pmsm.h
r62 r63 12 12 //! State evolution model for a PMSM drive and its derivative with respect to \$x\$ 13 13 class IMpmsm : public diffbifn { 14 protected: 14 15 double Rs, Ls, dt, Ypm, kp, p, J, Mz; 15 16 16 17 public: 17 18 IMpmsm() :diffbifn (rx.count(), rx, ru ) {}; 19 //! Set mechanical and electrical variables 20 void set_parameters ( double Rs0, double Ls0, double dt0, double Ypm0, double kp0, double p0, double J0, double Mz0 ) {Rs=Rs0; Ls=Ls0; dt=dt0; Ypm=Ypm0; kp=kp0; p=p0; J=J0; Mz=Mz0;} 21 22 vec eval ( const vec &x0, const vec &u0 ) { 23 // last state 24 double iam = x0 ( 0 ); 25 double ibm = x0 ( 1 ); 26 double omm = x0 ( 2 ); 27 double thm = x0 ( 3 ); 28 double uam = u0 ( 0 ); 29 double ubm = u0 ( 1 ); 30 31 vec xk=zeros ( 4 ); 32 //ia 33 xk ( 0 ) = ( 1.0- Rs/Ls*dt ) * iam + Ypm/Ls*dt*omm * sin ( thm ) + uam*dt/Ls; 34 //ib 35 xk ( 1 ) = ( 1.0- Rs/Ls*dt ) * ibm - Ypm/Ls*dt*omm * cos ( thm ) + ubm*dt/Ls; 36 //om 37 xk ( 2 ) = omm + kp*p*p * Ypm/J*dt* ( ibm * cos ( thm )-iam * sin ( thm ) ) - p/J*dt*Mz; 38 //th 39 xk ( 3 ) = rem(thm + omm*dt,2*pi); // <0..2pi> 40 return xk; 41 } 42 43 void dfdx_cond ( const vec &x0, const vec &u0, mat &A, bool full=true ) { 44 double iam = x0 ( 0 ); 45 double ibm = x0 ( 1 ); 46 double omm = x0 ( 2 ); 47 double thm = x0 ( 3 ); 48 // d ia 49 A ( 0,0 ) = ( 1.0- Rs/Ls*dt ); A ( 0,1 ) = 0.0; 50 A ( 0,2 ) = Ypm/Ls*dt* sin ( thm ); A ( 0,3 ) = Ypm/Ls*dt*omm * ( cos ( thm ) ); 51 // d ib 52 A ( 1,0 ) = 0.0 ; A ( 1,1 ) = ( 1.0- Rs/Ls*dt ); 53 A ( 1,2 ) = -Ypm/Ls*dt* cos ( thm ); A ( 1,3 ) = Ypm/Ls*dt*omm * ( sin ( thm ) ); 54 // d om 55 A ( 2,0 ) = kp*p*p * Ypm/J*dt* ( - sin ( thm ) ); 56 A ( 2,1 ) = kp*p*p * Ypm/J*dt* ( cos ( thm ) ); 57 A ( 2,2 ) = 1.0; 58 A ( 2,3 ) = kp*p*p * Ypm/J*dt* ( -ibm * sin ( thm )-iam * cos ( thm ) ); 59 // d th 60 A ( 3,0 ) = 0.0; A ( 3,1 ) = 0.0; A ( 3,2 ) = dt; A ( 3,3 ) = 1.0; 61 } 62 63 void dfdu_cond ( const vec &x0, const vec &u0, mat &A, bool full=true ) {it_error ( "not needed" );}; 64 65 }; 66 67 class IMpmsmStat : public IMpmsm { 68 IMpmsmStat() :IMpmsm() {}; 18 69 //! Set mechanical and electrical variables 19 70 void set_parameters ( double Rs0, double Ls0, double dt0, double Ypm0, double kp0, double p0, double J0, double Mz0 ) {Rs=Rs0; Ls=Ls0; dt=dt0; Ypm=Ypm0; kp=kp0; p=p0; J=J0; Mz=Mz0;} -
tests/pmsm_sim.cpp
r54 r63 18 18 #include "pmsm.h" 19 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 else32 sprintf ( tmpstr,"%s_%d",Xn.c_str(),j );33 // Create variables and their attributes34 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 20 41 21 using namespace itpp; … … 146 126 //Exit program: 147 127 148 ////////////////149 // Just Testing150 NcFile nc ( "pmsm_sim.nc", NcFile::Replace ); // Create and leave in define mode151 if ( ! nc.is_valid() ) { std::cerr << "creation of NCFile failed."<<endl;}152 153 write_to_nc ( nc,Xt,"X","{isa isb om th }" );154 write_to_nc ( nc,XtM,"XtM","{q1 q2 isa isb om th }" );155 write_to_nc ( nc,XtE,"XE","{isa isb om th }" );156 write_to_nc ( nc,Dt,"Dt","{isa isb ua ub }" );157 128 return 0; 158 129 } -
tests/pmsm_sim2.cpp
r62 r63 32 32 preA.set_submatrix ( dimy+dimx,dimy,Q._Ch() ); 33 33 }; 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 44 ll = -0.5* ( 4 * 1.83787706640935 +_P->logdet() +xtrue* ( inv(_P->to_mat())*xtrue ) ); 45 } 46 }; 47 class EKF_unQful : public EKFfull , public BMcond { 48 public: 49 //! Default constructor 50 EKF_unQful ( RV rx, RV ry,RV ru,RV rQ ) :EKFfull ( rx,ry,ru ),BMcond ( rQ ) {}; 51 void condition ( const vec &Q0 ) { 52 Q=diag(Q0); 53 }; 54 void bayes(const vec dt){ 55 EKFfull::bayes(dt); 56 57 vec xtrue(4); 58 //UGLY HACK!!! reliance on a predictor!! 59 xtrue(0)=x[0]; 60 xtrue(1)=x[1]; 61 xtrue(2)=x[2]; 62 xtrue(3)=x[3]; 63 64 BM::ll = -0.5* ( 4 * 1.83787706640935 +log(det(P)) +xtrue* ( inv(P)*xtrue ) ); 65 } 34 66 }; 35 67 … … 82 114 if (t>8.5) x[8]=25.2; // 21A 83 115 84 // x[8]=0.0; 116 if (t>9) Ww=2.*M_PI*0; 117 x[8]=0.0; 85 118 } 86 119 … … 90 123 double h = 1e-6; 91 124 int Nsimstep = 125; 92 int Npart = 100;125 int Npart = 50; 93 126 94 127 //StrSim:06: … … 104 137 vec mu0= "0.0 0.0 0.0 0.0"; 105 138 // vec Qdiag ( "0.01 0.01 0.01 0.0001" ); //zdenek: 0.01 0.01 0.0001 0.0001 106 vec Qdiag ( "1 8 18 157.9 0.0001" ); //zdenek: 0.01 0.01 0.0001 0.0001107 vec Rdiag ( " 90 90" ); //var(diff(xth)) = "0.034 0.034"139 vec Qdiag ( "10 10 10 0.001" ); //zdenek: 0.01 0.01 0.0001 0.0001 140 vec Rdiag ( "100 100" ); //var(diff(xth)) = "0.034 0.034" 108 141 chmat Q ( Qdiag ); 109 142 chmat R ( Rdiag ); 110 143 EKFCh KFE ( rx,ry,ru ); 111 KFE.set_est ( mu0, ( 1*eye ( 4 ) ) );112 KFE.set_parameters ( &fxu,&hxu,Q diag,Rdiag);144 KFE.set_est ( mu0, chmat( 1*eye ( 4 ) ) ); 145 KFE.set_parameters ( &fxu,&hxu,Q,R); 113 146 114 147 RV rQ ( "100","{Q}","4","0" ); 115 EKF_unQ KFEp ( rx,ry,ru,rQ );116 KFEp.set_est ( mu0, chmat ( 1*ones ( 4 )) );117 KFEp.set_parameters ( &fxu,&hxu, Q,R);148 EKF_unQful KFEp ( rx,ry,ru,rQ ); 149 KFEp.set_est ( mu0, 1*ones ( 4 ) ); 150 KFEp.set_parameters ( &fxu,&hxu,diag(Qdiag),diag(Rdiag) ); 118 151 119 152 mgamma_fix evolQ ( rQ,rQ ); 120 MPF<EKF_unQ > M ( rx,rQ,evolQ,evolQ,Npart,KFEp );153 MPF<EKF_unQful> M ( rx,rQ,evolQ,evolQ,Npart,KFEp ); 121 154 // initialize 122 155 evolQ.set_parameters ( 1000.0 ,Qdiag, 0.5); //sigma = 1/10 mu … … 124 157 epdf& pfinit=evolQ._epdf(); 125 158 M.set_est ( pfinit ); 126 evolQ.set_parameters ( 5000.0, Qdiag, 0.9999 );159 evolQ.set_parameters ( 100000.0, Qdiag, 0.9999 ); 127 160 128 161 // … … 166 199 } 167 200 168 it_file fou ( "pmsm_sim.it" );201 /* it_file fou ( "pmsm_sim.it" ); 169 202 170 203 fou << Name ( "xth" ) << Xt; … … 172 205 fou << Name ( "xthE" ) << XtE; 173 206 fou << Name ( "xthM" ) << XtM; 174 fou << Name ( "SSAT" ) << SSAT; 207 fou << Name ( "SSAT" ) << SSAT;*/ 175 208 //Exit program: 176 209 -
tests/pmsm_sim3.cpp
r62 r63 27 27 void set_simulator_t(double &Ww) { 28 28 29 if (t>0. 2) x[8]=1.2; // 1A //0.2ZP29 if (t>0.0002) x[8]=1.2; // 1A //0.2ZP 30 30 if (t>0.4) x[8]=10.8; // 9A 31 31 if (t>0.6) x[8]=25.2; // 21A … … 73 73 if (t>8.5) x[8]=25.2; // 21A 74 74 75 //x[8]=0.0;75 x[8]=0.0; 76 76 } 77 77 … … 81 81 double h = 1e-6; 82 82 int Nsimstep = 125; 83 int Npart = 500;83 int Npart = 100; 84 84 85 85 86 86 vec mu0= "0.0 0.0 0.0 0.0"; 87 vec Qdiag ( "0.0 1 0.01 0.0001 0.0001" ); //zdenek: 0.01 0.01 0.0001 0.000187 vec Qdiag ( "0.05 0.05 0.002 0.001" ); //zdenek: 0.01 0.01 0.0001 0.0001 88 88 vec Rdiag ( "0.05 0.05" ); //var(diff(xth)) = "0.034 0.034" 89 89 chmat Q ( Qdiag ); … … 99 99 MPF<EKFfixed> M ( rx,rQ,evolQ,evolQ,Npart,KFE ); 100 100 // initialize 101 evolQ.set_parameters ( 10 00.0 ,Qdiag, 0.5); //sigma = 1/10 mu101 evolQ.set_parameters ( 10.0 ,Qdiag, 1.0); //sigma = 1/10 mu 102 102 evolQ.condition ( Qdiag ); //Zdenek default 103 103 epdf& pfinit=evolQ._epdf(); 104 104 M.set_est ( pfinit ); 105 evolQ.set_parameters ( 100000.0, Qdiag, 0.99999 );105 evolQ.set_parameters ( 500000.0, Qdiag, 0.9999 ); 106 106 107 107 … … 111 111 112 112 mat Xt=zeros ( Ndat ,9 ); //true state from simulator 113 mat Dt=zeros ( Ndat,4 +2); //observation113 mat Dt=zeros ( Ndat,4 ); //observation 114 114 mat XtE=zeros ( Ndat, 4 ); 115 115 mat XtM=zeros ( Ndat,4+4 ); //Q + x … … 118 118 pmsmsim_set_parameters ( 0.28,0.003465,0.1989,0.0,4,1.5,0.04, 200., 3e-6, h ); 119 119 double Ww=0.0; 120 static int k_rampa=1; 121 static long k_rampa_tmp=0; 122 120 123 vec dt ( 2 ); 121 124 vec ut ( 2 ); … … 125 128 for ( int ii=0; ii<Nsimstep;ii++ ) { 126 129 //simulator 127 set_simulator_t(Ww); 130 Ww+=k_rampa*2.*M_PI*2e-4; //1000Hz/s 131 if ( Ww>2.*M_PI*150. ) { 132 Ww=2.*M_PI*150.; 133 if ( k_rampa_tmp<500000 ) k_rampa_tmp++; 134 else {k_rampa=-1;k_rampa_tmp=0;} 135 }; 136 if ( Ww<-2.*M_PI*150. ) Ww=-2.*M_PI*150.; /* */ 137 // set_simulator_t(Ww); 128 138 pmsmsim_step ( Ww ); 129 139 }; … … 138 148 139 149 Xt.set_row ( tK,vec ( x,9 ) ); //vec from C-array 140 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))) ));150 Dt.set_row ( tK, concat ( dt,ut)); 141 151 XtE.set_row ( tK,KFEep.mean() ); 152 double qNaN = std::numeric_limits<double>::quiet_NaN(); 142 153 XtM.set_row ( tK,Mep.mean() ); 143 154 }