1 | function [loss] = main_lq4(T, ref_profile, theta0, simulator, graf, inddq) |
---|
2 | % main - hlavni skript >>>>>>>PLNY STAV<<<<<<< |
---|
3 | % clear all; |
---|
4 | % oznaceni: s ... system |
---|
5 | % k ... kalman (EKF) |
---|
6 | % l ... rizeni (LQR) |
---|
7 | |
---|
8 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
9 | %%%%%pouziti SIMULATORU |
---|
10 | % simulator = 1; |
---|
11 | % simulator = 0; |
---|
12 | |
---|
13 | if((simulator == 1)||(simulator == 10)) |
---|
14 | sim_param = pmsm_sim; |
---|
15 | % sim_param(9) = 0; %vypne dead-time |
---|
16 | pmsm_sim(sim_param); |
---|
17 | end |
---|
18 | %%%%% |
---|
19 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
20 | |
---|
21 | % KONSTANTY |
---|
22 | % T = 120000; %horizont |
---|
23 | % T = 40000; |
---|
24 | dt = 0.000125; %casovy krok |
---|
25 | |
---|
26 | % Rs = 0.28; |
---|
27 | % Ls = 0.003465; |
---|
28 | % psipm = 0.1989; |
---|
29 | % B = 0; |
---|
30 | % kp = 1.5; |
---|
31 | % pp = 4.0; |
---|
32 | % J = 0.04; |
---|
33 | |
---|
34 | % Lq = 1.05*Ls; |
---|
35 | % Ld = 0.95*Ls; |
---|
36 | |
---|
37 | % a = 0.9898; |
---|
38 | % b = 0.0072; |
---|
39 | c = 0.0361; |
---|
40 | % d = 1.0; |
---|
41 | % e = 0.0149; |
---|
42 | |
---|
43 | % ref_profile = [0, -1, 3, 6, 9, 6, 3, 0, 0, 0, 0, 0, 0,-3, -6, -3];%/9*200; |
---|
44 | % ref_profile = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; |
---|
45 | % ref_profile = [0, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 0]; |
---|
46 | % ref_profile = [1, 10, 50, 200, 200, 30, 0, 0, -1, -10, -50, -200, -200, -30, 0]; |
---|
47 | % ref_profile = [20, 20, 20, 50, 50, 50, -10, -10, -10, 0, 0, 0, 20, 20, 20]; |
---|
48 | |
---|
49 | %kovariance EKF na stavu, ktery vytvari hyperstav |
---|
50 | % Q_k = diag([0.001, 0.00001]); |
---|
51 | % R_k = diag([0.015, 0.015]); |
---|
52 | Q_k = diag([0.1, 0.1, 0.01, 0.0001]); |
---|
53 | R_k = diag([0.05, 0.05]); |
---|
54 | |
---|
55 | %kovariance EKF na hyperstavu |
---|
56 | % Qh_k = diag([0.001, 0.00001, 0.00001, 0.00001, 0.00001]); |
---|
57 | % Rh_k = diag([0.015, 0.015]); |
---|
58 | Qh_k = 0.1*eye(14); |
---|
59 | Qh_k(1:4,1:4) = diag([0.1, 0.1, 0.01, 0.0001]); |
---|
60 | Rh_k = diag([0.05, 0.05]); |
---|
61 | |
---|
62 | %hodnoty sumu v systemu |
---|
63 | nQ = diag([0.0013, 0.0013, 5.0e-6, 1.0e-10]); |
---|
64 | nR = diag([0.0006, 0.0006]); |
---|
65 | |
---|
66 | iter_l = 10;% pocet iteraci ve vypoctu rizeni |
---|
67 | |
---|
68 | B_l = zeros(15,2); |
---|
69 | B_l(1,1) = c; |
---|
70 | B_l(2,2) = c; |
---|
71 | |
---|
72 | Q_l = zeros(15); |
---|
73 | Q_l(3,3) = 1; %ome |
---|
74 | Q_l(10,10) = 1; %Var ome |
---|
75 | % Q_l(14,14) = 1; %Var the |
---|
76 | % o t Po Pot Pt |
---|
77 | % Q_l = diag([1 0 1 0 0 0]); % asi spravne z teoretickeho hlediska |
---|
78 | % Q_l = diag([1 0 1 0 1 0]); |
---|
79 | % Q_l = diag([1 0 0 0 0 0]); |
---|
80 | r = 0.0001; |
---|
81 | R_l = diag([r, r]); |
---|
82 | |
---|
83 | |
---|
84 | |
---|
85 | % PROMENNE |
---|
86 | x_s = zeros(4,T); %stav |
---|
87 | y_s = zeros(2,T); %mereni |
---|
88 | x_k = zeros(14,T); %odhad hyperstavu |
---|
89 | % P_k = zeros(14); %kovariance hyperstavu |
---|
90 | u_l = zeros(2,T); %rizeni |
---|
91 | % S_l = zeros(15); %jadro ztraty |
---|
92 | % pre_k = zeros(10,1); %predikce stavu |
---|
93 | |
---|
94 | |
---|
95 | % POCATECNI HODNOTY |
---|
96 | noise = 1; %prepinac sumu |
---|
97 | % noise = 0; |
---|
98 | |
---|
99 | % theta0 = 0;%1.5;%1.7; %pocatecni poloha |
---|
100 | % Ps0 = eye(4); %odhad pocatecni kovariance stavu (apriorni) |
---|
101 | Pk0 = eye(14); %pocatecni kovariance hyperstavu |
---|
102 | ST = eye(15); %koncova ztrata |
---|
103 | |
---|
104 | |
---|
105 | % INICIALIZACE |
---|
106 | x_k(4,1) = theta0; |
---|
107 | |
---|
108 | %Ps0 = eye |
---|
109 | x_k(5,1) = 1; |
---|
110 | x_k(7,1) = 1; |
---|
111 | x_k(11,1) = 1; |
---|
112 | x_k(14,1) = 1; |
---|
113 | |
---|
114 | P_k = Pk0; |
---|
115 | S_l = ST; |
---|
116 | |
---|
117 | ref_ome = zeros(1, T); |
---|
118 | for k = 1:T, |
---|
119 | index = floor(k*dt); |
---|
120 | if(index>0) |
---|
121 | lower = ref_profile(index); |
---|
122 | else |
---|
123 | lower = 0; |
---|
124 | end |
---|
125 | if(index<T*dt) |
---|
126 | upper = ref_profile(index+1); |
---|
127 | else |
---|
128 | upper = 0; |
---|
129 | end |
---|
130 | ref_ome(k) = lower + (upper-lower)*dt*(k-index/dt); |
---|
131 | end |
---|
132 | |
---|
133 | % Derivace pro prvni EKF |
---|
134 | [A_k, C_k, pre_k, A_l] = assembDeriv4(x_k(:,1), u_l(:,1), x_k(:,1), Q_k, R_k, 0, inddq); |
---|
135 | |
---|
136 | % HLAVNI SMYCKA |
---|
137 | for t = 1:T-1, |
---|
138 | % EKF |
---|
139 | [x_k(:,t+1), P_k] = extKF4(x_k(:,t), y_s(:,t), u_l(:,t), pre_k, A_k, C_k, P_k, Qh_k, Rh_k); |
---|
140 | % x_k(1:4,t+1) = x_s(:,t); %cidlo |
---|
141 | |
---|
142 | % Derivace |
---|
143 | [A_k, C_k, pre_k, A_l] = assembDeriv4(x_k(:,t+1), u_l(:,t), x_k(:,t+1), Q_k, R_k, ref_ome(t), inddq); |
---|
144 | |
---|
145 | % LQ |
---|
146 | %!!! upraveno na QR verzi - tj. parametry jsou odmocniny |
---|
147 | [u_l(:,t+1), S_l] = ctrlLQ4(x_k(:,t+1), ref_ome(t), A_l, B_l, sqrtm(ST), Q_l, sqrtm(R_l), iter_l); |
---|
148 | |
---|
149 | if u_l(1,t+1) > 100 |
---|
150 | u_l(1,t+1) = 100; |
---|
151 | elseif u_l(1,t+1) < -100 |
---|
152 | u_l(1,t+1) = -100; |
---|
153 | end |
---|
154 | if u_l(2,t+1) > 100 |
---|
155 | u_l(2,t+1) = 100; |
---|
156 | elseif u_l(2,t+1) < -100 |
---|
157 | u_l(2,t+1) = -100; |
---|
158 | end |
---|
159 | |
---|
160 | % Vyvoj systemu |
---|
161 | [x_s(:,t+1), y_s(:,t+1)] = evolSys(x_s(:,t), u_l(:,t+1), nQ, nR, noise, simulator); |
---|
162 | end |
---|
163 | |
---|
164 | if(graf == 1) |
---|
165 | %vykresleni |
---|
166 | cas = (1:T)*dt; |
---|
167 | figure; |
---|
168 | subplot(2,1,1); |
---|
169 | plot(cas,x_k(3,:),cas,x_s(3,:),cas,ref_ome); |
---|
170 | title('Prubeh otacek v case'); |
---|
171 | xlabel('cas [s]'); |
---|
172 | ylabel('otacky [rad/s]'); |
---|
173 | legend('odhad','skutecne','pozadovane'); |
---|
174 | subplot(2,1,2); |
---|
175 | plot(cas,atan2(sin(x_k(4,:)),cos(x_k(4,:))),cas,atan2(sin(x_s(4,:)),cos(x_s(4,:)))); |
---|
176 | title('Prubeh polohy v case'); |
---|
177 | xlabel('cas [s]'); |
---|
178 | ylabel('poloha [rad]'); |
---|
179 | |
---|
180 | figure; |
---|
181 | plot(cas,x_s(3,:)-ref_ome); |
---|
182 | title('Prubeh chyby (skutecne - pozadovane otacky v case)'); |
---|
183 | xlabel('cas [s]'); |
---|
184 | ylabel('chyba [rad/s]'); |
---|
185 | end |
---|
186 | |
---|
187 | loss = sum((x_s(3,:)-ref_ome).^2); |
---|
188 | end |
---|