-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.m
213 lines (167 loc) · 5.8 KB
/
main.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
clear all; close all; clc
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% IC's and simulation parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
delta_t = 0.0005;
t_start = 0;
t_end = 20;
t = t_start: delta_t: t_end;
V_dot_target_initial = -10;
% Add disturbance?
%d = 2*(0.5-rand());% Random # between -1 and 1
d = 1;
%d = 0;
% Alpha's are one type of 'gain' for the SMC
alpha0 = 1;
alpha1 = 1;
% Eta is the other 'gain' for the SMC
eta = 1.1;
% For crude FBL
Kp = -10;
% Saturation
apply_saturation = true;
u_min = -2;
u_max = 2;
x_IC = [1 1 -1];
% Pre-allocate
x_OL = zeros(length(t), 3);
x_OL(1,:) = x_IC;
y_OL = zeros(length(t), 1);
y_OL(1) = x_IC(2); % y = x2
x_CL = zeros(length(t), 3);
x_CL(1,:) = x_IC;
y_CL = zeros(length(t),1 );
y_CL(1) = x_IC(2); % y = x2
V = zeros(length(t), 1);
V(1) = 1;
u_lyap = zeros(length(t),1 );
u_robust = zeros(length(t),1 );
for epoch = 2: length(t)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Put the system in normal form, i.e. calculate xi
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
r = 2; % Relative order of the system
dh_dx = [0 1 0]; % 1x3
f = [ -x_CL(epoch-1,1);
x_CL(epoch-1,3);
x_CL(epoch-1,1)*x_CL(epoch-1,3) ];
% 3x1
Lf_h = dh_dx * f; % scalar
dLf_h_dx = [0 0 1]; % 1x3
Lf_2_h = dLf_h_dx*f; % scalar
g = [(2+x_CL(epoch-1,3)^2)/(1+x_CL(epoch-1,3)^2); 0; 1]; % 3x1
Lg_Lf_h = dLf_h_dx * g; % scalar, dLf_h/dx*g
xi(1) = x_CL(epoch-1,2); % xi(1) = h(x) = x2
xi(2) = Lf_h;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Calculate u_lyap with the switched Lyapunov algorithm
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Calculate dV1_dot_du
dV1_dot_du = xi(r)*Lg_Lf_h;
% Calculate dV2_dot_du
dV2_dot_du = (xi(r)*(0.9+0.1*abs(xi(r)-1)) + 0.1*V(epoch-1)*sign( xi(r)-1 ) )*Lg_Lf_h;
% Calculate V_dot_target
V_dot_target = (V(epoch-1)/V(1))^2*V_dot_target_initial;
% Compare dV1_dot_du and dV2_dot_du to choose the CLF
dV_dot_du(epoch,:) = [dV1_dot_du dV2_dot_du];
[M,I] = max(abs(dV_dot_du));
% Calculate u_lyap with the CLF of choice
if ( I==1 ) % use V1
using_V1(epoch) = y_CL(epoch);
u_lyap(epoch) = (V_dot_target - xi(1)*Lf_h - xi(r)*Lf_2_h) /...
dV1_dot_du;
else %use V2
using_V2(epoch) = y_CL(epoch);
u_lyap(epoch) = (V_dot_target -...
xi(1)*(0.9+0.1*abs(xi(r)-1))*Lf_h-... % for xi(1)
(xi(r)*(0.9+0.1*abs(xi(r)-1) )+0.1*V(epoch-1)*sign(xi(r)-1))*Lf_2_h )/... % for xi(r)
dV2_dot_du;
end
if (apply_saturation)
% Apply saturation
if (u_lyap(epoch) > u_max)
u_lyap(epoch) = u_max;
end
if (u_lyap(epoch) < u_min)
u_lyap(epoch) = u_min;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Calculate u_robust with crude FBL, then proportional
% control
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
u_robust(epoch) = -x_CL(epoch-1,2)*x_CL(epoch-1,3) +Kp * xi(1);
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % Calculate u_robust with the SMC algorithm
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% % Calculate omega (the surface)
%
% dxi1_dt = xi(2); % xi(1)_dot = xi(2)
% %dxi2_dt = 0; % TO DO - filter to calculate this
%
% omega = +alpha0*xi(1)+alpha1*dxi1_dt;
% %omega(2) = -alpha0*xi(2)-alpha1*dxi2_dt;
%
% % Calculate u_robust
% u_robust(epoch) = -eta*sign(omega);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Apply (u = u_lyap + u_robust) to the system and simulate for one time step
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
xy = simulate_sys( x_CL(epoch-1,:), y_CL(epoch-1), u_lyap(epoch), delta_t);
x_CL(epoch,:) = xy(1:3); % First 3 elements are x
y_CL(epoch) = xy(end); % Final element is y
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Simulate the open-loop system
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
xy = simulate_sys( x_OL(epoch-1,:), y_OL(epoch-1), 0, delta_t);
x_OL(epoch,:) = xy(1:3); % First 3 elements are x
y_OL(epoch) = xy(end); % Final element is y
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Update
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
V(epoch) = 0.5*(xi(1)^2+xi(2)^2);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Plot
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
set(gcf,'color','w');
subplot(2,2,1)
plot(t, x_OL)
legend('x_1','x_2','x_3','location','NorthWest')
xlabel('Time [s]')
ylabel('x')
title('x: Open Loop')
subplot(2,2,2)
plot(t, y_OL)
xlabel('Time [s]')
ylabel('y')
title('y: Open Loop')
subplot(2,2,3)
plot(t, x_CL)
legend('x_1','x_2','x_3','location','NorthWest')
xlabel('Time [s]')
ylabel('x')
%title('x: Closed Loop')
subplot(2,2,4)
plot(t, y_CL)
xlabel('Time [s]')
ylabel('y')
%title('y: Closed Loop')
figure
set(gcf,'color','w');
subplot(2,1,1)
plot(t,u_lyap,'o')
xlabel('Time [s]')
ylabel('u_l_y_a_p')
title('u_l_y_a_p: Lyapunov control effort to stabilize the nominal dynamics')
subplot(2,1,2)
plot(t,u_robust,'o')
xlabel('Time [s]')
ylabel('u_robust')
title('u_robust: SMC control effort for robustness')
figure
set(gcf,'color','w');
plot(t, V)
xlabel('Time [s]')
ylabel('V(xi)')