summaryrefslogtreecommitdiffstats
path: root/templates/MPC.m
blob: b2d411dd04d2a6ebc6efc907ac560a377b5fa298 (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Copyright (c) 2023, Amon Lahr, Simon Muntwiler, Antoine Leeman & Fabian Flürenbrock Institute for Dynamic Systems and Control, ETH Zurich.
%
% All rights reserved.
%
% Please see the LICENSE file that has been included as part of this package.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% classdef MPC
%     properties
%         yalmip_optimizer
%     end
% 
%     methods
%         function obj = MPC(Q,R,N,params)
%             nu = params.model.nu;
%             nx = params.model.nx;
% 
%             % define optimization variables
%             U = sdpvar(repmat(nu,1,N),ones(1,N),'full');
%             X0 = sdpvar(nx,1,'full');
% 
%             % YOUR CODE HERE
%             
%             opts = sdpsettings('verbose',1,'solver','quadprog');
%             obj.yalmip_optimizer = optimizer(constraints,objective,opts,X0,{U{1} objective});
%         end
% 
%         function [u, ctrl_info] = eval(obj,x)
%             %% evaluate control action by solving MPC problem, e.g.
%             tic;
%             [optimizer_out,errorcode] = obj.yalmip_optimizer(x);
%             solvetime = toc;
%             
%             [u, objective] = optimizer_out{:};
% 
%             feasible = true;
%             if (errorcode ~= 0)
%                 feasible = false;
%             end
% 
%             ctrl_info = struct('ctrl_feas',feasible,'objective',objective,'solvetime',solvetime);
%         end
%     end
% end

classdef MPC
    properties
        yalmip_optimizer
    end

    methods
        function obj = MPC(Q,R,N,params)
            nu = params.model.nu;
            nx = params.model.nx;
            % YOUR CODE HERE
            % define optimization variables
            A=params.model.A;
            B=params.model.B;
            U = sdpvar(repmat(nu,1,N),ones(1,N),'full');
            X = sdpvar(repmat(nx,1,N+1),ones(1,N+1),'full');

            [K,P,~] = dlqr(A,B,Q,R);

            % define constraints
%             s_max=params.constraints.MaxAbsPositionXZ;
%             y_max=params.constraints.MaxAbsPositionY;
%             u_max = params.constraints.MaxAbsThrust;
            H_x = params.constraints.StateMatrix;
            h_x = params.constraints.StateRHS;
            H_u = params.constraints.InputMatrix;
            h_u = params.constraints.InputRHS;
            X0 = sdpvar(nx,1,'full');
            objective = 0;
            constraints = X{1} == X0;
            for k = 1:N
                constraints = [ ...
                    constraints, ...
                    X{k+1} == A*X{k} + B*U{k} , ...
                    H_x * X{k} <= h_x, ...
                    H_u * U{k} <= h_u ...
                ];

                objective = objective + X{k}'*Q*X{k} + U{k}'*R*U{k};
            end
            objective=objective+X{N+1}'*P*X{N+1};
            % terminal constraint
%             constraints = [ ...
%                 constraints, ...
%                 X{N+1} == zeros(nx,1)
%             ];
            opts = sdpsettings('verbose',1,'solver','quadprog');
            obj.yalmip_optimizer = optimizer(constraints,objective,opts,X0,{U{1} objective});
        end

        function [u, ctrl_info] = eval(obj,x)
            % evaluate control action by solving MPC problem
            tic;
            [optimizer_out,errorcode,~] = obj.yalmip_optimizer{x};
            solvetime = toc;

            % extract optimal control action and objective function value
            u = optimizer_out{1};
            objective = optimizer_out{2};

            % check feasibility of optimization problem
            feasible = ~isnan(objective) && ~isinf(objective);
            if (errorcode ~= 0)
                feasible = false;
            end

            % create control info struct
            ctrl_info = struct('ctrl_feas',feasible,'objective',objective,'solvetime',solvetime);
        end
    end
end