This repository was 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
Expand file tree
/
Copy pathprop.go
More file actions
544 lines (494 loc) · 15.8 KB
/
prop.go
File metadata and controls
544 lines (494 loc) · 15.8 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
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
package smd
import (
"fmt"
"math"
"time"
"github.com/gonum/floats"
)
// ControlLaw defines an enum of control laws.
type ControlLaw uint8
// ControlLawType defines the way to sum different Lyuapunov optimal CL
type ControlLawType uint8
type hohmannStatus uint8
const (
tangential ControlLaw = iota + 1
antiTangential
coast
multiOpti
hohmann
// OptiΔaCL allows to optimize thrust for semi major axis change
OptiΔaCL
// OptiΔiCL allows to optimize thrust for inclination change
OptiΔiCL
// OptiΔeCL allows to optimize thrust for eccentricity change
OptiΔeCL
// OptiΔΩCL allows to optimize thrust forRAAN change
OptiΔΩCL
// OptiΔωCL allows to optimize thrust for argument of perigee change
OptiΔωCL
// Ruggiero uses the eponym method of combining the control laws
Ruggiero ControlLawType = iota + 1
// Naasz is another type of combination of control law
Naasz
hohmannCompute hohmannStatus = iota + 1
hohmmanInitΔv
hohmmanFinalΔv
hohmmanCoast
hohmmanCompleted
)
func (cl ControlLaw) String() string {
switch cl {
case tangential:
return "tan"
case antiTangential:
return "aTan"
case coast:
return "coast"
case OptiΔaCL:
return "optiΔa"
case OptiΔeCL:
return "optiΔe"
case OptiΔiCL:
return "optiΔi"
case OptiΔΩCL:
return "optiΔΩ"
case OptiΔωCL:
return "optiΔω"
case multiOpti:
return "multiOpti"
case hohmann:
return "Hohmann"
}
panic("cannot stringify unknown control law")
}
func (meth ControlLawType) String() string {
switch meth {
case Ruggiero:
return "Ruggiero"
case Naasz:
return "Naasz"
}
panic("cannot stringify unknown control law summation method")
}
// ThrustControl defines a thrust control interface.
type ThrustControl interface {
Control(o Orbit) []float64
Type() ControlLaw
Reason() string
}
// GenericCL partially defines a ThrustControl.
type GenericCL struct {
reason string
cl ControlLaw
}
// Reason implements the ThrustControl interface.
func (cl GenericCL) Reason() string {
return cl.reason
}
// Type implements the ThrustControl interface.
func (cl GenericCL) Type() ControlLaw {
return cl.cl
}
func newGenericCLFromCL(cl ControlLaw) GenericCL {
return GenericCL{cl.String(), cl}
}
/* Let's define some control laws. */
// Coast defines an thrust control law which does not thrust.
type Coast struct {
reason string
}
// Reason implements the ThrustControl interface.
func (cl Coast) Reason() string {
return cl.reason
}
// Type implements the ThrustControl interface.
func (cl Coast) Type() ControlLaw {
return coast
}
// Control implements the ThrustControl interface.
func (cl Coast) Control(o Orbit) []float64 {
return []float64{0, 0, 0}
}
// Tangential defines a tangential thrust control law
type Tangential struct {
reason string
}
// Reason implements the ThrustControl interface.
func (cl Tangential) Reason() string {
return cl.reason
}
// Type implements the ThrustControl interface.
func (cl Tangential) Type() ControlLaw {
return tangential
}
// Control implements the ThrustControl interface.
func (cl Tangential) Control(o Orbit) []float64 {
return []float64{0, 1, 0}
}
// AntiTangential defines an antitangential thrust control law
type AntiTangential struct {
reason string
}
// Reason implements the ThrustControl interface.
func (cl AntiTangential) Reason() string {
return cl.reason
}
// Type implements the ThrustControl interface.
func (cl AntiTangential) Type() ControlLaw {
return antiTangential
}
// Control implements the ThrustControl interface.
func (cl AntiTangential) Control(o Orbit) []float64 {
return []float64{0, -1, 0}
}
/* Following optimal thrust change are from IEPC 2011's paper:
Low-Thrust Maneuvers for the Efficient Correction of Orbital Elements
A. Ruggiero, S. Marcuccio and M. Andrenucci */
func unitΔvFromAngles(α, β float64) []float64 {
sinα, cosα := math.Sincos(α)
sinβ, cosβ := math.Sincos(β)
return []float64{sinα * cosβ, cosα * cosβ, sinβ}
}
func anglesFromUnitΔv(Δv []float64) (α, β float64) {
β = math.Asin(Δv[2])
cosβ := math.Cos(β)
α = math.Asin(Δv[0] / cosβ)
return
}
// OptimalThrust is an optimal thrust.
type OptimalThrust struct {
ctrl func(o Orbit) []float64
GenericCL
}
// Control implements the ThrustControl interface.
func (cl OptimalThrust) Control(o Orbit) []float64 {
return cl.ctrl(o)
}
// NewOptimalThrust returns a new optimal Δe.
func NewOptimalThrust(cl ControlLaw, reason string) ThrustControl {
var ctrl func(o Orbit) []float64
switch cl {
case OptiΔaCL:
ctrl = func(o Orbit) []float64 {
_, e, _, _, _, ν, _, _, _ := o.Elements()
sinν, cosν := math.Sincos(ν)
return unitΔvFromAngles(math.Atan2(e*sinν, 1+e*cosν), 0.0)
}
break
case OptiΔeCL:
ctrl = func(o Orbit) []float64 {
_, cosE := o.SinCosE()
_, _, _, _, _, ν, _, _, _ := o.Elements()
sinν, cosν := math.Sincos(ν)
return unitΔvFromAngles(math.Atan2(sinν, cosν+cosE), 0.0)
}
break
case OptiΔiCL:
ctrl = func(o Orbit) []float64 {
_, _, _, _, ω, ν, _, _, _ := o.Elements()
return unitΔvFromAngles(0.0, Sign(math.Cos(ω+ν))*math.Pi/2)
}
break
case OptiΔΩCL:
ctrl = func(o Orbit) []float64 {
_, _, _, _, ω, ν, _, _, _ := o.Elements()
return unitΔvFromAngles(0.0, Sign(math.Sin(ω+ν))*math.Pi/2)
}
break
case OptiΔωCL:
// The argument of periapsis control is from Petropoulos and in plane.
// The out of plane will change other orbital elements at the same time.
// We determine which one to use based on the efficiency of each.
ctrl = func(o Orbit) []float64 {
_, e, i, _, ω, ν, _, _, _ := o.Elements()
oe2 := 1 - math.Pow(e, 2)
e3 := math.Pow(e, 3)
νOptiα := math.Acos(math.Pow(oe2/(2*e3)+math.Sqrt(0.25*math.Pow(oe2/e3, 2)+1/27.), 1/3.) - math.Pow(-oe2/(2*e3)+math.Sqrt(0.25*math.Pow(oe2/e3, 2)+1/27.), 1/3.) - 1/e)
νOptiβ := math.Acos(-e*math.Cos(ω)) - ω
if math.Abs(ν-νOptiα) < math.Abs(ν-νOptiβ) {
// The true anomaly is closer to the optimal in plane thrust, so let's do an in-plane thrust.
p := o.SemiParameter()
sinν, cosν := math.Sincos(ν)
return unitΔvFromAngles(math.Atan2(-p*cosν, (p+o.RNorm())*sinν), 0.0)
}
return unitΔvFromAngles(0.0, Sign(-math.Sin(ω+ν))*math.Cos(i)*math.Pi/2)
}
break
default:
panic(fmt.Errorf("optmized %s not yet implemented", cl))
}
return OptimalThrust{ctrl, GenericCL{reason, cl}}
}
// OptimalΔOrbit combines all the control laws from Ruggiero et al.
type OptimalΔOrbit struct {
Initd, cleared bool
controls []ThrustControl
method ControlLawType
// local copy of the OEs of the inital and target orbits
oInita, oInite, oIniti, oInitΩ, oInitω, oInitν float64
oTgta, oTgte, oTgti, oTgtΩ, oTgtω, oTgtν float64
Distanceε, Eccentricityε, Angleε float64
GenericCL
}
// NewOptimalΔOrbit generates a new OptimalΔOrbit based on the provided target orbit.
func NewOptimalΔOrbit(target Orbit, method ControlLawType, laws []ControlLaw) *OptimalΔOrbit {
cl := OptimalΔOrbit{}
cl.cleared = false
cl.method = method
cl.oTgta, cl.oTgte, cl.oTgti, cl.oTgtΩ, cl.oTgtω, cl.oTgtν, _, _, _ = target.Elements()
cl.Distanceε = distanceε
cl.Eccentricityε = eccentricityε
cl.Angleε = angleε
if len(laws) == 0 {
laws = []ControlLaw{OptiΔaCL, OptiΔeCL, OptiΔiCL, OptiΔΩCL, OptiΔωCL}
}
cl.controls = make([]ThrustControl, len(laws))
for i, law := range laws {
cl.controls[i] = NewOptimalThrust(law, law.String())
}
if len(cl.controls) > 1 {
cl.GenericCL = GenericCL{"ΔOrbit", multiOpti}
} else {
cl.GenericCL = GenericCL{"ΔOrbit", cl.controls[0].Type()}
}
return &cl
}
// SetTarget changes the target of this optimal control
func (cl *OptimalΔOrbit) SetTarget(target Orbit) {
cl.oTgta, cl.oTgte, cl.oTgti, cl.oTgtΩ, cl.oTgtω, cl.oTgtν, _, _, _ = target.Elements()
}
// SetEpsilons changes the target of this optimal control
func (cl *OptimalΔOrbit) SetEpsilons(distanceε, eccentricityε, angleε float64) {
cl.Distanceε = distanceε
cl.Eccentricityε = eccentricityε
cl.Angleε = angleε
}
func (cl *OptimalΔOrbit) String() string {
return "OptimalΔOrbit"
}
// Control implements the ThrustControl interface.
func (cl *OptimalΔOrbit) Control(o Orbit) []float64 {
thrust := []float64{0, 0, 0}
if !cl.Initd {
cl.Initd = true
cl.oInita, cl.oInite, cl.oIniti, cl.oInitΩ, cl.oInitω, cl.oInitν, _, _, _ = o.Elements()
if len(cl.controls) == 5 {
// Let's populate this with the appropriate laws, so we're resetting it.
cl.controls = make([]ThrustControl, 0)
if !floats.EqualWithinAbs(cl.oInita, cl.oTgta, cl.Distanceε) {
cl.controls = append(cl.controls, NewOptimalThrust(OptiΔaCL, "Δa"))
}
if !floats.EqualWithinAbs(cl.oInite, cl.oTgte, cl.Eccentricityε) {
cl.controls = append(cl.controls, NewOptimalThrust(OptiΔeCL, "Δe"))
}
if !floats.EqualWithinAbs(cl.oIniti, cl.oTgti, cl.Angleε) {
cl.controls = append(cl.controls, NewOptimalThrust(OptiΔiCL, "Δi"))
}
if !floats.EqualWithinAbs(cl.oInitΩ, cl.oTgtΩ, cl.Angleε) {
cl.controls = append(cl.controls, NewOptimalThrust(OptiΔΩCL, "ΔΩ"))
}
if !floats.EqualWithinAbs(cl.oInitω, cl.oTgtω, cl.Angleε) {
cl.controls = append(cl.controls, NewOptimalThrust(OptiΔωCL, "Δω"))
}
}
return thrust
}
cl.cleared = true // Will be set to false if not yet converged.
a, e, i, Ω, ω, _, _, _, _ := o.Elements()
switch cl.method {
case Ruggiero:
factor := func(oscul, init, target, tol float64) float64 {
if floats.EqualWithinAbs(oscul, target, tol) {
return 0
}
if floats.EqualWithinAbs(init, target, tol) {
init += tol // Adding a small error to avoid NaN while still making the correction
}
return (target - oscul) / math.Abs(target-init)
}
for _, ctrl := range cl.controls {
var oscul, init, target, tol float64
switch ctrl.Type() {
case OptiΔaCL:
oscul = a
init = cl.oInita
target = cl.oTgta
tol = cl.Distanceε
case OptiΔeCL:
oscul = e
init = cl.oInite
target = cl.oTgte
tol = cl.Eccentricityε
case OptiΔiCL:
oscul = i
init = cl.oIniti
target = cl.oTgti
tol = cl.Angleε
case OptiΔΩCL:
oscul = Ω
init = cl.oInitΩ
target = cl.oTgtΩ
tol = cl.Angleε
case OptiΔωCL:
oscul = ω
init = cl.oInitω
target = cl.oTgtω
tol = cl.Angleε
}
// XXX: This summation may be wrong: |\sum x_i| != \sum |x_i|.
if fact := factor(oscul, init, target, tol); fact != 0 {
cl.cleared = false // We're not actually done.
tmpThrust := ctrl.Control(o)
for i := 0; i < 3; i++ {
thrust[i] += fact * tmpThrust[i]
}
}
}
case Naasz:
// Note that, as described in Hatten MSc. thesis, the summing method only
// works one way (because of the δO^2) per OE. So I added the sign function
// to fix it.
//dε, eε, aε := o.epsilons()
for _, ctrl := range cl.controls {
var weight, δO float64
p := o.SemiParameter()
h := o.HNorm()
sinω, cosω := math.Sincos(ω)
switch ctrl.Type() {
case OptiΔaCL:
δO = cl.oTgta - a
if math.Abs(δO) < cl.Distanceε {
δO = 0
}
weight = Sign(δO) * math.Pow(h, 2) / (4 * math.Pow(a, 4) * math.Pow(1+e, 2))
case OptiΔeCL:
δO = cl.oTgte - e
if math.Abs(δO) < cl.Eccentricityε {
δO = 0
}
weight = Sign(δO) * math.Pow(h, 2) / (4 * math.Pow(p, 2))
case OptiΔiCL:
δO = cl.oTgti - i
if math.Abs(δO) < cl.Angleε {
δO = 0
}
weight = Sign(δO) * math.Pow((h+e*h*math.Cos(ω+math.Asin(e*sinω)))/(p*(math.Pow(e*sinω, 2)-1)), 2)
case OptiΔΩCL:
δO = cl.oTgtΩ - Ω
if δO > math.Pi {
// Enforce short path to correct angle.
δO *= -1
}
if math.Abs(δO) < cl.Angleε {
δO = 0
}
weight = Sign(δO) * math.Pow((h*math.Sin(i)*(e*math.Sin(ω+math.Asin(e*cosω))-1))/(p*(1-math.Pow(e*cosω, 2))), 2)
case OptiΔωCL:
δO = cl.oTgtω - ω
if δO > math.Pi {
// Enforce short path to correct angle.
δO *= -1
}
if math.Abs(δO) < cl.Angleε {
δO = 0
}
weight = Sign(δO) * (math.Pow(e*h, 2) / (4 * math.Pow(p, 2))) * (1 - math.Pow(e, 2)/4)
}
if δO != 0 {
cl.cleared = false // We're not actually done.
tmpThrust := ctrl.Control(o)
fact := 0.5 * weight * math.Pow(δO, 2)
for i := 0; i < 3; i++ {
thrust[i] += fact * tmpThrust[i]
}
}
}
default:
panic(fmt.Errorf("control law sumation %+v not yet supported", cl.method))
}
return Unit(thrust)
}
// HohmannΔv computes the Δv needed to go from one orbit to another, and performs an instantaneous Δv.
type HohmannΔv struct {
target Orbit
status hohmannStatus
ΔvBurnInit, ΔvInit, ΔvFinal float64
tof time.Duration
GenericCL
}
// Precompute computes and displays the Hohmann transfer orbit.
func (cl *HohmannΔv) Precompute(o Orbit) {
_, e, i, _, _, ν, _, _, _ := o.Elements()
_, _, iTgt, _, _, νTgt, _, _, _ := cl.target.Elements()
if !floats.EqualWithinAbs(νTgt, ν, angleε) && !floats.EqualWithinAbs(νTgt, ν+math.Pi, angleε) && !floats.EqualWithinAbs(νTgt, ν-math.Pi, angleε) {
panic(fmt.Errorf("cannot perform Hohmann between orbits with misaligned semi-major axes\nini: %s\ntgt: %s", o, cl.target))
}
if !floats.EqualWithinAbs(e, 0, eccentricityε) {
panic(fmt.Errorf("cannot perform Hohmann from a non elliptical orbit"))
}
if !floats.EqualWithinAbs(iTgt, i, angleε) {
panic(fmt.Errorf("cannot perform Hohmann between non co-planar orbits\nini: %s\ntgt: %s", o, cl.target))
}
if !floats.EqualWithinAbs(ν, 0, angleε) && !floats.EqualWithinAbs(ν, math.Pi, angleε) {
fmt.Printf("[WARNING] Hohmann transfer started neither at apoapsis nor at periapasis (inefficient)\n")
}
rInit := o.RNorm()
rFinal := cl.target.RNorm()
vInit := o.VNorm()
vFinal := cl.target.VNorm()
vDeparture, vArrival, tof := Hohmann(rInit, vInit, rFinal, vFinal, o.Origin)
cl.ΔvInit = vDeparture - vInit
cl.ΔvFinal = vArrival - vFinal
cl.tof = tof
durStr := cl.tof.String() + fmt.Sprintf(" (~%.1fd)", cl.tof.Hours()/24)
fmt.Printf("=== HOHMANN TRANSFER INFO ===\nHohmann transfer information - T.O.F.: %s\nvInit=%f km/s\tvFinal=%f km/s\nvDeparture=%f km/s\t vArrival=%f km/s\nΔvInit=%f km/s\tΔvFinal=%f\n=== HOHMANN TRANSFER END ====\n", durStr, vInit, vFinal, vDeparture, vArrival, cl.ΔvInit, cl.ΔvFinal)
}
// Control implements the ThrustControl interface.
func (cl *HohmannΔv) Control(o Orbit) []float64 {
switch cl.status {
case hohmmanCoast:
fallthrough
case hohmmanCompleted:
return []float64{0, 0, 0}
case hohmmanInitΔv:
if floats.EqualWithinAbs(cl.ΔvBurnInit-o.VNorm(), cl.ΔvInit, velocityε) {
// We have applied enough Δv, so let's stop burning.
cl.status = hohmmanCoast
return []float64{0, 0, 0}
}
return []float64{Sign(cl.ΔvInit), 0, 0}
case hohmmanFinalΔv:
if floats.EqualWithinAbs(cl.ΔvBurnInit-o.VNorm(), cl.ΔvFinal, velocityε) {
// We have applied enough Δv, so let's stop burning.
cl.status = hohmmanCompleted
cl.ΔvBurnInit = 0 // Reset to zero after burn is completed.
return []float64{0, 0, 0}
}
return []float64{Sign(cl.ΔvFinal), 0, 0}
default:
panic("unreachable code")
}
}
// NewHohmannΔv defines a new inversion control law.
func NewHohmannΔv(target Orbit) HohmannΔv {
_, e, _, _, _, _, _, _, _ := target.Elements()
if !floats.EqualWithinAbs(e, 0, eccentricityε) {
panic(fmt.Errorf("cannot perform Hohmann to a non elliptical orbit"))
}
return HohmannΔv{target, hohmannCompute, 0, 0, 0, time.Duration(-1) * time.Second, newGenericCLFromCL(hohmann)}
}
// Maneuver stores a maneuver in the VNC frame
type Maneuver struct {
R, N, C float64
done bool
}
// Δv returns the Δv in km/s
func (m Maneuver) Δv() float64 {
return math.Sqrt(m.R*m.R + m.N*m.N + m.C*m.C)
}
func (m Maneuver) String() string {
return fmt.Sprintf("burn [%f %f %f] km/s -- executed: %v", m.R, m.N, m.C, m.done)
}
func NewManeuver(R, N, C float64) Maneuver {
return Maneuver{R, N, C, false}
}