-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.asv
450 lines (371 loc) · 11.5 KB
/
main.asv
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
clc,clear;
% load parameter
[A, B, C, x0] = load_parameter(3, 1, 1, 5);
% design specifications check
D = zeros(3, 2);
t = 0 : 0.1 : 10;
u1 = [ones(size(t,2),1),zeros(size(t,2),1)];
u2 = [zeros(size(t,2),1),ones(size(t,2),1)];
u0 = [zeros(size(t,2),1),zeros(size(t,2),1)];
%% Q1
% pole placement feedback
% Assume that you can measure all the six state variables, design a state feedback controller using
% the pole place method, simulate the designed system and show all the six state responses to non-
% zero initial state with zero external inputs. Discuss effects of the positions of the poles on system
% performance, and also monitor control signal size. In this step, both the disturbance and set point
% can be assumed to be zero.
[K11,K1] = pole_placement(A, B);
%%
% check for K1
% u=[1 0]
ss_q1 = ss(A-B*K1, B, C, D);
[y,t,x]=lsim(ss_q1,u1,t,zeros(6,1));
q1_info = stepinfo(y,t);
figure(1);
subplot(3,1,1);
plot(t,y(:,1));
title({'Output curve with u=[1 0]', ...
['overshoot: ', num2str(q1_info(1).Overshoot),'%'], ...
['2% setting time: ', num2str(q1_info(1).SettlingTime),'s' ]});
xlabel('Time (sec)');
ylabel('Cart Position (m)');
subplot(3,1,2);
plot(t,y(:,2));
title({'Output curve with u=[1 0]', ...
['overshoot: ', num2str(q1_info(2).Overshoot),'%'], ...
['2% setting time: ', num2str(q1_info(2).SettlingTime),'s' ]});
xlabel('Time (sec)');
ylabel('Handle Angle (rad)');
subplot(3,1,3);
plot(t,y(:,3));
title({'Output curve with u=[1 0]', ...
['overshoot: ', num2str(q1_info(3).Overshoot),'%'], ...
['2% setting time: ', num2str(q1_info(3).SettlingTime),'s' ]});
xlabel('Time (sec)');
ylabel('Bike Angle (rad)');
% u=[0 1]
[y,t,x]=lsim(ss_q1,u2,t,zeros(6,1));
q1_info = stepinfo(y,t);
figure(2);
subplot(3,1,1);
plot(t,y(:,1));
title({'Output curve with u=[0 1]', ...
['overshoot: ', num2str(q1_info(1).Overshoot),'%'], ...
['2% setting time: ', num2str(q1_info(1).SettlingTime),'s' ]});
xlabel('Time (sec)');
ylabel('Cart Position (m)');
subplot(3,1,2);
plot(t,y(:,2));
title({'Output curve with u=[0 1]', ...
['overshoot: ', num2str(q1_info(2).Overshoot),'%'], ...
['2% setting time: ', num2str(q1_info(2).SettlingTime),'s' ]});
xlabel('Time (sec)');
ylabel('Handle Angle (rad)');
subplot(3,1,3);
plot(t,y(:,3));
title({'Output curve with u=[0 1]', ...
['overshoot: ', num2str(q1_info(3).Overshoot),'%'], ...
['2% setting time: ', num2str(q1_info(3).SettlingTime),'s' ]});
xlabel('Time (sec)');
ylabel('Bike Angle (rad)');
%%
% check for K11
% C2=[1 0 0 0 0 0; 0 1 0 0 0 0];
% D2=[0 0; 0 0];
% F=inv(C2/(B*K11-A)*B);
% ss_q2 = ss(A-B*K11, B*F, C2, D2);
ss_q1 = ss(A-B*K11, B, C, D);
% u=[1 0]
[y,t,x]=lsim(ss_q1,u1,t,zeros(6,1));
% [y,t,x]=lsim(ss_q2,u0,t,x0);
q1_info = stepinfo(y,t);
figure(3);
subplot(3,1,1);
plot(t,y(:,1));
title({'Output curve with u=[0 1]', ...
['overshoot: ', num2str(q1_info(1).Overshoot),'%'], ...
['2% setting time: ', num2str(q1_info(1).SettlingTime),'s' ]});
xlabel('Time (sec)');
ylabel('Cart Position (m)');
subplot(3,1,2);
plot(t,y(:,2));
title({'Output curve with u=[0 1]', ...
['overshoot: ', num2str(q1_info(2).Overshoot),'%'], ...
['2% setting time: ', num2str(q1_info(2).SettlingTime),'s' ]});
xlabel('Time (sec)');
ylabel('Handle Angle (rad)');
subplot(3,1,3);
plot(t,y(:,3));
title({'Output curve with u=[0 1]', ...
['overshoot: ', num2str(q1_info(3).Overshoot),'%'], ...
['2% setting time: ', num2str(q1_info(3).SettlingTime),'s' ]});
xlabel('Time (sec)');
ylabel('Bike Angle (rad)');
% u=[0 1]
[y,t,x]=lsim(ss_q1,u2,t,zeros(6,1));
% [y,t,x]=lsim(ss_q2,u0,t,x0);
q1_info = stepinfo(y,t);
figure(4);
subplot(3,1,1);
plot(t,y(:,1));
title({'Output curve with u=[0 1]', ...
['overshoot: ', num2str(q1_info(1).Overshoot),'%'], ...
['2% setting time: ', num2str(q1_info(1).SettlingTime),'s' ]});
xlabel('Time (sec)');
ylabel('Cart Position (m)');
subplot(3,1,2);
plot(t,y(:,2));
title({'Output curve with u=[0 1]', ...
['overshoot: ', num2str(q1_info(2).Overshoot),'%'], ...
['2% setting time: ', num2str(q1_info(2).SettlingTime),'s' ]});
xlabel('Time (sec)');
ylabel('Handle Angle (rad)');
subplot(3,1,3);
plot(t,y(:,3));
title({'Output curve with u=[0 1]', ...
['overshoot: ', num2str(q1_info(3).Overshoot),'%'], ...
['2% setting time: ', num2str(q1_info(3).SettlingTime),'s' ]});
xlabel('Time (sec)');
ylabel('Bike Angle (rad)');
% task 1 check
% 6 state responses to x0 with zero inputs
[y,t,x]=lsim(ss_q1,u0,t,x0);
q1_info = stepinfo(x,t);
figure(5);
for i = 1:6
subplot(3,2,i);
plot(t,x(:,i));
title(['Response for x',num2str(i)]);
xlabel('Time (sec)');
ylabel(['x',num2str(i)]);
% hold on;
% legend_str{i} = ['overshoot: ', num2str(q1_info(i).Overshoot),'%, ', ...
% '2% setting time: ', num2str(q1_info(i).SettlingTime),'s'];
end
% legend(legend_str);
sgtitle('Six state responses with zero inputs');
% monitor the control signal size
u=-x*K11';
figure(6);
for i = 1:2
subplot(2,1,i);
plot(t,u(:,i));
title(['Control signal u',num2str(i)]);
xlabel('Time (sec)');
ylabel(['u',num2str(i)]);
end
% legend(legend_str);
sgtitle('Control signal size');
%% Q2
% LQR control
% Assume that you can measure all the six state variables, design a state feedback controller using
% the LQR method, simulate the designed system and show all the state responses to non-zero
% initial state with zero external inputs. Discuss effects of weightings Q and R on system
% performance, and also monitor control signal size. In this step, both the disturbance and set point
% can be assumed to be zero.
K2 = lqr_control(A, B);
% check
ss_q2 = ss(A-B*K2, B, C, D);
% u=[1 0]
[y, t, x] = lsim(ss_q2, u1, t, zeros(6,1));
q2_info = stepinfo(y,t);
figure(7);
subplot(3,1,1);
plot(t,y(:,1));
title({'Output curve with u=[1 0]', ...
['overshoot: ', num2str(q2_info(1).Overshoot),'%'], ...
['2% setting time: ', num2str(q2_info(1).SettlingTime),'s' ]});
xlabel('Time (sec)');
ylabel('Cart Position (m)');
subplot(3,1,2);
plot(t,y(:,2));
title({'Output curve with u=[1 0]', ...
['overshoot: ', num2str(q2_info(2).Overshoot),'%'], ...
['2% setting time: ', num2str(q2_info(2).SettlingTime),'s' ]});
xlabel('Time (sec)');
ylabel('Handle Angle (rad)');
subplot(3,1,3);
plot(t,y(:,3));
title({'Output curve with u=[1 0]', ...
['overshoot: ', num2str(q2_info(3).Overshoot),'%'], ...
['2% setting time: ', num2str(q2_info(3).SettlingTime),'s' ]});
xlabel('Time (sec)');
ylabel('Bike Angle (rad)');
% u=[0 1]
[y, t, x] = lsim(ss_q2, u2, t, zeros(6,1));
q2_info = stepinfo(y,t);
figure(8);
subplot(3,1,1);
plot(t,y(:,1));
title({'Output curve with u=[0 1]', ...
['overshoot: ', num2str(q2_info(1).Overshoot),'%'], ...
['2% setting time: ', num2str(q2_info(1).SettlingTime),'s' ]});
xlabel('Time (sec)');
ylabel('Cart Position (m)');
subplot(3,1,2);
plot(t,y(:,2));
title({'Output curve with u=[0 1]', ...
['overshoot: ', num2str(q2_info(2).Overshoot),'%'], ...
['2% setting time: ', num2str(q2_info(2).SettlingTime),'s' ]});
xlabel('Time (sec)');
ylabel('Handle Angle (rad)');
subplot(3,1,3);
plot(t,y(:,3));
title({'Output curve with u=[0 1]', ...
['overshoot: ', num2str(q2_info(3).Overshoot),'%'], ...
['2% setting time: ', num2str(q2_info(3).SettlingTime),'s' ]});
xlabel('Time (sec)');
ylabel('Bike Angle (rad)');
% task 2 check
% monitor control signal size
u=-x*K2';
figure(9);
for i = 1:2
subplot(2,1,i);
plot(t,u(:,i));
title(['Control signal u',num2str(i)]);
xlabel('Time (sec)');
ylabel(['u',num2str(i)]);
end
% legend(legend_str);
sgtitle('Control signal size');
%% Q3
% Assume you can only measure the three outputs.
% Design a state observer, simulate the resultant observer-based LQR control system,
% monitor the state estimation error, investigate effects of observer poles
% on state estimation error and closed-loop control performance. In this step, both
% the disturbance and set point can be assumed to be zero. (10 points)
%% Q4
% Design a decoupling controller with closed-loop stability and simulate the
% step set point response of the resultant control system to verify
% decoupling performance with stability. In this step, the disturbance can be
% assumed to be zero. Is the decoupled system internally stable?
% Please provide both the step (transient) response with zero initial states
% and the initial response with respect to x0 of the decoupled system
% to support your conclusion.
C2=[1 0 0 0 0 0; 0 0 1 0 0 0];
D2=[0 0;0 0];
% 1. state feedback
[K4,F4]=decoupler_sf(A,B,C2);
% check
ss_q4 = ss(A-B*K4, B*F4, C2, D2);
% u=[1 0]
[y, t, x] = lsim(ss_q4, u1, t, zeros(6,1));
q4_info = stepinfo(y,t);
figure(10);
for i = 1:2
plot(t,y(:,i));
if i==1
legend_str{i} = ['y1: overshoot: ', num2str(q4_info(i).Overshoot),'%, ', ...
'2% setting time: ', num2str(q4_info(i).SettlingTime),'s'];
else
legend_str{i} = 'y2';
end
hold on;
end
legend(legend_str);
xlabel('Time (sec)');
ylabel('y');
sgtitle('Output response with input u=[1 0]');
% u=[1 0]
[y, t, x] = lsim(ss_q4, u2, t, zeros(6,1));
q4_info = stepinfo(y,t);
figure(11);
for i = 1:2
plot(t,y(:,i));
if i==2
legend_str{i} = ['y2: overshoot: ', num2str(q4_info(i).Overshoot),'%, ', ...
'2% setting time: ', num2str(q4_info(i).SettlingTime),'s'];
else
legend_str{i} = 'y1';
end
hold on;
end
legend(legend_str);
xlabel('Time (sec)');
ylabel('y');
sgtitle('Output response with input u=[0 1]');
%% task 4 check
% check if external stable (zeros initial states / x0)
% x0
% u1=[1 0]
[y, t, x] = lsim(ss_q4, u1, t, x0);
figure(12);
for i = 1:2
plot(t,y(:,i));
legend_str{i} = ['y',num2str(i)];
hold on;
end
legend(legend_str);
xlabel('Time (sec)');
ylabel('y');
sgtitle('Output response with input u=[1 0]');
% u2=[0 1]
[y, t, x] = lsim(ss_q4, u2, t, x0);
q4_info = stepinfo(y,t);
figure(13);
for i = 1:2
plot(t,y(:,i));
legend_str{i} = ['y',num2str(i)];
hold on;
end
legend(legend_str);
xlabel('Time (sec)');
ylabel('y');
sgtitle('Output response with input u=[0 1]');
% % check if external stable
% syms s;
% H4 = C2/(s*eye(6)-A+B*K4)*B*F4;
%
% [numerator,denominator]=numden(H4);
% n11=double(coeffs(numerator(1,1)));
% n22=double(coeffs(numerator(2,2)));
% d11=double(coeffs(denominator(1,1)));
% d22=double(coeffs(denominator(2,2)));
% G11=tf(n11,d11);
% G22=tf(n22,d22);
% figure(12);
% subplot(2,1,1);
% step(G11);
% subplot(2,1,2);
% step(G22);
% check if internally stable
p=pole(ss_q4);
for i=1:size(p)
if real(p(i))>0
disp(['Find pole with negative part:',num2str(p(i))]);
end
end
% also plot x to check internal instability
[y,t,x]=lsim(ss_q4, u1, t, x0);
figure(14);
for i=1:6
subplot(3,2,i);
plot(t,x(:,i));
title(['State x',num2str(i)]);
xlabel('Time (sec)');
ylabel(['x',num2str(i)]);
end
sgtitle('Response of x with input u=[1 0] and initial state x0')
% 这里状态应该是看不到的,做完第三题再回头看
%%
% 不知道输出反馈能否使内部稳定,做完第三题再看
% % 2. output feedback
% [Kd,Ks,H]=decoupler_of(A,B,C2);
%
%
% [K41,K411] = pole_placement(A-B*Kd, B);
%
% [numerator,denominator]=numden(H);
% n11=double(coeffs(numerator(1,1)));
% n12=double(coeffs(numerator(1,2)));
% n21=double(coeffs(numerator(2,1)));
% n22=double(coeffs(numerator(2,2)));
% d11=double(coeffs(denominator(1,1)));
% d12=double(coeffs(denominator(1,2)));
% d21=double(coeffs(denominator(2,1)));
% d22=double(coeffs(denominator(2,2)));
%
% G11=tf(n11,d11);
% step(G11);
%% Q5