-
Notifications
You must be signed in to change notification settings - Fork 0
/
launch.ks
110 lines (91 loc) · 3.42 KB
/
launch.ks
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
//launch
// these defaults seem to work well when launching from KSC
declare parameter finalAltitude is 80000.
declare parameter compassHeading is 90.
declare parameter turnRate is 12.
// clear screen to display only important information
clearScreen.
print "RUNNING launch".
// define utility functions
runoncepath("lib/terminal").
runoncepath("lib/engine").
// countdown to launch
print "Count down:".
from {local countdown is 3.} until countdown = 0 step {set countdown to countdown - 1.} do {
print "..." + countdown.
wait 1.
}
// setup trigger to stage whenever thrust is zero or static engines flameout
when maxThrust = 0 or engineFlameout() then {
print "Staging.".
stage.
wait until stage:ready.
preserve.
}
// setup trigger to deploy action group 1 (solar panels, antennas, etc.) at less than 0.01 dynamic pressure
when ship:velocity:surface:mag > 1000 and ship:dynamicpressure < 0.01 then {
ag1 on.
}
// setup constants and variables for launch
set targetTWR to 2.0.
lock gravAcc to body:mu/((body:radius + ship:altitude)^2).
lock weight to gravAcc * ship:mass.
lock throttle to throttleForThrust(targetTWR * weight).
local initialSpeed is 100.
sas on.
set sasMode to "stability".
set yaw to compassHeading.
set pitch to 90.
when ship:velocity:surface:mag > initialSpeed then {
sas off.
lock steering to heading(yaw, pitch).
}
// execute launch up until ship's apoapsis is at target
until ship:apoapsis > finalAltitude {
// launch straight up with no turning
if ship:velocity:surface:mag < initialSpeed {
print "Accelerating to " + initialSpeed + " m/s" at(0,15).
// pitch down in accordance to turn rate
} else if ship:velocity:surface:mag >= initialSpeed and ship:velocity:surface:mag < 80*turnRate+initialSpeed {
set pitch to max(90 - (ship:velocity:surface:mag-initialSpeed)/turnRate, 10).
set yaw to compassHeading.
print "Pitching to " + round(pitch) + " degrees " at (0,15).
}
}
// Cut throttle and coast until ship exits atmosphere
clearline(15).
print "Reached apoapsis of " + round(ship:apoapsis) + " meters, cutting throttle" at (0,15).
print "Cruising to " + round(ship:body:atm:height) + " meters" at (0,16).
lock throttle to 0.
wait until ship:altitude > ship:body:atm:height.
// burn again to account for air friction losses
if ship:apoapsis < finalAltitude {
kuniverse:timewarp:cancelwarp().
wait until kuniverse:timewarp:isSettled().
lock throttle to throttleForThrust(targetTWR * weight).
wait until ship:apoapsis > finalAltitude.
lock throttle to 0.
}
// sets user's throttle setting to zero to prevent throttle from
// returning to the throttle value it was before it was run
set ship:control:pilotMainThrottle to 0.
// unlock steering and turn on stability assist
unlock steering.
sas on.
// if it's possible to make nodes, create and execute a circularization node
if career():canMakeNodes {
runoncepath("lib/node").
// create circularization maneuver node
local circNode is nodeChangePeriapsis(apoapsis).
if circNode:isType("Node") {
add circNode.
clearScreen.
print "Reached apoapsis of " + round(apoapsis) + " meters, cutting throttle" at (0,15).
print "Executing circularization node in " + round(circNode:eta) + " seconds" at (0,16).
// run execute next node script
run maneuver.
}
// else just point sas at prograde
} else {
set sasMode to "prograde".
}