This repository has been archived by the owner on Aug 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwaypoints.go
385 lines (335 loc) · 10.8 KB
/
waypoints.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
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
package smd
import (
"fmt"
"time"
)
// WaypointActionEnum defines the possible waypoint actions.
type WaypointActionEnum uint8
const (
// ADDCARGO is a waypoint action associated to a piece of cargo
ADDCARGO WaypointActionEnum = iota + 1
// DROPCARGO is the opposite of ADD
DROPCARGO
// REFEARTH switches the orbit reference to the Earth
REFEARTH
// REFMARS switches the orbit reference to Mars
REFMARS
//REFSUN switches the orbit reference to the Sun
REFSUN
)
// WaypointAction defines what happens when a given waypoint is reached.
type WaypointAction struct {
Type WaypointActionEnum
Cargo *Cargo
}
// Waypoint defines the Waypoint interface.
type Waypoint interface {
Cleared() bool // returns whether waypoint has been reached
Action() *WaypointAction
ThrustDirection(Orbit, time.Time) (ThrustControl, bool)
String() string
}
// NewOutwardSpiral defines a new outward spiral from a celestial object.
func NewOutwardSpiral(body CelestialObject, action *WaypointAction) *ReachDistance {
if action != nil && action.Type == REFSUN {
// This is handled by the SetState function of Mission and the propagator
// will crash if there are multiple attempts to switch to another
action = nil
}
return &ReachDistance{body.SOI, action, true, false}
}
// Loiter is a type of waypoint which allows the vehicle to stay at a given position for a given duration.
type Loiter struct {
duration time.Duration
startDT time.Time
endDT time.Time
startedLoitering bool
action *WaypointAction
cleared bool
}
// String implements the Waypoint interface.
func (wp *Loiter) String() string {
return fmt.Sprintf("Coasting for %s.", wp.duration)
}
// Cleared implements the Waypoint interface.
func (wp *Loiter) Cleared() bool {
return wp.cleared
}
// ThrustDirection implements the Waypoint interface.
func (wp *Loiter) ThrustDirection(o Orbit, dt time.Time) (ThrustControl, bool) {
dv := Coast{}
if !wp.startedLoitering {
// First time this is called, starting timer.
wp.startedLoitering = true
wp.startDT = dt
wp.endDT = dt.Add(wp.duration)
return dv, false
}
if dt.Before(wp.endDT) {
return dv, false
}
wp.cleared = true
return dv, true
}
// Action implements the Waypoint interface.
func (wp *Loiter) Action() *WaypointAction {
if wp.cleared {
return wp.action
}
return nil
}
// NewLoiter defines a new loitering waypoint, i.e. "wait until a given time".
func NewLoiter(duration time.Duration, action *WaypointAction) *Loiter {
return &Loiter{duration, time.Unix(0, 0), time.Unix(0, 0), false, action, false}
}
// ReachDistance is a type of waypoint which thrusts until a given distance is reached from the central body.
type ReachDistance struct {
distance float64
action *WaypointAction
further, cleared bool
}
// String implements the Waypoint interface.
func (wp *ReachDistance) String() string {
return fmt.Sprintf("Reach distance of %.1f km.", wp.distance)
}
// Cleared implements the Waypoint interface.
func (wp *ReachDistance) Cleared() bool {
return wp.cleared
}
// ThrustDirection implements the Waypoint interface.
func (wp *ReachDistance) ThrustDirection(o Orbit, dt time.Time) (ThrustControl, bool) {
if wp.further {
if o.RNorm() >= wp.distance {
wp.cleared = true
return Coast{}, true
}
return Tangential{}, false
}
if o.RNorm() <= wp.distance {
wp.cleared = true
return Coast{}, true
}
return AntiTangential{}, false
}
// Action implements the Waypoint interface.
func (wp *ReachDistance) Action() *WaypointAction {
return wp.action
}
// NewReachDistance defines a new spiral until a given distance is reached.
func NewReachDistance(distance float64, further bool, action *WaypointAction) *ReachDistance {
return &ReachDistance{distance, action, further, false}
}
// OrbitTarget allows to target an orbit.
type OrbitTarget struct {
target Orbit
ctrl *OptimalΔOrbit
action *WaypointAction
xprt *ThurstAngleExport
cleared bool
}
// String implements the Waypoint interface.
func (wp *OrbitTarget) String() string {
return fmt.Sprintf("targeting orbit")
}
// Cleared implements the Waypoint interface.
func (wp *OrbitTarget) Cleared() bool {
return wp.cleared
}
// Action implements the Waypoint interface.
func (wp *OrbitTarget) Action() *WaypointAction {
if wp.cleared {
return wp.action
}
return nil
}
// SetEpsilons allows to set the epsilons of the control law
func (wp *OrbitTarget) SetEpsilons(distanceε, eccentricityε, angleε float64) {
wp.ctrl.SetEpsilons(distanceε, eccentricityε, angleε)
}
// ThrustDirection implements the optimal orbit target.
func (wp *OrbitTarget) ThrustDirection(o Orbit, dt time.Time) (ThrustControl, bool) {
if ok, err := wp.target.EqualsWithin(o, wp.ctrl.Distanceε, wp.ctrl.Eccentricityε, wp.ctrl.Angleε); ok {
wp.cleared = true
} else if wp.ctrl.cleared {
fmt.Printf("[WARNING] OrbitTarget reached @%s *but* %s: %s\n", dt, err, o.String())
wp.cleared = true
}
if wp.xprt != nil {
α, β := anglesFromUnitΔv(Unit(wp.ctrl.Control(o)))
wp.xprt.Store(dt, Rad2deg(α), Rad2deg(β))
}
return wp.ctrl, wp.cleared
}
// ThrustDirection implements the optimal orbit target.
func (wp *OrbitTarget) SetExport(t *ThurstAngleExport) {
wp.xprt = t
}
// NewOrbitTarget defines a new orbit target.
func NewOrbitTarget(target Orbit, action *WaypointAction, meth ControlLawType, laws ...ControlLaw) *OrbitTarget {
if target.Periapsis() < target.Origin.Radius || target.Apoapsis() < target.Origin.Radius {
fmt.Printf("[WARNING] Target orbit on collision course with %s\n", target.Origin)
}
return &OrbitTarget{target, NewOptimalΔOrbit(target, meth, laws), action, nil, false}
}
// HohmannTransfer allows to perform an Hohmann transfer.
type HohmannTransfer struct {
action *WaypointAction
ctrl HohmannΔv
arrivalDT time.Time
cleared bool
}
// String implements the Waypoint interface.
func (wp *HohmannTransfer) String() string {
return fmt.Sprintf("Hohmann transfer")
}
// Cleared implements the Waypoint interface.
func (wp *HohmannTransfer) Cleared() bool {
return wp.cleared
}
// Action implements the Waypoint interface.
func (wp *HohmannTransfer) Action() *WaypointAction {
if wp.cleared {
return wp.action
}
return nil
}
// ThrustDirection implements the optimal orbit target.
func (wp *HohmannTransfer) ThrustDirection(o Orbit, dt time.Time) (ThrustControl, bool) {
switch wp.ctrl.status {
case hohmannCompute:
wp.ctrl.Precompute(o)
// Update the upcoming status of Hohmann
wp.ctrl.status = hohmmanInitΔv
// Initialize the Δv with the current knowledge.
wp.ctrl.ΔvBurnInit = o.VNorm()
// Compute the arrivial DT
wp.arrivalDT = dt.Add(wp.ctrl.tof)
break
case hohmmanInitΔv:
// Nothing to do.
case hohmmanCoast:
if dt.After(wp.arrivalDT.Add(-StepSize)) {
// Next step will be the arrivial DT.
wp.ctrl.status = hohmmanFinalΔv
// Initialize the Δv with the current knowledge.
wp.ctrl.ΔvBurnInit = o.VNorm()
}
case hohmmanFinalΔv:
// Nothing to do.
case hohmmanCompleted:
// This state is changed in the control. Hence, the cleared status is only
// available until the *subsequent* call to ThrustDirection.
wp.cleared = true
}
return &wp.ctrl, wp.cleared
}
// NewHohmannTransfer defines a new Hohmann transfer
func NewHohmannTransfer(target Orbit, action *WaypointAction) *HohmannTransfer {
if target.Periapsis() < target.Origin.Radius || target.Apoapsis() < target.Origin.Radius {
fmt.Printf("[WARNING] Target orbit on collision course with %s\n", target.Origin)
}
epoch := time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)
return &HohmannTransfer{action, NewHohmannΔv(target), epoch, false}
}
// ToElliptical decelerates the vehicle until its orbit is elliptical.
type ToElliptical struct {
action *WaypointAction
cleared bool
}
// String implements the Waypoint interface.
func (wp *ToElliptical) String() string {
return fmt.Sprintf("to elliptical")
}
// Cleared implements the Waypoint interface.
func (wp *ToElliptical) Cleared() bool {
return wp.cleared
}
// Action implements the Waypoint interface.
func (wp *ToElliptical) Action() *WaypointAction {
if wp.cleared {
return wp.action
}
return nil
}
// ThrustDirection implements the optimal orbit target.
func (wp *ToElliptical) ThrustDirection(o Orbit, dt time.Time) (ThrustControl, bool) {
// We use an arbitrary 0.9 eccentricity because if we set it just under the eccentricityε
// threshold, rounding errors make it such that it's still computed as hyperbolic. Also,
// such a high eccentricity is still good enough.
_, e, _, _, _, _, _, _, _ := o.Elements()
if e < 0.9+eccentricityε {
wp.cleared = true
}
return AntiTangential{"toElliptical"}, wp.cleared
}
// NewToElliptical defines a ToElliptical waypoint.
func NewToElliptical(action *WaypointAction) *ToElliptical {
return &ToElliptical{action, false}
}
// ToHyperbolic accelerates the vehicle until its orbit is elliptical.
type ToHyperbolic struct {
action *WaypointAction
cleared bool
}
// String implements the Waypoint interface.
func (wp *ToHyperbolic) String() string {
return fmt.Sprintf("to hyperbolic")
}
// Cleared implements the Waypoint interface.
func (wp *ToHyperbolic) Cleared() bool {
return wp.cleared
}
// Action implements the Waypoint interface.
func (wp *ToHyperbolic) Action() *WaypointAction {
if wp.cleared {
return wp.action
}
return nil
}
// ThrustDirection implements the optimal orbit target.
func (wp *ToHyperbolic) ThrustDirection(o Orbit, dt time.Time) (ThrustControl, bool) {
_, e, _, _, _, _, _, _, _ := o.Elements()
if e > 1.1 {
wp.cleared = true
}
return Tangential{"toHyperbolic"}, wp.cleared
}
// NewToHyperbolic defines a ToElliptical waypoint.
func NewToHyperbolic(action *WaypointAction) *ToHyperbolic {
return &ToHyperbolic{action, false}
}
// CruiseToDistance is a type of waypoint which cruises until a given distance is reached from the central body.
type CruiseToDistance struct {
distance float64
action *WaypointAction
further, cleared bool
}
// String implements the Waypoint interface.
func (wp *CruiseToDistance) String() string {
return fmt.Sprintf("Cruise distance of %.1f km.", wp.distance)
}
// Cleared implements the Waypoint interface.
func (wp *CruiseToDistance) Cleared() bool {
return wp.cleared
}
// ThrustDirection implements the Waypoint interface.
func (wp *CruiseToDistance) ThrustDirection(o Orbit, dt time.Time) (ThrustControl, bool) {
if wp.further {
if o.RNorm() >= wp.distance {
wp.cleared = true
}
return Coast{}, wp.cleared
}
if o.RNorm() <= wp.distance {
wp.cleared = true
}
return Coast{}, wp.cleared
}
// Action implements the Waypoint interface.
func (wp *CruiseToDistance) Action() *WaypointAction {
return wp.action
}
// NewCruiseToDistance defines a new cruising regime until a given distance is reached.
func NewCruiseToDistance(distance float64, further bool, action *WaypointAction) *CruiseToDistance {
return &CruiseToDistance{distance, action, further, false}
}