Changeset 1201 for applications/pmsm/simulator_zdenek
- Timestamp:
- 09/29/10 11:22:31 (14 years ago)
- Location:
- applications/pmsm/simulator_zdenek/ekf_example
- Files:
-
- 2 modified
Legend:
- Unmodified
- Added
- Removed
-
applications/pmsm/simulator_zdenek/ekf_example/ekf_obj.cpp
r1183 r1201 34 34 35 35 if ( BM::evalll ) { 36 //from enorm36 /* //from enorm 37 37 vec xdif(x,4);//first 4 of x 38 38 //UGLY HACK!!! reliance on a predictor!! … … 42 42 xdif(3)=x[3]-zprevod(x_pred[3],15);*/ 43 43 44 xdif -=xhat; //(xdif=x-xhat)44 // xdif -=xhat; //(xdif=x-xhat) 45 45 46 46 mat Pfull(4,4); … … 51 51 52 52 53 BM::ll = -0.5* ( 4 * 1.83787706640935 +log ( det ( Pfull ) ) +xdif* ( inv(Pfull)*xdif ) );53 // BM::ll = -0.5* ( 4 * 1.83787706640935 +log ( det ( Pfull ) ) +xdif* ( inv(Pfull)*xdif ) );*/ 54 54 } 55 55 }; … … 77 77 78 78 // implementace v PC 79 t_sin=prevod(sin(Thetaref*x_est[3]/32768.),15); 80 t_cos=prevod(cos(Thetaref*x_est[3]/32768.),15); 81 79 //t_sin=prevod(sin(Thetaref*x_est[3]/32768.),15); 80 //t_cos=prevod(cos(Thetaref*x_est[3]/32768.),15); 81 82 t_sin=prevod(sin(Thetaref*x_est[3]/Qm),15); 83 t_cos=prevod(cos(Thetaref*x_est[3]/Qm),15); 84 82 85 tmp=((long)cB*x_est[2])>>15; 83 86 x_pred[0]=((long)cA*x_est[0]+(long)tmp*t_sin+(long)cC*ux[0])>>15; … … 153 156 { 154 157 // Tuning of matrix Q 155 Q[0]=prevod(.0 1,15); // 0.05158 Q[0]=prevod(.05,15); // 0.05 156 159 Q[5]=Q[0]; 157 Q[10]=prevod(0.000 1,15); // 1e-3158 Q[15]=prevod(0.00 01,15); // 1e-3160 Q[10]=prevod(0.0002,15); // 1e-3 161 Q[15]=prevod(0.001,15); // 1e-3 159 162 160 163 // Tuning of matrix R 161 R[0]=prevod(0. 05,15); // 0.05164 R[0]=prevod(0.1,15); // 0.05 162 165 R[3]=R[0]; 163 166 … … 170 173 // cF=prevod(p*Tv*Mref/J/Wref,15); 171 174 cG=prevod(Tv*Wref*4/Thetaref,15); 172 cH=prevod(Tv*Wref*Fmag/Iref/Ls*Thetaref,15); 173 // cI=prevod(kp*p*p*Tv*Fmag*Iref/J/Wref*Thetaref); 175 // cH=prevod(Tv*Wref*Fmag/Iref/Ls*Thetaref,15); 176 cH=prevod(Tv*Wref*Fmag/Iref/Ls,15); 177 // cI=prevod(kp*p*p*Tv*Fmag*Iref/J/Wref*Thetaref); 174 178 175 179 /* Init matrix PSI with permanently constant terms */ … … 179 183 PSI[14]=cG; 180 184 PSI[15]=0x7FFF; 185 186 P_est[0]=0x7FFF; 187 P_est[5]=0x7FFF; 188 P_est[10]=0x7FFF; 189 P_est[15]=0x7FFF; 181 190 } 182 191 … … 232 241 D = est._R()._D(); 233 242 234 bool DBG=true;235 if (DBG){236 mat L;237 ivec tmp;238 mat X=randn(5,5);239 mat XX=X*X.T();240 mat T=diag(sqrt(1.0/diag(XX)));241 XX= T*XX*T;242 243 ldmat ldd(XX);244 U = ldd._L().T();245 D = ldd._D()*0.9;246 }247 248 243 //////////// 249 244 … … 254 249 255 250 ////// 256 vec xref(5);251 /* vec xref(4); 257 252 xref(0)= 30.0*1.4142; 258 253 xref(1)= 30.0*1.4142; 259 254 xref(2)= 6.283185*200.; 260 xref(3) = 3.141593; 261 xref(4) = 34.0; 262 263 if (DBG){ 264 xref = ones(5); 265 } 266 267 imat Utf; 268 ivec Dtf; 269 UDtof(U,D,Utf,Dtf,xref); 270 mat invxref=diag(1.0/xref); 271 imat Af=round_i(invxref*A*diag(xref)*(1<<15)); 272 mat_to_int(Af, PSI); 273 mat_to_int(Utf,Uf); 274 vec_to_int(Dtf, Df); 275 276 // cout << Dtf << endl; 277 278 // A*U == PSI*U 279 mmultAU(PSI,Uf,PSIU,5,5); 280 /* cout << ivec(PSIU,25) << endl; 281 cout << (Af*Utf)/(1<<15) <<endl;*/ 255 xref(3) = 3.141593;*/ 256 //xref(4) = 34.0; 257 282 258 283 259 vec Din = D; … … 336 312 337 313 } 338 UDtof(U,D,Utf,Dtf,xref); 339 // cout << G << endl << Din << endl; 340 imat Urw=Utf.T(); 341 cout << ivec(Urw._data(),25) << endl << Dtf << endl; 342 } 343 344 int Qf[25]; 345 imat Qrnd=round_i(diag(elem_div(diag(Q),pow(xref,2)))*(1<<15)); 346 // cout << "Qr" << Qrnd<<endl; 347 mat_to_int(Qrnd,Qf); 348 349 // cout << Df <<endl; 350 thorton(Uf,Df,PSIU,Qf,Gf,Dfold,5); 351 352 if (DBG){ 353 _mu = vec("0.500 0.200 0.300 0.400 0.1"); 354 } 355 356 cout << "bierman double I" <<endl; 357 UDtof(U,D,Utf,Dtf,xref); 358 cout << "Uf: " << Utf << endl; 359 cout << "Df: " << Dtf << endl; 360 cout << "xf:" << round_i(elem_div(_mu,xref)*(1<<15)) <<endl; 361 mat_to_int(Utf,Uf); 362 vec_to_int(Dtf, Df); 363 314 } 315 364 316 // bierman 365 317 … … 370 322 vec xp=_mu; // used in bierman 371 323 372 if (DBG){373 yp = vec("0.40 0.300");374 }375 324 376 325 for (int iy=0; iy<dimy; iy++){ … … 402 351 } 403 352 404 UDtof(U,D,Utf,Dtf,xref);405 cout << "Uf: " << Utf << endl;406 cout << "Df: " << Dtf << endl;407 cout << "xf:" << round_i(elem_div(_mu,xref)*(1<<15)) <<endl;408 409 int difz[2];410 vec_to_int(round_i((yt-yp)/xref(0)*(1<<15)), difz);411 int xf[5];412 vec_to_int(round_i(elem_div(xp,xref)*(1<<15)), xf);413 int Rf[2];//={1,1};414 vec_to_int(round_i(R/pow(xref(0),2)*(1<<15)), Rf);415 //beirman(int *difz, int *xp, int *U, int *D, int *R, unsigned int dimy, unsigned int dimx416 417 cout <<endl<< "bierman fixed" <<endl;418 cout << "Uf: "; for (i=0; i<25;i++) cout << Uf[i] << ","; cout <<endl;419 cout << "Df: "; for (i=0; i<5;i++) cout << Df[i] << ","; cout << endl;420 cout << "xf: "; for (i=0; i<5;i++) cout << xf[i] << ","; cout << endl;421 422 cout << "dz: "; for (i=0; i<2;i++) cout << difz[i] << ","; cout << endl;423 424 int xf_old[5];425 vec_to_int(ivec(xf,5),xf_old);426 bierman(difz,xf, Uf, Df, Rf, 2, 5);427 428 UDtof(U,D,Utf,Dtf,xref);429 cout << "Uf: "; for (i=0; i<25;i++) cout << Uf[i] << ","; cout <<endl;430 cout << "Df: "; for (i=0; i<5;i++) cout << Df[i] << ","; cout << endl;431 cout << "xf: "; for (i=0; i<5;i++) cout << xf[i] << ","; cout << endl;432 cout << endl;433 434 435 353 436 354 ///// … … 495 413 496 414 // implementace v PC 415 /* t_sin=prevod(sin(Thetaref*x_est[3]/32768.),15); 416 t_cos=prevod(cos(Thetaref*x_est[3]/32768.),15);*/ 497 417 t_sin=prevod(sin(Thetaref*x_est[3]/32768.),15); 498 418 t_cos=prevod(cos(Thetaref*x_est[3]/32768.),15); 499 419 500 420 tmp=((long)cB*x_est[2])>>15; 501 x_est[0]=((long)cA*x_est[0]+(long)tmp*t_sin+(long)cC* uf[0])>>15;502 x_est[1]=((long)cA*x_est[1]-(long)tmp*t_cos+(long)cC* uf[1])>>15;421 x_est[0]=((long)cA*x_est[0]+(long)tmp*t_sin+(long)cC*(uf[0]<<2))>>15; 422 x_est[1]=((long)cA*x_est[1]-(long)tmp*t_cos+(long)cC*(uf[1]<<2))>>15; 503 423 x_est[2]=x_est[2]; 504 424 x_est[3]=(((long)x_est[3]<<15)+(long)cG*x_est[2])>>15; 425 426 if(x_est[3]>(1<<15)) x_est[3]-=2*(1<<15); 427 if(x_est[3]<-(1<<15)) x_est[3]+=2*(1<<15); 505 428 506 429 //void EKFfixed::update_psi(void) 507 430 { 508 431 PSI[2]=((long)cB*t_sin)>>15; 509 tmp=((long)c H*x_est[2])>>15;432 tmp=((long)cB*x_est[2])>>15; 510 433 PSI[3]=((long)tmp*t_cos)>>15; 511 434 PSI[6]=-((long)cB*t_cos)>>15; 512 435 PSI[7]=((long)tmp*t_sin)>>15; 436 } 437 { 438 ivec Ad(PSI,16); 439 log_level.store(logA,get_from_ivec(Ad)); 513 440 } 514 441 … … 518 445 thorton(Uf,Df,PSIU,Q,G,Dfold,4); 519 446 447 { 448 ivec Ud(Uf,16); 449 log_level.store(logU,get_from_ivec(Ud)); 450 } 451 { 452 ivec Gd(G,16); 453 log_level.store(logG,get_from_ivec(Gd)); 454 } 455 456 520 457 int difz[2]; 521 difz[0]=Y_mes[0]-x_est[0]; 522 difz[1]=Y_mes[1]-x_est[1]; 458 difz[0]=(Y_mes[0]<<2)-x_est[0]; // Y_mes in q13!! 459 difz[1]=(Y_mes[1]<<2)-x_est[1]; 460 461 { 462 vec dd(4);dd(0)=Y_mes[0];dd(1)=Y_mes[1]; dd(2)=difz[0]; dd(3)=difz[1]; 463 log_level.store(logD,dd); 464 } 465 523 466 //bierman(int *difz, int *xp, int *U, int *D, int *R, unsigned int dimy, unsigned int dimx ); 524 467 int dR[2];dR[0]=R[0];dR[1]=R[3]; … … 527 470 // navrat estimovanych hodnot regulatoru 528 471 vec& mu = E._mu(); 529 (mu)(0)=zprevod(x_est[0], Qm)*Iref;530 (mu)(1)=zprevod(x_est[1], Qm)*Iref;531 (mu)(2)=zprevod(x_est[2], Qm)*Wref;472 (mu)(0)=zprevod(x_est[0],15)*Iref; 473 (mu)(1)=zprevod(x_est[1],15)*Iref; 474 (mu)(2)=zprevod(x_est[2],15)*Wref; 532 475 (mu)(3)=zprevod(x_est[3],15)*Thetaref; 533 476 … … 544 487 545 488 Uf[0]=0x7FFF; // 0.05 489 Uf[1]=Uf[2]=Uf[3]=Uf[4]=0; 546 490 Uf[5]=0x7FFF; 491 Uf[6]=Uf[6]=Uf[8]=Uf[9]=0; 547 492 Uf[10]=0x7FFF; // 1e-3 493 Uf[11]=Uf[12]=Uf[13]=Uf[4]=0; 548 494 Uf[15]=0x7FFF; // 1e-3 549 495 … … 564 510 // cE=prevod(kp*p*p*Tv*Fmag*Iref/J/Wref,15); 565 511 // cF=prevod(p*Tv*Mref/J/Wref,15); 566 cG=prevod(Tv*Wref*4/Thetaref,15); 567 cH=prevod(Tv*Wref*Fmag/Iref/Ls*Thetaref,15); 512 cG=prevod(Tv*Wref/Thetaref,15); //no *4!! 513 //cH=prevod(Tv*Wref*Fmag/Iref/Ls*Thetaref,15); 514 // cH=prevod(Tv*Wref*Fmag/Iref/Ls,15); <<< use cB instead 568 515 // cI=prevod(kp*p*p*Tv*Fmag*Iref/J/Wref*Thetaref); 569 516 -
applications/pmsm/simulator_zdenek/ekf_example/ekf_obj.h
r1179 r1201 28 28 void mat_to_int(const imat &M, int *I); 29 29 void vec_to_int(const ivec &v, int *I); 30 30 void UDtof(const mat &U, const vec &D, imat &Uf, ivec &Df, const vec &xref); 31 31 32 /*! 32 33 \brief Extended Kalman Filter with full matrices in fixed point arithmetic … … 108 109 class EKFfixedUD : public BM { 109 110 public: 111 LOG_LEVEL(EKFfixedUD,logU, logG, logD, logA); 112 110 113 void init_ekf(double Tv); 111 114 void ekf(double ux, double uy, double isxd, double isyd); … … 154 157 //!dummy! 155 158 const epdf& posterior() const {return E;}; 156 159 void log_register(logger &L, const string &prefix){ 160 BM::log_register ( L, prefix ); 161 162 L.add_vector ( log_level, logG, RV("G",16), prefix ); 163 L.add_vector ( log_level, logU, RV ("U", 16 ), prefix ); 164 L.add_vector ( log_level, logD, RV ("D", 4 ), prefix ); 165 L.add_vector ( log_level, logA, RV ("A", 16 ), prefix ); 166 167 }; 168 //void from_setting(); 157 169 }; 158 170 … … 174 186 //! D part 175 187 vec D; 176 177 int Uf[25]; 178 int Df[5]; 179 int Dfold[5]; 180 188 181 189 mat A; 182 190 mat C; … … 184 192 vec R; 185 193 186 int PSI[25];187 int PSIU[25];188 int Gf[25];189 190 194 enorm<ldmat> est; 191 195