1 | #ifndef PMSM_CTR_H |
---|
2 | #define PMSM_CTR_H |
---|
3 | |
---|
4 | #include <estim/particles.h> |
---|
5 | #include <design/ctrlbase.h> |
---|
6 | #include <design/lq_ctrl.h> |
---|
7 | #include "pmsm.h" |
---|
8 | |
---|
9 | #include <fstream> |
---|
10 | |
---|
11 | /*! \defgroup PMSM |
---|
12 | @{ |
---|
13 | */ |
---|
14 | |
---|
15 | using namespace bdm; |
---|
16 | |
---|
17 | |
---|
18 | /*! PI Controller*/ |
---|
19 | class PI_ctrl: public Controller{ |
---|
20 | public: |
---|
21 | //!integral state |
---|
22 | double S; |
---|
23 | double Pd; |
---|
24 | double Pi; |
---|
25 | double minOut; |
---|
26 | double maxOut; |
---|
27 | |
---|
28 | PI_ctrl(double Pd0, double Pi0, double minOut0, double maxOut0): S(0.0), Pd(Pd0), Pi(Pi0),minOut(minOut0),maxOut(maxOut0){} |
---|
29 | virtual vec ctrlaction ( const vec &cond ) { |
---|
30 | double tmp; |
---|
31 | tmp = Pd*cond(0)+S; |
---|
32 | |
---|
33 | S += Pi*cond(0); |
---|
34 | |
---|
35 | if (tmp>maxOut) tmp=maxOut; |
---|
36 | if (tmp<minOut) tmp=minOut; |
---|
37 | return vec_1(tmp); |
---|
38 | } |
---|
39 | }; |
---|
40 | |
---|
41 | /*! \brief Root class for controllers of PMSM |
---|
42 | * |
---|
43 | * It defines the interface for controllers that connect to the pmsmDSctrl. |
---|
44 | * The adapt() function calls bayes() of an internal BM. Note that it is assumed that the estimator is compatible with PMSM system. |
---|
45 | * I.e. it must have rv={ia, ib} and rvc={ua,ub}, any other BM will not work. |
---|
46 | * |
---|
47 | * Two cases are considered: |
---|
48 | * - 4 dimensional rv={ia, ib, om, th} and rvc={ua,ub} |
---|
49 | * - 2 dimensional rv={om, th} and rvc={ua,ub,ia_{t-1}, ib_{t-1} } |
---|
50 | * These are hardcoded and sitched based on dimensionality. |
---|
51 | */ |
---|
52 | class PMSMCtrl: public Controller{ |
---|
53 | protected: |
---|
54 | //! Est where rv and rcv are not used! |
---|
55 | shared_ptr<BM> Est; |
---|
56 | //! switch to use or not to use the internal estimator |
---|
57 | bool estim; |
---|
58 | |
---|
59 | // internal quantities filled by PMSMCtrl::ctrlaction() |
---|
60 | double isa; |
---|
61 | double isb; |
---|
62 | double ome; |
---|
63 | double the; |
---|
64 | double Ww; |
---|
65 | |
---|
66 | public: |
---|
67 | |
---|
68 | PMSMCtrl():Controller() { |
---|
69 | rv = RV("{ua ub }"); |
---|
70 | rvc = RV("{o_ia o_ib t_ua t_ub o_om o_th Ww }"); |
---|
71 | } |
---|
72 | |
---|
73 | void adapt(const itpp::vec& data){ |
---|
74 | if (estim){ |
---|
75 | vec y=data.get(0,1); |
---|
76 | vec u=data.get(2,3); |
---|
77 | if (Est->dimension()==2){ |
---|
78 | static vec cond(4); // |
---|
79 | cond.set_subvector(0,1,u); |
---|
80 | Est->bayes(y,cond); |
---|
81 | cond.set_subvector(2,3,y); // save 1-step delayed vectors |
---|
82 | |
---|
83 | } else { |
---|
84 | Est->bayes(y,u); |
---|
85 | |
---|
86 | } |
---|
87 | |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | |
---|
92 | virtual vec ctrlaction(const itpp::vec& cond) { |
---|
93 | |
---|
94 | if (estim){ |
---|
95 | if (Est->dimension()==2){ |
---|
96 | vec x_est=Est->posterior().mean(); |
---|
97 | isa=cond(0); |
---|
98 | isb=cond(1); |
---|
99 | ome=x_est(0); |
---|
100 | the=x_est(1); |
---|
101 | } else { |
---|
102 | vec x_est=Est->posterior().mean(); |
---|
103 | isa=x_est(0); |
---|
104 | isb=x_est(1); |
---|
105 | ome=x_est(2); |
---|
106 | the=x_est(3); |
---|
107 | } |
---|
108 | } else { |
---|
109 | isa=cond(0);//x_est(0); |
---|
110 | isb=cond(1);//x_est(1); |
---|
111 | ome=cond(4);//x[2];//x_est(2); |
---|
112 | the=cond(5);//x_est(3); |
---|
113 | } |
---|
114 | Ww=cond(6); |
---|
115 | |
---|
116 | return empty_vec; // dummy return |
---|
117 | }; |
---|
118 | void from_setting(const libconfig::Setting& set){ |
---|
119 | Controller::from_setting(set); |
---|
120 | Est=UI::build<BM>(set,"estim",UI::optional); |
---|
121 | estim = Est; |
---|
122 | } |
---|
123 | void log_register ( logger &L, const string &prefix ) { |
---|
124 | Controller::log_register(L,prefix); |
---|
125 | if (estim) Est->log_register(L,prefix); |
---|
126 | } |
---|
127 | void log_write() const{ |
---|
128 | if (estim) Est->log_write(); |
---|
129 | } |
---|
130 | }; |
---|
131 | //UIREGISTER(PMSMCtrl); -- abstract |
---|
132 | |
---|
133 | class PMSM_PICtrl: public PMSMCtrl{ |
---|
134 | public: |
---|
135 | PI_ctrl Cwq; |
---|
136 | PI_ctrl Cuq; |
---|
137 | PI_ctrl Cud; |
---|
138 | |
---|
139 | PMSM_PICtrl():PMSMCtrl(), |
---|
140 | Cwq(3.0, 3.0*0.000125/0.1, -1200, 1200), |
---|
141 | Cuq(20.0, 20.0*0.000125/0.005, -1200, 1200), |
---|
142 | Cud(20.0, 20.0*0.000125/0.005, -1200, 1200) {} |
---|
143 | |
---|
144 | |
---|
145 | virtual vec ctrlaction(const itpp::vec& cond) { |
---|
146 | PMSMCtrl::ctrlaction(cond); // fills isa,isb,om,the |
---|
147 | |
---|
148 | double Isd = isa*cos(the)+isb*sin(the); |
---|
149 | double Isq = isb*cos(the)-isa*sin(the); |
---|
150 | |
---|
151 | double Iqw=(Cwq.ctrlaction(vec_1(Ww-ome)))(0); // conversion from vec |
---|
152 | |
---|
153 | double ud = (Cud.ctrlaction(vec_1(-Isd)))(0); |
---|
154 | double uq = (Cuq.ctrlaction(vec_1(Iqw-Isq)))(0); |
---|
155 | |
---|
156 | const double Ls0=0.003465; // inductance |
---|
157 | const double Fmag0= 0.1989; // Magnetic?? |
---|
158 | |
---|
159 | ud-=Ls0*ome*Iqw; // har |
---|
160 | uq+=Fmag0*ome; |
---|
161 | |
---|
162 | double Um = sqrt(ud*ud+uq*uq); |
---|
163 | double beta = atan(uq/ud); |
---|
164 | if (ud<0) beta += M_PI; |
---|
165 | beta +=the; |
---|
166 | |
---|
167 | vec uab(2); |
---|
168 | uab(0)=Um*cos(beta); // usx = usa |
---|
169 | uab(1)=Um*sin(beta); // usy |
---|
170 | |
---|
171 | return uab; |
---|
172 | }; |
---|
173 | }; |
---|
174 | UIREGISTER(PMSM_PICtrl); |
---|
175 | |
---|
176 | /*************************************/ |
---|
177 | /* LQ alpa-beta */ |
---|
178 | /*************************************/ |
---|
179 | |
---|
180 | class PMSM_LQCtrl: public PMSMCtrl{ |
---|
181 | public: |
---|
182 | /* |
---|
183 | PMSMCtrl: |
---|
184 | double isa; |
---|
185 | double isb; |
---|
186 | double ome; |
---|
187 | double the; |
---|
188 | double Ww; |
---|
189 | */ |
---|
190 | |
---|
191 | //PMSM parameters |
---|
192 | const double a; |
---|
193 | const double b; |
---|
194 | const double c; |
---|
195 | const double d; |
---|
196 | const double e; |
---|
197 | |
---|
198 | //input penalty |
---|
199 | double r; |
---|
200 | |
---|
201 | //time step |
---|
202 | const double Dt; |
---|
203 | |
---|
204 | //receding horizon |
---|
205 | int rec_hor; |
---|
206 | |
---|
207 | //system matrices |
---|
208 | mat A; //5x5 |
---|
209 | mat B; //5x2 |
---|
210 | //mat C; //2x5 |
---|
211 | mat Q; //5x5 |
---|
212 | mat R; //2x2 |
---|
213 | |
---|
214 | //control matrix |
---|
215 | mat L; |
---|
216 | vec uab; |
---|
217 | vec icond; |
---|
218 | |
---|
219 | //control maximum |
---|
220 | double MAXu; |
---|
221 | int MAXuflag; |
---|
222 | |
---|
223 | //lqg controler class |
---|
224 | LQG_universal lq; |
---|
225 | |
---|
226 | //prediction |
---|
227 | vec p_isa, p_isb, p_ome, p_the; |
---|
228 | |
---|
229 | PMSM_LQCtrl():PMSMCtrl(), a(0.9898), b(0.0072), c(0.0361), d(1.0), e(0.0149), |
---|
230 | r(0.005), Dt(0.000125), rec_hor(10), //for r is a default value rewrited by pcp.txt file value |
---|
231 | A(5, 5), B(5, 2), Q(5, 5), R(2, 2), |
---|
232 | uab(2), icond(6), MAXu(100.0), MAXuflag(0) { |
---|
233 | //set fix matrices elements & dimensions |
---|
234 | A.zeros(); |
---|
235 | A(0, 0) = A(1, 1) = a; |
---|
236 | A(2, 2) = d; |
---|
237 | A(2, 4) = d - 1; |
---|
238 | A(3, 2) = A(3, 4) = Dt; |
---|
239 | A(3, 3) = A(4, 4) = 1.0; |
---|
240 | B.zeros(); |
---|
241 | B(0, 0) = B(1, 1) = c; |
---|
242 | //C.zeros(); |
---|
243 | // C(0, 0) = C(1, 1) = 1.0; |
---|
244 | Q.zeros(); |
---|
245 | Q(2, 2) = 1.0; |
---|
246 | R.zeros(); |
---|
247 | |
---|
248 | } |
---|
249 | |
---|
250 | |
---|
251 | virtual vec ctrlaction(const itpp::vec& cond) { |
---|
252 | PMSMCtrl::ctrlaction(cond); // fills isa,isb,ome,the,Ww |
---|
253 | |
---|
254 | int i; |
---|
255 | lq.resetTime(); |
---|
256 | |
---|
257 | //create prediction |
---|
258 | p_isa.zeros(); |
---|
259 | p_isb.zeros(); |
---|
260 | p_ome.zeros(); |
---|
261 | p_the.zeros(); |
---|
262 | p_isa(0) = isa; |
---|
263 | p_isb(0) = isb; |
---|
264 | p_ome(0) = ome; |
---|
265 | p_the(0) = the; |
---|
266 | |
---|
267 | for(i = 0; i < rec_hor-1; i++){ |
---|
268 | p_isa(i+1) = a*p_isa(i) + b*p_ome(i)*sin(p_the(i)) + c*0;//uab(0); //use last used control |
---|
269 | p_isb(i+1) = a*p_isb(i) - b*p_ome(i)*cos(p_the(i)) + c*0;//uab(1); |
---|
270 | p_ome(i+1) = d*p_ome(i) + e*(p_isb(i)*cos(p_the(i)) - p_isa(i)*sin(p_the(i))); |
---|
271 | p_the(i+1) = p_the(i) + Dt*p_ome(i); |
---|
272 | } |
---|
273 | |
---|
274 | //create control matrix |
---|
275 | for(i = rec_hor; i > 0; i--){ |
---|
276 | //set variable matrices elements (A matrix only) |
---|
277 | A(0, 2) = b*sin(p_the(i)); |
---|
278 | A(0, 3) = b*(p_ome(i))*cos(p_the(i)); |
---|
279 | A(0, 4) = b*sin(p_the(i)); |
---|
280 | A(1, 2) = -b*cos(p_the(i)); |
---|
281 | A(1, 3) = b*(p_ome(i))*sin(p_the(i)); |
---|
282 | A(1, 4) = -b*cos(p_the(i)); |
---|
283 | A(2, 0) = -e*sin(p_the(i)); |
---|
284 | A(2, 1) = e*cos(p_the(i)); |
---|
285 | A(2, 3) = -e*(p_isa(i)*cos(p_the(i)) + p_isb(i)*sin(p_the(i))); |
---|
286 | |
---|
287 | lq.Models(0).A = A; |
---|
288 | lq.redesign(); |
---|
289 | } |
---|
290 | lq.redesign(); |
---|
291 | |
---|
292 | L = lq.getL(); |
---|
293 | icond(0) = isa; |
---|
294 | icond(1) = isb; |
---|
295 | icond(2) = ome - Ww; |
---|
296 | icond(3) = the; |
---|
297 | icond(4) = Ww; |
---|
298 | icond(5) = 0; |
---|
299 | vec tmp = L*icond; |
---|
300 | |
---|
301 | uab = tmp(0,1); |
---|
302 | |
---|
303 | if(MAXuflag == 1){ //square cut off |
---|
304 | if(uab(0) > MAXu) uab(0) = MAXu; |
---|
305 | else if(uab(0) < -MAXu) uab(0) = -MAXu; |
---|
306 | if(uab(1) > MAXu) uab(1) = MAXu; |
---|
307 | else if(uab(1) < -MAXu) uab(1) = -MAXu; |
---|
308 | } |
---|
309 | else if(MAXuflag == 2){ //circular cut off |
---|
310 | double uampl = sqrt(uab(0)*uab(0)+uab(1)*uab(1)); |
---|
311 | double uangl = atan2(uab(1), uab(0)); |
---|
312 | if (uampl > MAXu) uampl = MAXu; |
---|
313 | uab(0) = uampl*cos(uangl); |
---|
314 | uab(1) = uampl*sin(uangl); |
---|
315 | } |
---|
316 | //else{ //(MAXuflag == 0) //no cut off } |
---|
317 | |
---|
318 | return uab; |
---|
319 | } |
---|
320 | |
---|
321 | void from_setting(const Setting &set){ |
---|
322 | PMSMCtrl::from_setting(set); |
---|
323 | UI::get(r,set, "r", UI::optional); |
---|
324 | UI::get(rec_hor,set, "h", UI::optional); |
---|
325 | UI::get(MAXu,set, "MAXu", UI::optional); |
---|
326 | UI::get(MAXuflag,set, "MAXuflag", UI::optional); |
---|
327 | } |
---|
328 | |
---|
329 | void validate(){ |
---|
330 | R(0,0)=R(1,1)=r; |
---|
331 | |
---|
332 | p_isa.set_length(rec_hor+1); |
---|
333 | p_isb.set_length(rec_hor+1); |
---|
334 | p_ome.set_length(rec_hor+1); |
---|
335 | p_the.set_length(rec_hor+1); |
---|
336 | |
---|
337 | Array<quadraticfn> qloss(2); |
---|
338 | qloss(0).Q.setCh(Q); |
---|
339 | qloss(0).rv = RV("x", 5, 1); |
---|
340 | qloss(1).Q.setCh(R); |
---|
341 | qloss(1).rv = RV("u", 2, 0); |
---|
342 | lq.Losses = qloss; |
---|
343 | |
---|
344 | //set lq |
---|
345 | lq.rv = RV("u", 2, 0); |
---|
346 | lq.set_rvc(RV("x", 5, 0)); |
---|
347 | lq.horizon = rec_hor; |
---|
348 | |
---|
349 | Array<linfnEx> model(2); |
---|
350 | model(0).A = A; |
---|
351 | model(0).B = vec("0 0 0 0 0"); |
---|
352 | model(0).rv = RV("x", 5, 0); |
---|
353 | model(0).rv_ret = RV("x", 5, 1); |
---|
354 | model(1).A = B; |
---|
355 | model(1).B = vec("0 0"); |
---|
356 | model(1).rv = RV("u", 2, 0); |
---|
357 | model(1).rv_ret = RV("x", 5, 1); |
---|
358 | lq.Models = model; |
---|
359 | |
---|
360 | lq.finalLoss.Q.setCh(Q); |
---|
361 | lq.finalLoss.rv = RV("x", 5, 1); |
---|
362 | |
---|
363 | lq.validate(); |
---|
364 | |
---|
365 | uab.zeros(); |
---|
366 | |
---|
367 | } |
---|
368 | }; |
---|
369 | UIREGISTER(PMSM_LQCtrl); |
---|
370 | |
---|
371 | /*************************************/ |
---|
372 | /* LQ d-q 1 */ |
---|
373 | /*************************************/ |
---|
374 | |
---|
375 | class PMSM_LQCtrl_dq: public PMSMCtrl{ |
---|
376 | public: |
---|
377 | /* |
---|
378 | PMSMCtrl: |
---|
379 | double isa; |
---|
380 | double isb; |
---|
381 | double ome; |
---|
382 | double the; |
---|
383 | double Ww; |
---|
384 | */ |
---|
385 | |
---|
386 | //PMSM parameters |
---|
387 | const double a; |
---|
388 | const double b; |
---|
389 | const double c; |
---|
390 | const double d; |
---|
391 | const double e; |
---|
392 | |
---|
393 | //input penalty |
---|
394 | double r; |
---|
395 | double rpd; //penalize differences u - u_old |
---|
396 | |
---|
397 | //time step |
---|
398 | const double Dt; |
---|
399 | |
---|
400 | //receding horizon |
---|
401 | int rec_hor; |
---|
402 | |
---|
403 | //system matrices |
---|
404 | mat A; //5x5 |
---|
405 | mat B; //5x2 |
---|
406 | //mat C; //2x5 |
---|
407 | mat Q; //5x5 |
---|
408 | mat R; //2x2 |
---|
409 | mat Rpd; //2x4 |
---|
410 | |
---|
411 | //control matrix |
---|
412 | mat L; |
---|
413 | vec uab; |
---|
414 | vec icondpd; |
---|
415 | vec u_old; |
---|
416 | |
---|
417 | //control maximum |
---|
418 | double MAXu; |
---|
419 | int MAXuflag; |
---|
420 | |
---|
421 | //lqg controler class |
---|
422 | LQG_universal lq; |
---|
423 | |
---|
424 | //prediction |
---|
425 | vec p_isa, p_isb, p_ome, p_the; |
---|
426 | |
---|
427 | PMSM_LQCtrl_dq():PMSMCtrl(), a(0.9898), b(0.0072), c(0.0361), d(1.0), e(0.0149), |
---|
428 | r(0.005), rpd(0.1), Dt(0.000125), rec_hor(10), //for r is a default value rewrited by pcp.txt file value |
---|
429 | A(5, 5), B(5, 2), Q(5, 5), R(2, 2), Rpd(2, 4), |
---|
430 | uab(2), u_old(2), icondpd(8), MAXu(100.0), MAXuflag(0) { |
---|
431 | //set fix matrices elements & dimensions |
---|
432 | A.zeros(); |
---|
433 | A(0, 0) = A(1, 1) = a; |
---|
434 | A(1, 2) = A(1, 4) = -b; |
---|
435 | A(2, 1) = e; |
---|
436 | A(2, 2) = d; |
---|
437 | A(2, 4) = d - 1; |
---|
438 | A(3, 2) = A(3, 4) = Dt; |
---|
439 | A(3, 3) = A(4, 4) = 1.0; |
---|
440 | B.zeros(); |
---|
441 | B(0, 0) = B(1, 1) = c; |
---|
442 | Q.zeros(); |
---|
443 | Q(2, 2) = 1.0; |
---|
444 | R.zeros(); |
---|
445 | Rpd.zeros(); |
---|
446 | u_old.zeros(); |
---|
447 | } |
---|
448 | |
---|
449 | |
---|
450 | virtual vec ctrlaction(const itpp::vec& cond) { |
---|
451 | PMSMCtrl::ctrlaction(cond); // fills isa,isb,ome,the,Ww |
---|
452 | |
---|
453 | vec udq; |
---|
454 | vec tmp; |
---|
455 | |
---|
456 | lq.resetTime(); |
---|
457 | |
---|
458 | L = lq.getL(); |
---|
459 | |
---|
460 | icondpd(0) = isa*cos(the) + isb*sin(the); |
---|
461 | icondpd(1) = isb*cos(the) - isa*sin(the); |
---|
462 | icondpd(2) = ome - Ww; |
---|
463 | icondpd(3) = the; |
---|
464 | icondpd(4) = Ww; |
---|
465 | icondpd(5) = u_old(0); |
---|
466 | icondpd(6) = u_old(1); |
---|
467 | icondpd(7) = 0; |
---|
468 | |
---|
469 | tmp = L*icondpd; |
---|
470 | |
---|
471 | udq = tmp(0,1); |
---|
472 | |
---|
473 | //uab = udq; //set size |
---|
474 | uab(0) = udq(0)*cos(the) - udq(1)*sin(the); |
---|
475 | uab(1) = udq(1)*cos(the) + udq(0)*sin(the); |
---|
476 | |
---|
477 | if(MAXuflag == 1){ //square cut off |
---|
478 | if(uab(0) > MAXu) uab(0) = MAXu; |
---|
479 | else if(uab(0) < -MAXu) uab(0) = -MAXu; |
---|
480 | if(uab(1) > MAXu) uab(1) = MAXu; |
---|
481 | else if(uab(1) < -MAXu) uab(1) = -MAXu; |
---|
482 | } |
---|
483 | else if(MAXuflag == 2){ //circular cut off |
---|
484 | double uampl = sqrt(uab(0)*uab(0)+uab(1)*uab(1)); |
---|
485 | double uangl = atan2(uab(1), uab(0)); |
---|
486 | if (uampl > MAXu) uampl = MAXu; |
---|
487 | uab(0) = uampl*cos(uangl); |
---|
488 | uab(1) = uampl*sin(uangl); |
---|
489 | } |
---|
490 | //else{ //(MAXuflag == 0) //no cut off } |
---|
491 | |
---|
492 | u_old = udq; |
---|
493 | |
---|
494 | return uab; |
---|
495 | } |
---|
496 | |
---|
497 | void from_setting(const Setting &set){ |
---|
498 | PMSMCtrl::from_setting(set); |
---|
499 | UI::get(r,set, "r", UI::optional); |
---|
500 | UI::get(rec_hor,set, "h", UI::optional); |
---|
501 | UI::get(MAXu,set, "MAXu", UI::optional); |
---|
502 | UI::get(MAXuflag,set, "MAXuflag", UI::optional); |
---|
503 | UI::get(rpd,set, "rpd", UI::optional); |
---|
504 | } |
---|
505 | |
---|
506 | void validate(){ |
---|
507 | R(0, 0) = R(1, 1) = r; |
---|
508 | Rpd(0, 0) = Rpd(1, 1) = rpd; |
---|
509 | Rpd(0, 2) = Rpd(1, 3) = -rpd; |
---|
510 | |
---|
511 | p_isa.set_length(rec_hor+1); |
---|
512 | p_isb.set_length(rec_hor+1); |
---|
513 | p_ome.set_length(rec_hor+1); |
---|
514 | p_the.set_length(rec_hor+1); |
---|
515 | |
---|
516 | Array<quadraticfn> qloss(3); |
---|
517 | qloss(0).Q.setCh(Q); |
---|
518 | qloss(0).rv = RV("x", 5, 1); |
---|
519 | qloss(1).Q.setCh(R); |
---|
520 | qloss(1).rv = RV("u", 2, 0); |
---|
521 | qloss(2).Q.setCh(Rpd); |
---|
522 | qloss(2).rv = RV("u", 2, 0); |
---|
523 | qloss(2).rv.add(RV("u", 2, -1)); |
---|
524 | lq.Losses = qloss; |
---|
525 | |
---|
526 | //set lq |
---|
527 | lq.rv = RV("u", 2, 0); |
---|
528 | RV tmp = RV("x", 5, 0); |
---|
529 | tmp.add(RV("u", 2, -1)); |
---|
530 | lq.set_rvc(tmp); |
---|
531 | lq.horizon = rec_hor; |
---|
532 | |
---|
533 | Array<linfnEx> model(2); |
---|
534 | model(0).A = A; |
---|
535 | model(0).B = vec("0 0 0 0 0"); |
---|
536 | model(0).rv = RV("x", 5, 0); |
---|
537 | model(0).rv_ret = RV("x", 5, 1); |
---|
538 | model(1).A = B; |
---|
539 | model(1).B = vec("0 0"); |
---|
540 | model(1).rv = RV("u", 2, 0); |
---|
541 | model(1).rv_ret = RV("x", 5, 1); |
---|
542 | lq.Models = model; |
---|
543 | |
---|
544 | lq.finalLoss.Q.setCh(Q); |
---|
545 | lq.finalLoss.rv = RV("x", 5, 1); |
---|
546 | |
---|
547 | lq.validate(); |
---|
548 | |
---|
549 | uab.zeros(); |
---|
550 | |
---|
551 | //create control matrix |
---|
552 | for(int i = rec_hor; i > 0; i--){ |
---|
553 | lq.redesign(); |
---|
554 | } |
---|
555 | lq.redesign(); |
---|
556 | } |
---|
557 | }; |
---|
558 | UIREGISTER(PMSM_LQCtrl_dq); |
---|
559 | |
---|
560 | /*************************************/ |
---|
561 | /* LQ d-q 2 */ |
---|
562 | /*************************************/ |
---|
563 | |
---|
564 | class PMSM_LQCtrl_dq2: public PMSMCtrl{ |
---|
565 | public: |
---|
566 | /* |
---|
567 | PMSMCtrl: |
---|
568 | double isa; |
---|
569 | double isb; |
---|
570 | double ome; |
---|
571 | double the; |
---|
572 | double Ww; |
---|
573 | */ |
---|
574 | |
---|
575 | //PMSM parameters |
---|
576 | const double a; |
---|
577 | const double b; |
---|
578 | const double c; |
---|
579 | const double d; |
---|
580 | const double e; |
---|
581 | |
---|
582 | //input penalty |
---|
583 | double r; |
---|
584 | double rpd; //penalize differences u - u_old |
---|
585 | |
---|
586 | //time step |
---|
587 | const double Dt; |
---|
588 | |
---|
589 | //receding horizon |
---|
590 | int rec_hor; |
---|
591 | |
---|
592 | //system matrices |
---|
593 | mat A; //5x5 |
---|
594 | mat B; //5x2 |
---|
595 | //mat C; //2x5 |
---|
596 | mat Q; //5x5 |
---|
597 | mat R; //2x2 |
---|
598 | mat Rpd; //2x4 |
---|
599 | |
---|
600 | //control matrix |
---|
601 | mat L; |
---|
602 | vec uab,udq; |
---|
603 | vec icondpd; |
---|
604 | vec u_old; |
---|
605 | |
---|
606 | //control maximum |
---|
607 | double MAXu; |
---|
608 | int MAXuflag; |
---|
609 | |
---|
610 | //lqg controler class |
---|
611 | LQG_universal lq; |
---|
612 | |
---|
613 | //prediction |
---|
614 | vec p_isd, p_isq, p_ome, p_the; |
---|
615 | |
---|
616 | PMSM_LQCtrl_dq2():PMSMCtrl(), a(0.9898), b(0.0072), c(0.0361), d(1.0), e(0.0149), |
---|
617 | r(0.005), rpd(0.1), Dt(0.000125), rec_hor(10), //for r is a default value rewrited by pcp.txt file value |
---|
618 | A(5, 5), B(5, 2), Q(5, 5), R(2, 2), Rpd(2, 4), |
---|
619 | uab(2), udq(2), u_old(2), icondpd(8), MAXu(100.0), MAXuflag(0) { |
---|
620 | //set fix matrices elements & dimensions |
---|
621 | A.zeros(); |
---|
622 | A(0, 0) = A(1, 1) = a; |
---|
623 | A(2, 1) = e; |
---|
624 | A(2, 2) = d; |
---|
625 | A(2, 4) = d - 1; |
---|
626 | A(3, 2) = A(3, 4) = Dt; |
---|
627 | A(3, 3) = A(4, 4) = 1.0; |
---|
628 | B.zeros(); |
---|
629 | B(0, 0) = B(1, 1) = c; |
---|
630 | Q.zeros(); |
---|
631 | Q(2, 2) = 1.0; |
---|
632 | R.zeros(); |
---|
633 | Rpd.zeros(); |
---|
634 | u_old.zeros(); |
---|
635 | } |
---|
636 | |
---|
637 | |
---|
638 | virtual vec ctrlaction(const itpp::vec& cond) { |
---|
639 | PMSMCtrl::ctrlaction(cond); // fills isa,isb,ome,the,Ww |
---|
640 | |
---|
641 | int i; |
---|
642 | vec tmp; |
---|
643 | |
---|
644 | lq.resetTime(); |
---|
645 | |
---|
646 | //create prediction |
---|
647 | p_isd.zeros(); |
---|
648 | p_isq.zeros(); |
---|
649 | p_ome.zeros(); |
---|
650 | p_the.zeros(); |
---|
651 | p_isd(0) = isa*cos(the) + isb*sin(the);//isa; |
---|
652 | p_isq(0) = isb*cos(the) - isa*sin(the);//isb; |
---|
653 | p_ome(0) = ome; |
---|
654 | p_the(0) = the; |
---|
655 | |
---|
656 | for(i = 0; i < rec_hor-1; i++){ |
---|
657 | p_isd(i+1) = a*p_isd(i) + p_isq(i)*p_ome(i)*Dt; + c*0.5*(udq(0)+u_old(0)); |
---|
658 | p_isq(i+1) = -p_isd(i)*p_ome(i)*Dt + a*p_isq(i) - b*p_ome(i); + c*0.5*(udq(1)+u_old(1)); |
---|
659 | p_ome(i+1) = d*p_ome(i) + e*p_isq(i); |
---|
660 | p_the(i+1) = p_the(i) + Dt*p_ome(i); |
---|
661 | } |
---|
662 | |
---|
663 | //create control matrix |
---|
664 | for(i = rec_hor; i > 0; i--){ |
---|
665 | //set variable matrices elements (A matrix only) |
---|
666 | A(0, 1) = Dt*p_ome(i); |
---|
667 | A(0, 2) = Dt*p_isq(i); |
---|
668 | A(0, 4) = Dt*p_isq(i); |
---|
669 | A(1, 0) = -Dt*p_ome(i); |
---|
670 | A(1, 2) = -Dt*p_isd(i); |
---|
671 | A(1, 4) = -Dt*p_isq(i); |
---|
672 | |
---|
673 | lq.Models(0).A = A; |
---|
674 | lq.redesign(); |
---|
675 | } |
---|
676 | lq.redesign(); |
---|
677 | |
---|
678 | L = lq.getL(); |
---|
679 | |
---|
680 | icondpd(0) = isa*cos(the) + isb*sin(the); |
---|
681 | icondpd(1) = isb*cos(the) - isa*sin(the); |
---|
682 | icondpd(2) = ome - Ww; |
---|
683 | icondpd(3) = the; |
---|
684 | icondpd(4) = Ww; |
---|
685 | icondpd(5) = u_old(0); |
---|
686 | icondpd(6) = u_old(1); |
---|
687 | icondpd(7) = 0; |
---|
688 | |
---|
689 | tmp = L*icondpd; |
---|
690 | |
---|
691 | udq = tmp(0,1); |
---|
692 | |
---|
693 | //uab = udq; //set size |
---|
694 | uab(0) = udq(0)*cos(the) - udq(1)*sin(the); |
---|
695 | uab(1) = udq(1)*cos(the) + udq(0)*sin(the); |
---|
696 | |
---|
697 | if(MAXuflag == 1){ //square cut off |
---|
698 | if(uab(0) > MAXu) uab(0) = MAXu; |
---|
699 | else if(uab(0) < -MAXu) uab(0) = -MAXu; |
---|
700 | if(uab(1) > MAXu) uab(1) = MAXu; |
---|
701 | else if(uab(1) < -MAXu) uab(1) = -MAXu; |
---|
702 | } |
---|
703 | else if(MAXuflag == 2){ //circular cut off |
---|
704 | double uampl = sqrt(uab(0)*uab(0)+uab(1)*uab(1)); |
---|
705 | double uangl = atan2(uab(1), uab(0)); |
---|
706 | if (uampl > MAXu) uampl = MAXu; |
---|
707 | uab(0) = uampl*cos(uangl); |
---|
708 | uab(1) = uampl*sin(uangl); |
---|
709 | } |
---|
710 | //else{ //(MAXuflag == 0) //no cut off } |
---|
711 | |
---|
712 | u_old = udq; |
---|
713 | |
---|
714 | return uab; |
---|
715 | } |
---|
716 | |
---|
717 | void from_setting(const Setting &set){ |
---|
718 | PMSMCtrl::from_setting(set); |
---|
719 | UI::get(r,set, "r", UI::optional); |
---|
720 | UI::get(rec_hor,set, "h", UI::optional); |
---|
721 | UI::get(MAXu,set, "MAXu", UI::optional); |
---|
722 | UI::get(MAXuflag,set, "MAXuflag", UI::optional); |
---|
723 | UI::get(rpd,set, "rpd", UI::optional); |
---|
724 | } |
---|
725 | |
---|
726 | void validate(){ |
---|
727 | R(0, 0) = R(1, 1) = r; |
---|
728 | Rpd(0, 0) = Rpd(1, 1) = rpd; |
---|
729 | Rpd(0, 2) = Rpd(1, 3) = -rpd; |
---|
730 | |
---|
731 | p_isd.set_length(rec_hor+1); |
---|
732 | p_isq.set_length(rec_hor+1); |
---|
733 | p_ome.set_length(rec_hor+1); |
---|
734 | p_the.set_length(rec_hor+1); |
---|
735 | |
---|
736 | Array<quadraticfn> qloss(3); |
---|
737 | qloss(0).Q.setCh(Q); |
---|
738 | qloss(0).rv = RV("x", 5, 1); |
---|
739 | qloss(1).Q.setCh(R); |
---|
740 | qloss(1).rv = RV("u", 2, 0); |
---|
741 | qloss(2).Q.setCh(Rpd); |
---|
742 | qloss(2).rv = RV("u", 2, 0); |
---|
743 | qloss(2).rv.add(RV("u", 2, -1)); |
---|
744 | lq.Losses = qloss; |
---|
745 | |
---|
746 | //set lq |
---|
747 | lq.rv = RV("u", 2, 0); |
---|
748 | RV tmp = RV("x", 5, 0); |
---|
749 | tmp.add(RV("u", 2, -1)); |
---|
750 | lq.set_rvc(tmp); |
---|
751 | |
---|
752 | lq.horizon = rec_hor; |
---|
753 | |
---|
754 | Array<linfnEx> model(2); |
---|
755 | model(0).A = A; |
---|
756 | model(0).B = vec("0 0 0 0 0"); |
---|
757 | model(0).rv = RV("x", 5, 0); |
---|
758 | model(0).rv_ret = RV("x", 5, 1); |
---|
759 | model(1).A = B; |
---|
760 | model(1).B = vec("0 0"); |
---|
761 | model(1).rv = RV("u", 2, 0); |
---|
762 | model(1).rv_ret = RV("x", 5, 1); |
---|
763 | lq.Models = model; |
---|
764 | |
---|
765 | lq.finalLoss.Q.setCh(Q); |
---|
766 | lq.finalLoss.rv = RV("x", 5, 1); |
---|
767 | |
---|
768 | lq.validate(); |
---|
769 | |
---|
770 | uab.zeros(); |
---|
771 | udq.zeros(); |
---|
772 | } |
---|
773 | }; |
---|
774 | UIREGISTER(PMSM_LQCtrl_dq2); |
---|
775 | |
---|
776 | /*!@}*/ |
---|
777 | #endif //PMSM_CTR_H |
---|