root/applications/dual/vahala/kim/basic_main.m @ 1435

Revision 1435, 5.4 kB (checked in by vahalam, 12 years ago)
Line 
1% main - hlavni skript
2clear all;
3% oznaceni: s ... system
4%           k ... kalman (EKF)
5%           l ... rizeni (LQR)
6
7% KONSTANTY
8T = 40000; %horizont
9dt = 0.000125; %casovy krok
10
11% Rs = 0.28;
12% Ls = 0.003465;
13% psipm = 0.1989;
14% B = 0;   
15% kp = 1.5;
16% pp = 4.0;
17% J = 0.04;
18
19% Lq = 1.05*Ls;
20% Ld = 0.95*Ls;
21
22a = 0.9898;
23b = 0.0072;
24c = 0.0361;
25d = 1.0;
26e = 0.0149;
27
28Ls = 0.003465;
29psipm = 0.1989;
30
31% ref_profile = [0, -1, 3, 6, 9, 6, 3, 0, 0, 0, 0, 0, 0,-3, -6, -3];%/9*200;
32ref_profile = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
33
34%kovariance EKF na stavu
35% Q_k = diag([0.001, 0.00001]);
36% R_k = diag([0.015, 0.015]);
37Q_k = diag([0.01, 0.0001]);
38R_k = diag([0.15, 0.15]);
39
40%hodnoty sumu v systemu
41nQ = diag([0.0013, 0.0013, 5.0e-6, 1.0e-10]);
42nR = diag([0.0006, 0.0006]);
43
44iter_l = 10;% pocet iteraci ve vypoctu rizeni
45
46% B_l = zeros(3,2);
47% B_l = zeros(2,2);
48% B_l(1,1) = c;
49% B_l(2,2) = c;
50
51Q_l = diag([1 0 0]);
52% % Q_l = diag([0 0 1 0 0]);
53r = 0.01;
54R_l = diag([r, r]);
55
56% PROMENNE
57x_s = zeros(4,T); %stav
58y_s = zeros(2,T); %mereni
59x_k = zeros(2,T); %odhad stavu
60P_k = zeros(2); %kovariance stavu
61u_l = zeros(2,T); %rizeni
62% S_l = zeros(3); %jadro ztraty
63S_l = zeros(2);
64
65% POCATECNI HODNOTY
66noise = 1; %prepinac sumu
67% noise = 0;
68
69theta0 = 1.5;%1.7; %pocatecni poloha
70P0 = eye(2); %odhad pocatecni kovariance stavu (apriorni)
71% ST = zeros(3); %koncova ztrata
72ST = ones(3);
73
74
75% INICIALIZACE
76x_s(4,1) = theta0;
77% x_s(3,1) = 5;
78P_k = P0;
79S_l = ST;
80
81ref_ome = zeros(1, T); 
82    for k = 1:T,
83           index = floor(k*dt);
84           if(index>0)
85               lower = ref_profile(index);
86           else
87               lower = 0;
88           end
89           if(index<T*dt)
90               upper = ref_profile(index+1);
91           else
92               upper = 0;
93           end
94           ref_ome(k) = lower + (upper-lower)*dt*(k-index/dt);
95    end
96% ref_ome = 0*ones(1, T);
97
98% Derivace pro prvni EKF
99ome = x_k(1,1);
100the = x_k(2,1);   
101ia = y_s(1,1);
102ib = y_s(2,1);
103A = [d, -e*(ia*cos(the)+ib*sin(the)); dt, 1.0];
104C = [b*sin(the), b*ome*cos(the); -b*cos(the), b*ome*sin(the)];
105
106
107ri = 0.0001;
108ai = (1-a*a)/c/c;
109Si = (1 - ai*r + sqrt((ai*r-1)^2+4*r/c/c))/2;
110Li = a*c*Si/(c*c*Si+ri);
111   
112A_l = [d,0,0;dt,1,dt;0,0,1];
113% B_l = zeros(2);
114% % A_l = [a 0 0 0 0; 0 a 0 0 0; 0 0 d 0 (d-1); 0 0 dt 1 dt; 0 0 0 0 1];
115% % B_l = zeros(5,2);
116% % B_l(1:2,1:2) = [c 0;0 c];
117
118%PI vektorove
119% kon_pi = 3.0;
120% kon_ii = 0.00375;
121% kon_pu = 20.0;
122% kon_iu = 0.05;
123% sum_iq = 0;
124% sum_ud = 0;
125% sum_uq = 0;
126
127
128
129% HLAVNI SMYCKA
130for t = 1:T-1,
131    % EKF     
132    Pp = A*P_k*A' + Q_k;
133    S = C*Pp*C' + R_k;
134    K = Pp*C'/S;
135    P_k = Pp - K*C*Pp;   
136   
137    xp = zeros(2,1);
138    xp(1) = d*x_k(1,t) + e*(y_s(2,t)*cos(x_k(2,t)) - y_s(1,t)*sin(x_k(2,t)));
139    xp(2) = x_k(2,t) + dt*x_k(1,t);   
140    yp = zeros(2,1);
141    yp(1) = a*y_s(1,t) + b*x_k(1,t)*sin(x_k(2,t)) + c*u_l(1,t);
142    yp(2) = a*y_s(2,t) - b*x_k(1,t)*cos(x_k(2,t)) + c*u_l(2,t);
143   
144    x_k(:,t+1) = xp + K*(y_s(:,t) - yp);
145   
146    %!!!
147%     tmp = x_k(:,t+1);
148%     x_k(:,t+1) = x_s(3:4,t);
149   
150    % Derivace   
151    ome = x_k(1,t+1);
152    the = x_k(2,t+1);   
153    ia = y_s(1,t);
154    ib = y_s(2,t);
155    A = [d, -e*(ia*cos(the)+ib*sin(the)); dt, 1.0];
156    C = [b*sin(the), b*ome*cos(the); -b*cos(the), b*ome*sin(the)];
157   
158%     id = ia*cos(the) + ib*sin(the);
159%     iq = ib*cos(the) - ia*sin(the);
160   
161    % LQ
162%     phi = zeros(2,1);
163%     phi(1) = d*x_k(1,t+1) + e*(y_s(2,t)*cos(x_k(2,t+1)) - y_s(1,t)*sin(x_k(2,t+1)));
164%     phi(2) = x_k(2,t+1) + dt*x_k(1,t+1);
165%     y = x_k(:,t+1);
166%     y(1) = y(1) - ref_ome(t);
167%     A_l = zeros(3);
168%     A_l(1:2,1:2) = A;
169%     A_l = A;
170%     A_l(1,2) = 0;
171%     A_l(1:2,3) = phi - A*y;
172%     A_l(3,3) = 1;
173    B_l = [-e*sin(the), e*cos(the); 0, 0; 0,0];
174    y = [(ome-ref_ome(t)); the; ref_ome(t)];
175% %     y = [ia; ib; (ome-ref_ome(t)); the; ref_ome(t)];   
176% %     A_l(1, 3) = b*sin(the);
177% %     A_l(2, 3) = -b*cos(the);
178% %     A_l(1, 5) = b*sin(the);
179% %     A_l(2, 5) = -b*cos(the);
180% %     A_l(3, 1) = -e*sin(the);
181% %     A_l(3, 2) = e*cos(the);
182    for i = 1:iter_l
183       S_l = A_l'*(S_l - S_l*B_l/(B_l'*S_l*B_l + R_l)*B_l'*S_l)*A_l + Q_l;
184    end
185    L = (B_l'*S_l*B_l + R_l)\B_l'*S_l*A_l;   
186%     yref = -L*y;%referencni proudy
187%     u_l(:,t+1) = b/c*ome*[-sin(the);cos(the)] + yref/c - Li*y_s(:,t);
188   
189%     sum_iq = sum_iq + ref_ome(t) - ome;
190%     ref_iq = kon_pi*(ref_ome(t) - ome) + kon_ii*sum_iq;
191%     sum_ud = sum_ud - id;
192%     u_d = kon_pu*(-id) + kon_iu*sum_ud;
193%     sum_uq = sum_uq + ref_iq - iq;
194%     u_q = kon_pu*(ref_iq - iq) + kon_iu*sum_uq;
195%     u_d = u_d - Ls*ome*ref_iq;
196%     u_q = u_q + psipm*ome;
197%       
198%     u_l(1, t+1) = u_d*cos(the) - u_q*sin(the);
199%     u_l(2, t+1) = u_q*cos(the) + u_d*sin(the);
200%     u_l(:,t+1) = b/c*ome*[-sin(the);cos(the)] + yref/c*[sin(the);-cos(the)] - Li*y_s(:,t);
201
202%     u_l(:,t+1) = yref/c - Li*y_s(:,t);
203%     u_l(:,t+1) = -L*[y;1];
204    u_l(:,t+1) = -L*y + b/c*ome*[-sin(the);cos(the)] - Li*y_s(:,t);
205    if u_l(1,t+1) > 100
206        u_l(1,t+1) = 100;
207    elseif u_l(1,t+1) < -100
208        u_l(1,t+1) = -100;
209    end
210    if u_l(2,t+1) > 100
211        u_l(2,t+1) = 100;
212    elseif u_l(2,t+1) < -100
213        u_l(2,t+1) = -100;
214    end
215%     u_l(:,t+1) = 0;
216    % Vyvoj systemu
217    [x_s(:,t+1), y_s(:,t+1)] = evolSys(x_s(:,t), u_l(:,t+1), nQ, nR, noise);
218   
219    %!!!
220%     x_k(:,t+1) = tmp;
221end
222
223figure;
224subplot(2,1,1);
225plot(1:T,x_k(1,:),1:T,x_s(3,:),1:T,ref_ome);
226subplot(2,1,2);
227plot(1:T,atan2(sin(x_k(2,:)),cos(x_k(2,:))),1:T,atan2(sin(x_s(4,:)),cos(x_s(4,:))));
228
Note: See TracBrowser for help on using the browser.