From ed31a1fda3abafce13c90928e6e3e2c7fbf85425 Mon Sep 17 00:00:00 2001 From: annabunches Date: Sun, 18 Jul 2021 15:05:26 -0400 Subject: [PATCH] Large-scale refactor, rewrite ascent code to just mimic what I actually do when flying manually. --- boot/rocket.ks | 1 + dbgparts.ks => debug/listparts.ks | 0 launch.ks | 120 +++++++++--------------------- lib/guidance.ks | 55 ++++++++++++++ 4 files changed, 92 insertions(+), 84 deletions(-) rename dbgparts.ks => debug/listparts.ks (100%) create mode 100644 lib/guidance.ks diff --git a/boot/rocket.ks b/boot/rocket.ks index c38faa2..7485fa9 100644 --- a/boot/rocket.ks +++ b/boot/rocket.ks @@ -1,4 +1,5 @@ COPYPATH("0:/noop", ""). COMPILE "0:/launch" to "1:/launch". +COMPILE "0:/lib/functions" to "1:/lib/functions". SET core:bootfilename to "noop". diff --git a/dbgparts.ks b/debug/listparts.ks similarity index 100% rename from dbgparts.ks rename to debug/listparts.ks diff --git a/launch.ks b/launch.ks index 44f422a..a4f830c 100644 --- a/launch.ks +++ b/launch.ks @@ -1,93 +1,45 @@ -DECLARE FUNCTION throttle_to_twr { - declare parameter x is 1.5. +run once "lib/functions". - local m is ship:mass. - local g is 9.81. - if has_grav_sensor = true { - set g to ship:sensors:grav:mag. +local APOAPSIS_TARGET is 80000. +local GRAVITY_TURN_START is 5000. +SensorCheck(). + +// Countdowns are cute. +print "Initiating automated launch sequence". +from { local x is 5. } until x = 0 step { set x to x - 1. } do { + print "..." + x. + wait 1. +} +print "Launching". + +// Disable subsystems. +RCS off. +SAS off. + +// throttle controls +when TWR() > 1.5 then { + if SHIP:ALTITUDE > 32000 { + lock THROTTLE to 1.0. + return false. } - print "Locking throttle to: " + min((x*m*g)/ship:availablethrust, 1.0). - lock throttle to min((x*m*g)/ship:availablethrust, 1.0). + + lock THROTTLE to ThrottleToTWR(1.5). + return true. } -DECLARE 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. - print "Mass: " + ship:mass. - print "Throttle: " + throttle. - print "Thrust (max): " + ship:availablethrust. - print "TWR = " + t/(m*g). - return t/(m*g). -} +// Main ascent control. +lock THROTTLE to 1.0. +lock STEERING to UP. +stage. -LOCAL has_grav_sensor is false. -DECLARE FUNCTION sensor_check { - list sensors in senselist. - for s in senselist { - if s:type = "grav" { - print "Gravometric sensor detected". - set has_grav_sensor to true. - return. - } - } - set has_grav_sensor to false. -} +wait until SHIP:ALTITUDE > 200. +lock STEERING to heading(90, 85, 90). -// countdown -sensor_check(). -LOCAL done is false. -PRINT "Initiating automated launch sequence". -FROM { local x is 5. } UNTIL x = 0 STEP { set x to x - 1. } DO { - PRINT "..." + x. - WAIT 1. -} - -// Logic for initial ascent. -SAS OFF. -LOCK throttle to 1.0. -LOCK steering to up. -STAGE. - -WHEN ship:altitude > 1000 THEN { - PRINT "Vectoring away from launchpad". - LOCK steering to HEADING(90, 88). - WHEN ship:altitude > 8000 THEN { - // begin "gravity" curve - PRINT "Beginning orbital insertion turn". - LOCK steering to HEADING(90,80). - WAIT 1. - LOCK steering to HEADING(90,70). - WAIT 1. - LOCK steering to HEADING(90,60). - WAIT 3. - LOCK steering to ship:prograde. - - WHEN ship:apoapsis > 70000 THEN { - LOCK throttle to 0.0. - // todo: automate orbital insertion. - PRINT "Initial ascent complete. Manually perform orbital insertion. Good luck, Kerman!". - SET done to true. - } - } -} - -// Control rate of ascent to reduce drag. -UNTIL ship:altitude > 32000 { - if twr() > 1.5 { - throttle_to_twr(1.5). - } +wait until SHIP:ALTITUDE > GRAVITY_TURN_START. +until SHIP:APOAPSIS > 80000 { + local angle is min(30, SHIP:PROGRADE:PITCH - 2). + lock STEERING to heading(90, angle, 90). wait 0.001. } -lock throttle to 1.0. -// throttle_to_twr(1.5). -// WHEN ship:altitude > 32000 THEN { -// lock throttle to 1.0. -// } - -WAIT UNTIL done. +set THROTTLE to 0.0. diff --git a/lib/guidance.ks b/lib/guidance.ks new file mode 100644 index 0000000..6634c4a --- /dev/null +++ b/lib/guidance.ks @@ -0,0 +1,55 @@ +@lazyglobal off. + +// 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 twr 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((twr*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). +} + +// Check for various sensors and set appropriate global constants. +// Currently only checks for grav sensor, as that's the only one used by this library. +global HAS_GRAV_SENSOR is false. +function SensorCheck { + local SensorList is 0. + list SENSORS in SensorList. + for s in SensorList { + if s:type = "grav" { + print "Gravometric sensor detected". + set HAS_GRAV_SENSOR to true. + return. + } + } + set HAS_GRAV_SENSOR to false. +} + +// adapted from https://github.com/sporadisk/KSP-KOS-scripts/blob/master/modules/GravityTurn.ks +// todo: I don't think I actually like this code... +function GravityTurn { + parameter TargetDirection is 90. + parameter TargetApoapsis is 80000. + parameter Factor is 1. + + local remFactor is (2 / Factor). + local remHeight is (SHIP:APOAPSIS/TargetApoapsis). + local angle is (90 * (1 - (remHeight ^ remFactor))). + return heading(TargetDirection, angle, 90). +}