-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelicopter_MPC.m
More file actions
227 lines (197 loc) · 7.51 KB
/
helicopter_MPC.m
File metadata and controls
227 lines (197 loc) · 7.51 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
% MPC tracking of the helicopter model
clear all
clc
% Helicopter model parameters ──────────────────────────────────────────────────
bx = 2; by = 2; bz = 18; bpsi = 111;
kx = -0.5; ky = -0.5; kpsi = -5; ki = 2;
parameters = [bx; by; bz; bpsi; kx; ky; kpsi; ki];
g = 9.81;
Ts = 0.1; % sampling time
x_constraints = [ % state constraints
-2, 2;
-2, 2;
-2, 2;
-3, 3;
-3, 3;
-2, 2;
-pi, 3*pi;
-25, 25;
];
u_constraints = 2*[ % input constraints
-1, 1;
-1, 1;
-1, 1;
-1, 1;
];
states = 8; % number of states
outputs = 4; % number of outputs
Q_tilde = 0.75*1e-3*eye(states); % Process noise covariance
R_tilde = 1e-2*eye(outputs); % Measurement noise covariance
P0 = eye(states); % Initial state covariance
model = Helicopter(parameters, Ts, x_constraints, u_constraints, P0, Q_tilde, R_tilde);
% Reference Trajectory Generation ──────────────────────────────────────────────
% (comment / uncomment the desired trajectory)
% % Circle trajectory
% N_guide = 100;
% radius = 0.5;
% shape = "circle";
% [x_ref, u_ref, Tend] = model.generate_trajectory(N_guide, shape, radius);
% max_start = 0.05;
% Lemniscate trajectory
N_guide = 100;
a = 1;
shape = "lemniscate";
[x_ref, u_ref, Tend] = model.generate_trajectory(N_guide, shape, a);
max_start = 0.05;
% Arbitrary trajectory
% N_guide = 5;
% Z_guide = [
% 1, 0, 0, pi/2;
% 0, 1, 0, pi;
% -1, 0, 0, 3*pi/2;
% 0, -1, 0, 0;
% 1, 0, 0, pi/2;
% ];
% N_points_filling = 25;
% shape = "arbitrary";
% N_basis = 2;
% order = 0;
% [x_ref, u_ref, Tend] = model.generate_trajectory(N_guide, shape, {N_points_filling, N_basis, order, Z_guide});
% max_start = 0.05;
% % Multiply periodic references for multiple laps
% n_laps = 2;
% x_ref = repmat(x_ref, n_laps, 1);
% u_ref = repmat(u_ref, n_laps, 1);
% Tend = Tend*n_laps;
% MPC ──────────────────────────────────────────────────────────────────────────
% Initial conditions
% x0 = zeros(model.n,1); % origin initial state
% x0 = x_ref(1, :)'; % first reference initial state
% x0 = [1; 0; 0; 0; 0; 0; pi/4; 0]; % custom initial state
x0 = x_ref(1, :)' + max_start*rand(states, 1); % random initial condition
% MPC parameters
N = 18; % prediction horizon
Q = diag([50, 50, 5, 10, 3, 3, 1, 2]); % state cost
R = diag([2, 2, 2, 2]); % input cost
preview = 1; % MPC preview flag
formulation = 0; % MPC formulation flag
noise = 1; % MPC noise flag
debug = 0; % MPC debug flag
% Simulation time and steps
x_ref = [x_ref; x_ref(1:N+1, :)]; % add N steps to complete a full loop
u_ref = [u_ref; u_ref(1:N+1, :)]; % add N steps to complete a full loop
Tend = Tend + (N+1)*Ts;
t = 0:Ts:Tend; % vector of time steps
Nsteps = length(t) - (N+1); % number of MPC optimization steps
% Optimization
mpc = MPC(model, x0, Tend, N, Q, R, x_ref, u_ref, preview, formulation, noise, debug);
[x, u] = mpc.optimize();
% % Plot ─────────────────────────────────────────────────────────────────────────
%
% % Main trajectory plot
% figure(1);
%
% % Reference trajectory
% ref_points = scatter(x_ref(:, 1), x_ref(:, 2), 5, 'filled', 'MarkerFaceColor', '#808080');
% hold on;
% arrow_length = 0.01;
% for i = 1:length(x_ref)
% x_arrow = arrow_length * cos(x_ref(i, 7));
% y_arrow = arrow_length * sin(x_ref(i, 7));
% quiver(x_ref(i, 1), x_ref(i, 2), x_arrow, y_arrow, 'AutoScale', 'off', 'Color', '#808080');
% end
% legend(ref_points,{'Reference trajectory'}, 'Location', 'northwest');
%
% % Labels
% title('Trajectory Tracking with MPC (Non-Linear Helicopter System)');
% xlabel('x'); ylabel('y');
% grid on;
% axis equal;
% hold on;
%
% % % Set plot limits
% % xlim([-1.5, 1.5]);
% % ylim([-1, 1]);
% % hold on;
%
% % ────────────────────
% % Wait for figure here
% pause(1);
%
% % Real trajectory
% for i = 1:Nsteps
% x_line = plot(x(1:i, 1), x(1:i, 2), 'blue', 'LineWidth', 1);
% x_line.Color(4) = 0.5; % line transparency 50%
% hold on;
% x_points = scatter(x(1:i, 1), x(1:i, 2), 5, 'blue', 'filled');
% hold on;
% quiver(x(1:i, 1), x(1:i, 2), arrow_length * cos(x(1:i, 7)), arrow_length * sin(x(1:i, 7)), 'AutoScale', 'off', 'Color', 'blue');
% target = scatter(x_ref(i, 1), x_ref(i, 2), 20, 'red', 'filled');
% hold on;
% legend([ref_points, x_points, target],{'Reference trajectory', 'Real trajectory', 'Target'}, 'Location', 'northwest');
% hold on;
%
% pause(0.05);
% if i < Nsteps
% delete(x_line);
% delete(target);
% end
% end
% GIF ──────────────────────────────────────────────────────────────────────────
% Main trajectory plot
figure(1);
filename = 'images/helicopter_MPC_lemniscate_noise2.gif'; % Output GIF filename
% Reference trajectory
ref_points = scatter(x_ref(:, 1), x_ref(:, 2), 5, 'filled', 'MarkerFaceColor', '#808080');
hold on;
arrow_length = 0.02;
for i = 1:length(x_ref)
x_arrow = arrow_length * cos(x_ref(i, 7));
y_arrow = arrow_length * sin(x_ref(i, 7));
quiver(x_ref(i, 1), x_ref(i, 2), x_arrow, y_arrow, 'AutoScale', 'off', 'Color', '#808080');
end
legend(ref_points,{'Reference trajectory'}, 'Location', 'northwest');
% Labels
title('Trajectory Tracking with MPC (Non-Linear Helicopter System)');
xlabel('x'); ylabel('y');
grid on;
axis equal;
hold on;
axis tight; % Adjust axis limits to fit the data tightly
hold on;
% Set plot limits
xlim([-1.7, 1.7]);
% xlim([-0.6, 0.6]);
ylim([-0.8, 0.8]);
hold on;
% Adjust figure to fit tightly around the plot
set(gca, 'LooseInset', get(gca, 'TightInset'));
% Capture initial frame for GIF
frame = getframe(gca);
img = frame2im(frame);
[imind, cm] = rgb2ind(img, 256);
imwrite(imind, cm, filename, 'gif', 'Loopcount', inf, 'DelayTime', 0.1);
% Real trajectory animation and GIF capture
for i = 1:Nsteps
x_line = plot(x(1:i, 1), x(1:i, 2), 'blue', 'LineWidth', 1);
x_line.Color(4) = 0.5; % line transparency 50%
hold on;
x_points = scatter(x(1:i, 1), x(1:i, 2), 5, 'blue', 'filled');
hold on;
quiver(x(1:i, 1), x(1:i, 2), arrow_length * cos(x(1:i, 7)), arrow_length * sin(x(1:i, 7)), 'AutoScale', 'off', 'Color', 'blue');
hold on;
target = scatter(x_ref(i, 1), x_ref(i, 2), 20, 'filled', 'MarkerFaceColor', 'none', 'MarkerEdgeColor', 'red');
hold on;
legend([ref_points, x_points, target],{'Reference trajectory', 'Real trajectory', 'Target'}, 'Location', 'northwest');
hold on;
% Capture frame for GIF
frame = getframe(gca);
img = frame2im(frame);
[imind, cm] = rgb2ind(img, 256);
imwrite(imind, cm, filename, 'gif', 'WriteMode', 'append', 'DelayTime', 0.1);
pause(0.05);
if i < Nsteps
delete(x_line);
delete(target);
end
end