root/applications/pmsm/pmsm_ctrl.h @ 1292

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