-
Notifications
You must be signed in to change notification settings - Fork 6
/
controller_mpc_2.m
67 lines (53 loc) · 1.95 KB
/
controller_mpc_2.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
% 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_2(Q, R, T, N, ~)
%N = 30;
% controller variables
persistent param yalmip_optimizer
% initialize controller, if not done already
if isempty(param)
[param, yalmip_optimizer] = init(Q, R, N);
end
% evaluate control action by solving MPC problem
[u_mpc,errorcode] = yalmip_optimizer(T-param.T_sp);
if (errorcode ~= 0)
warning('MPC2 infeasible');
end
p = u_mpc + param.p_sp;
end
function [param, yalmip_optimizer] = init(Q, R, N)
% get basic controller parameters
param = compute_controller_base_parameters;
% implement your MPC using Yalmip here
%https://www.mpt3.org/Main/CustomMPC
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 = [ X{1} == T0];
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 (updating the cost function bu summing up stage costs)
objective = objective + X{k}'*Q*X{k} + U{k}'*R*U{k};
end
% terminal set
constraints = [constraints, X{end}==zeros(3,1)];
objective = objective;
ops = sdpsettings('verbose',0,'solver','quadprog');
yalmip_optimizer = optimizer(constraints,objective,ops,T0,U{1});
end