Changeset 260

Show
Ignore:
Timestamp:
02/02/09 14:47:34 (15 years ago)
Author:
smidl
Message:

working UI example

Files:
7 modified

Legend:

Unmodified
Added
Removed
  • CMakeLists.txt

    r256 r260  
    2929        SET(CMAKE_CXX_FLAGS "-Wall -pipe  -Wno-unknown-pragmas") 
    3030        SET(CMAKE_CXX_FLAGS_RELEASE " -DNDEBUG -O2") 
    31         SET(CMAKE_CXX_FLAGS_DEBUG "-g") 
    32         SET(CMAKE_CXX_FLAGS "-O2 -Wall -Wno-unknown-pragmas") 
     31        SET(CMAKE_CXX_FLAGS_DEBUG "-g -O0") 
     32        SET(CMAKE_CXX_FLAGS "-Wall -Wno-unknown-pragmas") 
    3333        ADD_DEFINITIONS(-DNDEBUG) 
    3434        INCLUDE(CMakeLocal.txt OPTIONAL)         
  • bdm/stat/loggers.h

    r257 r260  
    1414#define LGR_H 
    1515 
    16 #include <itpp/itbase.h> 
    1716#include "libBM.h" 
    18 #include "../itpp_ext.h" 
    1917 
    2018namespace bdm{ 
  • bdm/uibuilder.cpp

    r257 r260  
    3737UIREGISTER(UIinternal); 
    3838 
     39        void UI_DBG ( Setting &S, const string &spc ) { 
     40                const char *Name=S.getName(); 
     41                if ( Name!=NULL ) {cout << spc << std::string ( Name );}; 
     42                Setting::Type T=S.getType(); 
     43                switch ( T ) { 
     44                        case Setting::TypeArray: 
     45                                cout << endl; 
     46                                for ( int i=0;i<S.getLength();i++ ) {UI_DBG ( S[i], spc+" " );}; 
     47                                break; 
     48                        case Setting::TypeList: 
     49                                cout << endl; 
     50                                for ( int i=0;i<S.getLength();i++ ) {UI_DBG ( S[i], spc+" " );}; 
     51                                break; 
     52                        case Setting::TypeGroup: 
     53                                cout << endl; 
     54                                for ( int i=0;i<S.getLength();i++ ) {UI_DBG ( S[i], spc+" " );} 
     55                                break; 
     56                        case Setting::TypeInt: 
     57                                cout << " = "; 
     58                                cout << int ( S ) <<endl; 
     59                                break; 
     60                        case Setting::TypeFloat: 
     61                                cout << " = "; 
     62                                cout << double ( S ) <<endl; 
     63                                break; 
     64                        case Setting::TypeString: 
     65                                cout << " = "; 
     66                                cout << ( const char* ) ( S ) <<endl; 
     67                                break; 
     68                        case Setting::TypeBoolean: 
     69                                cout << " = "; 
     70                                cout << bool ( S ) <<endl; 
     71                                break; 
     72                                default: {cout << "?";}; 
     73                } 
     74        } 
     75 
    3976} 
    4077//UIexternal* UIexternal_instance =new UIexternal(); 
  • bdm/uibuilder.h

    r257 r260  
    66#include "libconfig/libconfig.h++" 
    77 
    8 namespace bdm{ 
     8namespace bdm { 
    99 
    10 using namespace libconfig; 
    11 using namespace std; 
     10        using namespace libconfig; 
     11        using namespace std; 
    1212 
    1313#define CHECK_UITYPE(S,Type) it_assert_debug(S.getType()==Setting::Type, string("Wrong input path \"")+string(S.getPath())+string("\"")); 
     
    1616////////// GLOBAL VAriables 
    1717 
    18 class UIbuilder; 
     18        class UIbuilder; 
    1919//! Internal structure mapping strings to UIBuilder objects 
    20 typedef map<const string, const UIbuilder*> UImap; 
    21 extern UImap __uimap__; 
     20        typedef map<const string, const UIbuilder*> UImap; 
     21        extern UImap __uimap__; 
    2222 
    23 class UIFile : public Config{ 
     23        class UIFile : public Config { 
    2424        public: 
    25         UIFile(const char * fname):Config(){ 
    26                 try{Config::readFile(fname);}  
    27                 catch (ParseException& P) { 
    28                         char msg[200]; 
    29                         sprintf(msg,"Error in file %s  on line %d.", fname, P.getLine()); 
    30                         it_error(msg); 
    31                 }  
    32                 catch (FileIOException f) {it_error("File " + string(fname) + " not found");}  
    33         } 
    34 }; 
     25                UIFile ( const char * fname ) :Config() { 
     26                        try{Config::readFile ( fname );} 
     27                        catch ( ParseException& P ) { 
     28                                char msg[200]; 
     29                                sprintf ( msg,"Error in file %s  on line %d.", fname, P.getLine() ); 
     30                                it_error ( msg ); 
     31                        } 
     32                        catch ( FileIOException f ) {it_error ( "File " + string ( fname ) + " not found" );} 
     33                } 
     34        }; 
    3535 
    36 /*!\brief Builds computational object from a UserInfo structure 
     36        /*!\brief Builds computational object from a UserInfo structure 
    3737 
    38 Return value is a pointer to the created object (memory management issue?) 
    39 */ 
    40 class UIbuilder { 
     38        Return value is a pointer to the created object (memory management issue?) 
     39        */ 
     40        class UIbuilder { 
    4141        protected: 
    42                 static const UIbuilder* theinstance; 
    43         vec getvec ( Setting& S ) { 
    44                 CHECK_UITYPE(S,TypeArray); 
    45                 vec tmp; 
    46                 tmp.set_size ( S.getLength() ); 
    47                 for ( int i=0;i<S.getLength();i++ ) { 
    48                         switch (S[i].getType()) { 
    49                                 case Setting::TypeFloat : 
    50                                         tmp[i]=double(S[i]);break; 
    51                                 case Setting::TypeInt : 
    52                                         tmp[i]=int(S[i]);break; 
    53                                 case Setting::TypeBoolean : 
    54                                         tmp[i]=bool(S[i]);break; 
    55                                 default: it_error("libconfig error?"); 
    56                         }  
    57                 } 
    58                 return tmp; 
    59         }; 
    60         vec getivec ( Setting& S ) { 
    61                 CHECK_UITYPE(S,TypeArray); 
    62                 vec tmp; 
    63                 tmp.set_size ( S.getLength() ); 
    64                 for ( int i=0;i<S.getLength();i++ ) { 
    65                         switch (S[i].getType()) { 
    66                                 case Setting::TypeFloat : 
    67                                         tmp[i]=double(S[i]);break; 
    68                                 case Setting::TypeInt : 
    69                                         tmp[i]=int(S[i]);break; 
    70                                 case Setting::TypeBoolean : 
    71                                         tmp[i]=bool(S[i]);break; 
    72                                         default: it_error("libconfig error?"); 
    73                         }  
    74                 } 
    75                 return tmp; 
    76         }; 
     42                const vec getvec ( Setting& S ) const { 
     43                        CHECK_UITYPE ( S,TypeArray ); 
     44                        vec tmp; 
     45                        tmp.set_size ( S.getLength() ); 
     46                        for ( int i=0;i<S.getLength();i++ ) { 
     47                                switch ( S[i].getType() ) { 
     48                                        case Setting::TypeFloat : 
     49                                                tmp[i]=double ( S[i] );break; 
     50                                        case Setting::TypeInt : 
     51                                                tmp[i]=int ( S[i] );break; 
     52                                        case Setting::TypeBoolean : 
     53                                                tmp[i]=bool ( S[i] );break; 
     54                                        default: it_error ( "libconfig error?" ); 
     55                                } 
     56                        } 
     57                        return tmp; 
     58                }; 
     59                const vec getivec ( Setting& S ) const { 
     60                        CHECK_UITYPE ( S,TypeArray ); 
     61                        vec tmp; 
     62                        tmp.set_size ( S.getLength() ); 
     63                        for ( int i=0;i<S.getLength();i++ ) { 
     64                                switch ( S[i].getType() ) { 
     65                                        case Setting::TypeFloat : 
     66                                                tmp[i]=double ( S[i] );break; 
     67                                        case Setting::TypeInt : 
     68                                                tmp[i]=int ( S[i] );break; 
     69                                        case Setting::TypeBoolean : 
     70                                                tmp[i]=bool ( S[i] );break; 
     71                                        default: it_error ( "libconfig error?" ); 
     72                                } 
     73                        } 
     74                        return tmp; 
     75                }; 
    7776        public: 
    7877                //!Constructor needs to be run only once macro UIREGISTER 
    79                 UIbuilder(const string &typ){__uimap__.insert(make_pair(typ,this));} 
     78                UIbuilder ( const string &typ ) {__uimap__.insert ( make_pair ( typ,this ) );} 
    8079                //! Function building the computational object 
    81                 virtual bdmroot* build(Setting &S) const =0; 
    82 }; 
     80                virtual bdmroot* build ( Setting &S ) const =0; 
     81        }; 
    8382 
    84 class UIexternal:public UIbuilder{ 
     83        class UIexternal:public UIbuilder { 
    8584        public: 
    86                 UIexternal():UIbuilder("external"){} 
    87                 bdmroot* build(Setting &S) const; 
    88 }; 
     85                UIexternal() :UIbuilder ( "external" ) {} 
     86                bdmroot* build ( Setting &S ) const; 
     87        }; 
    8988 
    90 class UIinternal:public UIbuilder{ 
     89        class UIinternal:public UIbuilder { 
    9190        public: 
    92                 UIinternal():UIbuilder("internal"){} 
    93                 bdmroot* build(Setting &S) const; 
    94 }; 
     91                UIinternal() :UIbuilder ( "internal" ) {} 
     92                bdmroot* build ( Setting &S ) const; 
     93        }; 
     94 
     95        //! [Debugging] Print values in current S to cout 
     96        void UI_DBG ( Setting &S, const string &spc ); 
    9597 
    9698//! Prototype of a UI builder. Return value is by the second argument since it type checking via \c dynamic_cast. 
    97 template<class T> 
    98 void UIbuild(Setting &S, T* &ret){ 
    99         CHECK_UITYPE(S,TypeGroup); 
    100         // Check if field "type" is present, if not it is not a valid UI 
    101         it_assert_debug(S.exists("type"), string(S.getPath())+" is not a valid UI!"); 
    102                  
    103         const string typ=S["type"];      
    104         // Find "type" in list of registred UI builders 
    105         UImap::const_iterator iter = __uimap__.find( typ ); 
    106         if( iter == __uimap__.end()) { 
    107                 it_error("UI of type \"" + typ + "\" is not registered!"); 
    108         } 
    109          
    110         //BUILD the result 
    111         ret = dynamic_cast<T*>(iter->second->build(S)); 
    112 }; 
     99        template<class T> 
     100        void UIbuild ( Setting &S, T* &ret ) { 
     101                CHECK_UITYPE ( S,TypeGroup ); 
     102                // Check if field "type" is present, if not it is not a valid UI 
     103                it_assert_debug ( S.exists ( "type" ), string ( S.getPath() ) +" is not a valid UI!" ); 
    113104 
    114 //! Auxiliary function allowing recursivity in S 
    115 template<class T> 
    116 void UIcall(Setting &S, void (*func)(Setting&, T), T Tmp ){ 
    117         CHECK_UITYPE(S,TypeGroup); 
    118         // Check if field "type" is present, if not it is not a valid UI 
    119         it_assert_debug(S.exists("type"), string(S.getPath())+" is not a valid UI!"); 
    120                  
    121         const string typ=S["type"];      
    122         if(typ=="internal"){ 
    123                 try {    
    124                         Setting* Stmp = &S; 
    125                         do {Stmp=&(Stmp->getParent());} while (!Stmp->isRoot()); 
    126                         Setting& intS=Stmp->lookup((const char*)S["path"]); 
    127                         func(intS, Tmp); // <======== calling UIset 
     105                const string typ=S["type"]; 
     106                // Find "type" in list of registred UI builders 
     107                UImap::const_iterator iter = __uimap__.find ( typ ); 
     108                if ( iter == __uimap__.end() ) { 
     109                        it_error ( "UI of type \"" + typ + "\" is not registered!" ); 
     110                } 
     111 
     112                //BUILD the result 
     113                try { 
     114                        ret = dynamic_cast<T*> ( iter->second->build ( S ) ); 
     115                } 
     116                catch ( SettingTypeException e ) { 
     117                        UI_DBG(S,"");  
     118                        it_error ( "Setting " +string ( e.getPath() ) +" is of incorrect Type" );} 
     119                catch ( SettingNotFoundException e ) { 
     120                        UI_DBG(S,""); 
     121                        it_error ( "Setting " + string ( e.getPath() ) +" was not found" );} 
     122        }; 
     123 
     124//! Auxiliary function allowing recursivity in S (too complex, remove?) 
     125        template<class T> 
     126        void UIcall ( Setting &S, void ( *func ) ( Setting&, T ), T Tmp ) { 
     127                CHECK_UITYPE ( S,TypeGroup ); 
     128                // Check if field "type" is present, if not it is not a valid UI 
     129                it_assert_debug ( S.exists ( "type" ), string ( S.getPath() ) +" is not a valid UI!" ); 
     130 
     131                const string typ=S["type"]; 
     132                if ( typ=="internal" ) { 
     133                        try { 
     134                                Setting* Stmp = &S; 
     135                                do {Stmp=& ( Stmp->getParent() );} 
     136                                while ( !Stmp->isRoot() ); 
     137                                Setting& intS=Stmp->lookup ( ( const char* ) S["path"] ); 
     138                                func ( intS, Tmp ); // <======== calling func 
     139                                return; 
     140                        } 
     141                        catch ( ... ) { 
     142                                it_error ( "Internal field " + string ( S.getPath() ) + " not valid" ); 
     143                        } 
     144                } 
     145                if ( typ=="external" ) { 
     146                        UIFile C(S["filename"]); 
     147                        try { 
     148                                func ( C.lookup ( ( const char* ) S["path"] ), Tmp ); 
     149                        } 
     150                        catch ( ... ) { 
     151                                it_error ( "External field " + string ( S.getPath() ) + " not valid" ); 
     152                        } 
    128153                        return; 
    129154                } 
    130                 catch (...) { it_error("Internal field " + string(S.getPath()) + " not valid"); 
    131                 } 
    132         } 
    133         if(typ=="extern"){ 
    134                 Config C; 
    135                 bdmroot* tmp; 
    136                 try{C.readFile((const char*)S["filename"]);} catch (...){ 
    137                         it_error("File " + string((const char*)S["filename"]) + " not found or broken"); 
    138                 } 
    139                 try {func(C.lookup((const char*)S["path"]), Tmp);} catch (...) { //<=========== calling UIset 
    140                         it_error("External field " + string(S.getPath()) + " not valid"); 
    141                 } 
    142                 return; 
    143         } 
    144          
    145         // v======================= calling final set 
    146         func(S, Tmp); 
    147 }; 
     155 
     156                // v======================= calling final func 
     157                func ( S, Tmp ); 
     158        }; 
    148159 
    149160} 
    150 #endif UIBUILD 
     161#endif //UIBUILD 
  • pmsm/pmsmDS.h

    r258 r260  
    1212 
    1313#include <stat/loggers.h> 
    14  
     14#include "simulator.h" 
    1515#include "pmsm.h" 
    16 #include "simulator.h" 
    17 #include <uibuilder.h> 
    1816 
    1917//! Simulator of PMSM machine with predefined profile on omega 
     
    2220protected: 
    2321        //! indeces of logged variables 
    24         int L_x, L_ou, L_oy, L_iu; 
     22        int L_x, L_ou, L_oy, L_iu, L_optu; 
    2523        //! Setpoints of omega in timespans given by dt_prof 
    2624        vec profileWw; 
     
    2927        //! Number of miliseconds per discrete time step 
    3028        int Dt; 
     29        //! options for logging, string in which each character has a meaning: 
     30        //! \c u - log predictions of 'true' voltage 
     31        string opt; 
    3132public: 
    32         pmsmDS ( int Dt0 ) : Dt ( Dt0 ) {} 
     33        //! Constructor with fixed sampling period 
     34        pmsmDS (string opt0="" ) : Dt ( 125 ), opt(opt0) {} 
    3335        void set_parameters ( double Rs0, double Ls0, double Fmag0, double Bf0, double p0, double kp0, double J0, double Uc0, double DT0, double dt0 ) { 
    3436                pmsmsim_set_parameters ( Rs0, Ls0, Fmag0, Bf0, p0, kp0, J0, Uc0, DT0, dt0 ); 
     
    4244                static double Ww; // W 
    4345                if ( t>=dt_prof*ind ) { 
     46                        ind++; 
    4447                        if ( ind<profileWw.length() ) { 
    45                                 ind++; 
    4648                                //linear increase 
    47                                 dW = profileWw ( ind )-profileWw ( ind-1 ); 
     49                                if ( profileWw.length() ==1 ) { 
     50                                        Ww=profileWw ( 0 ); dW=0.0;} 
     51                                else { 
     52                                        dW = profileWw ( ind )-profileWw ( ind-1 ); 
     53                                        dW *=125e-6/dt_prof; 
     54                                } 
    4855                        } 
    4956                        else { 
     
    5865        void log_add ( logger &L ) { 
    5966                L_x = L.add ( rx, "x" ); 
    60                 L_oy = L.add ( ry, "oi" ); 
    61                 L_ou = L.add ( ru, "ou" ); 
    62                 L_iu = L.add ( ru, "iu" ); 
     67                L_oy = L.add ( ry, "obs" ); 
     68                L_ou = L.add ( ru, "obs" ); 
     69                L_iu = L.add ( ru, "true" ); 
     70                // log differences 
     71                if (opt.find("u")==string::npos){ 
     72                        L_optu = L.add(ru, "model"); 
     73                } 
    6374        } 
    6475 
     
    6879                L.logit ( L_ou, vec_2 ( KalmanObs[0],KalmanObs[1] ) ); 
    6980                L.logit ( L_iu, vec_2 ( KalmanObs[4],KalmanObs[5] ) ); 
    70         } 
     81                if (opt.find("u")==string::npos){ 
     82                        double sq3=sqrt(3.0); 
     83                        double ua,ub; 
     84                        double i1=x[0]; 
     85                        double i2=0.5*(-i1+sq3*x[1]); 
     86                        double i3=0.5*(-i1-sq3*x[1]); 
     87                        double u1=KalmanObs[0]; 
     88                        double u2=0.5*(-u1+sq3*KalmanObs[1]); 
     89                        double u3=0.5*(-u1-sq3*KalmanObs[1]); 
     90                         
     91                        double du1=0.7*(double(i1>0.1) - double(i1<-0.1))+0.05*i1; 
     92                        double du2=0.7*(double(i2>0.1) - double(i2<-0.1))+0.05*i2; 
     93                        double du3=0.7*(double(i3>0.1) - double(i3<-0.1))+0.05*i3; 
     94                        ua = (2.0*(u1-du1)-(u2-du2)-(u3-du3))/3.0; 
     95                        ub = ((u2-du2)-(u3-du3))/sq3; 
     96                        L.logit( L_optu , vec_2 (ua,ub)); 
     97                } 
     98                } 
    7199 
    72100        void set_profile ( double dt, const vec &Ww ) {dt_prof=dt; profileWw=Ww;} 
    73101}; 
    74  
    75 class UIpmsm: public UIbuilder { 
    76 public: 
    77         UIpmsm() :UIbuilder ( "pmsm" ) {} 
    78         //Non-standard BUILD!! Does not create 
    79         void build ( Setting &S, void** result ) const { 
    80  
    81         }; 
    82 }; 
    83  
    84 class UIpmsmDS: public UIbuilder { 
    85         static void tmp_set ( Setting &S, pmsmDS* tmp ) { 
    86                 tmp->set_parameters ( S["Rs"], S["Ls"], S["Fmag"], S["Bf"], S["p"], S["kp"], \ 
    87                                       S["J"], S["Uc"], S["DT"], 1.0e-6 ); 
    88         }; 
    89 public: 
    90         UIpmsmDS() :UIbuilder ( "pmsmDS" ) {}; 
    91         bdmroot* build ( Setting &S ) const { 
    92                 pmsmDS* tmp = new pmsmDS ( S["DT"] ); 
    93                 //allowing recursive Settings 
    94                 UIcall<pmsmDS*> ( S["params"], &tmp_set , tmp ); 
    95         }; 
    96  
    97 }; 
    98 UIREGISTER(UIpmsmDS); 
  • pmsm/sim.cpp

    r257 r260  
    1111*/ 
    1212 
    13 #include <uibuilder.h> 
    14 #include <stat/libDS.h> 
    15 #include "pmsmDS.h" 
    16  
     13#include "pmsm_ui.h" 
    1714#include <stat/loggers_ui.h> 
    1815 
     
    3532                DS->step(); 
    3633                DS->logit(*L); 
     34                L->step(); 
    3735        } 
    3836         
  • tests/UI/UIbuilder_test.cpp

    r257 r260  
    2222        UItest():UIbuilder("test"){} 
    2323        bdmroot* build(Setting &S) const{ 
    24                 try{ 
    2524                        int a=S["a"]; 
    2625                        string St; 
     
    2827                        cls* tmp = new cls(a,St); 
    2928                        return tmp; 
    30                 } 
    31                 catch (...){ 
    32                         it_error(string(S.getPath()) + " is not a valid test UI"); 
    33                 } 
    3429        }        
    3530}; 
     
    3934        UItest2():UIbuilder("test2"){} 
    4035        bdmroot* build(Setting &S) const{ 
    41                 try{ 
    4236                        int a=S["a"]; 
    4337                        string St; 
    4438                        S.lookupValue("S",St); 
    4539                        return  new cls2(a,St); 
    46                 } 
    47                 catch (...){ 
    48                         it_error(string(S.getPath()) + " is not a valid test UI"); 
    49                 } 
    5040        }        
    5141        int no(){return 2;}