forked from Monteeee/LQR_Trees_demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mass_test.m
115 lines (73 loc) · 1.76 KB
/
mass_test.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
controlRate = robotics.Rate(10);
%% system dynamics
x = sym('x', [2 1]);
u = sym('u', [1 1]);
x_ = sym('x_', [2 1]);
u_ = sym('u_', [1 1]);
m = 1;
k = 0.5;
f_original = [ x(2) ; -k/m * x(1) + 1/m * u(1)];
init = [3;0];
l = init;
A = [0 1; -k/m 0];
B = [0 ; 1/m];
F = 0;
dt = 1.0/10;
recordl = zeros(100, 1);
recordv = zeros(100, 1);
%% LQR controller
eq_x = [2.0; 0];
eq_u = [eq_x(1)*k];
[A_lin, B_lin] = sym_linearization(f_original, x, u, eq_x, eq_u);
Q = [ 5 0 ;
0 5 ];
R = [ 1 ];
[K, S, ~] = lqr(A_lin, B_lin, Q, R);
%% simulation
for i=1:100
delta = l - eq_x;
F = - K * delta;
dl = A * l + B .* F;
recordl(i) = l(1);
recordv(i) = l(2);
waitfor(controlRate);
l = l + dl.*dt;
end
plot([1:100], recordl)
%% SOS verification
mypi = 3.14159265358;
% tolerance
epsilon = 0.0001;
% initial value of rho
rho = 10000.0;
% define f_hat
% closed loop function
f_cl = toCL(f_original, x, u, eq_x, eq_u, K);
% taylor expansion
f_cl_t = symbolic_Taylor(f_cl, x, eq_x);
for i=1:length(f_cl_t)
[C, T] = coeffs(f_cl_t(i));
C(abs(C) < 1e-7) = 0;
f_cl_t(i) = dot(C, T);
end
f_hat = vpa(f_cl_t, 5)
% define dJ*_hat(x_)
dJ = 2 .* (x_.') * S * f_hat;
% define J*(x_)
J = (x_.') * S * x_;
% define norm part
nor22 = epsilon * ( x_(1)^2 + x_(2)^2 );
% ----------- replace x_ with px ------------
% dJ = subs(dJ, x_(i), px(i));
% J = subs(J, x_(i), px(i));
% nor22 = subs(nor22, x_(i), px(i));
% define a sos program
Program1 = sosprogram(x_);
% define h(x_) as sums of squares
[Program1, h] = sossosvar(Program1, x_);
% add inequality constraint
Program1 = sosineq(Program1, -dJ - h*(rho - J) - nor22);
% set solver option
Program1 = sossolve(Program1);
SOLV = sosgetsol(Program1, h)
% OUTPUT IS REASONABLE