-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.m
373 lines (315 loc) · 14.8 KB
/
main.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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
% Sai Chikine
% ASEN 6020 Optimal Trajectories
% Final Project
%% let there be light
clear; close all; clc;
set(groot,'defaultLineLineWidth', 1.5)
set(groot,'defaultAxesFontSize', 16)
set(groot,'defaulttextinterpreter','latex');
set(groot, 'defaultAxesTickLabelInterpreter','latex');
set(groot, 'defaultLegendInterpreter','latex');
p = gcp;
%% Load (setup)
%load('environment_EM_spacecraft_smallsat.mat');
load('environment_EM_spacecraft_cubesat.mat');
load('initial_state_lyap_trans.mat');
load('target_state_lyap_trans.mat');
%%
% ODE tolerances
ode_opts = odeset('RelTol',1e-13,'AbsTol',1e-20);
%% Load ref traj
load('ref_connection.mat');
fprintf('Reference trajectory loaded.\n');
X0 = connection_result.X0;
tof = connection_result.traj{1}(end);
X0 = initial_state(1:6);
tof = 5.4881;
%% Get ref traj with lots of points
tic
[t_ref,X_ref_hist] = ode113(@(t,X) CR3BP(t,X,mu_EM), linspace(0,tof,10000), X0, ode_opts);
toc
ref_traj = {t_ref, X_ref_hist};
% %% Plot reference trajectory (2D)
%
% figure
% addToolbarExplorationButtons(gcf) % Adds buttons to figure toolbar
% plot(1-mu_EM, 0, 'ok', 'markerfacecolor', 'm', 'markersize', 8, 'DisplayName', 'Moon'); hold on % Moon
% plot(x_L1, 0, 'ok', 'markerfacecolor', 'r', 'DisplayName', 'L1 Point'); hold on % L1 location
% plot(x_L2, 0, 'ok' , 'markerfacecolor', 'b', 'DisplayName', 'L2 Point'); hold on % L2 location
% plot(connection_result.traj{2}(1,1), connection_result.traj{2}(2,1), 'ok', 'markerfacecolor', 'y', 'DisplayName', 'Initial Point'); hold on
% plot(connection_result.traj{2}(1,:), connection_result.traj{2}(2,:), 'r-','DisplayName', 'Reference Trajectory (Heteroclinic Connection)'); hold on
% title('Reference Trajectory')
% xlabel('X')
% ylabel('Y')
% axis('equal')
% grid on;
% legend();
% hold off
%%
% From plot, pick start and end states
% index_start = 660;
% %index_start = 853;
% index_end = 1160;
%
% % index_start = 1;
% % index_end = length(connection_result.traj{1});
%
% rng(50);
%
% X0 = connection_result.traj{2}(:,index_start) + [normrnd(0,1.3e-6,[2,1]);0;normrnd(0,10e-4,[2,1]);0];
% %X0 = connection_result.traj{2}(:,index_start) + [normrnd(0,1e-5,[2,1]);0;normrnd(0,1e-5,[2,1]);0];
% t0 = connection_result.traj{1}(index_start);
% Xf = connection_result.traj{2}(:,index_end);
% tf = connection_result.traj{1}(index_end);
%
% tfmin0 = tf-t0;
% cf_guess = 1;
%% Same as DDP EM transfer
tfmin0 = tof;
cf_guess = 1;
Xf = X0_arr_guess(1:6);
t0 = 0;
tf = tof;
%% Plot reference trajectory (from index_start to index_end)
figure
addToolbarExplorationButtons(gcf) % Adds buttons to figure toolbar
plot(1-mu_EM, 0, 'ok', 'markerfacecolor', 'm', 'markersize', 8, 'DisplayName', 'Moon'); hold on % Moon
plot(x_L1, 0, 'dk', 'markerfacecolor', 'r', 'DisplayName', 'L1 Point'); hold on % L1 location
plot(x_L2, 0, 'dk' , 'markerfacecolor', 'b', 'DisplayName', 'L2 Point'); hold on % L2 location
plot(connection_result.traj{2}(1,index_start), connection_result.traj{2}(2,index_start), 'ok', 'markerfacecolor', 'y', 'DisplayName', 'Initial Point'); hold on
plot(connection_result.traj{2}(1,index_start:index_end), connection_result.traj{2}(2,index_start:index_end), 'r-','DisplayName', 'Reference Trajectory (Heteroclinic Connection)'); hold on
plot(connection_result.traj{2}(1,index_end), connection_result.traj{2}(2,index_end), 'ok', 'markerfacecolor', 'r', 'DisplayName', 'Target Point'); hold on
title('Reference Trajectory','FontSize',14)
xlabel('X')
ylabel('Y')
axis('equal')
grid on;
legend('FontSize',12);
%% Plot DDP ref traj
figure
addToolbarExplorationButtons(gcf)
plot(1-mu_EM, 0, 'ok', 'markerfacecolor', 'm', 'markersize', 8, 'DisplayName', 'Moon'); hold on % Moon
plot(x_L1, 0, 'dk', 'markerfacecolor', 'r', 'DisplayName', 'L1 Point'); hold on % L1 location
plot(x_L2, 0, 'dk' , 'markerfacecolor', 'b', 'DisplayName', 'L2 Point'); hold on % L2 location
plot(ref_traj{2}(1,1), ref_traj{2}(1,2), 'ok', 'markerfacecolor', 'y', 'DisplayName', 'Initial Point'); hold on
plot(ref_traj{2}(:,1), ref_traj{2}(:,2), 'r-','DisplayName', 'Reference Trajectory (Heteroclinic Connection)'); hold on
plot(Xf(1), Xf(2), 'ok', 'markerfacecolor', 'r', 'DisplayName', 'Target Point'); hold on
title('Reference Trajectory','FontSize',14)
xlabel('X')
ylabel('Y')
axis('equal')
grid on;
legend('FontSize',12);
%%
% Get initial guess for costates from linear problem (fixed-endpoint LQR)
l = 6; % number of states
R = 2*eye(3);
A_func = @(X) Jacobian(X,mu_EM); % A matrix depends on state (time varying)
%B = [zeros(3,3); 47*max_thrust_mag/m_sc*eye(3)]; %dfdu %works for original
%scenario
B = [zeros(3,3); 500*max_thrust_mag/m_sc*eye(3)];
augSTM_0 = eye(12,12);
X_ref = X0;
Xlarge0 = [X_ref; reshape(augSTM_0,[],1)];
%deltaX0 = X_ref - connection_result.traj{2}(:,index_start); % for original
%scenario
deltaX0 = X_ref - X0;
%ode_opts = odeset('RelTol',3e-14,'AbsTol',1e-22);
%[~, Xlarge_loop_hist] = ode113(@(t,Xlarge) CR3BP_costate_STM_dynamics(t,Xlarge,B,R,mu_EM), [t0, t0+cf_guess*tfmin0], Xlarge0, ode_opts);
[~, Xlarge_loop_hist] = ode113(@(t,Xlarge) CR3BP_costate_STM_dynamics(t,Xlarge,B,R,mu_EM), ...
[t0, t0+cf_guess*tfmin0], [X_ref; reshape(eye(12),[],1)], ode_opts);
deltaXf = Xf - Xlarge_loop_hist(end,1:6)';
STM_t = reshape(Xlarge_loop_hist(end,7:end),12,12);
% Solve for lambda0 (at tnow)
lambda0 = STM_t(1:6,7:12)\(deltaXf - STM_t(1:6,1:6)*deltaX0);
%% Test initial guess for lambda0
m0 = m_sc;
Tmax = max_thrust_mag;
amax = max_thrust_mag/m_sc;
params_accel = struct('m_norm',MU, 'c',exh_vel, 'mu',mu_EM, 'amax',amax, 'Tmax',max_thrust_mag,'L_EM',L_EM, 'T_EM',T_EM, 'force_norm',FU, 'vel_norm',VU, 'accel_norm', AU);
chi0 = [X0; lambda0];
fprintf("Controlled trajectory with guess for lambda0\n")
tic
[t_opt, X_opt] = ode113(@(t,X) state_costate_reduced_dynamics(t,X,m0,@opt_control_min_energy_reduced,params_accel), [t0, t0+cf_guess*tfmin0], chi0, ode_opts);
[t_no, X_no] = ode113(@(t,X) CR3BP(t,X,mu_EM), [t0, t0+cf_guess*tfmin0], X0, ode_opts);
toc
%% Plot controlled traj guess w/ uncontrolled
figure
addToolbarExplorationButtons(gcf) % Adds buttons to figure toolbar
plot(1-mu_EM, 0, 'ok', 'markerfacecolor', 'm', 'markersize', 8, 'DisplayName', 'Moon'); hold on % Moon
plot(x_L1, 0, 'dk', 'markerfacecolor', 'r', 'DisplayName', 'L1 Point'); hold on % L1 location
plot(x_L2, 0, 'dk' , 'markerfacecolor', 'b', 'DisplayName', 'L2 Point'); hold on % L2 location
plot(X_opt(1,1), X_opt(1,2), 'ok', 'markerfacecolor', 'y', 'DisplayName', 'Initial Point'); hold on
plot(Xf(1), Xf(2), 'ok', 'markerfacecolor', 'g', 'DisplayName', 'Target'); hold on
plot(X_opt(:,1), X_opt(:,2), 'r-','DisplayName', 'Initial Guess Controlled Trajectory'); hold on
plot(X_no(:,1), X_no(:,2), 'b-','DisplayName','Uncontrolled Trajectory'); hold off
title('Controlled Trajectory using Linearized Initial Guess','FontSize',14)
xlabel('X')
ylabel('Y')
axis('equal')
grid on;
legend('FontSize',12);
hold off
%% Correct into nonlinear model (without mass)
cfr = 1;
%cfr = 0.9305;
% tfmin = tf-t0;
% tf0 = tf-t0;
tfmin = tof;
lambda_i = lambda0;
free_vars = lambda_i;
shooting_func_min_energy_reduced_simple = @(free_vars) shooting_func_min_energy_reduced(0,free_vars,X0,m0,Xf,cfr*tfmin,params_accel);
min_energy_reduced_result = shooter(free_vars,shooting_func_min_energy_reduced_simple,'tol',1e-10,'max_iter',5000);
%% Plot results from above
free_vars_converged_reduced = min_energy_reduced_result.free_vars;
lambda0_masspre = free_vars_converged_reduced;
err_mag_hist = min_energy_reduced_result.err_hist;
chi0 = [X0; free_vars_converged_reduced];
fprintf("Controlled trajectory with corrected lambda0\n")
tic
[t_opt_pass1, X_opt_pass1] = ode113(@(t,X) state_costate_reduced_dynamics(t,X,m0,@opt_control_min_energy_reduced,params_accel), [t0, t0+cfr*tfmin0], chi0, ode_opts);
figure
addToolbarExplorationButtons(gcf) % Adds buttons to figure toolbar
plot(1-mu_EM, 0, 'ok', 'markerfacecolor', 'm', 'markersize', 8, 'DisplayName', 'Moon'); hold on % Moon
plot(x_L1, 0, 'dk', 'markerfacecolor', 'r', 'DisplayName', 'L1 Point'); hold on % L1 location
plot(x_L2, 0, 'dk' , 'markerfacecolor', 'b', 'DisplayName', 'L2 Point'); hold on % L2 location
plot(X_opt_pass1(1,1), X_opt_pass1(1,2), 'ok', 'markerfacecolor', 'y', 'DisplayName', 'Initial Point'); hold on
plot(Xf(1), Xf(2), 'ok', 'markerfacecolor', 'g', 'DisplayName', 'Target'); hold on
plot(X_opt_pass1(:,1), X_opt_pass1(:,2), 'r-','DisplayName', 'Converged Controlled Trajectory (Reduced Model)'); hold on
plot(X_no(:,1), X_no(:,2), 'b-','DisplayName','Uncontrolled Trajectory'); hold off
title('Controlled Trajectory using Corrected Initial Costate (Reduced Model)','FontSize',14)
xlabel('X')
ylabel('Y')
axis('equal')
grid on;
legend('FontSize',12);
hold off
figure
addToolbarExplorationButtons(gcf)
semilogy(1:1:length(err_mag_hist), err_mag_hist, '-x','DisplayName', 'Residual (Error) Magnitude');
xlim([1, inf])
xlabel('Iteration','FontSize',14)
ylabel('Residual Magnitude','FontSize',14)
title('Residual Magnitude vs Iteration Number','FontSize',14)
grid on
legend('FontSize',12)
%%%
% chi0 = [X0; free_vars];
% tic
% [t_opt, X_opt] = ode113(@(t,X) state_costate_reduced_dynamics(t,X,m0,@opt_control_min_energy_reduced,params_accel), [t0, t0+cf*tfmin], chi0, ode_opts);
% [t_no, X_no] = ode113(@(t,X) CR3BP(t,X,mu_EM), [t0, tf], X0, ode_opts);
% toc
%
% figure
% addToolbarExplorationButtons(gcf) % Adds buttons to figure toolbar
% plot(1-mu_EM, 0, 'ok', 'markerfacecolor', 'm', 'markersize', 8, 'DisplayName', 'Moon'); hold on % Moon
% plot(x_L1, 0, 'ok', 'markerfacecolor', 'r', 'DisplayName', 'L1 Point'); hold on % L1 location
% plot(x_L2, 0, 'ok' , 'markerfacecolor', 'b', 'DisplayName', 'L2 Point'); hold on % L2 location
% plot(X_opt(1,1), X_opt(1,2), 'ok', 'markerfacecolor', 'y', 'DisplayName', 'Initial Point'); hold on
% plot(Xf(1), Xf(2), 'ok', 'markerfacecolor', 'g', 'DisplayName', 'Target'); hold on
% plot(X_opt(:,1), X_opt(:,2), 'r-','DisplayName', 'Initial Guess Controlled Trajectory'); hold on
% plot(X_no(:,1), X_no(:,2), 'b-','DisplayName','Uncontrolled Trajectory'); hold off
% title('Reference Trajectory')
% xlabel('X')
% ylabel('Y')
% axis('equal')
% grid on;
% legend();
% hold off
%% with mass
cff = 1;
%cff = 0.91;
tfmin = tf-t0;
%Tmax = 0.32; % newtons
%exh_vel = 3000; % m/s
m0 = m_sc;
tf0 = tf-t0;
lambda_i = [lambda0_masspre; 1e-6];
free_vars = lambda_i;
shooting_func_min_energy_simple = @(free_vars) shooting_func_min_energy(0,free_vars,X0,m0,Xf,cff*tfmin,params_accel);
min_energy_full_result = shooter(free_vars,shooting_func_min_energy_simple,'tol',1e-10,'max_iter',5000);
%%
free_vars_converged_full = min_energy_full_result.free_vars;
%% Plot Results
chi0 = [X0; m0; free_vars_converged_full];
tic
[t_opt_pass2, X_opt_pass2] = ode113(@(t,X) state_costate_full_dynamics(t,X,@opt_control_min_energy,params_accel), [t0, t0+cff*tfmin], chi0, ode_opts);
toc
% Compute control history
final_control_results = get_control_hist_min_energy_full(X_opt_pass2,params_accel.c);
% figure
% addToolbarExplorationButtons(gcf) % Adds buttons to figure toolbar
% plot3(1-mu_EM, 0, 0, 'ok', 'markerfacecolor', 'm', 'markersize', 8, 'DisplayName', 'Moon'); hold on % Moon
% plot3(x_L1, 0, 0, 'dk', 'markerfacecolor', 'r', 'DisplayName', 'L1 Point'); hold on % L1 location
% plot3(x_L2, 0, 0, 'dk' , 'markerfacecolor', 'b', 'DisplayName', 'L2 Point'); hold on % L2 location
% plot3(X0(1), X0(2), X0(3), 'ok', 'markerfacecolor', 'y', 'DisplayName', 'Initial Point'); hold on
% plot3(Xf(1), Xf(2), Xf(3), 'ok', 'markerfacecolor', 'g', 'DisplayName', 'Target'); hold on
% plot3(X_opt_pass2(:,1), X_opt_pass2(:,2), X_opt_pass2(:,3), 'r-','DisplayName', 'Controlled Trajectory w Variable Mass'); hold off
% title('Controlled Trajectory Using Corrected Initial Costate (Full Model with Mass)')
% xlabel('X')
% ylabel('Y')
% grid on;
% % 3D axis equal
% h = get(gca,'DataAspectRatio');
% if h(3)==1
% set(gca,'DataAspectRatio',[1 1 1/max(h(1:2))])
% else
% set(gca,'DataAspectRatio',[1 1 h(3)])
% end
% legend();
% Plot test (2D)
figure
addToolbarExplorationButtons(gcf) % Adds buttons to figure toolbar
plot(1-mu_EM, 0, 'ok', 'markerfacecolor', 'm', 'markersize', 8, 'DisplayName', 'Moon'); hold on % Moon
plot(x_L1, 0, 'dk', 'markerfacecolor', 'r', 'DisplayName', 'L1 Point'); hold on % L1 location
plot(x_L2, 0, 'dk' , 'markerfacecolor', 'b', 'DisplayName', 'L2 Point'); hold on % L2 location
plot(X0(1), X0(2), 'ok', 'markerfacecolor', 'y', 'DisplayName', 'Initial Point'); hold on
plot(Xf(1), Xf(2), 'ok', 'markerfacecolor', 'g', 'DisplayName', 'Target'); hold on
plot(X_opt_pass2(:,1), X_opt_pass2(:,2), 'r-','DisplayName', 'Controlled Trajectory w Variable Mass'); hold on
plot(X_no(:,1), X_no(:,2), 'b-','DisplayName','Uncontrolled Trajectory'); hold off
title('Controlled Trajectory Using Corrected Initial Costate (Full Model with Mass)','FontSize',14)
xlabel('X')
ylabel('Y')
grid on;
axis('equal')
legend('FontSize',12);
% Plot control vector and thrust factor history
figure
sgtitle('Control and Mass History','FontSize',14)
subplot(1,2,1)
plot(t_opt_pass2-t0, FU*1e6*Tmax.*final_control_results.u_vec_hist(1,:), 'DisplayName', 'Thrust Vector (x component) [mN]'); hold on
plot(t_opt_pass2-t0, FU*1e6*Tmax.*final_control_results.u_vec_hist(2,:), 'DisplayName', 'Thrust Vector (y component) [mN]'); hold off
title('Control Vector History','FontSize',12)
xlabel('Time [non-dimensional units]','FontSize',12)
ylabel('Control Vector Magnitude [milli-Newtons]','FontSize',12)
grid on
legend('FontSize',12)
subplot(1,2,2)
yyaxis left
plot(t_opt_pass2-t0, final_control_results.u_hist, 'DisplayName', 'Thrust Factor');
ylabel('Thrust Factor','FontSize',12)
yyaxis right
plot(t_opt_pass2-t0, MU*X_opt_pass2(:,7), 'DisplayName', 'Spacecraft Mass');
ylabel('Mass [kg]','FontSize',12)
xlabel('Time [non-dimensional units]','FontSize',12)
title('Thrust Factor and Spacecract Mass vs Time','FontSize',12)
legend('FontSize',12)
grid on
% Plot residual history
figure
addToolbarExplorationButtons(gcf)
semilogy(1:1:length(err_mag_hist), err_mag_hist, '-x','DisplayName', 'Residual (Error) Magnitude');
xlim([1, inf])
xlabel('Iteration','FontSize',14)
ylabel('Residual Magnitude','FontSize',14)
title('Residual Magnitude vs Iteration Number','FontSize',14)
grid on
legend('FontSize',12)
max_thrust_used = FU*1e6*max(Tmax.*final_control_results.u_hist); % [mN]
total_deltav = trapz(TU.*t_opt_pass2,AU*Tmax/m_sc.*final_control_results.u_hist); % [m/s]
fuel_used = MU*(m_sc - X_opt_pass2(end,7)); % [kg]
final_mass = MU*X_opt_pass2(end,7); % [kg]
fprintf("Max Thrust Used: %d mN\n",max_thrust_used);
fprintf("Total Delta V: %d m/s\n",total_deltav);
fprintf("Fuel Mass Used: %d kg\n",fuel_used);
fprintf("Spacecraft Final mass: %d kg\n",final_mass);