-
Notifications
You must be signed in to change notification settings - Fork 6
/
controller_mpc_1_forces.m
66 lines (51 loc) · 1.78 KB
/
controller_mpc_1_forces.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
% BRIEF:
% Controller function template. Input and output dimension MUST NOT be
% modified.
% INPUT:
% Q: State weighting matrix, dimension (3,3)
% R: Input weighting matrix, dimension (3,3)
% T: Measured system temperatures, dimension (3,1)
% N: MPC horizon length, dimension (1,1)
% OUTPUT:
% p: Heating and cooling power, dimension (3,1)
function p = controller_mpc_1_forces(Q, R, T, N, ~)
% controller variables
persistent param forces_optimizer
% initialize controller, if not done already
if isempty(param)
[param, forces_optimizer] = init(Q, R, N);
end
% evaluate control action by solving MPC problem
[u_mpc,errorcode, ~] = forces_optimizer{T-param.T_sp};
if (errorcode ~= 1)
warning('MPC1 infeasible');
end
p = u_mpc + param.p_sp;
end
function [param, forces_optimizer] = init(Q, R, N)
% get basic controller parameters
param = compute_controller_base_parameters;
% get terminal cost
[~,P,~] = dlqr(param.A, param.B, Q, R);
% implement your MPC using Yalmip2Forces interface here
nx = size(param.A,1);
nu = size(param.B,2);
U = sdpvar(repmat(nu,1,N-1),ones(1,N-1),'full');
X = sdpvar(repmat(nx,1,N),ones(1,N),'full');
%T0 = sdpvar(nx,1,'full');
objective = 0;
constraints = [];
for k = 1:N-1
% assing domain
constraints = [constraints; X{k+1} == param.A*X{k} + param.B*U{k} ];
% input constraints
constraints = [constraints; param.Ucons(:,1) <= U{k} <= param.Ucons(:,2) ];
% state constraints
constraints = [constraints; param.Xcons(:,1) <= X{k+1} <= param.Xcons(:,2)];
% objective
objective = objective + X{k}'*Q*X{k} + U{k}'*R*U{k};
end
objective = objective + X{N}'*P*X{N};
codeoptions = getOptions('MPC_FORCES');
forces_optimizer = optimizerFORCES(constraints, objective, codeoptions, X{1}, U{1}, {'t_init'}, {'p0'});
end