root/applications/pmsm/pmsm_ctrl.h @ 1259

Revision 1259, 6.8 kB (checked in by vahalam, 14 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 o_ua o_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        const 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                                p_isa(rec_hor+1), p_isb(rec_hor+1), p_ome(rec_hor+1), p_the(rec_hor+1)
206        {                       
207                //set fix matrices elements & dimensions
208                A.zeros();
209                        A(0, 0) = A(1, 1) = a;
210                        A(2, 2) = d;
211                        A(2, 4) = d - 1;
212                        A(3, 2) = A(3, 4) = Dt;
213                        A(3, 3) = A(4, 4) = 1.0;
214                B.zeros();
215                        B(0, 0) = B(1, 1) = c;
216                //C.zeros();
217                //      C(0, 0) = C(1, 1) = 1.0;
218                Q.zeros();
219                        Q(2, 2) = 1.0;
220                R.zeros();
221                        //load input penalty r from file
222                        ifstream pcpfile;
223                        double pcp;
224                       
225                        pcpfile.open("pcp.txt");
226                        if(pcpfile){
227                                if(pcpfile >> pcp) r = pcp;     
228                                pcpfile.close();
229                        }
230                        R(0, 0) = R(1, 1) = r;
231                                                               
232                //set lq
233                lq.rv = RV("u", 2, 0);                 
234                lq.set_rvc(RV("x", 5, 0));
235                lq.horizon = rec_hor;   
236               
237                Array<linfnEx> model(2);
238                model(0).A = A;
239                model(0).B = vec("0 0 0 0 0");
240                model(0).rv = RV("x", 5, 0);
241                model(0).rv_ret = RV("x", 5, 1);
242                model(1).A = B;
243                model(1).B = vec("0 0");
244                model(1).rv = RV("u", 2, 0);
245                model(1).rv_ret = RV("x", 5, 1);
246                lq.Models = model;
247               
248                Array<quadraticfn> qloss(2);
249                qloss(0).Q.setCh(Q);
250                qloss(0).rv = RV("x", 5, 1);
251                qloss(1).Q.setCh(R);
252                qloss(1).rv = RV("u", 2, 0);           
253                lq.Losses = qloss;
254               
255                lq.finalLoss.Q.setCh(Q);
256                lq.finalLoss.rv = RV("x", 5, 1);
257               
258                lq.validate();
259                                               
260                uab.zeros();
261        }
262               
263       
264        virtual vec ctrlaction(const itpp::vec& cond) {
265                PMSMCtrl::ctrlaction(cond); // fills isa,isb,ome,the,Ww
266               
267                int i;
268                lq.resetTime();
269               
270                //create prediction
271                p_isa.zeros();
272                p_isb.zeros();
273                p_ome.zeros();
274                p_the.zeros();
275                p_isa(0) = isa;
276                p_isb(0) = isb;
277                p_ome(0) = ome;
278                p_the(0) = the;
279               
280                for(i = 0; i < rec_hor-1; i++){
281                        p_isa(i+1) = a*p_isa(i) + b*p_ome(i)*sin(p_the(i)) + c*0;//uab(0); //use last used control
282                        p_isb(i+1) = a*p_isb(i) - b*p_ome(i)*cos(p_the(i)) + c*0;//uab(1);
283                        p_ome(i+1) = d*p_ome(i) + e*(p_isb(i)*cos(p_the(i)) - p_isa(i)*sin(p_the(i)));
284                        p_the(i+1) = p_the(i) + Dt*p_ome(i);
285                }
286               
287                //create control matrix
288                for(i = rec_hor; i > 0; i--){           
289                        //set variable matrices elements (A matrix only)
290                        A(0, 2) = b*sin(p_the(i));
291                        A(0, 3) = b*(p_ome(i))*cos(p_the(i));
292                        A(0, 4) = b*sin(p_the(i));
293                        A(1, 2) = -b*cos(p_the(i));
294                        A(1, 3) = b*(p_ome(i))*sin(p_the(i));
295                        A(1, 4) = -b*cos(p_the(i));
296                        A(2, 0) = -e*sin(p_the(i));
297                        A(2, 1) = e*cos(p_the(i));
298                        A(2, 3) = -e*(p_isa(i)*cos(p_the(i)) + p_isb(i)*sin(p_the(i)));
299                       
300                        lq.Models(0).A = A;             
301                        lq.redesign();
302                }
303                lq.redesign();
304               
305                L = lq.getL();
306                icond(0) = isa;
307                icond(1) = isb;
308                icond(2) = ome - Ww;
309                icond(3) = the;
310                icond(4) = Ww;
311                icond(5) = 0;
312                vec tmp = L*icond;                 
313               
314                uab = tmp(0,1);
315               
316                if(uab(0) > MAXu) uab(0) = MAXu;
317                else if(uab(0) < -MAXu) uab(0) = -MAXu;
318                if(uab(1) > MAXu) uab(1) = MAXu;
319                else if(uab(1) < -MAXu) uab(1) = -MAXu;
320               
321                return uab;
322        };
323};
324UIREGISTER(PMSM_LQCtrl);
325
326/*!@}*/
327#endif //PMSM_CTR_H
Note: See TracBrowser for help on using the browser.