- Timestamp:
- 03/20/08 21:41:51 (17 years ago)
- Location:
- tests
- Files:
-
- 2 modified
Legend:
- Unmodified
- Added
- Removed
-
tests/pmsm.h
r42 r48 2 2 #define PMSM_H 3 3 4 //TODO hardcoded RVs!!! 4 5 RV rx ( "1 2 3 4", "{ia, ib, om, th}", ones_i ( 4 ), zeros_i ( 4 )); 5 6 RV ru ( "5 6", "{ua, ub}", ones_i ( 2 ) ,zeros_i ( 2 )); … … 10 11 double Rs, Ls, dt, Ypm, kp, p, J, Mz; 11 12 12 //TODO hardcoded RVs!!!13 13 public: 14 14 IMpmsm() :diffbifn ( rx, ru ) {}; … … 33 33 xk ( 2 ) = omm + kp*p*p * Ypm/J*dt* ( ibm * cos ( thm )-iam * sin ( thm ) ) - p/J*dt*Mz; 34 34 //th 35 xk ( 3 ) = rem(thm + omm*dt, pi); //or 2*pi?35 xk ( 3 ) = rem(thm + omm*dt,2*pi); // <0..2pi> 36 36 return xk; 37 37 } -
tests/pmsm_sim.cpp
r46 r48 20 20 21 21 using namespace itpp; 22 /* 23 // PMSM with Q on Ia and Ib given externally 24 class EKF_unQ : public EKF<chmat> , public BMcond { 22 //!Extended Kalman filter with unknown \c Q 23 class EKF_unQ : public EKFCh , public BMcond { 25 24 public: 26 EKF_unQ( rx,ry,ru,rQ):EKF<chmat>(rx,ry,ru),BMcond(rQ){}; 27 void condition(const vec &Q0){}; 28 };*/ 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 }; 29 33 30 34 … … 33 37 int Ndat = 10000; 34 38 double h = 1e-6; 35 int Nsimstep = 20; 39 int Nsimstep = 125; 40 int Npart = 100; 36 41 37 const int Nll = 10;38 39 // cout << KF;40 42 // internal model 41 43 IMpmsm fxu; … … 46 48 47 49 vec mu0= "0.0 0.0 0.0 0.0"; 48 vec Qdiag ( "0.03 0.03 0.001 0.00001" ); 49 vec Rdiag ( "0.000001 0.000001" ); //var(diff(xth)) = "0.034 0.034" 50 vec vQ = "0.01:0.01:0.1"; 50 vec Qdiag ( "0.05 0.05 0.001 0.001" ); //zdenek: 0.05 0.05 0.001 0.001 51 vec Rdiag ( "0.03 0.03" ); //var(diff(xth)) = "0.034 0.034" 51 52 chmat Q ( Qdiag ); 52 53 chmat R ( Rdiag ); 53 54 EKFCh KFE ( rx,ry,ru ); 55 KFE.set_parameters ( &fxu,&hxu,Q,R ); 54 56 KFE.set_est ( mu0, chmat ( 1000*ones ( 4 ) ) ); 55 KFE.set_parameters ( &fxu,&hxu,Q,R );56 57 57 mat ll ( Nll,Ndat ); 58 RV rQ ( "100","{Q}","4","0" ); 59 EKF_unQ KFEp ( rx,ry,ru,rQ ); 60 KFEp.set_parameters ( &fxu,&hxu,Q,R ); 61 KFEp.set_est ( mu0, chmat ( 1000*ones ( 4 ) ) ); 58 62 59 EKFCh* kfArray[Nll]; 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.05 0.05 0.001 0.001" ); //Zdenek default 68 epdf& pfinit=evolQ._epdf(); 69 M.set_est ( pfinit ); 70 evolQ.set_parameters ( 1000.0 ); 60 71 61 62 for ( int i=0;i<Nll;i++ ) { 63 vec Qid ( Qdiag ); 64 Qid ( 0 ) = vQ ( i ); Qid ( 1 ) = vQ ( i ); 65 kfArray[i]= new EKFCh ( rx,ry,ru ); 66 kfArray[i]->set_est ( mu0, chmat ( 100*ones ( 4 ) ) ); 67 kfArray[i]->set_parameters ( &fxu,&hxu,chmat ( Qid ),R ); 68 } 72 // 69 73 70 74 epdf& KFEep = KFE._epdf(); 75 epdf& Mep = M._epdf(); 71 76 72 77 mat Xt=zeros ( 9,Ndat ); //true state from simulator 73 78 mat Dt=zeros ( 4,Ndat ); //observation 74 79 mat XtE=zeros ( 4,Ndat ); 80 mat XtM=zeros ( 8,Ndat ); //Q + x 75 81 76 82 // SET SIMULATOR … … 80 86 static long k_rampa_tmp=0; 81 87 vec dt ( 2 ); 88 vec dtVS =zeros( 2 ); 89 vec xtVS =zeros(4); 90 vec et ( 4 ); 91 vec wt ( 2 ); 82 92 vec ut ( 2 ); 93 mat XtV=zeros ( 4,Ndat ); 83 94 84 95 for ( int tK=1;tK<Ndat;tK++ ) { … … 101 112 dt ( 0 ) = KalmanObs[2]; 102 113 dt ( 1 ) = KalmanObs[3]; 114 115 // My own simulator for testing : Asuming ut is OK 116 NorRNG.sample_vector ( 4,et ); 117 NorRNG.sample_vector ( 2,wt ); 118 xtVS = fxu.eval ( xtVS,ut ) + Q.sqrt_mult ( et ); 119 dtVS = hxu.eval ( xtVS,ut ) + R.sqrt_mult ( wt ); 120 103 121 //estimator 104 122 KFE.bayes ( concat ( dt,ut ) ); 105 for ( int i=0;i<Nll;i++ ) { 106 kfArray[i]->bayes ( concat ( dt,ut ) ); 107 ll ( i,tK ) =ll ( i,tK-1 ) + kfArray[i]->_ll(); 108 } 123 M.bayes ( concat ( dt,ut ) ); 109 124 110 Xt.set_col ( tK,vec ( x,9 ) ); 125 Xt.set_col ( tK,vec ( x,9 ) ); //vec from C-array 111 126 Dt.set_col ( tK, concat ( dt,ut ) ); 112 127 XtE.set_col ( tK,KFEep.mean() ); 128 XtM.set_col ( tK,Mep.mean() ); 129 XtV.set_col ( tK,xtVS ); 113 130 } 114 131 … … 118 135 fou << Name ( "Dt" ) << Dt; 119 136 fou << Name ( "xthE" ) << XtE; 120 fou << Name ( " ll" ) << ll;121 fou << Name ( " llgrid" ) << vQ;137 fou << Name ( "xthM" ) << XtM; 138 fou << Name ( "xthV" ) << XtV; 122 139 //Exit program: 123 140 return 0;