Changeset 1295 for applications/pmsm

Show
Ignore:
Timestamp:
03/14/11 09:32:42 (13 years ago)
Author:
smidl
Message:

new modle of pmsm

Location:
applications/pmsm
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • applications/pmsm/cfg/zcu.cfg

    r295 r1295  
    22        Rs=0.28; // Resistance 
    33        Ls=0.003465; // inductance 
     4        Ld=0.003465; // inductance 
     5        Lq=0.003; // inductance 
    46        Fmag= 0.1989; // Magnetic?? 
    57        kp = 1.5; 
  • applications/pmsm/pmsm.h

    r1292 r1295  
    125125UIREGISTER ( IMpmsm ); 
    126126 
     127//! Evolution model of \f$ \omega, \vartheta\f$ for a PMSM drive and its derivative with respect to \f$x\f$ 
     128class IMpmsmOT : public diffbifn { 
     129protected: 
     130        double Rs, Ls, dt, Ypm, kp, p,  J, Mz; 
     131         
     132        bool compensate; 
     133        bool cutoff; 
     134public: 
     135        IMpmsmOT() :diffbifn ( ) {dimy=2; dimx = 2; dimu=4; dimc=6;compensate=true;cutoff=true;}; 
     136        //! Set mechanical and electrical variables 
     137        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;} 
     138         
     139        void modelpwm(const vec &x0, const vec u0, double &ua, double &ub){ 
     140                /*              ua=u0[0]; 
     141                 *              ub=u0[1]; 
     142                 *              return;*/ 
     143                double sq3=sqrt ( 3.0 ); 
     144                double i1=x0(0); 
     145                double i2=0.5* ( -i1+sq3*x0[1] ); 
     146                double i3=0.5* ( -i1-sq3*x0[1] ); 
     147                double u1=u0(0); 
     148                double u2=0.5* ( -u1+sq3*u0(1) ); 
     149                double u3=0.5* ( -u1-sq3*u0(1) ); 
     150                 
     151                double du1=1.4* ( double ( i1>0.3 ) - double ( i1<-0.3 ) ) +0.2*i1; 
     152                double du2=1.4* ( double ( i2>0.3 ) - double ( i2<-0.3 ) ) +0.2*i2; 
     153                double du3=1.4* ( double ( i3>0.3 ) - double ( i3<-0.3 ) ) +0.2*i3; 
     154                ua = ( 2.0* ( u1-du1 )- ( u2-du2 )- ( u3-du3 ) ) /3.0; 
     155                ub = ( ( u2-du2 )- ( u3-du3 ) ) /sq3; 
     156        } 
     157         
     158        vec eval ( const vec &x0, const vec &u0 ) { 
     159                // last state 
     160                const double &omm = x0 ( 0 ); 
     161                const double &thm = x0 ( 1 ); 
     162                double uam; 
     163                double ubm; 
     164                 
     165                if (compensate){ 
     166                        modelpwm(x0,u0,uam,ubm); 
     167                } else { 
     168                        uam = u0(0); 
     169                        ubm = u0(1); 
     170                } 
     171                 
     172                const double &iam = u0 ( 2 ); 
     173                const double &ibm = u0 ( 3 ); 
     174                 
     175                vec xk( 2 ); 
     176                //ia 
     177                //om 
     178                xk ( 0 ) = omm + kp*p*p * Ypm/J*dt* ( ibm * cos ( thm )-iam * sin ( thm ) ) - p/J*dt*Mz; 
     179                //th 
     180                xk ( 1 ) = thm + omm*dt; // <0..2pi> 
     181                if (cutoff) { 
     182                        if ( xk ( 1 ) >pi ) xk ( 1 )-=2*pi; 
     183                        if ( xk ( 1 ) <-pi ) xk ( 1 ) +=2*pi; 
     184                } 
     185                return xk; 
     186        } 
     187         
     188        void dfdx_cond ( const vec &x0, const vec &u0, mat &A, bool full=true ) { 
     189                const double &omm = x0 ( 0 ); 
     190                const double &thm = x0 ( 1 ); 
     191 
     192                const double &iam = u0 ( 2 ); 
     193                const double &ibm = u0 ( 3 ); 
     194                 
     195                // d ia 
     196                // d om 
     197                A ( 0,0 ) = 1.0; 
     198                A ( 0,1 ) = kp*p*p * Ypm/J*dt* ( -ibm * sin ( thm )-iam * cos ( thm ) ); 
     199                // d th 
     200                A ( 1,0 ) = dt;  
     201                A ( 1,1 ) = 1.0; 
     202        } 
     203         
     204        void dfdu_cond ( const vec &x0, const vec &u0, mat &A, bool full=true ) {it_error ( "not needed" );}; 
     205         
     206        void from_setting( const Setting &root ) 
     207        {        
     208                 
     209                const SettingResolver& params_b=root["params"]; 
     210                const Setting& params=params_b.result; 
     211                 
     212                set_parameters ( params["Rs"], params["Ls"], 125e-6, params["Fmag"], \ 
     213                params["kp"], params["p"], params["J"], 0.0); 
     214                int comp=0; 
     215                if (UI::get(comp,root,"compensate",UI::optional)){compensate=(comp==1);} 
     216                int cuto=0; 
     217                if (UI::get(cuto,root,"cutoff",UI::optional)){cutoff=(cuto==1);} 
     218        }; 
     219         
     220        // TODO dodelat void to_setting( Setting &root ) const; 
     221}; 
     222 
     223UIREGISTER ( IMpmsmOT ); 
     224 
    127225 
    128226//! State evolution model for a PMSM drive and its derivative with respect to \f$x\f$ 
     
    233331                if (UI::get(comp,root,"compensate",UI::optional)){compensate=(comp==1);} 
    234332                int cuto=0; 
    235                 if (UI::get(cuto,root,"cutoff",UI::optional)){cutoff=(cuto==1);} 
     333                if (UI::get(cuto,root,"cutoff",UI::optional)){ 
     334                        cutoff=(cuto==1); 
     335                } 
    236336        }; 
    237337         
     
    240340 
    241341UIREGISTER ( IMpmsmDQ ); 
    242  
    243  
    244  
    245  
    246  
    247342 
    248343 
     
    544639UIREGISTER ( OMpmsmDQ ); 
    545640 
     641//! Observation model for PMSM drive in reduced form coordinates 
     642class OMpmsmOT: public diffbifn { 
     643public: 
     644        double Rs, Ls, dt, Ypm, kp, p,  J, Mz; 
     645         
     646        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;} 
     647         
     648        OMpmsmOT() :diffbifn () {dimy=2;dimx=2;dimu=4;}; 
     649         
     650        vec eval ( const vec &x0, const vec &u0 ) { 
     651                vec y ( 2 ); 
     652                const double &omm = x0(0); 
     653                const double &thm = x0(1); 
     654                 
     655                const double &uam = u0 ( 0 ); 
     656                const double &ubm = u0 ( 1 ); 
     657                const double &iam = u0 ( 2 ); 
     658                const double &ibm = u0 ( 3 ); 
     659                 
     660                y ( 0 ) = ( 1.0- Rs/Ls*dt ) * iam + Ypm/Ls*dt*omm * sin ( thm ) + uam*dt/Ls; 
     661                //ib 
     662                y ( 1 ) = ( 1.0- Rs/Ls*dt ) * ibm - Ypm/Ls*dt*omm * cos ( thm ) + ubm*dt/Ls; 
     663                 
     664                return y; 
     665        } 
     666         
     667        void dfdx_cond ( const vec &x0, const vec &u0, mat &A, bool full=true ) { 
     668                const double &omm = x0(0); 
     669                const double &thm = x0(1); 
     670                                 
     671                A ( 0,0 ) = Ypm/Ls*dt* sin ( thm );  
     672                A ( 0,1 ) = Ypm/Ls*dt*omm * ( cos ( thm ) ); 
     673                // d ib 
     674                A ( 1,0 ) = -Ypm/Ls*dt* cos ( thm );  
     675                A ( 1,1 ) = Ypm/Ls*dt*omm * ( sin ( thm ) ); 
     676                 
     677                 
     678        } 
     679 
     680void from_setting( const Setting &root ) 
     681{        
     682         
     683        const SettingResolver& params_b=root["params"]; 
     684        const Setting& params=params_b.result; 
     685         
     686        set_parameters ( params["Rs"], params["Ls"], 125e-6, params["Fmag"], \ 
     687        params["kp"], params["p"], params["J"], 0.0); 
     688}; 
     689 
     690}; 
     691 
     692UIREGISTER ( OMpmsmOT ); 
     693 
    546694 
    547695/*!@}*/ 
  • applications/pmsm/pmsm_estim.cpp

    r1192 r1295  
    33\file 
    44\brief Multi-Estimator (developped for PMSM) 
    5   
     5 
    66 */ 
    77 
     
    2424 
    2525void mexFunction ( int n_output, mxArray *output[], int n_input, const mxArray *input[] ) { 
    26         // Check the number of inputs and output arguments 
    27         if ( n_input<2 ) mexErrMsgTxt ( "Usage:\n" 
    28                                                 "[Res,estimators,Res2]=estimator(system, estimators, experiment, logger)\n" 
    29                                                 "  system     = struct('class','datasource',...);  % Estimated system\n" 
    30                                                 "  estimators = {struct('class','estimator',...),  % Estimators\n" 
    31                                                 "                struct('class','estimator',...),...} \n" 
    32                                                 "  === optional ===" 
    33                                                 "  experiment = struct('ndat',10);                 % number of data in experiment, full length of finite datasources, 10 otherwise \n" 
    34                                                 "  logger     = struct('class','mexlogger');       % How to store results, default=mexlog, i.e. matlab structure\n\n" 
    35                                                 "Output:\n" 
    36                                                 "  Res          Matlab structure with logged results, \n"   
    37                                                 "  estimators   Array of estimators updated with data \n" 
    38                                                 "  Res2         When logfull log_level is on, this structure is filled with structures of posterior densities\n\n" 
    39                                                 "see documentation of classes datasource, BM, and mexlogger and their offsprings in BDM." ); 
     26    // Check the number of inputs and output arguments 
     27    if ( n_input<2 ) mexErrMsgTxt ( "Usage:\n" 
     28                                        "[Res,estimators,Res2]=estimator(system, estimators, experiment, logger)\n" 
     29                                        "  system     = struct('class','datasource',...);  % Estimated system\n" 
     30                                        "  estimators = {struct('class','estimator',...),  % Estimators\n" 
     31                                        "                struct('class','estimator',...),...} \n" 
     32                                        "  === optional ===" 
     33                                        "  experiment = struct('ndat',10);                 % number of data in experiment, full length of finite datasources, 10 otherwise \n" 
     34                                        "  logger     = struct('class','mexlogger');       % How to store results, default=mexlog, i.e. matlab structure\n\n" 
     35                                        "Output:\n" 
     36                                        "  Res          Matlab structure with logged results, \n" 
     37                                        "  estimators   Array of estimators updated with data \n" 
     38                                        "  Res2         When logfull log_level is on, this structure is filled with structures of posterior densities\n\n" 
     39                                        "see documentation of classes datasource, BM, and mexlogger and their offsprings in BDM." ); 
    4040 
    41         RV::clear_all(); 
    42         //CONFIG 
    43         UImxArray F; 
    44         try { 
    45                 F.addGroup ( input[0],"system" ); 
    46                 F.addList ( input[1],"estimators" ); 
    47                 if ( n_input>2 ) { 
    48                         F.addGroup ( input[2],"experiment" ); 
    49                 } 
    50                 if ( n_input>3 ) { 
    51                         F.addGroup ( input[3],"logger" ); 
    52                 } 
    53         } catch ( SettingException e ) { 
    54                 it_error ( "error: "+string ( e.getPath() ) ); 
    55         } 
     41    RV::clear_all(); 
     42    //CONFIG 
     43    UImxArray F; 
     44    try { 
     45        F.addGroup ( input[0],"system" ); 
     46        F.addList ( input[1],"estimators" ); 
     47        if ( n_input>2 ) { 
     48            F.addGroup ( input[2],"experiment" ); 
     49        } 
     50        if ( n_input>3 ) { 
     51            F.addGroup ( input[3],"logger" ); 
     52        } 
     53    } catch ( SettingException e ) { 
     54        it_error ( "error: "+string ( e.getPath() ) ); 
     55    } 
    5656 
    57         //DBG 
    58         F.writeFile ( "pmsm_estim.cfg" ); 
     57    //DBG 
     58    F.writeFile ( "pmsm_estim.cfg" ); 
    5959 
    6060#else 
    6161int main ( int argc, char* argv[] ) { 
    62         const char *fname; 
    63         if ( argc>1 ) {fname = argv[1]; } 
    64         else { cout << "Missing configuration file.\n Usage: \n $> estimator config_file.cfg"<<endl; abort(); } 
    65         UIFile F ( fname ); 
     62    const char *fname; 
     63    if ( argc>1 ) { 
     64        fname = argv[1]; 
     65    } 
     66    else { 
     67        cout << "Missing configuration file.\n Usage: \n $> estimator config_file.cfg"<<endl; 
     68        abort(); 
     69    } 
     70    UIFile F ( fname ); 
    6671#endif 
    67          
    68         shared_ptr<logger> L = UI::build <logger>( F, "logger" ); 
    69         shared_ptr<DS>  pDS = UI::build<DS> ( F, "system" ); 
    70         Array<shared_ptr<BM> > Es;                      // array of estimators 
    71         UI::get( Es, F, "estimators" ); 
    72         int nE = Es.length();   //number of estimators 
    73         int Ndat;                               //number of data records 
    74         F.lookupValue ( "experiment.ndat",Ndat ); 
    75                          
    76         if ( !L ) { 
    77         #ifdef MEX 
    78                         //mex logger has only from_setting constructor - we  have to fill it... 
    79                         L=new mexlog ( Ndat ); 
    80         #else 
    81                         L=new stdlog(); 
    82         #endif 
     72 
     73    shared_ptr<logger> L = UI::build <logger>( F, "logger" ); 
     74    shared_ptr<DS>  pDS = UI::build<DS> ( F, "system" ); 
     75    Array<shared_ptr<BM> > Es;                  // array of estimators 
     76    UI::get( Es, F, "estimators" ); 
     77    int nE = Es.length();       //number of estimators 
     78    int Ndat;                           //number of data records 
     79    F.lookupValue ( "experiment.ndat",Ndat ); 
     80 
     81    if ( !L ) { 
     82#ifdef MEX 
     83        //mex logger has only from_setting constructor - we  have to fill it... 
     84        L=new mexlog ( Ndat ); 
     85#else 
     86        L=new stdlog(); 
     87#endif 
     88    } 
     89    pDS->log_register ( *L, "true" ); 
     90    string Ename; 
     91    for (int i=0; i<nE; i++) { 
     92        try { 
     93            UI::get ( Ename, F.getRoot()["estimators"][i], "name",UI::optional ); 
     94        } catch ( ...) { 
     95            Ename="Est"+num2str ( i ); 
     96        } 
     97        Es(i)->log_register(*L,Ename); // estimate 
     98    } 
     99    L->init(); 
     100 
     101    vec dt=zeros ( pDS->_drv()._dsize() );   //data variable 
     102    Array<datalink*> Dls(nE); 
     103    Array<datalink*> Dlsc(nE); 
     104    Array<datalink_buffered*> Dls_buf (0); 
     105    for ( int i=0; i<Es.length(); i++ ) { 
     106        //check if they are properly named 
     107        bdm_assert(Es(i)->_yrv()._dsize() == Es(i)->dimensiony(), "Estimator["+num2str(i)+"] does not name yrv properly." 
     108                   "size(yrv)="+num2str(Es(i)->_yrv()._dsize() ) + ", declared dimension of y="+num2str(Es(i)->dimensiony())); 
     109        bdm_assert(Es(i)->_rvc()._dsize() == Es(i)->dimensionc(), "Estimator["+num2str(i)+"] does not name rvc properly." 
     110                   "size(rvc)="+num2str(Es(i)->_rvc()._dsize() ) + ", declared dimension of rvc="+num2str(Es(i)->dimensionc())); 
     111        //connect actual data 
     112        Dls ( i ) = new datalink ( Es ( i )->_yrv(),pDS->_drv() ); //datalink between a datasource and estimator 
     113        //connect data in condition 
     114        if (Es ( i )->_rvc().mint()<0) { 
     115            //delayed values are required 
     116 
     117            //create delayed dl 
     118            int ith_buf=Dls_buf.size(); 
     119            Dls_buf.set_size( ith_buf + 1, true); 
     120            Dls_buf(ith_buf) = new datalink_buffered(); 
     121            //add dl to list of buffered DS 
     122            Dlsc(i) = Dls_buf(ith_buf); 
     123            Dlsc(i)->set_connection ( Es ( i )->_rvc(),pDS->_drv() ); //datalink between a datasource and estimator 
     124 
     125            bdm_assert_debug(Dlsc(i)->_downsize() == Es ( i )->_rvc()._dsize(), "Data required by Est[" + num2str(i) + "], " + 
     126                             Es(i)->_rvc().to_string() + ", are not available in DS drv:" + pDS->_drv().to_string();); 
     127        } else { 
     128            Dlsc ( i ) = new datalink ( Es ( i )->_rvc(),pDS->_drv() ); //datalink between a datasource and estimator 
     129        } 
     130    } 
     131 
     132    // Main cycle 
     133    for ( int tK=1;tK<Ndat;tK++ ) { 
     134        // Data Source 
     135        pDS->getdata ( dt );                                    // read data 
     136        pDS->log_write (); 
     137 
     138        // Estimators 
     139        for (int i=0; i<nE; i++) { 
     140            Es(i)->bayes ( Dls(i)->pushdown ( dt ), Dlsc(i)->pushdown(dt) );            // update estimates 
     141 
     142            Es(i)->log_write (); 
     143        } 
     144        // Regulators 
     145        L->step(); 
     146                pDS->step();                                                    // simulator step 
     147                for (int i=0; i<Dls_buf.length(); i++){ 
     148                        Dls_buf(i)->store_data(dt); 
    83149                } 
    84         pDS->log_register ( *L, "true" ); 
    85         string Ename; 
    86         for (int i=0; i<nE; i++){ 
    87                 try { 
    88                         UI::get ( Ename, F.getRoot()["estimators"][i], "name",UI::optional ); 
    89                 } catch ( ...) { 
    90                         Ename="Est"+num2str ( i ); 
    91                 } 
    92                 Es(i)->log_register(*L,Ename); // estimate 
    93         } 
    94         L->init(); 
    95  
    96         vec dt=zeros ( pDS->_drv()._dsize() );   //data variable 
    97         Array<datalink*> Dls(nE);  
    98         Array<datalink*> Dlsc(nE);  
    99         for (int i=0; i<nE; i++){ 
    100                 Dls(i)=new datalink( Es(i)->_yrv(),pDS->_drv() ); //datalink between a datasource and estimator 
    101                 Dlsc(i)=new datalink( Es(i)->_rvc(),pDS->_drv() ); //datalink between a datasource and estimator 
    102         } 
    103          
    104         // Main cycle 
    105         for ( int tK=1;tK<Ndat;tK++ ) { 
    106                 // Data Source 
    107                 pDS->step();                                                    // simulator step 
    108                 pDS->getdata ( dt );                                    // read data 
    109                 pDS->log_write (); 
    110                  
    111                 // Estimators 
    112                 for (int i=0; i<nE; i++){ 
    113                         Es(i)->bayes ( Dls(i)->pushdown ( dt ), Dlsc(i)->pushdown(dt) );                // update estimates 
    114  
    115                         Es(i)->log_write (); 
    116                 } 
    117                 // Regulators 
    118                 L->step(); 
    119150        } 
    120151 
    121         L->finalize(); 
     152    L->finalize(); 
    122153#ifdef MEX 
    123         mexlog* mL=dynamic_cast<mexlog*> ( L.get() ); 
     154    mexlog* mL=dynamic_cast<mexlog*> ( L.get() ); 
    124155 
    125         if ( mL ) { // user wants output!! 
    126                 if ( n_output<1 ) mexErrMsgTxt ( "Wrong number of output variables!" ); 
    127                 output[0] = mL->toCell(); 
    128                 if (n_output>1){ // write estimators 
    129                         UImxArray Ests; 
    130                         UI::save(Es, Ests,"estimators"); 
    131                         output[1]=UImxArray::create_mxArray(Ests); 
    132                 } 
    133                 if (n_output>2) { 
    134                         mL->_setting_conf().setAutoConvert(true); 
    135                         output[2]= UImxArray::create_mxArray(mL->_setting_conf().getRoot()); 
    136                 } 
    137         } 
     156    if ( mL ) { // user wants output!! 
     157        if ( n_output<1 ) mexErrMsgTxt ( "Wrong number of output variables!" ); 
     158        output[0] = mL->toCell(); 
     159        if (n_output>1) { // write estimators 
     160            UImxArray Ests; 
     161            UI::save(Es, Ests,"estimators"); 
     162            output[1]=UImxArray::create_mxArray(Ests); 
     163        } 
     164        if (n_output>2) { 
     165            mL->_setting_conf().setAutoConvert(true); 
     166            output[2]= UImxArray::create_mxArray(mL->_setting_conf().getRoot()); 
     167        } 
     168    } 
    138169#endif 
    139170 
    140         for (int i=0; i<nE; i++){ 
    141                 delete Dls(i); 
    142                 delete Dlsc(i); 
    143         } 
     171    for (int i=0; i<nE; i++) { 
     172        delete Dls(i); 
     173        delete Dlsc(i); 
     174    } 
    144175} 
    145176