/
waypoint_test.go
199 lines (179 loc) · 6.12 KB
/
waypoint_test.go
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
package smd
import (
"testing"
"time"
"github.com/gonum/floats"
)
func TestLoiter(t *testing.T) {
action := &WaypointAction{ADDCARGO, nil}
wp := NewLoiter(time.Duration(1)*time.Minute, action)
if wp.Cleared() {
t.Fatal("Waypoint was cleared at creation.")
}
initTime := time.Unix(0, 0)
o := *NewOrbitFromRV([]float64{100, 0, 0}, []float64{0, 0, 0}, Sun)
ctrl, reached := wp.ThrustDirection(o, initTime)
dV := ctrl.Control(o)
if reached {
t.Fatal("Loiter waypoint was reached too early.")
}
if Norm(dV) != 0 {
t.Fatal("Loiter waypoint required a velocity change.")
}
if wp.Action() != nil {
t.Fatal("Loiter waypoint returned an action before being reached.")
}
o = *NewOrbitFromRV([]float64{100, 0, 0}, []float64{0, 0, 0}, Sun)
ctrl, reached = wp.ThrustDirection(o, initTime.Add(time.Duration(1)*time.Second))
dV = ctrl.Control(o)
if reached {
t.Fatal("Loiter waypoint was reached too early.")
}
if Norm(dV) != 0 {
t.Fatal("Loiter waypoint required a velocity change.")
}
o = *NewOrbitFromRV([]float64{100, 0, 0}, []float64{0, 0, 0}, Sun)
ctrl, reached = wp.ThrustDirection(o, initTime.Add(time.Duration(1)*time.Minute))
dV = ctrl.Control(o)
if !reached {
t.Fatal("Loiter waypoint was not reached as it should have been.")
}
if Norm(dV) != 0 {
t.Fatal("Reached loiter waypoint returned a velocity change after being reached.")
}
if wp.Action() == nil {
t.Fatal("Loiter waypoint did not return any action after being reached.")
}
if len(wp.String()) == 0 {
t.Fatal("Loiter waypoint string is empty.")
}
}
func TestHohmannΔv(t *testing.T) {
target := *NewOrbitFromOE(Earth.Radius+35781.34857, 0, 0, 0, 0, 90, Earth)
oscul := *NewOrbitFromOE(Earth.Radius+191.34411, 0, 0, 0, 0, 90, Earth)
ΔvFinalExp := -1.478187
ΔvInitExp := 2.457038
ΔvThrustingPlus := []float64{1, 0, 0}
ΔvThrustingMinus := []float64{-1, 0, 0}
ΔvCoasting := []float64{0, 0, 0}
tofExp := time.Duration(5)*time.Hour + time.Duration(15)*time.Minute + time.Duration(24)*time.Second
initDT := time.Date(2017, 1, 20, 12, 13, 14, 15, time.UTC)
apoDT := initDT.Add(tofExp)
postApoDT := initDT.Add(tofExp + StepSize)
assertPanic(t, func() {
tgt := *NewOrbitFromOE(Earth.Radius+35781.34857, 0.5, 0, 0, 0, 90, Earth)
NewHohmannTransfer(tgt, nil)
})
wp := NewHohmannTransfer(target, nil)
assertPanic(t, func() {
osc := *NewOrbitFromOE(Earth.Radius+191.34411, eccentricityε, 0, 0, 0, 180, Earth)
wp.ThrustDirection(osc, initDT)
})
assertPanic(t, func() {
osc := *NewOrbitFromOE(Earth.Radius+191.34411, 0.5, 0, 0, 0, 90, Earth)
wp.ThrustDirection(osc, initDT)
})
assertPanic(t, func() {
osc := *NewOrbitFromOE(Earth.Radius+191.34411, eccentricityε, 45, 0, 0, 90, Earth)
wp.ThrustDirection(osc, initDT)
})
ctrl, cleared := wp.ThrustDirection(oscul, initDT)
if cleared {
t.Fatalf("Hohmann waypoint cleared on initial call")
}
Δv := ctrl.Control(oscul)
// The Precompute was just called, let's check the values.
if !floats.EqualWithinAbs(wp.ctrl.ΔvInit, ΔvInitExp, velocityε) {
t.Fatalf("ΔvInit=%f != %f", wp.ctrl.ΔvInit, ΔvInitExp)
}
if !floats.EqualWithinAbs(wp.ctrl.ΔvFinal, ΔvFinalExp, velocityε) {
t.Fatalf("ΔvFinal=%f != %f", wp.ctrl.ΔvFinal, ΔvFinalExp)
}
if !vectorsEqual(Δv, ΔvThrustingPlus) {
t.Fatalf("expected Hohmann thrusting positively, instead got: %+v", Δv)
}
// Let's increase the velocity norm simply to simulate that the initial Δv was applied.
R, V := oscul.RV()
V[0] += ΔvInitExp
oscul = *NewOrbitFromRV(R, V, oscul.Origin)
ctrl, cleared = wp.ThrustDirection(oscul, initDT)
if cleared {
t.Fatalf("Hohmann waypoint cleared on second call")
}
Δv = ctrl.Control(oscul)
if !vectorsEqual(Δv, ΔvCoasting) {
t.Fatalf("expected Hohmann coasting, instead got: %+v", Δv)
}
// Let's increase the date to when we are supposed to do the final burn.
ctrl, cleared = wp.ThrustDirection(oscul, apoDT)
if cleared {
t.Fatalf("Hohmann waypoint cleared on third call")
}
Δv = ctrl.Control(oscul)
if !vectorsEqual(Δv, ΔvThrustingMinus) {
t.Fatalf("expected Hohmann thrusting negatively, instead got: %+v", Δv)
}
// Let's increase the velocity norm simply to simulate that the final Δv was applied.
R, V = oscul.RV()
V[0] += ΔvFinalExp
oscul = *NewOrbitFromRV(R, V, oscul.Origin)
ctrl, cleared = wp.ThrustDirection(oscul, apoDT)
if cleared {
t.Fatalf("Hohmann waypoint cleared on fourth call")
}
Δv = ctrl.Control(oscul)
if !vectorsEqual(Δv, ΔvCoasting) {
t.Fatalf("expected Hohmann coasting, instead got: %+v", Δv)
}
// Let's increase the date to when we are supposed to do the final burn.
ctrl, cleared = wp.ThrustDirection(oscul, postApoDT)
if !cleared {
t.Fatalf("Hohmann waypoint cleared on final call")
}
Δv = ctrl.Control(oscul)
if !vectorsEqual(Δv, ΔvCoasting) {
t.Fatalf("expected Hohmann coasting, instead got: %+v", Δv)
}
}
func TestToElliptical(t *testing.T) {
// Example action
ref2Mars := WaypointAction{Type: REFMARS, Cargo: nil}
wp := NewToElliptical(&ref2Mars)
dt := time.Unix(0, 0)
// Generate an evident hyperbolic orbit
o := Earth.HelioOrbit(dt)
o.ToXCentric(Earth, dt.Add(time.Duration(7*24)*time.Hour))
ctrl, cleared := wp.ThrustDirection(o, dt)
if cleared {
t.Fatal("cleared was true for hyperbolic orbit")
}
if ctrl.Type() != antiTangential {
t.Fatal("expected the control to be antiTangential")
}
o = *NewOrbitFromOE(Earth.Radius+191.34411, 0.2, 0, 0, 0, 90, Earth)
_, cleared = wp.ThrustDirection(o, dt)
if !cleared {
t.Fatal("cleared was false for elliptical orbit")
}
}
func TestToHyperbolic(t *testing.T) {
// Example action
ref2Sun := WaypointAction{Type: REFSUN, Cargo: nil}
wp := NewToHyperbolic(&ref2Sun)
dt := time.Unix(0, 0)
o := *NewOrbitFromOE(Earth.Radius+191.34411, 0.2, 0, 0, 0, 90, Earth)
ctrl, cleared := wp.ThrustDirection(o, dt)
if cleared {
t.Fatal("cleared was true for elliptical orbit")
}
if ctrl.Type() != tangential {
t.Fatal("expected the control to be antiTangential")
}
// Generate an evident hyperbolic orbit
o = Earth.HelioOrbit(dt)
o.ToXCentric(Earth, dt.Add(time.Duration(7*24)*time.Hour))
_, cleared = wp.ThrustDirection(o, dt)
if !cleared {
t.Fatal("cleared was false for hyperbolic orbit")
}
}