1 | #ifndef PMSM_H |
---|
2 | #define PMSM_H |
---|
3 | |
---|
4 | #include <stat/functions.h> |
---|
5 | #include "user_info.h" |
---|
6 | |
---|
7 | /*! \defgroup PMSM |
---|
8 | @{ |
---|
9 | */ |
---|
10 | |
---|
11 | using namespace bdm; |
---|
12 | |
---|
13 | //TODO hardcoded RVs!!! |
---|
14 | RV rx ( "{ia ib om th }"); |
---|
15 | RV ru ( "{ua ub }"); |
---|
16 | RV ry ( "{oia oib }"); |
---|
17 | |
---|
18 | // class uipmsm : public uibase{ |
---|
19 | // double Rs, Ls, dt, Ypm, kp, p, J, Mz; |
---|
20 | // }; |
---|
21 | |
---|
22 | //! State evolution model for a PMSM drive and its derivative with respect to \f$x\f$ |
---|
23 | class IMpmsm : public diffbifn { |
---|
24 | protected: |
---|
25 | double Rs, Ls, dt, Ypm, kp, p, J, Mz; |
---|
26 | |
---|
27 | public: |
---|
28 | IMpmsm() :diffbifn ( ) {dimy=4; dimx = 4; dimu=2;}; |
---|
29 | //! Set mechanical and electrical variables |
---|
30 | virtual void set_parameters ( double Rs0, double Ls0, double dt0, double Ypm0, double kp0, double p0, double J0, double Mz0 ) {Rs=Rs0; Ls=Ls0; dt=dt0; Ypm=Ypm0; kp=kp0; p=p0; J=J0; Mz=Mz0;} |
---|
31 | |
---|
32 | void modelpwm(const vec &x0, const vec u0, double &ua, double &ub){ |
---|
33 | /* ua=u0[0]; |
---|
34 | ub=u0[1]; |
---|
35 | return;*/ |
---|
36 | double sq3=sqrt ( 3.0 ); |
---|
37 | double i1=x0(0); |
---|
38 | double i2=0.5* ( -i1+sq3*x0[1] ); |
---|
39 | double i3=0.5* ( -i1-sq3*x0[1] ); |
---|
40 | double u1=u0(0); |
---|
41 | double u2=0.5* ( -u1+sq3*u0(1) ); |
---|
42 | double u3=0.5* ( -u1-sq3*u0(1) ); |
---|
43 | |
---|
44 | double du1=1.4* ( double ( i1>0.3 ) - double ( i1<-0.3 ) ) +0.2*i1; |
---|
45 | double du2=1.4* ( double ( i2>0.3 ) - double ( i2<-0.3 ) ) +0.2*i2; |
---|
46 | double du3=1.4* ( double ( i3>0.3 ) - double ( i3<-0.3 ) ) +0.2*i3; |
---|
47 | ua = ( 2.0* ( u1-du1 )- ( u2-du2 )- ( u3-du3 ) ) /3.0; |
---|
48 | ub = ( ( u2-du2 )- ( u3-du3 ) ) /sq3; |
---|
49 | } |
---|
50 | |
---|
51 | vec eval ( const vec &x0, const vec &u0 ) { |
---|
52 | // last state |
---|
53 | const double &iam = x0 ( 0 ); |
---|
54 | const double &ibm = x0 ( 1 ); |
---|
55 | const double &omm = x0 ( 2 ); |
---|
56 | const double &thm = x0 ( 3 ); |
---|
57 | double uam; |
---|
58 | double ubm; |
---|
59 | |
---|
60 | modelpwm(x0,u0,uam,ubm); |
---|
61 | |
---|
62 | vec xk( 4 ); |
---|
63 | //ia |
---|
64 | xk ( 0 ) = ( 1.0- Rs/Ls*dt ) * iam + Ypm/Ls*dt*omm * sin ( thm ) + uam*dt/Ls; |
---|
65 | //ib |
---|
66 | xk ( 1 ) = ( 1.0- Rs/Ls*dt ) * ibm - Ypm/Ls*dt*omm * cos ( thm ) + ubm*dt/Ls; |
---|
67 | //om |
---|
68 | xk ( 2 ) = omm + kp*p*p * Ypm/J*dt* ( ibm * cos ( thm )-iam * sin ( thm ) ) - p/J*dt*Mz; |
---|
69 | //th |
---|
70 | xk ( 3 ) = thm + omm*dt; // <0..2pi> |
---|
71 | if ( xk ( 3 ) >pi ) xk ( 3 )-=2*pi; |
---|
72 | if ( xk ( 3 ) <-pi ) xk ( 3 ) +=2*pi; |
---|
73 | return xk; |
---|
74 | } |
---|
75 | |
---|
76 | void dfdx_cond ( const vec &x0, const vec &u0, mat &A, bool full=true ) { |
---|
77 | const double &iam = x0 ( 0 ); |
---|
78 | const double &ibm = x0 ( 1 ); |
---|
79 | const double &omm = x0 ( 2 ); |
---|
80 | const double &thm = x0 ( 3 ); |
---|
81 | // d ia |
---|
82 | A ( 0,0 ) = ( 1.0- Rs/Ls*dt ); A ( 0,1 ) = 0.0; |
---|
83 | A ( 0,2 ) = Ypm/Ls*dt* sin ( thm ); A ( 0,3 ) = Ypm/Ls*dt*omm * ( cos ( thm ) ); |
---|
84 | // d ib |
---|
85 | A ( 1,0 ) = 0.0 ; A ( 1,1 ) = ( 1.0- Rs/Ls*dt ); |
---|
86 | A ( 1,2 ) = -Ypm/Ls*dt* cos ( thm ); A ( 1,3 ) = Ypm/Ls*dt*omm * ( sin ( thm ) ); |
---|
87 | // d om |
---|
88 | A ( 2,0 ) = kp*p*p * Ypm/J*dt* ( - sin ( thm ) ); |
---|
89 | A ( 2,1 ) = kp*p*p * Ypm/J*dt* ( cos ( thm ) ); |
---|
90 | A ( 2,2 ) = 1.0; |
---|
91 | A ( 2,3 ) = kp*p*p * Ypm/J*dt* ( -ibm * sin ( thm )-iam * cos ( thm ) ); |
---|
92 | // d th |
---|
93 | A ( 3,0 ) = 0.0; A ( 3,1 ) = 0.0; A ( 3,2 ) = dt; A ( 3,3 ) = 1.0; |
---|
94 | } |
---|
95 | |
---|
96 | void dfdu_cond ( const vec &x0, const vec &u0, mat &A, bool full=true ) {it_error ( "not needed" );}; |
---|
97 | |
---|
98 | void from_setting( const Setting &root ) |
---|
99 | { |
---|
100 | UI::SettingResolver params_exp(root["params"]); |
---|
101 | const Setting& params=params_exp.result; |
---|
102 | |
---|
103 | set_parameters ( params["Rs"], params["Ls"], 125e-6, params["Fmag"], \ |
---|
104 | params["kp"], params["p"], params["J"], 0.0); |
---|
105 | |
---|
106 | /* set_parameters ( root["params"]["Rs"], root["params"]["Ls"], 125e-6, root["params"]["Fmag"], \ |
---|
107 | root["params"]["kp"], root["params"]["p"], root["params"]["J"], 0.0 );*/ |
---|
108 | }; |
---|
109 | |
---|
110 | // TODO dodelat void to_setting( Setting &root ) const; |
---|
111 | }; |
---|
112 | |
---|
113 | UIREGISTER ( IMpmsm ); |
---|
114 | |
---|
115 | //! State evolution model for a PMSM drive and its derivative with respect to \f$x\f$ |
---|
116 | class IMpmsm2o : public IMpmsm { |
---|
117 | protected: |
---|
118 | // double Rs, Ls, dt, Ypm, kp, p, J, Mz; |
---|
119 | //! store first derivatives for the use in second derivatives |
---|
120 | double dia, dib, dom, dth; |
---|
121 | //! d2t = dt^2/2, cth = cos(th), sth=sin(th) |
---|
122 | double d2t, cth, sth; |
---|
123 | double iam, ibm, omm, thm, uam, ubm; |
---|
124 | public: |
---|
125 | IMpmsm2o() :IMpmsm () {}; |
---|
126 | //! Set mechanical and electrical variables |
---|
127 | void set_parameters ( double Rs0, double Ls0, double dt0, double Ypm0, double kp0, double p0, double J0, double Mz0 ) {Rs=Rs0; Ls=Ls0; dt=dt0; Ypm=Ypm0; kp=kp0; p=p0; J=J0; Mz=Mz0; d2t=dt*dt/2;} |
---|
128 | |
---|
129 | vec eval ( const vec &x0, const vec &u0 ) { |
---|
130 | // last state |
---|
131 | iam = x0 ( 0 ); |
---|
132 | ibm = x0 ( 1 ); |
---|
133 | omm = x0 ( 2 ); |
---|
134 | thm = x0 ( 3 ); |
---|
135 | uam = u0 ( 0 ); |
---|
136 | ubm = u0 ( 1 ); |
---|
137 | |
---|
138 | cth = cos(thm); |
---|
139 | sth = sin(thm); |
---|
140 | |
---|
141 | dia = (- Rs/Ls*iam + Ypm/Ls*omm * sth + uam/Ls); |
---|
142 | dib = (- Rs/Ls*ibm - Ypm/Ls*omm * cth + ubm/Ls); |
---|
143 | dom = kp*p*p * Ypm/J *( ibm * cth-iam * sth ) - p/J*Mz; |
---|
144 | dth = omm; |
---|
145 | |
---|
146 | vec xk=zeros ( 4 ); |
---|
147 | xk ( 0 ) = iam + dt*dia;// +d2t*d2ia; |
---|
148 | xk ( 1 ) = ibm + dt*dib;// +d2t*d2ib; |
---|
149 | xk ( 2 ) = omm +dt*dom;// +d2t*d2om; |
---|
150 | xk ( 3 ) = thm + dt*dth;// +d2t*dom; // <0..2pi> |
---|
151 | |
---|
152 | if ( xk ( 3 ) >pi ) xk ( 3 )-=2*pi; |
---|
153 | if ( xk ( 3 ) <-pi ) xk ( 3 ) +=2*pi; |
---|
154 | return xk; |
---|
155 | } |
---|
156 | |
---|
157 | //! eval 2nd order Taylor expansion, MUST be used only as a follow up AFTER eval()!! |
---|
158 | vec eval2o(const vec &du){ |
---|
159 | double dua = du ( 0 )/dt; |
---|
160 | double dub = du ( 1 )/dt; |
---|
161 | |
---|
162 | vec xth2o(4); |
---|
163 | xth2o(0) = (- Rs/Ls*dia + Ypm/Ls*(dom * sth + omm*cth) + dua/Ls); |
---|
164 | xth2o(1) = (- Rs/Ls*dib - Ypm/Ls*(dom * cth - omm*sth) + dub/Ls); |
---|
165 | xth2o(2) = kp*p*p * Ypm/J *( dib * cth-ibm*sth - (dia * sth + iam *cth)); |
---|
166 | xth2o(3) = dom; |
---|
167 | // multiply by dt^2/2 |
---|
168 | xth2o*=d2t/2; |
---|
169 | return xth2o; |
---|
170 | } |
---|
171 | void dfdx_cond ( const vec &x0, const vec &u0, mat &A, bool full=true ) { |
---|
172 | iam = x0 ( 0 ); |
---|
173 | ibm = x0 ( 1 ); |
---|
174 | omm = x0 ( 2 ); |
---|
175 | thm = x0 ( 3 ); |
---|
176 | // d ia |
---|
177 | A ( 0,0 ) = ( 1.0- Rs/Ls*dt ); A ( 0,1 ) = 0.0; |
---|
178 | A ( 0,2 ) = Ypm/Ls*dt* sin ( thm ); A ( 0,3 ) = Ypm/Ls*dt*omm * ( cos ( thm ) ); |
---|
179 | // d ib |
---|
180 | A ( 1,0 ) = 0.0 ; A ( 1,1 ) = ( 1.0- Rs/Ls*dt ); |
---|
181 | A ( 1,2 ) = -Ypm/Ls*dt* cos ( thm ); A ( 1,3 ) = Ypm/Ls*dt*omm * ( sin ( thm ) ); |
---|
182 | // d om |
---|
183 | A ( 2,0 ) = kp*p*p * Ypm/J*dt* ( - sin ( thm ) ); |
---|
184 | A ( 2,1 ) = kp*p*p * Ypm/J*dt* ( cos ( thm ) ); |
---|
185 | A ( 2,2 ) = 1.0; |
---|
186 | A ( 2,3 ) = kp*p*p * Ypm/J*dt* ( -ibm * sin ( thm )-iam * cos ( thm ) ); |
---|
187 | // d th |
---|
188 | A ( 3,0 ) = 0.0; A ( 3,1 ) = 0.0; A ( 3,2 ) = dt; A ( 3,3 ) = 1.0; |
---|
189 | // FOR d2t*dom!!!!!!!!! |
---|
190 | /* A ( 3,0 ) = dt* kp*p*p * Ypm/J*dt* ( - sin ( thm ) ); |
---|
191 | A ( 3,1 ) = dt* kp*p*p * Ypm/J*dt* ( cos ( thm ) ); |
---|
192 | A ( 3,2 ) = dt; |
---|
193 | A ( 3,3 ) = 1.0 + dt* kp*p*p * Ypm/J*dt* ( -ibm * sin ( thm )-iam * cos ( thm ) );*/ |
---|
194 | } |
---|
195 | |
---|
196 | void dfdu_cond ( const vec &x0, const vec &u0, mat &A, bool full=true ) {it_error ( "not needed" );}; |
---|
197 | |
---|
198 | }; |
---|
199 | |
---|
200 | |
---|
201 | UIREGISTER ( IMpmsm2o ); |
---|
202 | |
---|
203 | //! State evolution model for a PMSM drive and its derivative with respect to \f$x\f$, equation for \f$\omega\f$ is omitted.$ |
---|
204 | class IMpmsmStat : public IMpmsm { |
---|
205 | public: |
---|
206 | IMpmsmStat() :IMpmsm() {}; |
---|
207 | //! Set mechanical and electrical variables |
---|
208 | void set_parameters ( double Rs0, double Ls0, double dt0, double Ypm0, double kp0, double p0, double J0, double Mz0 ) {Rs=Rs0; Ls=Ls0; dt=dt0; Ypm=Ypm0; kp=kp0; p=p0; J=J0; Mz=Mz0;} |
---|
209 | |
---|
210 | vec eval ( const vec &x0, const vec &u0 ) { |
---|
211 | // last state |
---|
212 | double iam = x0 ( 0 ); |
---|
213 | double ibm = x0 ( 1 ); |
---|
214 | double omm = x0 ( 2 ); |
---|
215 | double thm = x0 ( 3 ); |
---|
216 | double uam = u0 ( 0 ); |
---|
217 | double ubm = u0 ( 1 ); |
---|
218 | |
---|
219 | vec xk=zeros ( 4 ); |
---|
220 | //ia |
---|
221 | xk ( 0 ) = ( 1.0- Rs/Ls*dt ) * iam + Ypm/Ls*dt*omm * sin ( thm ) + uam*dt/Ls; |
---|
222 | //ib |
---|
223 | xk ( 1 ) = ( 1.0- Rs/Ls*dt ) * ibm - Ypm/Ls*dt*omm * cos ( thm ) + ubm*dt/Ls; |
---|
224 | //om |
---|
225 | xk ( 2 ) = omm - p/J*dt*Mz;// + kp*p*p * Ypm/J*dt* ( ibm * cos ( thm )-iam * sin ( thm ) ); |
---|
226 | //th |
---|
227 | xk ( 3 ) = rem(thm + omm*dt,2*pi); // <0..2pi> |
---|
228 | return xk; |
---|
229 | } |
---|
230 | |
---|
231 | void dfdx_cond ( const vec &x0, const vec &u0, mat &A, bool full=true ) { |
---|
232 | // double iam = x0 ( 0 ); |
---|
233 | // double ibm = x0 ( 1 ); |
---|
234 | double omm = x0 ( 2 ); |
---|
235 | double thm = x0 ( 3 ); |
---|
236 | // d ia |
---|
237 | A ( 0,0 ) = ( 1.0- Rs/Ls*dt ); A ( 0,1 ) = 0.0; |
---|
238 | A ( 0,2 ) = Ypm/Ls*dt* sin ( thm ); A ( 0,3 ) = Ypm/Ls*dt*omm * ( cos ( thm ) ); |
---|
239 | // d ib |
---|
240 | A ( 1,0 ) = 0.0 ; A ( 1,1 ) = ( 1.0- Rs/Ls*dt ); |
---|
241 | A ( 1,2 ) = -Ypm/Ls*dt* cos ( thm ); A ( 1,3 ) = Ypm/Ls*dt*omm * ( sin ( thm ) ); |
---|
242 | // d om |
---|
243 | A ( 2,0 ) = 0.0;//kp*p*p * Ypm/J*dt* ( - sin ( thm ) ); |
---|
244 | A ( 2,1 ) = 0.0;//kp*p*p * Ypm/J*dt* ( cos ( thm ) ); |
---|
245 | A ( 2,2 ) = 1.0; |
---|
246 | A ( 2,3 ) = 0.0;//kp*p*p * Ypm/J*dt* ( -ibm * sin ( thm )-iam * cos ( thm ) ); |
---|
247 | // d th |
---|
248 | A ( 3,0 ) = 0.0; A ( 3,1 ) = 0.0; A ( 3,2 ) = dt; A ( 3,3 ) = 1.0; |
---|
249 | } |
---|
250 | |
---|
251 | void dfdu_cond ( const vec &x0, const vec &u0, mat &A, bool full=true ) {it_error ( "not needed" );}; |
---|
252 | |
---|
253 | }; |
---|
254 | |
---|
255 | UIREGISTER ( IMpmsmStat ); |
---|
256 | |
---|
257 | |
---|
258 | //! State for PMSM with unknown Mz |
---|
259 | class IMpmsmMz: public IMpmsm{ |
---|
260 | public: |
---|
261 | IMpmsmMz() {dimy=5; dimx = 5; dimu=2;}; |
---|
262 | //! extend eval by Mz |
---|
263 | vec eval ( const vec &x0, const vec &u0 ) { |
---|
264 | vec x(4); |
---|
265 | Mz = x0(4); //last of the state is Mz |
---|
266 | |
---|
267 | //teh first 4 states are same as before (given that Mz is set) |
---|
268 | x=IMpmsm::eval(x0,u0); // including model of drops! |
---|
269 | return concat(x,Mz); |
---|
270 | } |
---|
271 | void dfdx_cond ( const vec &x0, const vec &u0, mat &A, bool full=true ) { |
---|
272 | //call initial |
---|
273 | if (full) A.clear(); |
---|
274 | IMpmsm::dfdx_cond(x0,u0,A,full); |
---|
275 | A(2,4)=- p/J*dt; |
---|
276 | A(4,4)=1.0; |
---|
277 | } |
---|
278 | }; |
---|
279 | |
---|
280 | UIREGISTER ( IMpmsmMz ); |
---|
281 | |
---|
282 | //! State for PMSM with unknown Mz |
---|
283 | class IMpmsmStatMz: public IMpmsmStat{ |
---|
284 | public: |
---|
285 | IMpmsmStatMz() {dimy=5; dimx = 5; dimu=2;}; |
---|
286 | //! extend eval by Mz |
---|
287 | vec eval ( const vec &x0, const vec &u0 ) { |
---|
288 | vec x(4); |
---|
289 | Mz = x0(4); //last of the state is Mz |
---|
290 | |
---|
291 | //teh first 4 states are same as before (given that Mz is set) |
---|
292 | x=IMpmsmStat::eval(x0,u0); // including model of drops! |
---|
293 | return concat(x,Mz); |
---|
294 | } |
---|
295 | void dfdx_cond ( const vec &x0, const vec &u0, mat &A, bool full=true ) { |
---|
296 | //call initial |
---|
297 | if (full) A.clear(); |
---|
298 | IMpmsmStat::dfdx_cond(x0,u0,A,full); |
---|
299 | A(2,4)=- p/J*dt; |
---|
300 | A(4,4)=1.0; |
---|
301 | } |
---|
302 | }; |
---|
303 | |
---|
304 | UIREGISTER ( IMpmsmStatMz ); |
---|
305 | |
---|
306 | |
---|
307 | //! Observation model for PMSM drive and its derivative with respect to \f$x\f$ |
---|
308 | class OMpmsm: public diffbifn { |
---|
309 | public: |
---|
310 | OMpmsm() :diffbifn () {dimy=2;dimx=4;dimu=2;}; |
---|
311 | |
---|
312 | vec eval ( const vec &x0, const vec &u0 ) { |
---|
313 | vec y ( 2 ); |
---|
314 | y ( 0 ) = x0 ( 0 ); |
---|
315 | y ( 1 ) = x0 ( 1 ); |
---|
316 | return y; |
---|
317 | } |
---|
318 | |
---|
319 | void dfdx_cond ( const vec &x0, const vec &u0, mat &A, bool full=true ) { |
---|
320 | A.clear(); |
---|
321 | A ( 0,0 ) = 1.0; |
---|
322 | A ( 1,1 ) = 1.0; |
---|
323 | } |
---|
324 | }; |
---|
325 | |
---|
326 | UIREGISTER ( OMpmsm ); |
---|
327 | |
---|
328 | //! Observation model for PMSM drive and its derivative with respect to \f$x\f$ for full vector of observations |
---|
329 | class OMpmsm4: public diffbifn { |
---|
330 | public: |
---|
331 | OMpmsm4() :diffbifn () {dimy=4;dimx=4;dimu=2;}; |
---|
332 | |
---|
333 | vec eval ( const vec &x0, const vec &u0 ) { |
---|
334 | vec y ( 4 ); |
---|
335 | y = x0 ; |
---|
336 | return y; |
---|
337 | } |
---|
338 | |
---|
339 | void dfdx_cond ( const vec &x0, const vec &u0, mat &A, bool full=true ) { |
---|
340 | if (full) A=eye(4); |
---|
341 | } |
---|
342 | }; |
---|
343 | |
---|
344 | UIREGISTER ( OMpmsm4 ); |
---|
345 | |
---|
346 | |
---|
347 | |
---|
348 | |
---|
349 | |
---|
350 | /*!@}*/ |
---|
351 | #endif //PMSM_H |
---|