root/applications/pmsm/pmsm_estim.cpp @ 1184

Revision 1184, 4.5 kB (checked in by smidl, 14 years ago)

corrections of PMSM simulations

  • Property svn:eol-style set to native
Line 
1
2/*!
3\file
4\brief Multi-Estimator (developped for PMSM)
5 
6 */
7
8#include "base/user_info.h"
9#include "base/loggers.h"
10#include "estim/kalman.h"
11#include "pmsmDS.h"
12#include "filters.h"
13#include "base/datasources.h"
14#include "simulator_zdenek/ekf_example/ekf_obj.h"
15
16using namespace bdm;
17#ifdef MEX
18#include <itpp/itmex.h>
19#include <mex/mex_BM.h>
20#include <mex/mex_logger.h>
21#include <mex/mex_datasource.h>
22#include <mex/mex_function.h>
23
24void mexFunction ( int n_output, mxArray *output[], int n_input, const mxArray *input[] ) {
25        // Check the number of inputs and output arguments
26        if ( n_input<2 ) mexErrMsgTxt ( "Usage:\n"
27                                                "[Res,estimators,Res2]=estimator(system, estimators, experiment, logger)\n"
28                                                "  system     = struct('class','datasource',...);  % Estimated system\n"
29                                                "  estimators = {struct('class','estimator',...),  % Estimators\n"
30                                                "                struct('class','estimator',...),...} \n"
31                                                "  === optional ==="
32                                                "  experiment = struct('ndat',10);                 % number of data in experiment, full length of finite datasources, 10 otherwise \n"
33                                                "  logger     = struct('class','mexlogger');       % How to store results, default=mexlog, i.e. matlab structure\n\n"
34                                                "Output:\n"
35                                                "  Res          Matlab structure with logged results, \n" 
36                                                "  estimators   Array of estimators updated with data \n"
37                                                "  Res2         When logfull log_level is on, this structure is filled with structures of posterior densities\n\n"
38                                                "see documentation of classes datasource, BM, and mexlogger and their offsprings in BDM." );
39
40        RV::clear_all();
41        //CONFIG
42        UImxArray F;
43        try {
44                F.addGroup ( input[0],"system" );
45                F.addList ( input[1],"estimators" );
46                if ( n_input>2 ) {
47                        F.addGroup ( input[2],"experiment" );
48                }
49                if ( n_input>3 ) {
50                        F.addGroup ( input[3],"logger" );
51                }
52        } catch ( SettingException e ) {
53                it_error ( "error: "+string ( e.getPath() ) );
54        }
55
56        //DBG
57        F.writeFile ( "pmsm_estim.cfg" );
58
59#else
60int main ( int argc, char* argv[] ) {
61        const char *fname;
62        if ( argc>1 ) {fname = argv[1]; }
63        else { cout << "Missing configuration file.\n Usage: \n $> estimator config_file.cfg"<<endl; abort(); }
64        UIFile F ( fname );
65#endif
66       
67        shared_ptr<logger> L = UI::build <logger>( F, "logger" );
68        shared_ptr<DS>  pDS = UI::build<DS> ( F, "system" );
69        Array<shared_ptr<BM> > Es;                      // array of estimators
70        UI::get( Es, F, "estimators" );
71        int nE = Es.length();   //number of estimators
72        int Ndat;                               //number of data records
73        F.lookupValue ( "experiment.ndat",Ndat );
74                       
75        if ( !L ) {
76        #ifdef MEX
77                        //mex logger has only from_setting constructor - we  have to fill it...
78                        L=new mexlog ( Ndat );
79        #else
80                        L=new stdlog();
81        #endif
82                }
83        pDS->log_register ( *L, "true" );
84        string Ename;
85        for (int i=0; i<nE; i++){
86                try {
87                        UI::get ( Ename, F.getRoot()["estimators"][i], "name",UI::optional );
88                } catch ( ...) {
89                        Ename="Est"+num2str ( i );
90                }
91                Es(i)->log_register(*L,Ename); // estimate
92        }
93        L->init();
94
95        vec dt=zeros ( pDS->_drv()._dsize() );   //data variable
96        Array<datalink*> Dls(nE); 
97        Array<datalink*> Dlsc(nE); 
98        for (int i=0; i<nE; i++){
99                Dls(i)=new datalink( Es(i)->_yrv(),pDS->_drv() ); //datalink between a datasource and estimator
100                Dlsc(i)=new datalink( Es(i)->_rvc(),pDS->_drv() ); //datalink between a datasource and estimator
101        }
102       
103        // Main cycle
104        for ( int tK=1;tK<Ndat;tK++ ) {
105                // Data Source
106                pDS->step();                                                    // simulator step
107                pDS->getdata ( dt );                                    // read data
108                pDS->log_write ();
109               
110                // Estimators
111                for (int i=0; i<nE; i++){
112                        Es(i)->bayes ( Dls(i)->pushdown ( dt ), Dlsc(i)->pushdown(dt) );                // update estimates
113
114                        Es(i)->log_write ();
115                }
116                // Regulators
117                L->step();
118        }
119
120        L->finalize();
121#ifdef MEX
122        mexlog* mL=dynamic_cast<mexlog*> ( L.get() );
123
124        if ( mL ) { // user wants output!!
125                if ( n_output<1 ) mexErrMsgTxt ( "Wrong number of output variables!" );
126                output[0] = mL->toCell();
127                if (n_output>1){ // write estimators
128                        UImxArray Ests;
129                        UI::save(Es, Ests,"estimators");
130                        output[1]=UImxArray::create_mxArray(Ests);
131                }
132                if (n_output>2) {
133                        mL->_setting_conf().setAutoConvert(true);
134                        output[2]= UImxArray::create_mxArray(mL->_setting_conf().getRoot());
135                }
136        }
137#endif
138
139        for (int i=0; i<nE; i++){
140                delete Dls(i);
141                delete Dlsc(i);
142        }
143}
Note: See TracBrowser for help on using the browser.