From 429a1d5302951d2fcbad295443ae98024838166b Mon Sep 17 00:00:00 2001 From: annabunches Date: Thu, 5 Aug 2021 21:45:19 -0400 Subject: [PATCH] Improvements to node execution, including attempt at better node time calculations and automatic staging. --- lib/node.ks | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 3 deletions(-) diff --git a/lib/node.ks b/lib/node.ks index 335ad2d..ba5acc3 100644 --- a/lib/node.ks +++ b/lib/node.ks @@ -11,8 +11,16 @@ function ExecNode { local leadT is BurnTime(NEXTNODE:DELTAV:MAG / 2). local t is BurnTime(NEXTNODE:DELTAV:MAG). + if willStage(NEXTNODE:DELTAV:MAG) { + print "WARNING: kOS will stage during this node execution. Safe cancellation requires reboot.". + when flameOut() then { + print "Flameout detected. Staging.". + stage. + } + } + print "Adjusting heading". - lock STEERING to NEXTNODE:DELTAV. + lock STEERING to LookDirUp(NEXTNODE:DELTAV, SHIP:FACING:TOPVECTOR). wait until VAng(SHIP:FACING:FOREVECTOR, STEERINGMANAGER:TARGET:FOREVECTOR) <= 0.1. print "Warping to node.". @@ -34,12 +42,16 @@ function ExecNode { // Calculate the time required to burn a given dV. // Assumes a perfectly spherical Kerbal in a vacuum. function BurnTime { - parameter totaldV, m is SHIP:MASS, s is STAGE:NUMBER. + parameter totaldV, s is STAGE:NUMBER. local totalT is 0.0. - until totaldV <= 0 { + local lastStage is false. + // We allow a small tolerance to deal with potential floating point errors. + until totaldV <= 0.001 { local F is stageThrust(). local Isp is stageISP(). + local m is stageMass(s). + // TODO: handle node execution in atmosphere? local dV is min(totaldV, SHIP:StageDeltaV(s):VACUUM). local t is calcBurnTime(dV, m, Isp, F). print "DEBUG: " + dV + " m/s^2 in stage " + s + ", in " + t + " seconds.". @@ -48,6 +60,7 @@ function BurnTime { set totalT to totalT + t. } + print "DEBUG: Total Burn Time: " + totalT + " s.". return totalT. } @@ -104,3 +117,41 @@ function stageThrust { return sum. } + +// Determine mass at start of target stage. +// This can handle Delta V-style launchers but +// only if the central rocket remains in the stack +// for exactly two stages, one of which is the current stage. +// More complex staging with partially depleted tanks may produce +// undefined behavior. +function stageMass { + parameter s is STAGE:NUMBER. + + local m is SHIP:MASS. + if s = SHIP:STAGENUM { return m. } + + local ps is List(). + list PARTS in ps. + for part in ps { + if part:DECOUPLEDIN <= s { + set m to m - part:MASS. + } else if part:HasSuffix("AVAILABLETHRUST") and part:AVAILABLETHRUST > 0 { + // This is an engine and it is currently active. Subtract the fuel + // it will burn before the next stage. + set m to m - (part:MAXMASSFLOW * SHIP:StageDeltaV(SHIP:STAGENUM):DURATION). + } + } + + return m. +} + +function flameOut { + local ens is List(). + list engines in ens. + for en in ens { + if en:FLAMEOUT { + return true. + } + } + return false. +}