summaryrefslogtreecommitdiffstats
path: root/templates
diff options
context:
space:
mode:
authorNao Pross <np@0hm.ch>2023-05-12 14:04:50 +0200
committerNao Pross <np@0hm.ch>2023-05-12 14:05:40 +0200
commitae60d0f998ab9d2156698383759c1038175a71cf (patch)
tree26b2eaa548853876406a9c8047cbaf37aef448e7 /templates
parentADD: lqr_tuning_script m and mat file, pass task 11 (diff)
downloadmpc_pe-ae60d0f998ab9d2156698383759c1038175a71cf.tar.gz
mpc_pe-ae60d0f998ab9d2156698383759c1038175a71cf.zip
ADD: Implement MPC_TE, pass 18
Diffstat (limited to 'templates')
-rw-r--r--templates/MPC_TE.m28
1 files changed, 27 insertions, 1 deletions
diff --git a/templates/MPC_TE.m b/templates/MPC_TE.m
index e0b55d2..6c300b8 100644
--- a/templates/MPC_TE.m
+++ b/templates/MPC_TE.m
@@ -15,7 +15,33 @@ classdef MPC_TE
function obj = MPC_TE(Q,R,N,params)
% YOUR CODE HERE
opts = sdpsettings('verbose',1,'solver','quadprog');
- obj.yalmip_optimizer = optimizer(constraints,objective,opts,X0,{U{1} objective});
+
+ % Create yalmip variables
+ U = sdpvar(repmat(params.model.nu,N,1),ones(1,N));
+ X = sdpvar(repmat(params.model.nx,N+1,1),ones(1,N+1));
+
+ % Build cost function
+ objective = 0;
+ constraints = [];
+ for k=1:N+1
+ if k <= N
+ % These constraints are for the index in 1, ..., N
+ objective = objective + X{k}' * Q * X{k} + U{k}' * R * U{k};
+ constraints = [constraints, ...
+ X{k+1} == params.model.A * X{k} + params.model.B * U{k}, ...
+ params.constraints.InputMatrix * U{k} <= params.constraints.InputRHS, ...
+ ];
+ end
+
+ % These contrains are for 1, ..., N+1
+ constraints = [constraints, ...
+ params.constraints.StateMatrix * X{k} <= params.constraints.StateRHS, ...
+ ];
+ end
+ % Terminal constraint
+ constraints = [constraints, X{N+1} == zeros(size(X{N+1}))];
+
+ obj.yalmip_optimizer = optimizer(constraints,objective,opts,X{1},{U{1} objective});
end
function [u, ctrl_info] = eval(obj,x)