-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdfunc.m
443 lines (354 loc) · 13.7 KB
/
sdfunc.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
% Authors: Alejandro R. Mosteo, Danilo Tardioli, Eduardo Montijano
% Copyright 2018-9999 Monmostar
% Licensed under GPLv3 https://www.gnu.org/licenses/gpl-3.0.en.html
%
classdef sdfunc
% Contains our own GUI-related things, to isolate them somehow from
% the automatic callbacks created by Matlab
methods(Static)
function common_callback(~, ~) % hObject, eventdata
sdfunc.update_LTI(guidata(gcf));
end
function h = init(handles)
handles.initializing.Position = [0 0 1 1];
%drawnow
handles.props = props();
% Control diagram
axes(handles.diagram);
diagram = imread('diagram2.jpg');
dpos = handles.diagram.Position;
diagram = imresize(diagram, [dpos(4) dpos(3)]);
imshow(diagram);
% Initialize panels
sdfunc.init_tfpanels(handles);
% Get TFs
[C, G] = sdfunc.gui_LTI_config(handles);
handles.props.sau = saudefense(handles.battle, ...
loop_single(handles.props.tff, sdconst.default_period, C*G, 1));
sau = handles.props.sau;
sdfunc.update_texts(handles, sau);
sau.plot_hist = handles.do_siso.Value;
handles.difficulty.Value = sau.difficulty;
sdfunc.update_difficulty_panel(handles);
handles.autoaim.Value = sau.auto_aim;
handles.autofire.Value = sau.gun.autofire;
handles.period.String = sprintf('%g', sau.T);
sdfunc.update_LTI(handles);
handles.start.Enable = 'on';
handles.initializing.Visible = 'off';
% hold(handles.siso, 'on');
% axis(handles.siso, [-sau.max_hist_time 0 -sau.W/2, sau.W/2]);
h = handles;
disp('Ready');
end
function ok = initialization_check()
% Check resolution to avoid misplaced widgets
set(0,'units','pixels');
sizes = get(0,'screensize');
width = sizes(3); height = sizes(4);
if width < 1280 || height < 720
errordlg(sprintf(strcat('La resolución de la pantalla debe ser al menos 1280x720.\n', ...
'Se ha detectado %dx%d'), width, height), ...
'My Error Dialog', 'modal');
ok = false;
else
ok = true;
end
end
function reset(h)
% Reinitializes minimal things to start a new competition
% Get TFs
[C, G] = sdfunc.gui_LTI_config(h);
% Fresh SAU
period = str2double(h.period.String);
h.props.sau = saudefense(h.battle, ...
loop_single(h.props.tff, period, C*G, 1));
h.difficulty.Value = h.props.sau.difficulty;
end
function init_tfpanels(handles)
% Clean previous
delete(allchild(handles.p_controller));
delete(allchild(handles.p_plant));
% Controller
switch handles.pop_controller.Value
case 1
handles.props.widget_controller = panel_p();
case 2
handles.props.widget_controller = panel_pd();
case 3
handles.props.widget_controller = panel_pid();
case 4
handles.props.widget_controller = panel_pid_kzz();
case 5
handles.props.widget_controller = panel_lead_net();
otherwise
error('Unknown controller: %s', handles.pop_controller.Value)
end
handles.props.widget_controller.prepare(handles.p_controller);
% Plant
switch handles.pop_plant.Value
case 1
handles.props.widget_plant = panel_motor_mbk();
case 2
handles.props.widget_plant = panel_motor_1st();
case 3
handles.props.widget_plant = panel_motor_2nd();
case 4
handles.props.widget_plant = panel_motor_2nd_zpk();
otherwise
error('Unknown plant: %s', handles.pop_plant.Value)
end
handles.props.widget_plant.prepare(handles.p_plant);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function done = looper(h)
h.props.sau.tic()
done = h.props.sau.iterate();
sdfunc.update_difficulty_panel(h);
sdfunc.update_texts(h, h.props.sau);
drawnow limitrate
spare = h.props.sau.T - h.props.sau.toc();
if spare > 0.001
pause(spare);
% else
% fprintf(2,'falling behing!\n');
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function enable_all(h, enable)
enabled = sdconst.onoff(enable);
h.difficulty.Enable = enabled;
h.pop_controller.Enable = enabled;
if h.props.league
enable_plant = 'off';
else
enable_plant = enable;
end
h.pop_plant.Enable = enable_plant;
for w=allchild(h.p_plant)'
if isprop(w, 'Enable')
w.Enable = enable_plant;
end
end
for w=allchild(h.p_controller)'
if isprop(w, 'Enable')
w.Enable = enabled;
end
end
end
function start_stop(h, compete)
h.props.running = ~h.props.running;
if h.props.running
disp('Running...');
else
disp('Stopped.');
end
% Disable contrary
if compete
h.start.Enable = sdconst.onoff(~h.props.running);
else
h.compete.Enable = sdconst.onoff(~h.props.running);
end
if h.props.running
if compete
h.compete.String = 'Pause';
sdfunc.enable_all(h, false);
else
h.start.String = 'Pause';
sdfunc.enable_all(h, true);
end
else
h.start.String = 'Test';
h.compete.String = 'Competition';
sdfunc.enable_all(h, true);
end
% Reset if competing for the first time after test
if ~h.props.competing && h.props.running && compete
sdfunc.reset(h);
seed = 2019;%mod(idivide(int64(round(now*100000)), int64(60), 'floor'), 10000);
% Changes once per minute
rng(seed);
h.text_seed.String = sprintf('Random seed: %d', seed);
end
if ~compete && h.props.running
seed = mod(round(now*1000000), 10000);
rng(seed);
h.text_seed.String = sprintf('Random seed: %d', seed);
end
h.props.competing = compete;
while h.props.running
done = sdfunc.looper(h);
if done && h.props.competing
sdfunc.enable_all(h, true);
msgbox({sprintf('Final score: %d points', h.props.sau.score), ...
sprintf('Time survived: %.2f seconds', ...
h.props.sau.iterations * h.props.sau.T)});
sdfunc.start_stop(h, true);
h.props.competing = false;
break
end
end
end
function [C, G] = gui_LTI_config(h)
C = h.props.widget_controller.get_tf();
G = h.props.widget_plant.get_tf();
end
function update_difficulty_panel(h)
h.panel_difficulty.Title = ...
sprintf('Difficulty: %5.3f', h.props.sau.difficulty);
h.difficulty.Value = h.props.sau.difficulty;
end
function update_texts(h, sau)
h.load.String = ...
sprintf('CPU Load: %.0f%%', mean(sau.load)*100);
if sau.load > 1
h.load.ForegroundColor = [1 0 0];
else
h.load.ForegroundColor = [0 0 0];
end
h.cooldown.String = ...
sprintf('Arming time (Ts): %.2f/%.4f', sau.gun.cooldown, sau.gun.ts);
if sau.gun.cooldown > 0
h.cooldown.ForegroundColor = [1 0 0];
else
h.cooldown.ForegroundColor = [0 0 0];
end
h.hits.String = sprintf('Hits: %d', sau.hits);
h.score.String = sprintf('Score: %d', sau.score);
h.alive.String = sprintf('Alive: %.2f"', sau.iterations*sau.T);
h.accel.String = ...
sprintf('Acceleration: %5.3f', sau.gun.get_a());
if abs(sau.gun.get_a()) > sau.gun.a_break
h.accel.ForegroundColor = [1 0 0];
elseif abs(sau.gun.get_a()) > sau.gun.a_arm
h.accel.ForegroundColor = [1 0.5 0];
else
h.accel.ForegroundColor = [0 0 0];
end
end
function update_error_plot(axe, C, G, tend)
axes(axe);
s=tf('s');
% error responses
[y1,t1] = step(feedback(1, C*G), tend);
[y2,t2] = step(1/s/(1 + C*G), tend);
plot(axe, t1, y1, 'b', t2, y2, 'r');
axis auto
ax = axis(axe);
ax(2) = tend(end);
axis(axe, ax);
%title('Manual response')
title(axe, '');
xlabel(axe, '');
ylabel(axe, '');
legend(axe, 'step error', 'ramp error', 'Location', 'northwest');
%drawnow
end
function update_response_plot(axe, sau, tend)
axes(axe);
cla(axe);
hold(axe, 'on');
% reference
plot(axe, [0; tend], [1; 1], ...
'Color', [0 0 0], 'LineStyle', ':');
% response time
if sau.gun.ts < Inf
plot(axe, [sau.gun.ts; sau.gun.ts], [0; 1], ...
'Color', [0 0 0], 'LineStyle', ':');
xticks([0, sau.gun.ts]);
end
% overshoot
plot(axe, [0; tend], [1 + sau.OS; 1 + sau.OS], ...
'Color', [1 0 0], 'LineStyle', ':');
% Use compensated as axis for when uncompensated is unstable
[y2,t2] = step(sau.loop.get_tf(), tend);
plot(axe, t2, y2, 'b');
axis auto
ax = axis(axe);
% [y1,t1] = step(feedback(G, 1), tend);
% plot(axe, t1, y1, 'r');
ax(2) = tend(end);
axis(axe, ax);
% step(axe, feedback(G, 1), tplot, 'r'); % uncompensated
% step(axe, feedback(C*G, 1), tplot, 'b'); % compensated
%legend(axe, 'step response', 'uncompensated', 'Location', 'southwest');
legend(axe, 'step response', 'Location', 'southeast');
title(axe, '');
ylabel(axe, '');
%drawnow
end
function update_siso_plot(handles)
sau = handles.props.sau;
axe = handles.siso;
if numel(sau.hist_r) > 1
X=(-numel(sau.hist_r)+1:0)' * sau.T;
handles.props.h_r.plot(axe, X, sau.hist_r, 'Color', [1 0 0]);
handles.props.h_y.plot(axe, X, sau.hist_y, 'Color', [0 0 1]);
% axis(axe, [-sau.max_hist_time 0 -1.05 1.05])
end
% title(axe, '');
% xlabel(axe, '');
% ylabel(axe, '');
% %drawnow
end
function update_rlocus(axe, C, G, info) %#ok
axes(axe);
cla(axe);
hold(axe, 'on');
set(axe, 'DefaultLineMarkerSize', 10);
set(axe, 'DefaultLineLineWidth', 2);
% r-locus:
rlocus(axe, C*G, 'b');
% closed-loop poles:
poles = pole(feedback(C*G, 1)) + 0.000001i;
plot(axe, poles, 'rx'); % maybe faster
%rlocus(axe, feedback(C*G, 1), 'r', 0);
axis auto
% Try to adjust around response time
% if ~isnan(info.SettlingTime)
% ranges = axis(axe);
% ranges(1) = -ceil(4/info.SettlingTime)*1.1;
% ranges(2) = abs(ceil(max(real(poles))))*1.1;
% axis(axe, ranges);
% end
h = zeros(2, 1);
h(1) = plot(NaN,NaN,'xb');
h(2) = plot(NaN,NaN,'xr');
legend(h, 'open-loop C(s)·G(s)', 'closed-loop poles');
title(axe, '');
%drawnow
end
function update_LTI(h)
sdfunc.enable_all(h, false);
h.start.Enable = 'off';
h.compete.Enable = 'off';
h.props.competing = false; % Since we just messed settings up...
h.updating.String = 'Updating.';
h.updating.Visible = 'on';
drawnow
[C, G] = sdfunc.gui_LTI_config(h);
info = stepinfo(feedback(C*G, 1));
if ~isnan(info.SettlingTime) % < sdconst.max_plot_ts
tend = ceil(info.SettlingTime+2);
%tplot=0:0.5:ceil(info.SettlingTime+2);
else
tend = sdconst.max_plot_ts;
end
h.props.sau.update_LTI(h.props.tff, C, G);
sdfunc.update_texts(h, h.props.sau);
h.period.String = sprintf('%.3f', h.props.sau.T);
% May have changed after sau.update_LTI
% TODO: obtain C, G, from sau as it is being used (if discretized)
sdfunc.update_response_plot(h.response, h.props.sau, tend);
h.updating.String = 'Updating..';
sdfunc.update_error_plot(h.error, C, G, tend);
h.updating.String = 'Updating...';
sdfunc.update_rlocus(h.rlocus, C, G, info);
h.updating.Visible = 'off';
drawnow
sdfunc.enable_all(h, ~h.props.competing);
h.start.Enable = sdconst.onoff(~h.props.running || ~h.props.competing);
h.compete.Enable = sdconst.onoff(~h.props.running || h.props.competing);
end
end
end