-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMPC_Control.m
62 lines (55 loc) · 1.64 KB
/
MPC_Control.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
classdef MPC_Control
properties
ctrl_opt % YALMIP object to compute control law
target_opt % YALMIP object to compute steady-state target
% Discrete-time system matrices
A, B, C, D
end
methods
function mpc = MPC_Control(sys, Ts)
% Discretize the system and extract the A,B,C,D matrices
sys_d = c2d(sys, Ts);
[mpc.A,mpc.B,mpc.C,mpc.D] = ssdata(sys_d);
mpc.target_opt = mpc.setup_steady_state_target();
mpc.ctrl_opt = mpc.setup_controller();
end
% Compute the MPC controller
function u = get_u(mpc, x, ref)
% Compute the target
if nargin < 3
ref_x = zeros(size(mpc.A,1),1);
ref_u = 0;
else
if length(struct(mpc.target_opt).diminOrig) == 2
if length(x) == 3
d_est = x(end);
else
d_est = 0;
end
[target, isfeasible] = mpc.target_opt(ref, d_est);
else
[target, isfeasible] = mpc.target_opt(ref);
end
[ref_x, ref_u] = deal(target{:});
assert(isfeasible==0, 'isfeasible in target computationn');
end
% Compute the control action
if length(struct(mpc.ctrl_opt).diminOrig) == 4
if length(x) == 3
d_est = x(end);
x = x(1:end-1);
else
d_est = 0;
end
[u, isfeasible] = mpc.ctrl_opt({x, ref_x, ref_u, d_est});
else
[u, isfeasible] = mpc.ctrl_opt({x, ref_x, ref_u});
end
assert(isfeasible==0, 'isfeasible in control computationn');
end
end
methods (Abstract)
setup_controller(mpc)
setup_steady_state_target(mpc)
end
end