Large-scale refactor, rewrite ascent code to just mimic what I actually do when flying manually.

This commit is contained in:
Anna Rose 2021-07-18 15:05:26 -04:00
parent c3ab101f8b
commit ed31a1fda3
4 changed files with 92 additions and 84 deletions

View File

@ -1,4 +1,5 @@
COPYPATH("0:/noop", ""). COPYPATH("0:/noop", "").
COMPILE "0:/launch" to "1:/launch". COMPILE "0:/launch" to "1:/launch".
COMPILE "0:/lib/functions" to "1:/lib/functions".
SET core:bootfilename to "noop". SET core:bootfilename to "noop".

120
launch.ks
View File

@ -1,93 +1,45 @@
DECLARE FUNCTION throttle_to_twr { run once "lib/functions".
declare parameter x is 1.5.
local m is ship:mass. local APOAPSIS_TARGET is 80000.
local g is 9.81. local GRAVITY_TURN_START is 5000.
if has_grav_sensor = true { SensorCheck().
set g to ship:sensors:grav:mag.
// 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 { // Main ascent control.
local m is ship:mass. lock THROTTLE to 1.0.
local g is 9.81. // fallback to this approximation that doesn't deal with lock STEERING to UP.
// altitude change. stage.
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).
}
LOCAL has_grav_sensor is false. wait until SHIP:ALTITUDE > 200.
DECLARE FUNCTION sensor_check { lock STEERING to heading(90, 85, 90).
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.
}
// countdown wait until SHIP:ALTITUDE > GRAVITY_TURN_START.
sensor_check(). until SHIP:APOAPSIS > 80000 {
LOCAL done is false. local angle is min(30, SHIP:PROGRADE:PITCH - 2).
PRINT "Initiating automated launch sequence". lock STEERING to heading(90, angle, 90).
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 0.001. wait 0.001.
} }
lock throttle to 1.0.
// throttle_to_twr(1.5). set THROTTLE to 0.0.
// WHEN ship:altitude > 32000 THEN {
// lock throttle to 1.0.
// }
WAIT UNTIL done.

55
lib/guidance.ks Normal file
View File

@ -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).
}