1 | |
---|
2 | /*! |
---|
3 | \file |
---|
4 | \brief Multi-Estimator (developped for PMSM) |
---|
5 | |
---|
6 | |
---|
7 | */ |
---|
8 | |
---|
9 | #include "base/user_info.h" |
---|
10 | #include "base/loggers.h" |
---|
11 | #include "estim/kalman.h" |
---|
12 | #include "pmsmDS.h" |
---|
13 | #include "filters.h" |
---|
14 | #include "base/datasources.h" |
---|
15 | #include "simulator_zdenek/ekf_example/ekf_obj.h" |
---|
16 | |
---|
17 | #include <itpp/itmex.h> |
---|
18 | #include <mex/mex_BM.h> |
---|
19 | #include <mex/mex_logger.h> |
---|
20 | #include <mex/mex_datasource.h> |
---|
21 | #include <mex/mex_function.h> |
---|
22 | |
---|
23 | using namespace bdm; |
---|
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 | string fname; |
---|
28 | if (n_input>0){ |
---|
29 | fname=mxArray2string(input[0]); |
---|
30 | } else { |
---|
31 | printf("file name missing"); |
---|
32 | } |
---|
33 | // ------------------ |
---|
34 | |
---|
35 | UIFile F ( fname.c_str()); |
---|
36 | |
---|
37 | logger* L; |
---|
38 | DS * DS; |
---|
39 | Array<BM*> Es; // array of estimators |
---|
40 | int Ndat; //number of data records |
---|
41 | int nE; //number of estimators |
---|
42 | |
---|
43 | try { |
---|
44 | UIbuild ( F.lookup ( "logger" ),L ); |
---|
45 | UIbuild ( F.lookup ( "system" ),DS ); |
---|
46 | F.lookupValue ( "experiment.ndat",Ndat ); |
---|
47 | Setting& S=F.lookup ( "estimator" ); |
---|
48 | nE = S.getLength(); |
---|
49 | Es.set_size(nE); |
---|
50 | for(int i=0;i<nE;i++){ |
---|
51 | UIbuild (S[i] ,Es(i) ); |
---|
52 | } |
---|
53 | } |
---|
54 | catch UICATCH; |
---|
55 | |
---|
56 | DS->log_add ( *L ); |
---|
57 | string nic=""; |
---|
58 | for (int i=0; i<nE; i++){ |
---|
59 | Es(i)->log_add(*L,nic); // estimate |
---|
60 | } |
---|
61 | L->init(); |
---|
62 | |
---|
63 | vec dt=zeros ( DS->_drv()._dsize() ); //data variable |
---|
64 | Array<datalink*> Dls(nE); |
---|
65 | for (int i=0; i<nE; i++){ |
---|
66 | Dls(i)=new datalink( Es(i)->_drv(),DS->_drv() ); //datalink between a datasource and estimator |
---|
67 | } |
---|
68 | |
---|
69 | // Main cycle |
---|
70 | for ( int tK=1;tK<Ndat;tK++ ) { |
---|
71 | // Data Source |
---|
72 | DS->step(); // simulator step |
---|
73 | DS->getdata ( dt ); // read data |
---|
74 | DS->logit ( *L ); |
---|
75 | |
---|
76 | // Estimators |
---|
77 | for (int i=0; i<nE; i++){ |
---|
78 | Es(i)->bayes ( Dls(i)->pushdown ( dt ) ); // update estimates |
---|
79 | |
---|
80 | Es(i)->logit (*L); |
---|
81 | } |
---|
82 | // Regulators |
---|
83 | L->step(); |
---|
84 | } |
---|
85 | |
---|
86 | L->finalize(); |
---|
87 | |
---|
88 | // ------------------ End of routine ----------------------------- |
---|
89 | |
---|
90 | mex_logger* mL=dynamic_cast<mex_logger*>(L); |
---|
91 | |
---|
92 | if (mL) { // user wants output!! |
---|
93 | if ( n_output<1 ) mexErrMsgTxt ( "Wrong number of output variables!" ); |
---|
94 | output[0] = mL->toCell(); |
---|
95 | } |
---|
96 | |
---|
97 | |
---|
98 | delete L; |
---|
99 | delete DS; |
---|
100 | for (int i=0; i<nE; i++){ |
---|
101 | delete Dls(i); |
---|
102 | delete Es(i); |
---|
103 | } |
---|
104 | } |
---|