-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorbit.ks
More file actions
94 lines (71 loc) · 2.92 KB
/
orbit.ks
File metadata and controls
94 lines (71 loc) · 2.92 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
@LAZYGLOBAL OFF.
runOncePath("math/math").
runOncePath("math/vel_vec").
runOncePath("basix").
function circularizeOrbit {
parameter level, levelEta.
parameter autoWarp is true.
print "Circularizing.".
local m_time is time:seconds + levelEta.
local v0 is velocityat(ship, m_time):orbit:mag.
local v1 is getCircOrbitV(body, level).
local deltaV is v1 - v0.
local mnv is node(m_time, 0, 0, deltaV).
if hasNode {
if abs(nextNode:prograde - deltaV) > getManeuverPrecisionDeltaV() or abs(nextNode:eta - levelEta) > 1 {
removeManeuverFromFlightPlan(nextNode).
}
else {
set mnv to nextNode.
}
}
local precision is getManeuverPrecisionMetres() * 2.
local checkObt_func is { parameter o. return o:apoapsis > 0 and o:periapsis > 0 and (o:apoapsis - o:periapsis) < precision. }.
executeManeuver(mnv, checkObt_func, autoWarp).
}
function changeCircOrbit {
parameter newLevel.
local oldLevel is ship:altitude.
local startEta is choose eta:periapsis if newLevel > oldLevel else eta:apoapsis.
print "Changing orbit level in: " + startEta.
print "Going to transitional orbit.".
local m_time is time:seconds + startEta.
local deltaV1 is calculateDeltaVToAltitude(newLevel, m_time).
local mnv is node(m_time, 0, 0, deltaV1).
local precision is getManeuverPrecisionMetres().
local checkObt_func is choose { parameter o. return o:apoapsis > newLevel - precision. } if newLevel > oldLevel else { parameter o. return o:periapsis < newLevel + precision. }.
executeManeuver(mnv, checkObt_func).
wait 1.
local circLevel is choose ship:apoapsis if newLevel > oldLevel else ship:periapsis.
local circEta is choose eta:apoapsis if newLevel > oldLevel else eta:periapsis.
circularizeOrbit(circLevel, circEta).
}
function circularizeOrbitWithoutManeuver {
print "Circularizing manually.".
local throttelCoeff is 1.5.
local minMag is 0.01.
local precision is 100.
local level is ship:apoapsis.
local v0 is getOrbitApoapsisV(body, level, ship:orbit:semimajoraxis).
local v1 is getCircOrbitV(body, level).
local deltaV is v1 - v0.
print "ETA to apoapsis: " + eta:apoapsis.
print "Ship V at apoapsis: " + v0.
print "Circular Orbit V: " + v1.
print "DeltaV: " + deltaV.
local burnTime is getEstimatedBurnTime(deltaV).
print "Burn time: " + burnTime.
//local targetVVector is velocity_vec_at(180).
lock steering to velocity_vec_at(180).
local startTime is time:seconds + eta:apoapsis - burnTime / 2.
lock acc to ship:availablethrust / ship:mass.
wait until time:seconds >= startTime.
// TODO: Play with throttle accuracy
lock throttle to choose 1 if acc <= 0 else min(1, throttelCoeff * (v1 - getOrbitApoapsisV(body, level, ship:orbit:semimajoraxis)) / acc).
// TODO: Play more with end burn conditions and precision
until (v1 - ship:velocity:orbit:mag) < minMag or ship:periapsis >= level or (obt:apoapsis > 0 and obt:periapsis > 0 and (obt:apoapsis - obt:periapsis) < precision) {
autoStage().
wait 0.
}
stopEngine("circularizing").
}