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