Skip to content

Instantly share code, notes, and snippets.

@agoodman
Created June 14, 2022 01:35
Show Gist options
  • Save agoodman/4fe9381acdae321bf6d1b0bc44b4060a to your computer and use it in GitHub Desktop.
Save agoodman/4fe9381acdae321bf6d1b0bc44b4060a to your computer and use it in GitHub Desktop.
First stage kOS script for reusable orbital operations
// SpaceX-style super heavy booster launch and land script
// by aubranium
//
// designed to be used with FatBoyMk2.craft
//
// hoverslam code borrowed from an unknown SpaceX engineer on the internet and modified
set runmode to 1.
clearscreen.
set targetAltitude to 80000.
set munarAltitude to 25000.
set targetLng to -30.
set maxTWR to 3.
set targetTWR to 1.25.
set circularThreshold to 0.05.
set decelMult to 0.325.
set landingOffset to 36.8.
set iTWR to 0.
set steering to up.
set throttle to 0.
rcs off.
sas off.
wait 1.
stage.
until runmode = 0 {
if runmode = 1 { // launch
print "Launch".
brakes off.
lock steering to heading(90, 89).
set throttle to 1.
wait 1.
gear off.
lock speedCondition to verticalspeed > 80.
wait until speedCondition.
lock steering to heading(90, 85).
lock speedCondition to alt:radar > 4000.
wait until speedCondition.
lock steering to heading(90, 80).
// wait until ship:apoapsis > 5000.
// lock steering to srfprograde.
lock decoupleCondition to ship:apoapsis > 50000.
wait until decoupleCondition.
set throttle to 0.
set runmode to 2.
}
if runmode = 2 { // deploy
print "Deploy".
set throttle to 0.
rcs on.
wait 5.
stage.
// set KUniverse:ACTIVEVESSEL to VESSEL("FatBoy Probe").
wait 5.
set runmode to 3.
}
if runmode = 3 { // boost back
print "Boost back".
wait 5.
rcs on.
lock steering to heading(270, 0).
wait 25.
set throttle to 0.2.
wait 13.
set throttle to 0.
wait 15.
set runmode to 10.
}
if runmode = 10 { // hoverslam
print "Hoverslam".
brakes on.
lock g to body:mu / (alt:radar + body:radius)^2.
lock maxDecel to (ship:availablethrust / ship:mass) - g. // Maximum deceleration possible (m/s^2)
lock stopDist to ship:verticalspeed^2 / (2 * maxDecel * decelMult). // The distance the burn will require
lock idealThrottle to stopDist / alt:radar. // Throttle required for perfect hoverslam
lock impactTime to alt:radar / abs(ship:verticalspeed). // Time until impact, used for landing gear
lock steering to srfretrograde.
wait until alt:radar < 15000.
lock startCondition to alt:radar < stopDist.
wait until startCondition.
lock throttle to idealThrottle.
lock altitudeCondition to alt:radar < landingOffset.
lock verticalSpeedCondition to verticalspeed > -0.1.
when impactTime < 5 then { gear on. }
// wait until altitudeCondition or verticalSpeedCondition.
wait until verticalSpeedCondition.
set throttle to 0.
set runmode to 0.
}
}
print "Landed!".
print ship:geoposition.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment