Reorganize code files.

This commit is contained in:
2021-07-22 21:42:34 +00:00
parent c7438fcfe7
commit 41438700ad
4 changed files with 15 additions and 28 deletions

48
rocket/execnode.ks Normal file
View File

@ -0,0 +1,48 @@
runoncepath("lib/navigation").
runoncepath("lib/throttle").
SAS off.
local t is BurnTime(NEXTNODE:DELTAV:MAG).
print "Adjusting heading".
lock STEERING to NEXTNODE:DELTAV.
wait until VectorAngle(SHIP:FACING:FOREVECTOR, STEERINGMANAGER:TARGET:FOREVECTOR) <= 0.1.
print "Warping to node.".
KUNIVERSE:TIMEWARP:WarpTo(NEXTNODE:TIME - (t/2) - 5).
wait until NEXTNODE:ETA <= (t/2).
// todo: pid loop here or nah? overshoot would be tricky to deal with...
print "Executing burn.".
local dvMin is NEXTNODE:DELTAV:MAG.
local throt is 1.0.
lock THROTTLE to throt.
// debug
print "dVMin = " + dvMin.
print "dV = " + NEXTNODE:DELTAV:MAG.
// Execute the burn, throttling down by half every time we're
// consuming more than 25% of our dV in one update.
until NEXTNODE:DELTAV:MAG <= 0.25 or dVMin < NEXTNODE:DELTAV:MAG {
// debug
print "dVMin = " + dvMin.
print "dV = " + NEXTNODE:DELTAV:MAG.
if NEXTNODE:DELTAV:MAG < dVMin {
set dvMin to NEXTNODE:DELTAV:MAG.
}
wait 0.001.
}
unlock THROTTLE.
unlock STEERING.
SAS on.
print "Node execution complete.".
// debug
print "Final dVMin = " + dvMin.
print "Final dV = " + NEXTNODE:DELTAV:MAG.

68
rocket/launch.ks Normal file
View File

@ -0,0 +1,68 @@
runoncepath("lib/throttle").
runoncepath("lib/navigation").
parameter APOAPSIS_TARGET is 80000.
parameter GRAVITY_TURN_START is 8000.
parameter GRAVITY_PITCH is 75.
parameter INITIAL_PITCH is 85.
parameter MINIMUM_PITCH is 5.
local ATMO_TWR is 1.6.
// Configure subsystems.
RCS off.
SAS off.
// 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 0.5.
}
// 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(ATMO_TWR).
print "Throttling to maintain a TWR of " + ATMO_TWR.
// Main ascent control.
lock STEERING to heading(90,90,270).
stage.
wait until SHIP:ALTITUDE > 200.
print "Vectoring away from launchpad.".
lock STEERING to heading(90, INITIAL_PITCH, 270).
wait until SHIP:ALTITUDE > GRAVITY_TURN_START.
print "Pitching for gravity turn.".
// Pitch over...
lock STEERING to heading(90, GRAVITY_PITCH, 270).
// Wait until we have rotated to (approximately) that pitch...
wait until vectorangle(
SHIP:FACING:FOREVECTOR,
STEERINGMANAGER:TARGET:FOREVECTOR)
< 0.5.
// then wait until Prograde catches up (or passes us).
local targetPitch is GetPitch(STEERINGMANAGER:TARGET:FOREVECTOR).
wait until GetPitch(SHIP:SRFPROGRADE:FOREVECTOR) <= targetPitch.
print "Locking to prograde, letting gravity do the hard work.".
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.
wait until SHIP:ORBIT:APOAPSIS > APOAPSIS_TARGET.
lock THROTTLE to 0.0.
set SHIP:CONTROL:PILOTMAINTHROTTLE to 0.
wait 0.001. // make sure these control updates get applied
print "Target apoapsis acquired. Creating maneuver node.".
add CreateCircularizationNode().
runpath("/rocket/execnode").
print "Orbit acquired. Releasing controls. Good luck, Kerman.".
unlock THROTTLE.
unlock STEERING.
SAS on.

107
rocket/ui.ks Normal file
View File

@ -0,0 +1,107 @@
runoncepath("/lib/navigation").
function launchButtonPressed {
// adjust torque settings here...
local oldEpsilon is STEERINGMANAGER:TORQUEEPSILONMAX.
set STEERINGMANAGER:TORQUEEPSILONMAX to maxEpsilon:TEXT:ToNumber().
local oldStopTime is STEERINGMANAGER:MAXSTOPPINGTIME.
set STEERINGMANAGER:MAXSTOPPINGTIME to maxStopTime:TEXT:ToNumber().
run "/rocket/launch"(
targetApo:TEXT:ToNumber(),
gravTurnStart:TEXT:ToNumber(),
gravPitch:TEXT:ToNumber(),
initialPitch:TEXT:ToNumber(),
minimumPitch:TEXT:ToNumber()
).
set STEERINGMANAGER:TORQUEEPSILONMAX to oldEpsilon.
set STEERINGMANAGER:MAXSTOPPINGTIME to oldStopTime..
}
function executeNode {
run "/rocket/execnode".
}
function showLaunchMenu {
stk:ShowOnly(launchMenu).
}
// Main UI.
local interface is gui(250, 300).
set interface:X to 200.
set interface:Y to 700.
// Main menu elements in here.
local box is interface:AddVLayout().
local btn is box:AddButton("TERM").
set btn:TOGGLE to true.
set btn:ONTOGGLE to {
parameter activate.
if activate {
CORE:PART:GETMODULE("kOSProcessor"):DOEVENT("Open Terminal").
} else {
CORE:PART:GETMODULE("kOSProcessor"):DOEVENT("Close Terminal").
}
}.
set btn to box:AddButton("LAUNCH").
set btn:onClick to showLaunchMenu@.
set btn to box:AddButton("EXECNODE").
set btn:onClick to executeNode@.
local stk is interface:AddStack().
// Launch menu
local launchMenu is stk:AddVLayout().
set btn to launchMenu:AddButton("Execute").
set btn:onClick to launchButtonPressed@.
local launchConfig is box:AddScrollBox().
// set launchConfig:STYLE:WIDTH to 300.
// set launchConfig:STYLE:HEIGHT to 800.
set sbox to launchConfig:AddHLayout().
sbox:AddLabel("Target Apoapsis").
local targetApo is sbox:AddTextField("80000").
set sbox to launchConfig:AddHLayout().
sbox:AddLabel("Atmo TWR").
local targetTWR is sbox:AddTextField("1.6").
set sbox to launchConfig:AddHLayout().
sbox:AddLabel("Initial Pitch").
local initialPitch is sBox:AddTextField("85").
set sbox to launchConfig:AddHLayout().
sbox:AddLabel("Minimum Pitch").
local minimumPitch is sbox:AddTextField("5").
set sbox to launchConfig:AddHLayout().
sbox:AddLabel("Gravity Turn Alt").
local gravTurnStart is sbox:AddTextField("8000").
set sbox to launchConfig:AddHLayout().
sbox:AddLabel("Gravity Turn Pitch").
local gravPitch is sbox:AddTextField("75").
launchConfig:AddSpacing(5).
set sbox to launchConfig:AddHLayout().
sbox:AddLabel("Torque Epsilon Max").
local maxEpsilon is sbox:AddTextField("0.001").
set sbox to launchConfig:AddHLayout().
sbox:AddLabel("Max Stopping Time").
local maxStopTime is sbox:AddTextField("2.0").
// End Launch Menu
interface:show().
wait until false.