-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaneuver.ks
91 lines (68 loc) · 2.92 KB
/
maneuver.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
// this script executes the next maneuver node
print "RUNNING maneuver".
// import libraries
runOncePath("lib/engine").
runOncePath("lib/burn").
set nd to nextNode.
// calculate estimate of burn duration
local pressure is 0.
local flowRate is available_mass_flow_rate_at(pressure).
print "current max flow rate: " + available_mass_flow_rate().
print "vacuum max flow rate: " + flowRate.
local effectiveExhaustVelocity is ship:availableThrustAt(pressure)/flowRate. // isp in n*s/kg = ev in m/s
local burnDuration is burn_time(ship:mass, nd:deltav:mag, effectiveExhaustVelocity, flowRate).
local burnStart is mean_burn_time(ship:mass, nd:deltav:mag, effectiveExhaustVelocity, flowRate).
print("Estimated burn duration of " + round(burnDuration, 3) + " seconds").
print("Crude Estimate: " + round(nd:deltav:mag/(ship:availablethrust/ship:mass), 3) + " seconds").
print("Starting burn " + round(burnStart, 3) + " seconds before node ETA").
// load rocket state
local rocket_state is readJson("rocket_state.json").
// create key execute_maneuver if none exists
if not rocket_state:hassuffix("execute_maneuver") {
rocket_state:add("execute_maneuver", false).
}
// add a Kerbal Alarm Clock alarm to kick simulation out of warp a minute before the burn should start.
if addons:available("KAC") and rocket_state["execute_maneuver"] = false {
addAlarm("Maneuver", time:seconds + nd:eta - (burnStart + 60), "Maneuver Node", "Auto-Generated by kOS script").
}
// set execute_maneuver to true
set rocket_state["execute_maneuver"] to true.
// save rocket state now that we've changed it
writeJson(rocket_state, "rocket_state.json").
// wait until 60 seconds before burn
wait until nd:eta <= burnStart + 60.
// turn to face the direction the rocket's velocity is changing in
set dv0 to nd:deltaV.
sas off.
lock steering to dv0.
// wait until rocket is facing the right direction
wait until vang(dv0, ship:facing:vector) < 0.25.
// wait until burn start
wait until nd:eta <= burnStart + 10.
kuniverse:timewarp:cancelwarp().
wait until kuniverse:timewarp:isSettled().
wait until nd:eta <= burnStart.
// set execute_maneuver to false
set rocket_state["execute_maneuver"] to false.
writeJson(rocket_state, "rocket_state.json").
// create a setpoint we can manipulate
set throttleSetpoint to 0.
lock throttle to throttleSetpoint.
// execute maneuver node
local burnStop is burnDuration-burnStart.
set throttleSetpoint to 1.0.
wait until nd:eta <= -burnStop or vDot(dv0, nd:deltav) < 0.
set throttleSetpoint to 0.0.
// print stats
print "End burn, remain dv " + round(nd:deltav:mag,1) + "m/s, vdot: " + round(vdot(dv0, nd:deltav),1).
// remove node so we can execute future ones
remove nd.
// unlock controls
unlock steering.
unlock throttle.
// 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.