diff options
author | Nao Pross <np@0hm.ch> | 2023-05-12 21:40:25 +0200 |
---|---|---|
committer | Nao Pross <np@0hm.ch> | 2023-05-12 21:40:25 +0200 |
commit | 5c569a1f21abaf5c2c5654c9df07debf3943dd44 (patch) | |
tree | 838f91f7b1ab3e449db81c048394a2a0396fd32f /templates/MPC_TS.m | |
parent | FIX: comment (diff) | |
download | mpc_pe-5c569a1f21abaf5c2c5654c9df07debf3943dd44.tar.gz mpc_pe-5c569a1f21abaf5c2c5654c9df07debf3943dd44.zip |
ADD: Add MPC_TS pass task 20
Diffstat (limited to 'templates/MPC_TS.m')
-rw-r--r-- | templates/MPC_TS.m | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/templates/MPC_TS.m b/templates/MPC_TS.m index 05f92ff..cbe7b45 100644 --- a/templates/MPC_TS.m +++ b/templates/MPC_TS.m @@ -15,8 +15,34 @@ classdef MPC_TS function obj = MPC_TS(Q,R,N,H,h,params) % YOUR CODE HERE opts = sdpsettings('verbose',1,'solver','quadprog'); - obj.yalmip_optimizer = optimizer(constraints,objective,opts,X0,{U{1} objective}); - end + + % Create yalmip optimization 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 and constraints + Pinf = idare(params.model.A, params.model.B, Q, R); + objective = X{N+1}' * Pinf * X{N+1}; + 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, H*X{N+1} <= h]; + + obj.yalmip_optimizer = optimizer(constraints,objective,opts,X{1},{U{1} objective}); end function [u, ctrl_info] = eval(obj,x) %% evaluate control action by solving MPC problem, e.g. |