This repository has been archived by the owner on Jul 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathDCLFDCBF.m
105 lines (101 loc) · 3.82 KB
/
DCLFDCBF.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
classdef DCLFDCBF < handle
properties
system
params
x0
x_curr
time_curr = 0.0
xlog = []
ulog = []
u_cost = 0
obs
end
methods
function self = DCLFDCBF(x0, system, params)
% Define DCLF-DCBF controller
self.x0 = x0;
self.x_curr = x0;
self.system = system;
self.params = params;
end
function sim(self, time)
% Simulate the system until a given time
xk = self.x_curr;
while self.time_curr <= time
% Solve DCLF-DCBF
uk = self.solveDCLFDCBF();
xk = self.system.A * xk + self.system.B * uk;
% update system
self.x_curr = xk;
self.time_curr = self.time_curr + self.system.dt;
self.xlog = [self.xlog, xk];
self.ulog = [self.ulog, uk];
self.u_cost = self.u_cost + uk'*uk*self.system.dt;
end
end
function uopt = solveDCLFDCBF(self)
% Solve DCLF-DCBF
x = self.x_curr;
u = sdpvar(2,1);
delta = sdpvar(1,1);
constraints = [];
% add DCLF constraints
x_next = self.system.A * x + self.system.B * u;
v = x' * self.params.P * x;
v_next = x_next' * self.params.P * x_next;
constraints = [constraints; v_next - v + self.params.alpha * v <= delta];
% add DCBF constraints
pos = self.obs.pos;
r = self.obs.r;
b = (x(1:2)-pos)'*(x(1:2)-pos) - r^2;
b_next = (x_next(1:2)-pos)'*(x_next(1:2)-pos) - r^2;
constraints = [constraints; b_next - b + self.params.gamma*b >= 0];
% input constraints
constraints = [constraints; self.system.ul <= u <= self.system.uu];
% cost
cost = u'*self.params.H*u + delta'*self.params.p*delta;
% solve optimization
ops = sdpsettings('solver','ipopt','verbose',0);
diagnostics = optimize(constraints, cost, ops);
if diagnostics.problem == 0
feas = true;
uopt = value(u);
else
feas = false;
uopt = [];
end
fprintf('solver time: %f\n', diagnostics.solvertime);
end
function plot(self,figure_name)
% Plot simulation
figure('Renderer', 'painters', 'Position', [0 0 400 400]);
set(gca,'LooseInset',get(gca,'TightInset'));
hold on;
% plot trajectory
plot(self.xlog(1,:), self.xlog(2,:), 'ko-',...
'LineWidth', 1.0,'MarkerSize',4);
% plot obstacle
pos = self.obs.pos;
r = self.obs.r;
th = linspace(0,2*pi*100);
x = cos(th) ; y = sin(th) ;
plot(pos(1) + r*x, pos(2) + r*y, 'Color', [0.8500, 0.3250, 0.0980], 'LineWidth', 2);
plot(pos(1), pos(2), 'Color', [0.8500, 0.3250, 0.0980], 'MarkerSize', 5, 'LineWidth', 2);
% plot target position
plot(self.x0(1), self.x0(2), 'db', 'LineWidth', 1);
plot(0.0, 0.0, 'dr', 'LineWidth', 1);
h=get(gca,'Children');
h_legend = legend(h([end]), {'DCLF-DCBF'}, 'Location', 'SouthEast');
set(h_legend, 'Interpreter','latex');
set(gca,'LineWidth', 0.2, 'FontSize', 15);
grid on
xlabel('$x (m)$','interpreter','latex','FontSize',20);
ylabel('$y (m)$','interpreter','latex','FontSize',20);
xticks(-5:0);
yticks(-5:0);
xlim([-5,0.2]);
ylim([-5,0.2]);
print(gcf,strcat('figures/',figure_name), '-depsc');
end
end
end