-
Notifications
You must be signed in to change notification settings - Fork 17
/
Jankovic_CLF_CBF_QP.m
98 lines (80 loc) · 2.27 KB
/
Jankovic_CLF_CBF_QP.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
% JANKOVIC's MODIFIED CLF - CBF OPTIMIZATION PROBLEM
% Uses CPLEX to solve
% minimize 0.5*u'*H*u + k*delta'*delta
% s. t. LfV(x) + LgV(x)u <= -gamma(V(x)) - LgV(x)*delta
% Lfh(x) + Lgh(x)u >= -alpha(h(x))
%
% Sample use:
% u = Jankovic_CLF_CBF_QP(x,Q,q,r);
% [u, lambda] = Jankovic_CLF_CBF_QP(x,Q,q,r);
%
% INPUT: Elliptical obstacle parameters Q, q, r
% Current state x
% OUTPUT: Optimal control input for above QP u
% Correspdonding dual variables lambda
%[u(i,:) lambda] = Jankovic_CLF_CBF_QP(x(i-1,:),Q,q,r);
function [optimal_u, dualvars, delta] = Jankovic_CLF_CBF_QP(x,Q,q,r)
sysdim = size(x,2);
k = 1.0;
H = blkdiag(eye(sysdim), eye(sysdim)*k); % construct H
Aineq = [LgV(x) LgV(x);
-Lgh(x,Q,q,r) zeros(1,sysdim)]; % construct Aineq
bineq = [-jankovic_gamma(V(x)+LfV(x));
Lfh(x,Q,q,r)+gain_alpha(h(x,Q,q,r))]; % construct bineq
f = zeros(1,sysdim*2); % construct f (zero)
[u_opt, fval, exitflag, output, lambda] = cplexqp(H,f,Aineq,bineq);
dualvars = lambda.ineqlin;
delta = u_opt(sysdim+1:end);
optimal_u = u_opt(1:sysdim);
end
% Function defines PD matrix P for Lyapunov function
% V(x) = x'*P*x
function P = get_lyapunov_P()
P = diag([1 1]);
end
% Lyapunov function for system
% V(x) = x'*x
function lyapunov_V = V(x)
P = get_lyapunov_P();
lyapunov_V = x*P*x';
end
% VECTOR FUNCTIONS INPUT AND RETURN A ROW VECTOR
% Lie derivative of Lyapunov function along f
function Lie_fV = LfV(x)
Lie_fV = 0;
end
% Lie derivative of Lyapunov function along g
function Lie_gV = LgV(x)
P = get_lyapunov_P();
Lie_gV = 2*x*P;
end
% Safety function for forward invariance outside ellipse
function safety_h = h(x,Q,q,r)
safety_h = (x-q)*Q*(x-q)'-r^2;
end
function grad_h = h_gradient(x,Q,q,r)
end
% Lie derivative of safety function along f(x)
function Lie_fh = Lfh(x,Q,q,r)
Lie_fh = 0;
end
% Lie derivative of safety function along g(x)
function Lie_gh = Lgh(x,Q,q,r)
Lie_gh = 2*(x-q)*Q;
end
% Class K-infinity function alpha
% Characterizes gain on safety inequality
function a = gain_alpha(x)
a = x;
end
% Gain function gamma defined in Jankovic (2018)
% applies a positive, >= 1 gain if argument is positive
% applies no gain otherwise
function gamma = jankovic_gamma(x)
g = 1.0; % gain on positive inputs
if (x < 0)
gamma = x;
else
gamma = g*x;
end
end