-
Notifications
You must be signed in to change notification settings - Fork 61
/
plots.jl
1645 lines (1444 loc) · 47.7 KB
/
plots.jl
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#= Spacecraft rendezvous plots.
Sequential convex programming algorithms for trajectory optimization.
Copyright (C) 2021 Autonomous Controls Laboratory (University of Washington)
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>. =#
using PyPlot
using Colors
# ..:: Globals ::..
const fig_opts = Dict(
"font.family" => "serif",
"text.latex.preamble" => string(
"\\usepackage{newtxtext}",
"\\usepackage{newtxmath}",
"\\usepackage{siunitx}",
),
)
const gray = rgb2pyplot(weighted_color_mean(0.1, colorant"black", colorant"white"))
const red = rgb2pyplot(weighted_color_mean(0.2, parse(RGB, Red), colorant"white"))
const time_mark_clr = rgb2pyplot(weighted_color_mean(0.5, colorant"black", colorant"white"))
# ..:: Methods ::..
"""
plot_trajectory_2d(mdl, sol[; attitude])
Plot the final converged trajectory, projected onto 2D planes.
# Arguments
- `mdl`: the problem description object.
- `sol`: the problem solution.
# Keywords
- `attitude`: (optional) whether to also plot the attitude time history.
"""
function plot_trajectory_2d(
mdl::RendezvousProblem,
sol::SCPSolution;
attitude::Bool = false,
)::Nothing
# Parameters
algo = sol.algo
veh = mdl.vehicle
traj = mdl.traj
axis_inf_scale = 1e4
pad_x = 0.1
pad_y = 0.1
pad_label_abs = 10
equal_ar = true # Equal aspect ratio
input_scale = 0.2
# Plotting data
ct_res = 1000
dt_res = length(sol.td)
ct_τ = RealVector(LinRange(0, 1, ct_res))
dt_pos = sol.xd[veh.id_r, :]
ct_pos = hcat([sample(sol.xc, τ)[veh.id_r] for τ in ct_τ]...)
ct_vel = hcat([sample(sol.xc, τ)[veh.id_v] for τ in ct_τ]...)
ct_speed = squeeze(mapslices(norm, ct_vel, dims = (1)))
n_rcs = length(veh.id_rcs)
N = size(sol.ud, 2)
dt_thrust = zeros(3, N)
dir_rcs = [veh.csm.f_rcs[veh.csm.rcs_select[i]] for i = 1:n_rcs]
for k = 1:size(dt_thrust, 2)
q = Quaternion(sol.xd[veh.id_q, k])
dir_rcs_iner = [rotate(dir_rcs[i], q) for i = 1:n_rcs]
dt_thrust[:, k] = sum(sol.ud[veh.id_rcs[i], k] * dir_rcs_iner[i] for i = 1:n_rcs)
end
# Colormap
v_cmap =
generate_colormap("inferno"; minval = minimum(ct_speed), maxval = maximum(ct_speed))
data = [
Dict(:prj => [1, 3], :x_name => "x", :y_name => "z"),
Dict(:prj => [1, 2], :x_name => "x", :y_name => "y"),
Dict(:prj => [2, 3], :x_name => "y", :y_name => "z"),
]
num_plts = length(data)
if !attitude
fig = create_figure((6, 10), options = fig_opts)
gspec = fig.add_gridspec(
ncols = 1,
nrows = num_plts + 1,
height_ratios = [0.05, fill(1, num_plts)...],
)
else
fig_opts_copy = convert(Dict{String,Any}, copy(fig_opts))
fig_opts_copy["axes.labelsize"] = 16
fig_opts_copy["xtick.labelsize"] = 14
fig_opts_copy["ytick.labelsize"] = 14
fig = create_figure((10, 10), options = fig_opts_copy)
gspec = fig.add_gridspec(
ncols = 3,
nrows = num_plts + 1,
height_ratios = [0.05, fill(1, num_plts)...],
width_ratios = [0.64, 0.06, 0.3],
)
end
axes = []
for i_plt = 1:num_plts
# Data
prj = data[i_plt][:prj]
ax_x_name = data[i_plt][:x_name]
ax_y_name = data[i_plt][:y_name]
ax = setup_axis!(
get(gspec, (i_plt, 0));
xlabel = @sprintf("LVLH \$%s\$ position [m]", ax_x_name),
ylabel = @sprintf("LVLH \$%s\$ position [m]", ax_y_name),
)
push!(axes, ax)
# Trajectory projection and bounding box
ct_pos_2d = ct_pos[prj, :]
dt_pos_2d = dt_pos[prj, :]
dt_thrust_2d = dt_thrust[prj, :]
bbox = Dict(
:x => Dict(:min => minimum(ct_pos_2d[1, :]), :max => maximum(ct_pos_2d[1, :])),
:y => Dict(:min => minimum(ct_pos_2d[2, :]), :max => maximum(ct_pos_2d[2, :])),
)
padding = Dict(
:x => pad_x * (bbox[:x][:max] - bbox[:x][:min]),
:y => pad_y * (bbox[:y][:max] - bbox[:y][:min]),
)
# Plot axes "guidelines"
origin = [0; 0]#mdl.traj.rf[prj]
xtip = [1; 0]
ytip = [0; 1]
ax.plot(
[origin[1], origin[1] + xtip[1] * axis_inf_scale],
[origin[2], origin[2] + xtip[2] * axis_inf_scale],
color = DarkBlue,
linewidth = 0.5,
solid_capstyle = "round",
)
ax.plot(
[origin[1], origin[1] + ytip[1] * axis_inf_scale],
[origin[2], origin[2] + ytip[2] * axis_inf_scale],
color = DarkBlue,
linewidth = 0.5,
solid_capstyle = "round",
)
# Plot the continuous-time trajectory
line_segs = Vector{RealMatrix}(undef, 0)
line_clrs = Vector{NTuple{4,RealValue}}(undef, 0)
overlap = 3
for k = 1:ct_res-overlap
push!(line_segs, ct_pos_2d[:, k:k+overlap]')
push!(line_clrs, v_cmap.to_rgba(ct_speed[k]))
end
trajectory = PyPlot.matplotlib.collections.LineCollection(
line_segs,
zorder = 10,
colors = line_clrs,
linewidths = 3,
)
ax.add_collection(trajectory)
# Plot the discrete-time trajectory
ax.scatter(
dt_pos_2d[1, :],
dt_pos_2d[2, :],
marker = "o",
c = DarkBlue,
s = 5,
edgecolors = "white",
linewidths = 0.2,
zorder = 20,
)
# Axis limits
pad_value = max(padding[:x], padding[:y])
xmin = bbox[:x][:min] - pad_value
xmax = bbox[:x][:max] + pad_value
ymin = bbox[:y][:min] - pad_value
ymax = bbox[:y][:max] + pad_value
# Detect zero-range axes
if (xmax - xmin) <= 1e-5
xmin, xmax = -1, 1
end
if (ymax - ymin) <= 1e-5
ymin, ymax = -1, 1
end
if !equal_ar
ax.set_xlim(xmin, xmax)
ax.set_ylim(ymin, ymax)
else
# First, try to leave ymax unconstrained
# Check to make sure that all y values contained in the resulting
# box
set_axis_equal(ax, (xmin, xmax, ymin, missing))
y_rng = collect(ax.get_ylim())
if (any(dt_pos_2d[2, :] .> y_rng[2]) || any(dt_pos_2d[2, :] .< y_rng[1]))
# The data does not fit, leave xmax unconstrained instead
set_axis_equal(ax, (xmin, missing, ymin, ymax))
end
end
x_rng = collect(ax.get_xlim())
y_rng = collect(ax.get_ylim())
# Thrust directions
ref_sz = min(x_rng[2] - x_rng[1], y_rng[2] - y_rng[1])
max_sz = maximum(mapslices(norm, dt_thrust_2d, dims = 1))
scale_factor = ref_sz / max_sz * input_scale
thrust_segs = Vector{RealMatrix}(undef, 0)
for k = 1:dt_res
thrust_whisker_base = dt_pos_2d[:, k]
thrust_whisker_tip = thrust_whisker_base + scale_factor * dt_thrust_2d[:, k]
push!(thrust_segs, hcat(thrust_whisker_base, thrust_whisker_tip)')
end
thrusts = PyPlot.matplotlib.collections.LineCollection(
thrust_segs,
zorder = 15,
colors = Red,
linewidths = 1.5,
)
thrusts.set_capstyle("round")
ax.add_collection(thrusts)
# Label the LVLH axes
ax.annotate(
@sprintf("\$\\hat %s_{\\mathcal L}\$", ax_x_name),
xy = (x_rng[2], origin[2]),
xytext = (-pad_label_abs, 0),
textcoords = "offset points",
ha = "right",
va = "center",
bbox = Dict(:pad => 2, :fc => "white", :ec => "none"),
)
ax.annotate(
@sprintf("\$\\hat %s_{\\mathcal L}\$", ax_y_name),
xy = (origin[1], y_rng[2]),
xytext = (0, pad_label_abs),
textcoords = "offset points",
ha = "center",
va = "center",
bbox = Dict(:pad => 2, :fc => "white", :ec => "none"),
)
# Plume cone
if 1 in prj
θ_sweep_sph = LinRange(0, 2 * pi, 100)
sph_perim = hcat(map(θ -> traj.r_plume * [cos(θ); sin(θ)], θ_sweep_sph)...)
sph_x = sph_perim[1, :]
sph_y = sph_perim[2, :]
ax.plot(
sph_x,
sph_y,
color = Red,
linewidth = 0.6,
solid_capstyle = "round",
solid_joinstyle = "round",
zorder = 4,
)
end
# Approach cone
if 1 in prj
# Side-view
θ_sweep = LinRange(-traj.θ_appch, traj.θ_appch, 100)
cone_perim = hcat(map(θ -> traj.r_appch * [cos(θ); sin(θ)], θ_sweep)...)
cone_x = [0; cone_perim[1, :]; 0]
cone_y = [0; cone_perim[2, :]; 0]
# Draw the approach sphere
θ_sweep_sph = LinRange(traj.θ_appch, 2 * pi - traj.θ_appch, 100)
sph_perim = hcat(map(θ -> traj.r_appch * [cos(θ); sin(θ)], θ_sweep_sph)...)
sph_x = sph_perim[1, :]
sph_y = sph_perim[2, :]
ax.plot(
sph_x,
sph_y,
color = DarkBlue,
linewidth = 0.6,
solid_capstyle = "round",
solid_joinstyle = "round",
zorder = 4,
)
else
# Front-on view
θ_sweep = LinRange(0, 2 * pi, 100)
r_intersect = traj.r_appch * sin(traj.θ_appch)
cone_perim = hcat(map(θ -> r_intersect * [cos(θ); sin(θ)], θ_sweep)...)
cone_x = cone_perim[1, :]
cone_y = cone_perim[2, :]
end
ax.plot(
cone_x,
cone_y,
color = Green,
linestyle = "--",
linewidth = 1.5,
solid_capstyle = "round",
solid_joinstyle = "round",
dash_capstyle = "round",
dash_joinstyle = "round",
dashes = (3, 3),
zorder = 5,
)
ax.invert_yaxis()
end
fig.align_ylabels(axes)
# Colorbar
cbar_ax = fig.add_subplot(get(gspec, (0, 0)))
fig.colorbar(
v_cmap,
aspect = 40,
label = "Velocity \$\\|v\\|_2\$ [m/s]",
orientation = "horizontal",
cax = cbar_ax,
)
cbar_ax.xaxis.set_label_position("top")
cbar_ax.xaxis.set_ticks_position("top")
ax_pos = cbar_ax.get_position()
ax_pos = [ax_pos.x0, ax_pos.y0 - 0.05, ax_pos.width, ax_pos.height]
cbar_ax.set_position(ax_pos)
cbar_ax.xaxis.labelpad = 10
# >> Timeseries plots <<
if attitude
marker_darken_factor = 0.3
outline_w = 1.5
y1_clr = Blue
y2_clr = Red
darker_y1_clr = darken_color(y1_clr, marker_darken_factor)
darker_y2_clr = darken_color(y2_clr, marker_darken_factor)
# Plotting data
dt_τ = sol.td
tf = sol.p[veh.id_t]
ct_t = ct_τ * tf
dt_t = dt_τ * tf
ct_r = hcat([sample(sol.xc, τ)[veh.id_r] for τ in ct_τ]...)
ct_q = hcat([sample(sol.xc, τ)[veh.id_q] for τ in ct_τ]...)
ct_rpy = mapslices(q -> collect(rpy(Quaternion(q))), ct_q, dims = 1)
ct_ω = hcat([sample(sol.xc, τ)[veh.id_ω] for τ in ct_τ]...)
dt_q = sol.xd[veh.id_q, :]
dt_rpy = mapslices(q -> collect(rpy(Quaternion(q))), dt_q, dims = 1)
dt_ω = sol.xd[veh.id_ω, :]
ct_rpy = rad2deg.(ct_rpy)
dt_rpy = rad2deg.(dt_rpy)
ct_ω = rad2deg.(ct_ω)
dt_ω = rad2deg.(dt_ω)
# Find approach and plume sphere entry times
k_appch = findfirst(k -> norm(ct_r[:, k]) <= traj.r_appch, 1:ct_res)
k_plume = findfirst(k -> norm(ct_r[:, k]) <= traj.r_plume, 1:ct_res)
t_appch = ct_t[k_appch]
t_plume = ct_t[k_plume]
data = [
Dict(
:y1_dt => dt_rpy[3, :],
:y1_ct => ct_rpy[3, :],
:y2_dt => dt_ω[1, :],
:y2_ct => ct_ω[1, :],
:y1_label => "Roll angle [\$^\\circ\$]",
:y2_label => "Body \$x\$ angular rate [\$^\\circ\$/s]",
),
Dict(
:y1_dt => dt_rpy[2, :],
:y1_ct => ct_rpy[2, :],
:y2_dt => dt_ω[2, :],
:y2_ct => ct_ω[2, :],
:y1_label => "Pitch angle [\$^\\circ\$]",
:y2_label => "Body \$y\$ angular rate [\$^\\circ\$/s]",
),
Dict(
:y1_dt => dt_rpy[1, :],
:y1_ct => ct_rpy[1, :],
:y2_dt => dt_ω[3, :],
:y2_ct => ct_ω[3, :],
:y1_label => "Yaw angle [\$^\\circ\$]",
:y2_label => "Body \$z\$ angular rate [\$^\\circ\$/s]",
),
]
ax_list = []
ax2_list = []
for i = 1:3
# Data
y1_data_dt = data[i][:y1_dt]
y1_data_ct = data[i][:y1_ct]
y2_data_dt = data[i][:y2_dt]
y2_data_ct = data[i][:y2_ct]
ax = setup_axis!(get(gspec, (i, 2)), tight = "both", xlabel = "Time [s]")
ax2 = ax.twinx()
push!(ax_list, ax)
push!(ax2_list, ax2)
ax.set_ylabel(data[i][:y1_label], color = y1_clr)
ax.tick_params(axis = "y", colors = y1_clr)
ax.spines."left".set_edgecolor(y1_clr)
ax2.set_ylabel(data[i][:y2_label], color = y2_clr)
ax2.tick_params(axis = "y", colors = y2_clr)
ax2.spines."right".set_edgecolor(y2_clr)
ax2.spines."top".set_visible(false)
ax2.spines."bottom".set_visible(false)
ax2.spines."left".set_visible(false)
ax2.set_axisbelow(true)
# Continuous-time trajectory
ax.plot(
ct_t,
y1_data_ct,
color = y1_clr,
linewidth = 2,
solid_joinstyle = "round",
solid_capstyle = "round",
clip_on = false,
zorder = 10,
)
ax2.plot(
ct_t,
y2_data_ct,
color = y2_clr,
linewidth = 2,
solid_joinstyle = "round",
solid_capstyle = "round",
clip_on = false,
zorder = 10,
)
ax2.plot(
ct_t,
y2_data_ct,
color = "white",
linewidth = 2 + outline_w,
solid_joinstyle = "round",
solid_capstyle = "round",
clip_on = false,
zorder = 9,
)
# Discrete-time trajectory
ax.plot(
dt_t,
y1_data_dt,
linestyle = "none",
marker = "o",
markersize = 3.5,
markerfacecolor = darker_y1_clr,
markeredgecolor = "white",
markeredgewidth = 0.2,
clip_on = false,
zorder = 20,
)
ax2.plot(
dt_t,
y2_data_dt,
linestyle = "none",
marker = "o",
markersize = 3.5,
markerfacecolor = darker_y2_clr,
markeredgecolor = "white",
markeredgewidth = 0.2,
clip_on = false,
zorder = 20,
)
ax2.plot(
dt_t,
y2_data_dt,
linestyle = "none",
marker = "o",
markersize = 3.5 + outline_w,
markerfacecolor = "white",
markeredgewidth = 0,
clip_on = false,
zorder = 9,
)
# Plot reflines for the approach and plume sphere entry times
label_text = Dict(t_appch => "Approach", t_plume => "Plume")
ax_ylim = ax.get_ylim()
for t in [t_appch, t_plume]
ax.axvline(
x = t,
color = time_mark_clr,
linestyle = "--",
linewidth = 0.7,
dash_joinstyle = "round",
dash_capstyle = "round",
dashes = (3, 3),
zorder = 5,
)
ax.annotate(
label_text[t],
color = time_mark_clr,
xy = (t, ax_ylim[2]),
xytext = (0, -10),#0.1*(ax_ylim[2]-ax_ylim[1])),
textcoords = "offset points",
ha = "center",
va = "top",
rotation = 90,
zorder = 5,
bbox = Dict(:pad => 1, :fc => "white", :ec => "none"),
)
end
# Make an x tick for the final time
ax_xticks = ax.get_xticks()
ax_xlim = ax.get_xlim()
if ax_xticks[end] != tf
push!(ax_xticks, tf)
end
ax.set_xticks(ax_xticks)
ax.set_xlim(ax_xlim)
end
fig.align_ylabels(ax_list[1:3])
fig.align_ylabels(ax2_list[1:3])
end
if attitude
save_figure(
"rendezvous_3d_trajectory_2d_with_attitude.pdf",
algo,
tight_layout = false,
)
else
save_figure("rendezvous_3d_trajectory_2d.pdf", algo, tight_layout = false)
end
return nothing
end
"""
plot_state_timeseries(mdl, sol)
Plot the state component evolution as a function of time.
# Arguments
- `mdl`: the problem description object.
- `sol`: the problem solution.
"""
function plot_state_timeseries(mdl::RendezvousProblem, sol::SCPSolution)::Nothing
# Parameters
algo = sol.algo
veh = mdl.vehicle
traj = mdl.traj
marker_darken_factor = 0.3
outline_w = 1.5
y1_clr = Blue
y2_clr = Red
darker_y1_clr = darken_color(y1_clr, marker_darken_factor)
darker_y2_clr = darken_color(y2_clr, marker_darken_factor)
# >> Plotting data <<
ct_res = 1000
dt_τ = sol.td
ct_τ = RealVector(LinRange(0, 1, ct_res))
tf = sol.p[veh.id_t]
ct_t = ct_τ * tf
dt_t = dt_τ * tf
ct_r = hcat([sample(sol.xc, τ)[veh.id_r] for τ in ct_τ]...)
ct_v = hcat([sample(sol.xc, τ)[veh.id_v] for τ in ct_τ]...)
ct_q = hcat([sample(sol.xc, τ)[veh.id_q] for τ in ct_τ]...)
ct_rpy = mapslices(q -> collect(rpy(Quaternion(q))), ct_q, dims = 1)
ct_ω = hcat([sample(sol.xc, τ)[veh.id_ω] for τ in ct_τ]...)
dt_r = sol.xd[veh.id_r, :]
dt_v = sol.xd[veh.id_v, :]
dt_q = sol.xd[veh.id_q, :]
dt_rpy = mapslices(q -> collect(rpy(Quaternion(q))), dt_q, dims = 1)
dt_ω = sol.xd[veh.id_ω, :]
ct_rpy = rad2deg.(ct_rpy)
dt_rpy = rad2deg.(dt_rpy)
ct_ω = rad2deg.(ct_ω)
dt_ω = rad2deg.(dt_ω)
# Find approach and plume sphere entry times
k_appch = findfirst(k -> norm(ct_r[:, k]) <= traj.r_appch, 1:ct_res)
k_plume = findfirst(k -> norm(ct_r[:, k]) <= traj.r_plume, 1:ct_res)
t_appch = ct_t[k_appch]
t_plume = ct_t[k_plume]
data = [
Dict(
:y1_dt => dt_r[1, :],
:y1_ct => ct_r[1, :],
:y2_dt => dt_v[1, :],
:y2_ct => ct_v[1, :],
:y1_label => "LVLH \$x\$ position [m]",
:y2_label => "LVLH \$x\$ velocity [m/s]",
),
Dict(
:y1_dt => dt_r[2, :],
:y1_ct => ct_r[2, :],
:y2_dt => dt_v[2, :],
:y2_ct => ct_v[2, :],
:y1_label => "LVLH \$y\$ position [m]",
:y2_label => "LVLH \$y\$ velocity [m/s]",
),
Dict(
:y1_dt => dt_r[3, :],
:y1_ct => ct_r[3, :],
:y2_dt => dt_v[3, :],
:y2_ct => ct_v[3, :],
:y1_label => "LVLH \$z\$ position [m]",
:y2_label => "LVLH \$z\$ velocity [m/s]",
:x_label => "Time [s]",
),
Dict(
:y1_dt => dt_rpy[3, :],
:y1_ct => ct_rpy[3, :],
:y2_dt => dt_ω[1, :],
:y2_ct => ct_ω[1, :],
:y1_label => "Roll angle [\$^\\circ\$]",
:y2_label => "Body \$x\$ angular rate [\$^\\circ\$/s]",
),
Dict(
:y1_dt => dt_rpy[2, :],
:y1_ct => ct_rpy[2, :],
:y2_dt => dt_ω[2, :],
:y2_ct => ct_ω[2, :],
:y1_label => "Pitch angle [\$^\\circ\$]",
:y2_label => "Body \$y\$ angular rate [\$^\\circ\$/s]",
),
Dict(
:y1_dt => dt_rpy[1, :],
:y1_ct => ct_rpy[1, :],
:y2_dt => dt_ω[3, :],
:y2_ct => ct_ω[3, :],
:y1_label => "Yaw angle [\$^\\circ\$]",
:y2_label => "Body \$z\$ angular rate [\$^\\circ\$/s]",
:x_label => "Time [s]",
),
]
# >> Initialize figure <<
fig = create_figure((10, 10), options = fig_opts)
gspec = fig.add_gridspec(ncols = 2, nrows = 3)
id_splot = [1 2; 3 4; 5 6]
ax_list = []
ax2_list = []
for i = 1:length(data)
# Data
y1_data_dt = data[i][:y1_dt]
y1_data_ct = data[i][:y1_ct]
y2_data_dt = data[i][:y2_dt]
y2_data_ct = data[i][:y2_ct]
j = id_splot[i]
ax = setup_axis!(get(gspec, (j - 1)), tight = "both")
ax2 = ax.twinx()
push!(ax_list, ax)
push!(ax2_list, ax2)
if :x_label in keys(data[i])
ax.set_xlabel(data[i][:x_label])
else
ax.tick_params(
axis = "x",
which = "both",
bottom = false,
top = false,
labelbottom = false,
)
end
ax.set_ylabel(data[i][:y1_label], color = y1_clr)
ax.tick_params(axis = "y", colors = y1_clr)
ax.spines."left".set_edgecolor(y1_clr)
ax2.set_ylabel(data[i][:y2_label], color = y2_clr)
ax2.tick_params(axis = "y", colors = y2_clr)
ax2.spines."right".set_edgecolor(y2_clr)
ax2.spines."top".set_visible(false)
ax2.spines."bottom".set_visible(false)
ax2.spines."left".set_visible(false)
ax2.set_axisbelow(true)
# Continuous-time trajectory
ax.plot(
ct_t,
y1_data_ct,
color = y1_clr,
linewidth = 2,
solid_joinstyle = "round",
solid_capstyle = "round",
clip_on = false,
zorder = 10,
)
ax2.plot(
ct_t,
y2_data_ct,
color = y2_clr,
linewidth = 2,
solid_joinstyle = "round",
solid_capstyle = "round",
clip_on = false,
zorder = 10,
)
ax2.plot(
ct_t,
y2_data_ct,
color = "white",
linewidth = 2 + outline_w,
solid_joinstyle = "round",
solid_capstyle = "round",
clip_on = false,
zorder = 9,
)
# Discrete-time trajectory
ax.plot(
dt_t,
y1_data_dt,
linestyle = "none",
marker = "o",
markersize = 3.5,
markerfacecolor = darker_y1_clr,
markeredgecolor = "white",
markeredgewidth = 0.2,
clip_on = false,
zorder = 20,
)
ax2.plot(
dt_t,
y2_data_dt,
linestyle = "none",
marker = "o",
markersize = 3.5,
markerfacecolor = darker_y2_clr,
markeredgecolor = "white",
markeredgewidth = 0.2,
clip_on = false,
zorder = 20,
)
ax2.plot(
dt_t,
y2_data_dt,
linestyle = "none",
marker = "o",
markersize = 3.5 + outline_w,
markerfacecolor = "white",
markeredgewidth = 0,
clip_on = false,
zorder = 9,
)
# Plot reflines for the approach and plume sphere entry times
label_text = Dict(t_appch => "Approach", t_plume => "Plume")
ax_ylim = ax.get_ylim()
for t in [t_appch, t_plume]
ax.axvline(
x = t,
color = time_mark_clr,
linestyle = "--",
linewidth = 0.7,
dash_joinstyle = "round",
dash_capstyle = "round",
dashes = (3, 3),
zorder = 5,
)
ax.annotate(
label_text[t],
color = time_mark_clr,
xy = (t, ax_ylim[2]),
xytext = (0, -10),#0.1*(ax_ylim[2]-ax_ylim[1])),
textcoords = "offset points",
ha = "center",
va = "top",
rotation = 90,
zorder = 5,
bbox = Dict(:pad => 1, :fc => "white", :ec => "none"),
)
end
# Make an x tick for the final time
ax_xticks = ax.get_xticks()
ax_xlim = ax.get_xlim()
if ax_xticks[end] != tf
push!(ax_xticks, tf)
end
ax.set_xticks(ax_xticks)
ax.set_xlim(ax_xlim)
# Make a y-tick for the largest time for yaw
if i == 3
ax_yticks = ax.get_yticks()
ax_ylim = ax.get_ylim()
max_val = maximum(y1_data_ct)
if ax_yticks[end] != max_val
push!(ax_yticks, max_val)
end
ax.set_yticks(ax_yticks)
ax.set_ylim(ax_ylim)
end
end
fig.align_ylabels(ax_list[1:3])
fig.align_ylabels(ax2_list[1:3])
fig.align_ylabels(ax_list[4:6])
fig.align_ylabels(ax2_list[4:6])
save_figure("rendezvous_3d_timeseries.pdf", algo)
return nothing
end
"""
plot_control(mdl, sol[; quad])
Plot the control inputs versus time.
# Arguments
- `mdl`: the problem description object.
- `sol`: the problem solution.
- `history`: SCP iteration data history.
# Keywords
- `quad`: (optional): which quad in particular to plot the inputs for.
"""
function plot_inputs(
mdl::RendezvousProblem,
sol::SCPSolution,
history::SCPHistory;
quad::Optional{String} = nothing,
)::Nothing
# Parameters
interm_sol = [spbm.sol for spbm in history.subproblems]
num_iter = length(interm_sol)
algo = sol.algo
veh = mdl.vehicle
traj = mdl.traj
spread = 0.4
stem_colors = [Red, Green, Blue, DarkBlue]
marker_darken_factor = 0.3
padx = 0.05
polar_resol = 1000
axis_label_size = 13
# Plotting data
dt_τ = sol.td
tf = sol.p[veh.id_t]
dt_t = dt_τ * tf
dt_res = length(dt_τ)
Δt = tf / (dt_res - 1)
t_spread = Δt * spread / 2
# Find approach and plume sphere entry times
ct_res = 1000
ct_τ = RealVector(LinRange(0, 1, ct_res))
ct_t = ct_τ * tf
ct_pos = hcat([sample(sol.xc, τ)[veh.id_r] for τ in ct_τ]...)
k_appch = findfirst(k -> norm(ct_pos[:, k]) <= traj.r_appch, 1:ct_res)
k_plume = findfirst(k -> norm(ct_pos[:, k]) <= traj.r_plume, 1:ct_res)
t_appch = ct_t[k_appch]
t_plume = ct_t[k_plume]
# Get RCS controls solution history
f_quad = Dict[]
f_quad_ref = Dict[]
hom_val = Real[]
for i = 1:num_iter
f = interm_sol[i].ud[veh.id_rcs, :]
f_ref = interm_sol[i].ud[veh.id_rcs_ref, :]
push!(f_quad, Dict())
push!(f_quad_ref, Dict())
push!(hom_val, interm_sol[i].bay[:hom])
for quad in (:A, :B, :C, :D)
_f_quad = RealVector[]
_f_quad_ref = RealVector[]
for thruster in (:pf, :pa, :rf, :ra)
push!(_f_quad, f[veh.csm.rcs_select[quad, thruster], :])
push!(_f_quad_ref, f_ref[veh.csm.rcs_select[quad, thruster], :])
end
f_quad[i][quad] = hcat(_f_quad...)'
f_quad_ref[i][quad] = hcat(_f_quad_ref...)'
end
end
dirs = [
"\$+\\hat x_{\\mathcal B}\$",
"\$-\\hat x_{\\mathcal B}\$",
"\$+\\hat y_{\\mathcal B}\$ (A)",
"\$-\\hat y_{\\mathcal B}\$ (A)",
]
if !isnothing(quad)
if quad == "A"
dirs[3] = "\$+\\hat y_{\\mathcal B}\$"
dirs[4] = "\$-\\hat y_{\\mathcal B}\$"
elseif quad == "B"
dirs[3] = "\$+\\hat z_{\\mathcal B}\$"
dirs[4] = "\$-\\hat z_{\\mathcal B}\$"
elseif quad == "C"
dirs[3] = "\$-\\hat y_{\\mathcal B}\$"
dirs[4] = "\$+\\hat y_{\\mathcal B}\$"
elseif quad == "D"
dirs[3] = "\$-\\hat z_{\\mathcal B}\$"
dirs[4] = "\$+\\hat z_{\\mathcal B}\$"
end
end
thruster_label = (i) -> @sprintf("Thruster %s", dirs[i])
data = [
Dict(
:u => (i) -> f_quad[i][:A],
:u_ref => (i) -> f_quad_ref[i][:A],
:ylabel =>
"Quad A impulse, \$\\Delta t_{ik}F_{\\mathrm{rcs}}\$" *
" [\\si{\\newton\\second}]",
:legend => thruster_label,
),
Dict(
:u => (i) -> f_quad[i][:B],
:u_ref => (i) -> f_quad_ref[i][:B],
:ylabel =>
"Quad B impulse, \$\\Delta t_{ik}F_{\\mathrm{rcs}}\$" *
" [\\si{\\newton\\second}]",
:legend => thruster_label,
),
Dict(
:u => (i) -> f_quad[i][:C],
:u_ref => (i) -> f_quad_ref[i][:C],
:ylabel =>
"Quad C impulse, \$\\Delta t_{ik}F_{\\mathrm{rcs}}\$" *
" [\\si{\\newton\\second}]",
:legend => thruster_label,
),
Dict(
:u => (i) -> f_quad[i][:D],
:u_ref => (i) -> f_quad_ref[i][:D],
:ylabel =>
"Quad D impulse, \$\\Delta t_{ik}F_{\\mathrm{rcs}}\$" *
" [\\si{\\newton\\second}]",
:legend => thruster_label,
),
]
if !isnothing(quad)
data_map = Dict("A" => 1, "B" => 2, "C" => 3, "D" => 4)
data = [data[data_map[quad]]]