Changeset 1201 for applications/pmsm

Show
Ignore:
Timestamp:
09/29/10 11:22:31 (14 years ago)
Author:
smidl
Message:

verbose ekf

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  
    3434         
    3535        if ( BM::evalll ) { 
    36                 //from enorm 
     36/*              //from enorm 
    3737                vec xdif(x,4);//first 4 of x 
    3838                //UGLY HACK!!! reliance on a predictor!! 
     
    4242                xdif(3)=x[3]-zprevod(x_pred[3],15);*/ 
    4343                 
    44                 xdif -=xhat; //(xdif=x-xhat) 
     44//              xdif -=xhat; //(xdif=x-xhat) 
    4545                 
    4646                mat Pfull(4,4); 
     
    5151                 
    5252                 
    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 ) );*/ 
    5454        } 
    5555}; 
     
    7777 
    7878  // 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   
    8285  tmp=((long)cB*x_est[2])>>15; 
    8386  x_pred[0]=((long)cA*x_est[0]+(long)tmp*t_sin+(long)cC*ux[0])>>15; 
     
    153156{ 
    154157  // Tuning of matrix Q 
    155   Q[0]=prevod(.01,15);       // 0.05 
     158  Q[0]=prevod(.05,15);       // 0.05 
    156159  Q[5]=Q[0]; 
    157   Q[10]=prevod(0.0001,15);      // 1e-3 
    158   Q[15]=prevod(0.0001,15);      // 1e-3 
     160  Q[10]=prevod(0.0002,15);      // 1e-3 
     161  Q[15]=prevod(0.001,15);      // 1e-3 
    159162 
    160163  // Tuning of matrix R 
    161   R[0]=prevod(0.05,15);         // 0.05 
     164  R[0]=prevod(0.1,15);         // 0.05 
    162165  R[3]=R[0]; 
    163166 
     
    170173//  cF=prevod(p*Tv*Mref/J/Wref,15); 
    171174  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); 
    174178 
    175179  /* Init matrix PSI with permanently constant terms */ 
     
    179183  PSI[14]=cG; 
    180184  PSI[15]=0x7FFF; 
     185   
     186  P_est[0]=0x7FFF; 
     187  P_est[5]=0x7FFF; 
     188  P_est[10]=0x7FFF; 
     189  P_est[15]=0x7FFF; 
    181190} 
    182191 
     
    232241        D =  est._R()._D(); 
    233242         
    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                  
    248243        //////////// 
    249244         
     
    254249 
    255250        ////// 
    256         vec xref(5); 
     251/*      vec xref(4); 
    257252        xref(0)= 30.0*1.4142; 
    258253        xref(1)= 30.0*1.4142; 
    259254        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         
    282258         
    283259        vec Din = D; 
     
    336312                 
    337313                } 
    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 
    364316        // bierman 
    365317         
     
    370322        vec xp=_mu; // used in bierman 
    371323         
    372         if (DBG){ 
    373                 yp = vec("0.40 0.300"); 
    374         } 
    375324         
    376325        for (int iy=0; iy<dimy; iy++){ 
     
    402351        } 
    403352 
    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 dimx  
    416          
    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          
    435353         
    436354        ///// 
     
    495413         
    496414        // 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);*/ 
    497417        t_sin=prevod(sin(Thetaref*x_est[3]/32768.),15); 
    498418        t_cos=prevod(cos(Thetaref*x_est[3]/32768.),15); 
    499419         
    500420        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; 
    503423        x_est[2]=x_est[2]; 
    504424        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); 
    505428         
    506429        //void EKFfixed::update_psi(void) 
    507430        {                
    508431                PSI[2]=((long)cB*t_sin)>>15; 
    509                 tmp=((long)cH*x_est[2])>>15; 
     432                tmp=((long)cB*x_est[2])>>15; 
    510433                PSI[3]=((long)tmp*t_cos)>>15; 
    511434                PSI[6]=-((long)cB*t_cos)>>15; 
    512435                PSI[7]=((long)tmp*t_sin)>>15; 
     436        } 
     437        { 
     438                ivec Ad(PSI,16); 
     439                log_level.store(logA,get_from_ivec(Ad)); 
    513440        } 
    514441         
     
    518445        thorton(Uf,Df,PSIU,Q,G,Dfold,4); 
    519446         
     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         
    520457        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         
    523466        //bierman(int *difz, int *xp, int *U, int *D, int *R, unsigned int dimy, unsigned int dimx ); 
    524467        int dR[2];dR[0]=R[0];dR[1]=R[3]; 
     
    527470        // navrat estimovanych hodnot regulatoru 
    528471        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; 
    532475        (mu)(3)=zprevod(x_est[3],15)*Thetaref; 
    533476         
     
    544487 
    545488        Uf[0]=0x7FFF;       // 0.05 
     489        Uf[1]=Uf[2]=Uf[3]=Uf[4]=0; 
    546490        Uf[5]=0x7FFF; 
     491        Uf[6]=Uf[6]=Uf[8]=Uf[9]=0; 
    547492        Uf[10]=0x7FFF;      // 1e-3 
     493        Uf[11]=Uf[12]=Uf[13]=Uf[4]=0; 
    548494        Uf[15]=0x7FFF;      // 1e-3 
    549495         
     
    564510        //  cE=prevod(kp*p*p*Tv*Fmag*Iref/J/Wref,15); 
    565511        //  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 
    568515        //  cI=prevod(kp*p*p*Tv*Fmag*Iref/J/Wref*Thetaref); 
    569516         
  • applications/pmsm/simulator_zdenek/ekf_example/ekf_obj.h

    r1179 r1201  
    2828void mat_to_int(const imat &M, int *I); 
    2929void vec_to_int(const ivec &v, int *I); 
    30  
     30void UDtof(const mat &U, const vec &D, imat &Uf, ivec &Df, const vec &xref); 
     31         
    3132/*! 
    3233\brief Extended Kalman Filter with full matrices in fixed point arithmetic 
     
    108109class EKFfixedUD : public BM { 
    109110        public: 
     111                LOG_LEVEL(EKFfixedUD,logU, logG, logD, logA); 
     112                 
    110113                void init_ekf(double Tv); 
    111114                void ekf(double ux, double uy, double isxd, double isyd); 
     
    154157                //!dummy! 
    155158                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(); 
    157169}; 
    158170 
     
    174186                //! D part 
    175187                vec D; 
    176                  
    177                 int Uf[25]; 
    178                 int Df[5]; 
    179                 int Dfold[5]; 
    180                  
     188                                 
    181189                mat A; 
    182190                mat C; 
     
    184192                vec R; 
    185193                 
    186                 int PSI[25]; 
    187                 int PSIU[25]; 
    188                 int Gf[25]; 
    189  
    190194                enorm<ldmat> est; 
    191195