From 310a3fb3722d69b48f9516f10c787461506c8e1f Mon Sep 17 00:00:00 2001 From: annabunches Date: Tue, 10 Aug 2021 18:17:05 -0400 Subject: [PATCH] Refactor some functions only used once. --- lib/launch_rocket.ks | 22 +++++++++++++++++++++- lib/navigation.ks | 17 ----------------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/lib/launch_rocket.ks b/lib/launch_rocket.ks index 4ce1bd1..2ef3b62 100644 --- a/lib/launch_rocket.ks +++ b/lib/launch_rocket.ks @@ -2,6 +2,26 @@ runoncepath("lib/throttle"). runoncepath("lib/navigation"). runoncepath("lib/node"). +// Calculate the vector at which to ascend. +// This is basically prograde plus some anti-rotation logic and a lower bound. +function getAscentVector { + parameter minPitch. + // face prograde, but hold a solid eastern heading and don't + // rotate the ship + local newHeading is lookdirup(SHIP:SRFPROGRADE:FOREVECTOR, + heading(90, 0, 270):TOPVECTOR). + if getPitch(newHeading:FOREVECTOR) < minPitch { + set newHeading to heading(90, minPitch, 270). + } + return newHeading. +} + +// Returns the navball pitch of a given vector. +function getPitch { + parameter v is SHIP:FACING:FOREVECTOR. + return 90 - vectorangle(SHIP:UP:FOREVECTOR, v). +} + function Launch { parameter APOAPSIS_TARGET is 80000. parameter ATMO_TWR is 1.6. @@ -60,7 +80,7 @@ function Launch { wait until GetPitch(SHIP:SRFPROGRADE:FOREVECTOR) <= targetPitch. print "Locking to prograde, letting gravity do the hard work.". - lock STEERING to GetAscentVector(MINIMUM_PITCH). + lock STEERING to getAscentVector(MINIMUM_PITCH). wait until SHIP:ALTITUDE > 32000. // todo: if we have a pressure sensor we can use it to decide when to kick the throttle up instead, neat solution for e.g. Duna and Eve. lock THROTTLE to 1.0. diff --git a/lib/navigation.ks b/lib/navigation.ks index 7d879e7..3d14c59 100644 --- a/lib/navigation.ks +++ b/lib/navigation.ks @@ -1,22 +1,5 @@ // functions for calculating steering values. -function GetPitch { - parameter v is SHIP:FACING:FOREVECTOR. - return 90 - vectorangle(SHIP:UP:FOREVECTOR, v). -} - -function GetAscentVector { - parameter minPitch. - // face prograde, but hold a solid eastern heading and don't - // rotate the ship - local newHeading is lookdirup(SHIP:SRFPROGRADE:FOREVECTOR, - heading(90, 0, 270):TOPVECTOR). - if GetPitch(newHeading:FOREVECTOR) < minPitch { - set newHeading to heading(90, minPitch, 270). - } - return newHeading. -} - // Create a node that will circularize the orbit. // 'where' can be one of: // the special string "APO", for the next Apoapsis.