1 | #ifndef PMSMFIL_H |
---|
2 | #define PMSMFIL_H |
---|
3 | |
---|
4 | #include "pmsm.h" |
---|
5 | #include <estim/particles.h> |
---|
6 | |
---|
7 | /*! \defgroup PMSM |
---|
8 | @{ |
---|
9 | */ |
---|
10 | |
---|
11 | using namespace bdm; |
---|
12 | |
---|
13 | /* FAILURE :((( |
---|
14 | class MPFpmsm: public MPF { |
---|
15 | private: |
---|
16 | |
---|
17 | double Rs, Ls, dt, Ypm, kp, p, J, Mz; |
---|
18 | |
---|
19 | |
---|
20 | //! modelling theta as normal random walk with 2pi correction |
---|
21 | class rwtheta: public pdf_internal<enorm<fsqmat> > { |
---|
22 | double om_hat_dt; |
---|
23 | double om_var; |
---|
24 | double rth; |
---|
25 | MPFpmsm& p; |
---|
26 | public: |
---|
27 | rwtheta ( MPFpmsm& p0 ) :p ( p0 ) {}; |
---|
28 | void set_parameters ( double omh0, double omv0, double rth0 ) { |
---|
29 | om_hat_dt=omh0*p.dt; |
---|
30 | om_var=omv0; |
---|
31 | rth=rth0; |
---|
32 | |
---|
33 | mat R ( 1,1 ); |
---|
34 | R ( 0,0 ) =p.dt*p.dt*om_var+rth; |
---|
35 | vec mu ( 1 ); |
---|
36 | mu ( 0 ) = om_hat_dt; |
---|
37 | iepdf.set_parameters ( mu, fsqmat ( R ) ); |
---|
38 | } |
---|
39 | vec samplecond ( const vec &cond ) { |
---|
40 | iepdf._mu() =om_hat_dt+cond ( 0 ); |
---|
41 | |
---|
42 | // |
---|
43 | // iepdf._mu()= x[3];// ---------- |
---|
44 | vec th=iepdf.sample(); |
---|
45 | if ( th ( 0 ) >pi ) th ( 0 )-=2*pi; |
---|
46 | if ( th ( 0 ) <-pi ) th ( 0 ) +=2*pi; |
---|
47 | return th; |
---|
48 | } |
---|
49 | }; |
---|
50 | |
---|
51 | class PMSMlin: public KalmanFull { |
---|
52 | protected: |
---|
53 | MPFpmsm &p; |
---|
54 | double thm; |
---|
55 | public: |
---|
56 | PMSMlin ( MPFpmsm &p0 ) :p ( p0 ) {}; |
---|
57 | PMSMlin ( const PMSMlin &P0 ) : KalmanFull ( P0 ), p ( P0.p ), thm ( P0.thm ) {} |
---|
58 | |
---|
59 | PMSMlin* _copy() const { |
---|
60 | return new PMSMlin ( *this ); |
---|
61 | } |
---|
62 | |
---|
63 | void fillA() { |
---|
64 | //ia |
---|
65 | //using namespace p; |
---|
66 | A ( 0,0 ) = ( 1.0- p.Rs/p.Ls*p.dt ) ; |
---|
67 | A ( 0,2 ) = p.Ypm/p.Ls*p.dt* sin ( thm ); |
---|
68 | //ib |
---|
69 | A ( 1,1 ) = ( 1.0- p.Rs/p.Ls*p.dt ) ; |
---|
70 | A ( 1,2 ) = - p.Ypm/p.Ls*p.dt* cos ( thm ); |
---|
71 | //om |
---|
72 | A ( 2,0 ) = p.kp*p.p*p.p * p.Ypm/p.J*p.dt* ( -sin ( thm ) ); |
---|
73 | A ( 2,1 ) =p.kp*p.p*p.p * p.Ypm/p.J*p.dt* ( cos ( thm ) ); |
---|
74 | A ( 2,2 ) = 1.0; |
---|
75 | } |
---|
76 | |
---|
77 | void condition ( const vec &cond ) { |
---|
78 | thm = cond ( 0 ); |
---|
79 | fillA(); |
---|
80 | } |
---|
81 | }; |
---|
82 | |
---|
83 | shared_ptr<rwtheta> rwt; |
---|
84 | |
---|
85 | vec dQ; |
---|
86 | vec dR; |
---|
87 | public: |
---|
88 | void from_setting ( const Setting &set ) { |
---|
89 | BM::from_setting ( set ); //reads drv |
---|
90 | |
---|
91 | const SettingResolver& params_b=set["params"]; |
---|
92 | const Setting& params=params_b.result; |
---|
93 | |
---|
94 | Rs= params["Rs"]; |
---|
95 | Ls = params["Ls"]; |
---|
96 | dt = 125e-6; |
---|
97 | Ypm=params["Fmag"]; |
---|
98 | kp= params["kp"]; |
---|
99 | p= params["p"]; |
---|
100 | J = params["J"]; |
---|
101 | |
---|
102 | UI::get ( dQ, set, "dQ", UI::compulsory ); |
---|
103 | UI::get ( dR, set, "dR", UI::compulsory ); |
---|
104 | |
---|
105 | mat Q = diag ( dQ ( 0,2 ) ); |
---|
106 | mat R = diag ( dR ( 0,1 ) ); |
---|
107 | |
---|
108 | mat A=zeros ( 3,3 ); |
---|
109 | mat B=concat_vertical ( dt/Ls*eye ( 2 ), zeros ( 1,2 ) ); |
---|
110 | mat C=zeros ( 2,3 ); |
---|
111 | C ( 0,0 ) =1.0; |
---|
112 | C ( 1,1 ) =1.0; |
---|
113 | mat D= zeros ( 2,2 ); |
---|
114 | |
---|
115 | shared_ptr<PMSMlin> kal=new PMSMlin ( *this ); |
---|
116 | kal->set_parameters ( A,B,C,D,Q,R ); |
---|
117 | kal->set_statistics ( zeros ( 3 ), eye ( 3 ) ); |
---|
118 | kal->condition ( vec_1 ( 0.0 ) ); |
---|
119 | kal->validate(); |
---|
120 | |
---|
121 | pf = new PF; |
---|
122 | rwt=new rwtheta ( *this ); |
---|
123 | pf->prior_from_set ( set ); |
---|
124 | pf->resmethod_from_set ( set ); |
---|
125 | pf->set_model ( rwt,new pdf() ); |
---|
126 | |
---|
127 | shared_ptr<RV> rv = UI::build<RV> ( set, "rv", UI::optional ); |
---|
128 | if ( rv ) { |
---|
129 | set_rv ( *rv ); |
---|
130 | } |
---|
131 | |
---|
132 | if ( posterior()._rv()._dsize() >0 ) { |
---|
133 | kal->set_rv ( posterior()._rv().subselect ( "0,1,2" ) ); |
---|
134 | pf->set_rv ( posterior()._rv().subselect ( "3" ) ); |
---|
135 | } |
---|
136 | |
---|
137 | this->set_BM ( *kal ); |
---|
138 | |
---|
139 | UI::get ( log_level,set,UI::optional ); |
---|
140 | } |
---|
141 | void bayes ( const vec &yt, const vec &cond ) { |
---|
142 | const vec &mu = posterior().mean(); |
---|
143 | const vec &Var = posterior().variance(); |
---|
144 | rwt->set_parameters ( mu ( 3 ),Var ( 3 ),dQ ( 3 ) ); |
---|
145 | // COPY from MPF bayes: |
---|
146 | int i; |
---|
147 | int n=pf->__w().length(); |
---|
148 | vec &lls = pf->_lls(); |
---|
149 | |
---|
150 | // generate samples - time step |
---|
151 | for ( int i = 0; i < n; i++ ) { |
---|
152 | const vec &mu = BMs ( i )->posterior().mean(); |
---|
153 | const vec &Var = BMs ( i )->posterior().variance(); |
---|
154 | rwt->set_parameters ( mu ( 2 ),Var ( 2 ),dQ ( 3 ) ); |
---|
155 | |
---|
156 | Array<vec> &smp=pf->__samples(); |
---|
157 | smp ( i ) = rwt->samplecond ( smp ( i ) ); |
---|
158 | lls ( i ) = 0; |
---|
159 | } |
---|
160 | // weight them - data step |
---|
161 | #pragma parallel for |
---|
162 | for ( i = 0; i < n; i++ ) { |
---|
163 | BMs ( i ) -> bayes ( yt, concat(cond, pf->posterior()._sample ( i )) ); |
---|
164 | lls ( i ) += BMs ( i )->_ll(); |
---|
165 | } |
---|
166 | |
---|
167 | pf->bayes_weights(); |
---|
168 | |
---|
169 | ivec ind; |
---|
170 | if ( pf->do_resampling() ) { |
---|
171 | pf->resample ( ind ); |
---|
172 | |
---|
173 | #pragma omp parallel for |
---|
174 | for ( i = 0; i < n; i++ ) { |
---|
175 | if ( ind ( i ) != i ) {//replace the current Bm by a new one |
---|
176 | delete BMs ( i ); |
---|
177 | BMs ( i ) = BMs ( ind ( i ) )->_copy(); //copy constructor |
---|
178 | } |
---|
179 | }; |
---|
180 | } |
---|
181 | |
---|
182 | |
---|
183 | } |
---|
184 | }; |
---|
185 | |
---|
186 | UIREGISTER ( MPFpmsm ); |
---|
187 | |
---|
188 | */ |
---|
189 | |
---|
190 | /*!@}*/ |
---|
191 | #endif //PMSMFIL_H |
---|