diff --git a/boot/rocket.ks b/boot/rocket.ks index db61768..c80efca 100644 --- a/boot/rocket.ks +++ b/boot/rocket.ks @@ -8,6 +8,7 @@ deletepath("/boot/rocket"). compile "0:/ui/rocket" to "1:/init". compile "0:/lib/navigation" to "1:/lib/navigation". compile "0:/lib/throttle" to "1:/lib/throttle". +copypath("0:/lib/math", "1:/lib/math"). // larger when compiled compile "0:/launch" to "1:/launch". copypath("0:/execnode", "1:/execnode"). // larger when compiled diff --git a/launch.ks b/launch.ks index 8f238e7..694b43e 100644 --- a/launch.ks +++ b/launch.ks @@ -1,5 +1,5 @@ -run once "lib/throttle". -run once "lib/navigation". +runoncepath("lib/throttle"). +runoncepath("lib/navigation"). parameter APOAPSIS_TARGET is 80000. parameter GRAVITY_TURN_START is 8000. @@ -20,16 +20,10 @@ from { local x is 5. } until x = 0 step { set x to x - 1. } do { wait 0.5. } -// throttle controls -when TWR() > 1.5 then { - if SHIP:ALTITUDE > 32000 { - lock THROTTLE to 1.0. - return false. - } - - lock THROTTLE to ThrottleToTWR(1.5). - return true. -} +// Hold throttle to maintain 1.5 TWR. +// We do *not* use a PID Loop here because we can +// calculate the correct value discretely. +lock THROTTLE to ThrottleToTWR(1.5). // Main ascent control. lock THROTTLE to 1.0. diff --git a/lib/throttle.ks b/lib/throttle.ks index abe939a..ce34adb 100644 --- a/lib/throttle.ks +++ b/lib/throttle.ks @@ -1,29 +1,30 @@ // Functions for calculating thrust values. @lazyglobal off. +local G is 9.81. +lock G to GetGravAcc(). + +function GetGravAcc { + if HAS_GRAV_SENSOR = true { + return SHIP:SENSORS:GRAV:MAG. + } + return SHIP:BODY:MU / ((SHIP:BODY:RADIUS+SHIP:ALTITUDE)^2). +} + // Returns the throttle value you should use to achieve the // target TWR. If TWR can't be achieved, returns 1.0. (full throttle) function ThrottleToTWR { parameter targetTWR is 1.5. local m is SHIP:MASS. - local g is 9.81. - if HAS_GRAV_SENSOR = true { - set g to SHIP:SENSORS:GRAV:MAG. - } - return min((targetTWR*m*g)/SHIP:AVAILABLETHRUST, 1.0). + return min((targetTWR*m*G)/SHIP:AVAILABLETHRUST, 1.0). } // Calculates the ship's current TWR. function TWR { local m is ship:mass. - local g is 9.81. // fallback to this approximation that doesn't deal with - // altitude change. - if HAS_GRAV_SENSOR = true { - set g to SHIP:SENSORS:GRAV:MAG. - } local t is THROTTLE * SHIP:AVAILABLETHRUST. - return t/(m*g). + return t/(m*G). } // Check for various sensors and set appropriate global constants. @@ -51,7 +52,6 @@ function BurnTime { local m is SHIP:MASS * 1000. // Starting mass (kg) local e is CONSTANT():E. // Base of natural log local p is en[0]:ISP. // Engine ISP (s) - local g is 9.80665. // Gravitational acceleration constant (m/s²) - return g * m * p * (1 - e^(-dV/(g*p))) / f. + return G * m * p * (1 - e^(-dV/(G*p))) / f. }