-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdcdesigner.m
78 lines (59 loc) · 1.37 KB
/
dcdesigner.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
% Digital Control Designer
function tss = dcdesigner(poles)
% Inputs:
% P is in continous-time state-space
% (Discrete time) Poles in the form of [-0.2+0.1622i -0.2+0.1622i]
%% Parameters
b_plot = false;
global Ts
global U_MAX
global p
% LTI simulator parameters
x0 = [1; 0];
Ns = 5000;
dt = 0.001;
%% Pole placement design (to solve K)
% control system (continous)
A = p.A; B = p.B; C = p.C; D = p.D;
K = place(A, B, poles);
% control system (discrete)
pd = c2d(p, Ts, 'tustin');
Ad = pd.A; Bd = pd.B; Cd = pd.C; Dd = pd.D;
poles_d = exp(poles.*Ts); % poles in z-domain
Kd = place(Ad, Bd, poles_d);
Acl = Ad - Bd * Kd;
% check closed-loop stability
Acl_eig = eig(Acl);
% if (norm(Acl_eig(1),2) >= 1)
% disp("[Error] Unstable system!")
% else
% disp("System is stable.")
% end
%% Run LTI simulation
[t, x, u] = ltisim(x0, A, B, Kd, Ns, dt, Ts);
%% Plot results
if b_plot
% plot states
subplot(2,1,1);
for i = 1:size(x,2)
stairs(t, x(:,i));
hold on;
end
ylabel("States: x_k")
xlabel("t")
% plot inputs
subplot(2,1,2);
for i = 1:size(u,2)
stairs(t, u(:,i));
hold on;
end
ylabel("Intpus: u_k")
xlabel("t")
end
% Settling_Time
pi = stepinfo(x(:,1), t, 0.0, 'SettlingTimeThreshold',0.05);
tss = pi.SettlingTime;
if (tss > (Ns*0.95)*dt || max(abs(u)) > U_MAX || isnan(tss))
tss = 100;
end
end