[706] | 1 | |
---|
| 2 | /*! |
---|
| 3 | \file |
---|
| 4 | \brief Multi-Estimator (developped for PMSM) |
---|
[1295] | 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[] ) { |
---|
[1295] | 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." ); |
---|
[1184] | 40 | |
---|
[1295] | 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 | } |
---|
[1184] | 56 | |
---|
[1295] | 57 | //DBG |
---|
| 58 | F.writeFile ( "pmsm_estim.cfg" ); |
---|
[1184] | 59 | |
---|
| 60 | #else |
---|
[706] | 61 | int main ( int argc, char* argv[] ) { |
---|
[1295] | 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 ); |
---|
[1184] | 71 | #endif |
---|
[706] | 72 | |
---|
[1295] | 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(); |
---|
[706] | 146 | pDS->step(); // simulator step |
---|
[1295] | 147 | for (int i=0; i<Dls_buf.length(); i++){ |
---|
| 148 | Dls_buf(i)->store_data(dt); |
---|
[706] | 149 | } |
---|
| 150 | } |
---|
| 151 | |
---|
[1295] | 152 | L->finalize(); |
---|
[1184] | 153 | #ifdef MEX |
---|
[1295] | 154 | mexlog* mL=dynamic_cast<mexlog*> ( L.get() ); |
---|
[706] | 155 | |
---|
[1295] | 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 | } |
---|
[1184] | 169 | #endif |
---|
| 170 | |
---|
[1295] | 171 | for (int i=0; i<nE; i++) { |
---|
| 172 | delete Dls(i); |
---|
| 173 | delete Dlsc(i); |
---|
| 174 | } |
---|
[706] | 175 | } |
---|