kOS-scripts/lib/node.ks
Casper V. Kristensen a38712c5a8
Better cohesion.
2019-02-08 23:18:34 +01:00

64 lines
2.2 KiB
Plaintext

@LAZYGLOBAL OFF.
run once "lib/rocket".
run once "lib/warp".
function estimated_burn_duration {
//
// Calculate estimated burn duration for a maneuver node.
// Based on:
// https://en.wikipedia.org/wiki/Tsiolkovsky_rocket_equation
// https://space.stackexchange.com/questions/27375/how-do-i-calculate-a-rockets-burn-time-from-required-velocity
//
parameter node is NEXTNODE.
local exhaust_velocity is isp_sum() * (CONSTANT:G * KERBIN:mass).
return ((SHIP:mass * exhaust_velocity) / SHIP:maxthrust) * (1 - CONSTANT:E^(-node:deltav:mag/exhaust_velocity)).
}
function execute_node {
//
// Execute given maneuver node (the next one by default).
//
parameter node is NEXTNODE.
local burn_duration is estimated_burn_duration(node).
print "=== EXECUTE MANEUVER NODE ===".
print "Estimated burn duration: " + ROUND(burn_duration, 1) + "s".
print "Aligning ship with burn vector".
SAS off.
lock STEERING to node:deltav.
wait until vang(SHIP:facing:vector, node:deltav) < 0.5.
print "Initializing warp".
warp_for(max(0, node:eta - (burn_duration/2) - 5)). // 5s before we need to start burn
print "Approaching".
wait until node:eta <= ceiling(burn_duration/2). // ceiling since we'd rather start the burn too soon to have time for perfecting it
print "Burn!".
lock THROTTLE to 1.0.
// Decrease throttle linearly when the burn duration is less than 1 second
wait until estimated_burn_duration(node) <= 1.
lock THROTTLE to max(0.01, estimated_burn_duration(node)). // ensure we always finish the burn by burning with at least 1% power at all times
// The burn vector will start to drift once we have very little left to burn. Therefore, save the burn vector as it is right now, and lock steering to it, instead of the dynamic vector
local dv0 is node:deltav.
lock STEERING to dv0.
// Stop the burn once the saved vector, dv0, and current burn vector start facing opposite directions
wait until vdot(dv0, node:deltav) < 0.
set THROTTLE to 0.0.
print "=== MANEUVER NODE EXECUTED ===".
print round(node:deltav:mag, 3) + "m/s delta-v remaining".
unlock_control().
wait 1.
remove node.
}