-
Notifications
You must be signed in to change notification settings - Fork 0
/
MPC_Control_yaw.m
118 lines (88 loc) · 3.66 KB
/
MPC_Control_yaw.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
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
117
118
classdef MPC_Control_yaw < MPC_Control
methods
% Design a YALMIP optimizer object that takes a steady-state state
% and input (xs, us) and returns a control input
function ctrl_opt = setup_controller(mpc)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% INPUTS
% x(:,1) - initial state (estimate)
% xs, us - steady-state target
% OUTPUTS
% u(:,1) - input to apply to the system
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[n,m] = size(mpc.B);
% Steady-state targets (Ignore this before Todo 3.2)
xs = sdpvar(n, 1);
us = sdpvar(m, 1);
% SET THE HORIZON HERE
N = 15;
% Predicted state and input trajectories
x = sdpvar(n, N);
u = sdpvar(m, N-1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% YOUR CODE HERE YOUR CODE HERE YOUR CODE HERE YOUR CODE HERE
% NOTE: The matrices mpc.A, mpc.B, mpc.C and mpc.D are
% the DISCRETE-TIME MODEL of your system
% SET THE PROBLEM CONSTRAINTS con AND THE OBJECTIVE obj HERE
Q = eye(n); R = 1;
M = [1; -1]; m = [0.2; 0.2];
[K, Qf, ~] = dlqr(mpc.A, mpc.B, Q, R);
K = -K;
% Compute maximal invariant set
Xf = polytope(M*K,m);
% figure(3); plot(Xf.projection(3:4)); hold on;
Acl = mpc.A + mpc.B*K;
while 1
prevXf = Xf;
[T,t] = double(Xf);
preXf = polytope(T*Acl,t);
Xf = intersect(Xf, preXf);
if isequal(prevXf, Xf)
break
end
% plot(Xf.projection(3:4)); hold on;
%pause;
end
[Ff,ff] = double(Xf);
% figure(4);plot(Xf.projection(3:4)); xlabel("beta angle"); ylabel("beta speed");
% figure(5);plot(Xf.projection(1:2)); xlabel("x position"); ylabel("x speed");
con = (x(:,2)-xs == mpc.A*(x(:,1)-xs) + mpc.B*(u(1)-us)) + (M*(u(1)-us) <= m-M*us);
obj = ((x(:,1)-xs)'*Q*(x(:,1)-xs))+(u(:,1)-us)'*R*(u(:,1)-us);
for i = 2:N-1
con = [con, (x(:,i+1)-xs) == mpc.A*(x(:,i)-xs) + mpc.B*(u(i)-us)]; % System dynamics
con = [con, M*(u(i)-us) <= m-M*us]; % Input constraints
obj = obj + (x(:,i)-xs)'*Q*(x(:,i)-xs) + (u(i)-us)'*R*(u(i)-us); % Cost function
end
% YOUR CODE HERE YOUR CODE HERE YOUR CODE HERE YOUR CODE HERE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ctrl_opt = optimizer(con, obj, sdpsettings('solver','gurobi'), ...
{x(:,1), xs, us}, u(:,1));
end
% Design a YALMIP optimizer object that takes a position reference
% and returns a feasible steady-state state and input (xs, us)
function target_opt = setup_steady_state_target(mpc)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% INPUTS
% ref - reference to track
% OUTPUTS
% xs, us - steady-state target
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Steady-state targets
n = size(mpc.A,1);
xs = sdpvar(n, 1);
us = sdpvar;
% Reference position (Ignore this before Todo 3.2)
ref = sdpvar;
% WRITE THE CONSTRAINTS AND OBJECTIVE HERE
nx = size(mpc.A,1);
nu = size(mpc.B,2);
Q = eye(n); R = 20;
M = [1; -1]; m = [0.2; 0.2];
con = [M*us <= m ,...
xs == mpc.A*xs + mpc.B*us ];
obj = (mpc.C*xs - ref)'*(mpc.C*xs - ref);
% Compute the steady-state target
target_opt = optimizer(con, obj, sdpsettings('solver', 'gurobi'), ref, {xs, us});
end
end
end