-
Notifications
You must be signed in to change notification settings - Fork 0
/
do_demo.m
113 lines (91 loc) · 2.33 KB
/
do_demo.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
import casadi.*
T = 10; % Time horizon
N = 20; % number of control intervals
% Declare model variables
x1 = SX.sym('x1');
x2 = SX.sym('x2');
x = [x1; x2];
u = SX.sym('u');
% Model equations
xdot = [(1-x2^2)*x1 - x2 + u; x1];
% Objective term
L = x1^2 + x2^2 + u^2;
% Continuous time dynamics
f = Function('f', {x, u}, {xdot, L});
% Formulate discrete time dynamics
% Fixed step Runge-Kutta 4 integrator
M = 4; % RK4 steps per interval
DT = T/N/M;
X0 = MX.sym('X0', 2);
U = MX.sym('U');
X = X0;
Q = 0;
for j=1:M
[k1, k1_q] = f(X, U);
[k2, k2_q] = f(X + DT/2 * k1, U);
[k3, k3_q] = f(X + DT/2 * k2, U);
[k4, k4_q] = f(X + DT * k3, U);
X=X+DT/6*(k1 +2*k2 +2*k3 +k4);
Q = Q + DT/6*(k1_q + 2*k2_q + 2*k3_q + k4_q);
end
F = Function('F', {X0, U}, {X, Q}, {'x0','p'}, {'xf', 'qf'});
% Start with an empty NLP
w={};
w0 = [];
lbw = [];
ubw = [];
J = 0;
g={};
lbg = [];
ubg = [];
% "Lift" initial conditions
X0 = MX.sym('X0', 2);
w = {w{:}, X0};
lbw = [lbw; 0; 1];
ubw = [ubw; 0; 1];
w0 = [w0; 0; 1];
% Formulate the NLP
Xk = X0;
for k=0:N-1
% New NLP variable for the control
Uk = MX.sym(['U_' num2str(k)]);
w = {w{:}, Uk};
lbw = [lbw; -1];
ubw = [ubw; 1];
w0 = [w0; 0];
% Integrate till the end of the interval
Fk = F('x0', Xk, 'p', Uk);
Xk_end = Fk.xf;
J=J+Fk.qf;
% New NLP variable for state at end of interval
Xk = MX.sym(['X_' num2str(k+1)], 2);
w = {w{:}, Xk};
lbw = [lbw; -0.25; -inf];
ubw = [ubw; inf; inf];
w0 = [w0; 0; 0];
% Add equality constraint
g = {g{:}, Xk_end-Xk};
lbg = [lbg; 0; 0];
ubg = [ubg; 0; 0];
end
% Create an NLP solver
prob = struct('f', J, 'x', vertcat(w{:}), 'g', vertcat(g{:}));
options = struct('ipopt',struct('print_level',0),'print_time',false);
solver = nlpsol('solver', 'ipopt', prob, options);
s0 = MX.sym('s0',2);
lbw_sym = MX(lbw);
ubw_sym = MX(ubw);
lbw_sym(1:2) = s0;
ubw_sym(1:2) = s0;
sol_sym = solver('x0', w0, 'lbx', lbw_sym, 'ubx', ubw_sym,...
'lbg', lbg, 'ubg', ubg);
% Mapping from initial state to control action
function_name = 'f';
f = Function(function_name,{s0},{sol_sym.x(3)});
file_name = 'f.casadi';
f.save(file_name);
lib_path = GlobalOptions.getCasadiPath();
inc_path = GlobalOptions.getCasadiIncludePath();
mex('-v',['-I' inc_path],['-L' lib_path],'-lcasadi', 'casadi_fun.c')
%%
open_system('mpc_demo')