Changeset 135
- Timestamp:
- 07/25/08 15:04:05 (16 years ago)
- Files:
-
- 5 modified
Legend:
- Unmodified
- Added
- Removed
-
bdm/estim/arx.h
r134 r135 51 51 //! Full constructor 52 52 ARX (RV &rv, mat &V0, double &nu0, double frg0=1.0) : BM(rv),est(rv,V0,nu0), V(est._V()), nu(est._nu()), frg(frg0){last_lognc=est.lognc();tll=0.0;}; 53 void set_parameters(mat &V0, double &nu0){est._V()=V0;est._nu()=nu0;last_lognc=est.lognc();tll= 0.0;}53 void set_parameters(mat &V0, double &nu0){est._V()=V0;est._nu()=nu0;last_lognc=est.lognc();tll=last_lognc;} 54 54 void get_parameters(mat &V0, double &nu0){V0=est._V().to_mat(); nu0=est._nu();} 55 55 //! Here \f$dt = [y_t psi_t] \f$. -
matlab/mex/linefit2.cpp
r133 r135 12 12 // Convert input variables to IT++ format 13 13 mat Data = mxArray2mat ( input[0] ); 14 if ( Data.rows()!=2)15 { if ( Data.cols()==2) Data=Data.T(); else mexErrMsgTxt("Data are not 2D!");}14 if ( Data.rows() !=2 ) 15 { if ( Data.cols() ==2 ) Data=Data.T(); else mexErrMsgTxt ( "Data are not 2D!" );} 16 16 int ilow = mxArray2int ( input[1] )-1; 17 17 int ihi = mxArray2int ( input[2] )-1; //coreection for different indeces … … 20 20 // ------------------ Start of routine --------------------------- 21 21 int ndat=Data.cols(); 22 int npsi=Data.rows() +1; //add a constant22 int npsi=Data.rows() +1; //add a constant 23 23 RV thr ( "1","{ theta_r }",vec_1 ( npsi ),vec_1 ( 0 ) ); 24 24 25 mat V0=1e-8*eye ( npsi ); V0 ( 0,0 ) =1 0.0;26 vec rgr (npsi);27 double nu0= 2;25 mat V0=1e-8*eye ( npsi ); V0 ( 0,0 ) =1e-4; 26 vec rgr ( npsi ); 27 double nu0=0.1; 28 28 // fitting a linear part => third coef is "1" 29 rgr (2) = 1.0;29 rgr ( 2 ) = 1.0; 30 30 31 31 // RESULTS 32 mat Tlls=zeros (ihi,ihi);32 mat Tlls=zeros ( ihi+1,ihi+1); 33 33 // AR model 34 34 ARX Ar ( thr,V0,nu0 ); 35 35 36 36 //Initialize 37 for ( int i=ilow; i<ihi; i++) {38 rgr.set_subvector (0,Data.get_col ( i ));39 Ar.bayes (rgr);37 for ( int i=ilow+2; i<ihi-2; i++ ) { 38 rgr.set_subvector ( 0,Data.get_col ( i ) ); 39 Ar.bayes ( rgr ); 40 40 } 41 Ar.get_parameters(V0,nu0);42 V0 /=V0(1,1);43 nu0=3;44 Ar.set_parameters(V0,nu0);45 46 41 // FLATTEN 47 for (int i=ilow; i<ihi; i++) { 48 Ar.set_parameters(V0,nu0); 49 for (int t=i+1; t<ihi; t++) { 50 rgr.set_subvector(0,Data.get_col ( t )); 51 Ar.bayes(rgr); 52 Tlls(i,t)=Ar._tll(); 42 /* Ar.get_parameters ( V0,nu0 ); 43 V0 *=1.0/nu0/10000.; // same flattening factor as for nu0 44 nu0=1./10000.;*/ 45 Ar.set_parameters ( V0,nu0 ); 46 47 for ( int i=ilow; i<=ihi; i++ ) { 48 Ar.set_parameters ( V0,nu0 ); 49 for ( int t=i; t<=ihi; t++ ) { 50 rgr.set_subvector ( 0,Data.get_col ( t ) ); 51 Ar.bayes ( rgr ); 52 Tlls ( i,t ) =Ar._tll(); 53 53 } 54 54 } 55 55 56 max_index (Tlls,ilow,ihi);56 max_index ( Tlls,ilow,ihi ); 57 57 58 58 // ------------------ End of routine ----------------------------- 59 59 60 60 output[0] = mxCreateDoubleMatrix ( 2,1, mxREAL ); 61 ivec2mxArray ( vec_2(ilow+1,ihi+1) , output[0] );61 ivec2mxArray ( vec_2 ( ilow+1,ihi+1 ) , output[0] ); 62 62 63 63 if ( n_output>1 ) { 64 // Create output vectors65 output[1] = mxCreateDoubleMatrix ( Tlls.rows(),Tlls.cols(), mxREAL );66 // Convert the IT++ format to Matlab format for output67 mat2mxArray ( Tlls, output[1] );64 // Create output vectors 65 output[1] = mxCreateDoubleMatrix ( Tlls.rows(),Tlls.cols(), mxREAL ); 66 // Convert the IT++ format to Matlab format for output 67 mat2mxArray ( Tlls, output[1] ); 68 68 } 69 69 70 if ( n_output>2 ) { 71 Ar.set_parameters ( V0,nu0 ); 72 // Redo Ar for given points 73 for ( int i=ilow; i<=ihi; i++ ) { 74 rgr.set_subvector ( 0,Data.get_col ( i ) ); 75 Ar.bayes ( rgr ); 76 } 77 78 // Create output vectors 79 output[2] = mxCreateDoubleMatrix ( 3,1, mxREAL ); 80 // Convert the IT++ format to Matlab format for output 81 // mat2mxArray ( Ar._epdf().mean(), output[2] ); 82 mat2mxArray ( Ar._epdf().mean(), output[2] ); 83 } 84 70 85 } -
pmsm/sim_var.cpp
r131 r135 40 40 vec dt ( 2 ); 41 41 vec ut ( 2 ); 42 vec dut ( 4);42 vec dut ( 2 ); 43 43 vec dit (2); 44 44 vec xtm=zeros ( 4 ); 45 vec xte=zeros ( 4 ); 45 46 vec xdif=zeros ( 4 ); 46 47 vec xt ( 4 ); … … 95 96 //Number of steps of a simulator for one step of Kalman 96 97 for ( int ii=0; ii<Nsimstep;ii++ ) { 97 sim_profile_steps1 ( Ww , true);98 sim_profile_steps1 ( Ww , false); 98 99 pmsmsim_step ( Ww ); 99 100 }; … … 105 106 dut ( 0 ) = KalmanObs[4]; 106 107 dut ( 1 ) = KalmanObs[5]; 107 dut ( 2 ) = KalmanObs[6];108 dut ( 3 ) = KalmanObs[7];109 108 dit ( 0 ) = KalmanObs[8]; 110 109 dit ( 1 ) = KalmanObs[9]; 111 110 112 xt = fxu.eval ( xtm,ut );111 xte = fxu.eval ( xtm,ut ); 113 112 //Results: 114 // in xt we have simulat on according to the model113 // in xt we have simulation according to the model 115 114 // in x we have "reality" 116 xt m ( 0 ) =x[0];xtm ( 1 ) =x[1];xtm ( 2 ) =x[2];xtm( 3 ) =x[3];117 xdif = xt m-xt;115 xt ( 0 ) =x[0];xt ( 1 ) =x[1];xt ( 2 ) =x[2];xt ( 3 ) =x[3]; 116 xdif = xt-xte; //xtm is a copy of x[] 118 117 if (xdif(0)>3.0){ 119 118 cout << "here" <<endl; … … 122 121 if ( xdif ( 3 ) <-pi ) xdif ( 3 ) +=2*pi; 123 122 124 ddif = hxu.eval(xt m,ut) - dt;123 ddif = hxu.eval(xt,ut) - dit; 125 124 126 125 //Rt = 0.9*Rt + xdif^2 127 Qt*=0.01; 128 Qt += outer_product ( xdif,xdif ); //(x-xt)^2 129 Rt*=0.01; 130 Rt += outer_product ( ddif,ddif ); //(x-xt)^2 126 Qt*=0.1; 127 Qt += 10*outer_product ( xdif,xdif ); //(x-xt)^2 128 129 if (Qt(0,0)>3.0){ 130 cout << "here" <<endl; 131 } 132 // For future ref. 133 xtm = xt; 134 135 Rt*=0.1; 136 // Rt += 10*outer_product ( ddif,ddif ); //(x-xt)^2 131 137 132 138 //ESTIMATE 133 139 Efix.bayes(concat(dt,ut)); 134 140 // 135 Eop.set_parameters ( &fxu,&hxu,(Qt+1e- 16*eye(4)),(Rt+1e-3*eye(2)));141 Eop.set_parameters ( &fxu,&hxu,(Qt+1e-8*eye(4)),(Rt+1e-6*eye(2))); 136 142 Eop.bayes(concat(dt,ut)); 137 143 // … … 154 160 155 161 L.step(true); 156 //L.itsave("sim_var.it");162 //L.itsave("sim_var.it"); 157 163 158 164 -
pmsm/sim_var_arx.cpp
r131 r135 43 43 44 44 //Autoregressive model 45 ARX Ar_a ( rgr,V0,nu0 ,0.9 9);46 ARX Ar_b ( rgr,V0,nu0 ,0.9 9);45 ARX Ar_a ( rgr,V0,nu0 ,0.95 ); 46 ARX Ar_b ( rgr,V0,nu0 ,0.95 ); 47 47 epdf& pA= Ar_a._epdf(); 48 48 epdf& pB= Ar_b._epdf(); -
pmsm/simulator_zdenek/simulator.cpp
r130 r135 73 73 // real-time 74 74 double t=0.; //VS removed static due to clash with export in .h 75 76 // stator voltage components in alfa beta (inluding impact of the real dc-link voltage) 77 static double ualfa=0., ubeta=0.; 75 78 76 79 void pmsmsim_set_parameters(double Rs0, double Ls0, double Fmag0, double Bf0, double p0, double kp0, double J0, double Uc0, double DT0, double dt0) … … 114 117 citac2=abs(0-(int)(DT/h)); //VS: oprava, je to spravne? 115 118 citac_PR=h_reg_counter_mez; 119 120 // first interrupt occur after first period match => add 1 to both counter registers 121 citac++;smer=1; 122 citac2--; 116 123 117 124 modulace=1; // THIPWM … … 304 311 void pmsmsim_step(double Ww) // you must link array KalmanObs[] to EKF modul 305 312 { 306 double Umk, u a, ub;313 double Umk, ub, uc; 307 314 308 315 // while (t<=t_end) … … 310 317 pwm(modulace); 311 318 // *us=KalmanObs[0]; *(us+1)=KalmanObs[1]; 319 // *us=ualfa; *(us+1)=ubeta; 312 320 pmsm_model(5); 313 321 … … 315 323 { 316 324 // voltages and measured currents for EKF 317 Umk=*u*Uc/Ucn; 318 ua=Umk*cos(*(u+1)); 319 ub=Umk*cos(*(u+1)-2./3.*M_PI); 320 KalmanObs[0]=ua; // usx 321 KalmanObs[1]=(ua+2.*ub)/sqrt(3.); // usy 325 // Umk=*u*Uc/Ucn; 326 // ualfa=Umk*cos(*(u+1)); 327 // ub=Umk*cos(*(u+1)-2./3.*M_PI); 328 KalmanObs[0]=ualfa; // usx 329 //KalmanObs[1]=(ualfa+2.*ub)/sqrt(3.); // usy 330 KalmanObs[1]=ubeta; // usy 322 331 323 332 // real sampling - considered transport delay equal to the sampling period … … 341 350 Isx=x[0];Isy=x[1];speed=x[2];theta=x[3]; 342 351 352 // include ideal commanded stator voltage 353 Umk=*u*Uc/Ucn; 354 ualfa=Umk*cos(*(u+1)); // usx = usa 355 ub=Umk*cos(*(u+1)-2./3.*M_PI); 356 ubeta=(ualfa+2.*ub)/sqrt(3.); // usy 357 // uc=-ualfa-ub; 358 // ubeta=(ub-uc)/sqrt(3.); 359 343 360 h_reg_counter=0; 344 361 }