Changeset 1341 for applications/pmsm

Show
Ignore:
Timestamp:
04/28/11 22:18:55 (13 years ago)
Author:
smidl
Message:

extended reduced model for unknown Torque

Location:
applications/pmsm
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • applications/pmsm/pmsm.h

    r1327 r1341  
    2929        bool cutoff; 
    3030public: 
    31         IMpmsm() :diffbifn ( ) {dimy=4; dimx = 4; dimu=2; dimc=6;compensate=true;cutoff=true;}; 
     31        IMpmsm() :diffbifn ( ) {dimy=4; dimx = 4; dimu=2; dimc=6;compensate=false;cutoff=true;}; 
    3232        //! Set mechanical and electrical variables 
    3333        virtual 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;} 
     
    134134        bool cutoff; 
    135135public: 
    136         IMpmsmOT() :diffbifn ( ) {dimy=2; dimx = 2; dimu=4; dimc=6;compensate=true;cutoff=true;}; 
     136        IMpmsmOT() :diffbifn ( ) {dimy=2; dimx = 2; dimu=4; dimc=6;compensate=false;cutoff=true;}; 
    137137        //! Set mechanical and electrical variables 
    138138        virtual 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;} 
     
    188188         
    189189        void dfdx_cond ( const vec &x0, const vec &u0, mat &A, bool full=true ) { 
    190                 const double &omm = x0 ( 0 ); 
     190//              const double &omm = x0 ( 0 ); 
    191191                const double &thm = x0 ( 1 ); 
    192192 
     
    223223 
    224224UIREGISTER ( IMpmsmOT ); 
     225 
     226//! Evolution model of \f$ \omega, \vartheta\f$ for a PMSM drive and its derivative with respect to \f$x\f$ 
     227class IMpmsmOTM : public diffbifn { 
     228protected: 
     229        double Rs, Ls, dt, Ypm, kp, p,  J, Mz; 
     230         
     231        bool compensate; 
     232        bool cutoff; 
     233public: 
     234        IMpmsmOTM() :diffbifn ( ) {dimy=2; dimx = 3; dimu=4; dimc=6;compensate=false;cutoff=true;}; 
     235        //! Set mechanical and electrical variables 
     236        virtual 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;} 
     237         
     238        void modelpwm(const vec &x0, const vec u0, double &ua, double &ub){ 
     239                /*              ua=u0[0]; 
     240                 *              ub=u0[1]; 
     241                 *              return;*/ 
     242                double sq3=sqrt ( 3.0 ); 
     243                double i1=x0(0); 
     244                double i2=0.5* ( -i1+sq3*x0[1] ); 
     245                double i3=0.5* ( -i1-sq3*x0[1] ); 
     246                double u1=u0(0); 
     247                double u2=0.5* ( -u1+sq3*u0(1) ); 
     248                double u3=0.5* ( -u1-sq3*u0(1) ); 
     249                 
     250                double du1=1.4* ( double ( i1>0.3 ) - double ( i1<-0.3 ) ) +0.2*i1; 
     251                double du2=1.4* ( double ( i2>0.3 ) - double ( i2<-0.3 ) ) +0.2*i2; 
     252                double du3=1.4* ( double ( i3>0.3 ) - double ( i3<-0.3 ) ) +0.2*i3; 
     253                ua = ( 2.0* ( u1-du1 )- ( u2-du2 )- ( u3-du3 ) ) /3.0; 
     254                ub = ( ( u2-du2 )- ( u3-du3 ) ) /sq3; 
     255        } 
     256         
     257        vec eval ( const vec &x0, const vec &u0 ) { 
     258                // last state 
     259                const double &omm = x0 ( 0 ); 
     260                const double &thm = x0 ( 1 ); 
     261                const double &Mm  = x0 ( 2 ); 
     262                double uam; 
     263                double ubm; 
     264                 
     265                if (compensate){ 
     266                        modelpwm(x0,u0,uam,ubm); 
     267                } else { 
     268                        uam = u0(0); 
     269                        ubm = u0(1); 
     270                } 
     271                 
     272                const double &iam = u0 ( 2 ); 
     273                const double &ibm = u0 ( 3 ); 
     274                 
     275                vec xk( 3 ); 
     276                //ia 
     277                //om 
     278                xk ( 0 ) = omm + kp*p*p * Ypm/J*dt* ( ibm * cos ( thm )-iam * sin ( thm ) ) - p/J*dt*Mm; 
     279                //th 
     280                xk ( 1 ) = thm + omm*dt; // <0..2pi> 
     281                xk (2) = Mm; 
     282                 
     283                if (cutoff) { 
     284                        if ( xk ( 1 ) >pi ) xk ( 1 )-=2*pi; 
     285                        if ( xk ( 1 ) <-pi ) xk ( 1 ) +=2*pi; 
     286                } 
     287                return xk; 
     288        } 
     289         
     290        void dfdx_cond ( const vec &x0, const vec &u0, mat &A, bool full=true ) { 
     291//              const double &omm = x0 ( 0 ); 
     292                const double &thm = x0 ( 1 ); 
     293//              const double &Mm  = x0 ( 2 ); 
     294                 
     295                const double &iam = u0 ( 2 ); 
     296                const double &ibm = u0 ( 3 ); 
     297                 
     298                // d ia 
     299                // d om 
     300                A ( 0,0 ) = 1.0; 
     301                A ( 0,1 ) = kp*p*p * Ypm/J*dt* ( -ibm * sin ( thm )-iam * cos ( thm ) ); 
     302                A ( 0,2 ) = -p/J*dt; 
     303                // d th 
     304                A ( 1,0 ) = dt;  
     305                A ( 1,1 ) = 1.0; 
     306                A ( 1,2 ) = 0.0; 
     307                // d M 
     308                A ( 2,0 ) = 0.0; 
     309                A ( 2,1 ) = 0.0; 
     310                A ( 2,2 ) = 1.0; 
     311        } 
     312         
     313        void dfdu_cond ( const vec &x0, const vec &u0, mat &A, bool full=true ) {it_error ( "not needed" );}; 
     314         
     315        void from_setting( const Setting &root ) 
     316        {        
     317                 
     318                const SettingResolver& params_b=root["params"]; 
     319                const Setting& params=params_b.result; 
     320                 
     321                set_parameters ( params["Rs"], params["Ls"], 125e-6, params["Fmag"], \ 
     322                params["kp"], params["p"], params["J"], 0.0); 
     323                int comp=0; 
     324                if (UI::get(comp,root,"compensate",UI::optional)){compensate=(comp==1);} 
     325                int cuto=0; 
     326                if (UI::get(cuto,root,"cutoff",UI::optional)){cutoff=(cuto==1);} 
     327        }; 
     328         
     329        // TODO dodelat void to_setting( Setting &root ) const; 
     330}; 
     331 
     332UIREGISTER ( IMpmsmOTM ); 
    225333 
    226334//! Evolution model of \f$ \omega, \vartheta\f$ for a PMSM drive and its derivative with respect to \f$x\f$ -- Scaled to [-1,1] 
     
    766874UIREGISTER ( OMpmsmOT ); 
    767875 
     876//! Observation model for PMSM drive in reduced form coordinates 
     877class OMpmsmOTM: public diffbifn { 
     878public: 
     879        double Rs, Ls, dt, Ypm, kp, p,  J, Mz; 
     880         
     881        virtual 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;} 
     882         
     883        OMpmsmOTM() :diffbifn () {dimy=2;dimx=2;dimu=4;}; 
     884         
     885        vec eval ( const vec &x0, const vec &u0 ) { 
     886                vec y ( 2 ); 
     887                const double &omm = x0(0); 
     888                const double &thm = x0(1); 
     889                 
     890                const double &uam = u0 ( 0 ); 
     891                const double &ubm = u0 ( 1 ); 
     892                const double &iam = u0 ( 2 ); 
     893                const double &ibm = u0 ( 3 ); 
     894                 
     895                y ( 0 ) = ( 1.0- Rs/Ls*dt ) * iam + Ypm/Ls*dt*omm * sin ( thm ) + uam*dt/Ls; 
     896                //ib 
     897                y ( 1 ) = ( 1.0- Rs/Ls*dt ) * ibm - Ypm/Ls*dt*omm * cos ( thm ) + ubm*dt/Ls; 
     898                 
     899                return y; 
     900        } 
     901         
     902        void dfdx_cond ( const vec &x0, const vec &u0, mat &A, bool full=true ) { 
     903                const double &omm = x0(0); 
     904                const double &thm = x0(1); 
     905                 
     906                A ( 0,0 ) = Ypm/Ls*dt* sin ( thm );  
     907                A ( 0,1 ) = Ypm/Ls*dt*omm * ( cos ( thm ) ); 
     908                A( 0,2) = 0.0; 
     909                // d ib 
     910                A ( 1,0 ) = -Ypm/Ls*dt* cos ( thm );  
     911                A ( 1,1 ) = Ypm/Ls*dt*omm * ( sin ( thm ) ); 
     912                A(1,2) = 0.0; 
     913                 
     914        } 
     915         
     916        void from_setting( const Setting &root ) 
     917        {        
     918                 
     919                const SettingResolver& params_b=root["params"]; 
     920                const Setting& params=params_b.result; 
     921                 
     922                set_parameters ( params["Rs"], params["Ls"], 125e-6, params["Fmag"], \ 
     923                params["kp"], params["p"], params["J"], 0.0); 
     924        }; 
     925         
     926}; 
     927 
     928UIREGISTER ( OMpmsmOTM ); 
     929 
     930 
     931 
    768932//! Observation model for PMSM drive in reduced form coordinates -- scaled to [-1,1] 
    769933class OMpmsmOTsc: public diffbifn { 
  • applications/pmsm/simulator_zdenek/ekf_example/ekf_obj.cpp

    r1330 r1341  
    573573        x_est[0]=x_est[0]; 
    574574        //               q15              + q15*q13 
    575         x_est[1]=(((int32)x_est[1]<<15)+(int32)cG*x_est[0])>>15; 
     575        x_est[1]=(((int32)x_est[1]<<15)+(int32)cG*x_est[0]+(1<<14))>>15; 
    576576         
    577577//      if(x_est[1]>(1<<15)) x_est[1]-=2*(1<<15); 
     
    636636tmp=((int32)cB*x_est[0])>>15; // q15*q13 -> q13 
    637637//             q15*q13 +          q13*q15      + q15*q13?? 
    638 y_est[0]=((int32)cA*y_old[0]+(int32)tmp*t_sin+(int32)cC*(uf[0]))>>15; // in q13 
    639 y_est[1]=((int32)cA*y_old[1]-(int32)tmp*t_cos+(int32)cC*(uf[1]))>>15; // q13 
     638y_est[0]=((int32)cA*y_old[0]+(int32)tmp*t_sin+(int32)cC*(uf[0])+(1<<14))>>15; // in q13 
     639y_est[1]=((int32)cA*y_old[1]-(int32)tmp*t_cos+(int32)cC*(uf[1])+(1<<14))>>15; // q13 
    640640 
    641641         
     
    674674{ 
    675675        // Tuning of matrix Q 
    676         Q[0]=prevod(0.0005,15);      // 1e-3 
    677         Q[3]=prevod(0.0001,15);      // 1e-3 
     676        Q[0]=prevod(0.01,15);      // 1e-3 
     677        Q[3]=prevod(0.01,15);      // 1e-3 
    678678         
    679679        Uf[0]=0x7FFF;// !       // 0.05 
     
    685685         
    686686        // Tuning of matrix R 
    687         R[0]=prevod(0.005,15);         // 0.05 
     687        R[0]=prevod(0.01,15);         // 0.05 
    688688        R[3]=R[0]; 
    689689         
     
    708708} 
    709709 
     710void EKFfixedUD3::bayes(const itpp::vec& yt, const itpp::vec& ut) 
     711{ 
     712        ekf3(ut[0],ut[1],yt[0],yt[1]); 
     713} 
     714 
     715 
     716void EKFfixedUD3::ekf3(double ux, double uy, double isxd, double isyd) 
     717{ 
     718        // vypocet napeti v systemu (x,y) 
     719        int16 uf[2]; 
     720        uf[0]=prevod(ux/Uref,15); 
     721        uf[1]=prevod(uy/Uref,15); 
     722         
     723        int16 Y_mes[2]; 
     724        // zadani mereni 
     725        Y_mes[0]=prevod(isxd/Iref,15); 
     726        Y_mes[1]=prevod(isyd/Iref,15); 
     727         
     728        ////// vlastni rutina EKF -- ///////////////////////// 
     729        int16 t_sin,t_cos; 
     730        int32 tmp; 
     731         
     732        int16 Mm=x_est[2]; 
     733        x_est[0]=(((int32)x_est[0]<<15) + (int32)cF*x_est[2] + (1<<14))>>15; 
     734        //               q15              + q15*q13 
     735        x_est[1]=(((int32)x_est[1]<<15)+(int32)cG*x_est[0] + (1<<14))>>15; 
     736        x_est[2]=x_est[2]; 
     737         
     738        //      if(x_est[1]>(1<<15)) x_est[1]-=2*(1<<15); 
     739        //      if(x_est[1]<-(1<<15)) x_est[1]+=2*(1<<15); 
     740         
     741         
     742        ///////// end of copy /////////////// 
     743        mmultAU(PSI,Uf,PSIU,3,3); 
     744         
     745         
     746        //thorton(int16 *U, int16 *D, int16 *PSIU, int16 *Q, int16 *G, int16 *Dold, unsigned int16 dimx); 
     747        thorton_fast(Uf,Df,PSIU,Q,G,Dfold,3); 
     748         
     749        // implementace v PC 
     750        /*      t_sin=prevod(sin(Thetaref*x_est[3]/32768.),15); 
     751         *      t_cos=prevod(cos(Thetaref*x_est[3]/32768.),15);*/ 
     752        tmp=prevod(sin(Thetaref*x_est[1]/32768.),15); 
     753        if (tmp>32767) t_sin =32767; else t_sin=tmp; 
     754        tmp=prevod(cos(Thetaref*x_est[1]/32768.),15); 
     755        if (tmp>32767) t_cos =32767; else t_cos=tmp; 
     756         
     757        {        
     758                C[0]=((int32)cB*t_sin)>>15; 
     759                tmp=((int32)cH*x_est[0])>>15; // ! cH =cB with different scale!! 
     760                C[1]=((int32)tmp*t_cos)>>15; 
     761                C[2] = 0; 
     762                 
     763                C[3]=-((int32)cB*t_cos)>>15; 
     764                C[4]=((int32)tmp*t_sin)>>15; 
     765                C[5]=0; 
     766        }        
     767         
     768        tmp=((int32)cB*x_est[0])>>15; // q15*q13 -> q13 
     769        //             q15*q13 +          q13*q15      + q15*q13?? 
     770        y_est[0]=((int32)cA*y_old[0]+(int32)tmp*t_sin+(int32)cC*(uf[0])+(1<<14))>>15; // in q13 
     771        y_est[1]=((int32)cA*y_old[1]-(int32)tmp*t_cos+(int32)cC*(uf[1])+(1<<14))>>15; // q13 
     772         
     773         
     774        int16 difz[2]; 
     775        difz[0]=(Y_mes[0]-y_est[0]); // shift to q15!! 
     776        difz[1]=(Y_mes[1]-y_est[1]);//<<2; 
     777         
     778        y_old[0] = Y_mes[0]; 
     779        y_old[1] = Y_mes[1]; 
     780         
     781        //bierman(int16 *difz, int16 *xp, int16 *U, int16 *D, int16 *R, unsigned int16 dimy, unsigned int16 dimx ); 
     782        int16 dR[2];dR[0]=R[0];dR[1]=R[3]; 
     783        //int16 xb[4]; xb[0]=x_est[0]<<2; xb[1]=x_est[1]<<2;  xb[2]=x_est[2]<<2;  xb[3]=x_est[3];   
     784        bierman_fastC(difz,x_est,Uf,Df,C,dR,2,3); 
     785        //x_est[0] = xb[0]>>2; x_est[1]=xb[1]>>2; x_est[2]=xb[2]>>2; x_est[3]=xb[3]; 
     786         
     787        // navrat estimovanych hodnot regulatoru 
     788        vec& mu = E._mu(); 
     789        (mu)(0)=zprevod(x_est[0],15)*Wref; 
     790        (mu)(1)=zprevod(x_est[1],15)*Thetaref; 
     791        (mu)(2)=zprevod(x_est[2],15)*Mref; 
     792         
     793        //x_est[2]=x[2]*32768/Wref; 
     794        //x_est[3]=x[3]*32768/Thetaref; 
     795        //      mat T=diag(concat(vec_2(Iref,Iref), vec_2(Wref,Thetaref))); 
     796} 
     797 
     798void EKFfixedUD3::init_ekf3(double Tv) 
     799{ 
     800        // Tuning of matrix Q 
     801        Q[0]=prevod(0.001,15);      // 1e-3 
     802        Q[4]=prevod(0.001,15);      // 1e-3 
     803        Q[8]=prevod(0.001,15);      // 1e-3 
     804         
     805        Uf[0]=0x7FFF;// !       // 0.05 
     806        Uf[1]=Uf[2]=Uf[3]=0; 
     807        Uf[4]=0x7FFF;//! 
     808        Uf[5]=Uf[6]=Uf[7]=0; 
     809        Uf[8]=0x7FFF;//! 
     810         
     811        Df[0]=1<<14; 
     812        Df[1]=1<<14; 
     813        Df[2]=1<<14; 
     814         
     815        // Tuning of matrix R 
     816        R[0]=prevod(0.01,15);         // 0.05 
     817        R[3]=R[0]; 
     818         
     819        // Motor model parameters  
     820        cA=prevod(1-Tv*Rs/Ls,15); 
     821        cB=prevod(Tv*Wref*Fmag/Iref/Ls,15); 
     822        cC=prevod(Tv/Ls/Iref*Uref,15); 
     823        //  cD=prevod(1-Tv*Bf/J,15); 
     824        //  cE=prevod(kp*p*p*Tv*Fmag*Iref/J/Wref,15); 
     825        cF=prevod(-p*Tv*Mref/J/Wref,15); 
     826        cG=prevod(Tv*Wref/Thetaref,15); //in q15! 
     827        //cH=prevod(Tv*Wref*Fmag/Iref/Ls*Thetaref,15); 
     828        // cH=prevod(Tv*Wref*Thetaref*Fmag/Iref/Ls,15); // < =========== 
     829        cH=prevod(Tv*Wref*Fmag*Thetaref/Iref/Ls,15); //  
     830        //  cI=prevod(kp*p*p*Tv*Fmag*Iref/J/Wref*Thetaref); 
     831         
     832        /* Init matrix PSI with permanently constant terms */ 
     833        PSI[0]=0x7FFF; 
     834        PSI[1]=0; 
     835        PSI[2]= cF; 
     836         
     837        PSI[3]=cG; 
     838        PSI[4]=0x7FFF; 
     839        PSI[5]=0; 
     840         
     841        PSI[6]=0; 
     842        PSI[7]=0; 
     843        PSI[8]=0x7FFF; 
     844} 
     845 
    710846 
    711847void EKFfixedCh::bayes(const itpp::vec& yt, const itpp::vec& ut) 
  • applications/pmsm/simulator_zdenek/ekf_example/ekf_obj.h

    r1326 r1341  
    270270 
    271271/*! 
     272 * \brief Extended Kalman Filter with UD matrices in fixed point16 arithmetic 
     273 *  
     274 * An approximation of the exact Bayesian filter with Gaussian noices and non-linear evolutions of their mean. 
     275 */ 
     276class EKFfixedUD3 : public BM { 
     277public: 
     278        LOG_LEVEL(EKFfixedUD3,logU, logG, logD, logA, logC, logP); 
     279         
     280        void init_ekf3(double Tv); 
     281        void ekf3(double ux, double uy, double isxd, double isyd); 
     282         
     283        /* Constants - definovat jako konstanty ?? ?kde je vyhodnejsi aby v pameti byli?*/ 
     284        int16 Q[9]; /* matrix [4,4] */ 
     285        int16 R[4]; /* matrix [2,2] */ 
     286         
     287        int16 x_est[3]; /* estimate and prediction */ 
     288        int16 y_est[2]; /* estimate and prediction */ 
     289        int16 y_old[2]; /* estimate and prediction */ 
     290         
     291        int16 PSI[9]; /* matrix [4,4] */ 
     292        int16 PSIU[9]; /* matrix PIS*U, [4,4] */ 
     293        int16 C[6]; /* matrix [4,4] */ 
     294         
     295        int16 Uf[9]; // upper triangular of covariance (inplace) 
     296        int16 Df[3];  // diagonal covariance 
     297        int16 Dfold[3]; // temp of D 
     298        int16 G[9];  // temp for bierman 
     299         
     300        int16 cA, cB, cC, cG, cF, cH;  // cD, cE, cF, cI ... nepouzivane 
     301         
     302        enorm<fsqmat> E; 
     303        mat Ry; 
     304         
     305public: 
     306        //! Default constructor 
     307        EKFfixedUD3 ():BM(),E(),Ry(2,2){ 
     308                int16 i; 
     309                for(i=0;i<9;i++){Q[i]=0;} 
     310                for(i=0;i<4;i++){R[i]=0;} 
     311                 
     312                for(i=0;i<3;i++){x_est[i]=0;} 
     313                for(i=0;i<2;i++){y_est[i]=0;} 
     314                for(i=0;i<2;i++){y_old[i]=0;} 
     315                for(i=0;i<9;i++){Uf[i]=0;} 
     316                for(i=0;i<3;i++){Df[i]=0;} 
     317                for(i=0;i<4;i++){G[i]=0;} 
     318                for(i=0;i<3;i++){Dfold[i]=0;} 
     319                 
     320                for(i=0;i<9;i++){PSI[i]=0;} 
     321                for(i=0;i<6;i++){C[i]=0;} 
     322                 
     323                set_dim(3); 
     324                dimc = 2; 
     325                dimy = 2; 
     326                E._mu()=zeros(3); 
     327                E._R()=zeros(3,3); 
     328                init_ekf3(0.000125); 
     329        }; 
     330        //! Here dt = [yt;ut] of appropriate dimensions 
     331        void bayes ( const vec &yt, const vec &ut ); 
     332        //!dummy! 
     333        const epdf& posterior() const {return E;}; 
     334        void log_register(logger &L, const string &prefix){ 
     335                BM::log_register ( L, prefix );          
     336        }; 
     337        //void from_setting(); 
     338}; 
     339 
     340UIREGISTER(EKFfixedUD3); 
     341 
     342/*! 
    272343 * \brief Extended Kalman Filter with Chol matrices in fixed point16 arithmetic 
    273344 *