[706] | 1 | |
---|
| 2 | /*! |
---|
| 3 | \file |
---|
| 4 | \brief Multi-Estimator (developped for PMSM) |
---|
[1174] | 5 | |
---|
[706] | 6 | */ |
---|
| 7 | |
---|
| 8 | #include "base/user_info.h" |
---|
| 9 | #include "base/loggers.h" |
---|
| 10 | #include "estim/kalman.h" |
---|
[1192] | 11 | #include "estim/mixtures.h" |
---|
[706] | 12 | #include "pmsmDS.h" |
---|
| 13 | #include "filters.h" |
---|
| 14 | #include "base/datasources.h" |
---|
[1168] | 15 | #include "simulator_zdenek/ekf_example/ekf_obj.h" |
---|
[706] | 16 | |
---|
| 17 | using namespace bdm; |
---|
[1184] | 18 | #ifdef MEX |
---|
| 19 | #include <itpp/itmex.h> |
---|
| 20 | #include <mex/mex_BM.h> |
---|
| 21 | #include <mex/mex_logger.h> |
---|
| 22 | #include <mex/mex_datasource.h> |
---|
| 23 | #include <mex/mex_function.h> |
---|
| 24 | |
---|
| 25 | void 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." ); |
---|
| 40 | |
---|
| 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 | } |
---|
| 56 | |
---|
| 57 | //DBG |
---|
| 58 | F.writeFile ( "pmsm_estim.cfg" ); |
---|
| 59 | |
---|
| 60 | #else |
---|
[706] | 61 | int 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 ); |
---|
[1184] | 66 | #endif |
---|
| 67 | |
---|
[706] | 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 |
---|
[1184] | 71 | UI::get( Es, F, "estimators" ); |
---|
[706] | 72 | int nE = Es.length(); //number of estimators |
---|
| 73 | int Ndat; //number of data records |
---|
| 74 | F.lookupValue ( "experiment.ndat",Ndat ); |
---|
[1184] | 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 |
---|
| 83 | } |
---|
[706] | 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(); |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | L->finalize(); |
---|
[1184] | 122 | #ifdef MEX |
---|
| 123 | mexlog* mL=dynamic_cast<mexlog*> ( L.get() ); |
---|
[706] | 124 | |
---|
[1184] | 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 | } |
---|
| 138 | #endif |
---|
| 139 | |
---|
[706] | 140 | for (int i=0; i<nE; i++){ |
---|
| 141 | delete Dls(i); |
---|
| 142 | delete Dlsc(i); |
---|
| 143 | } |
---|
| 144 | } |
---|