Skip to content

Instantly share code, notes, and snippets.

@t0chas
Last active June 15, 2016 02:05
Show Gist options
  • Save t0chas/9a2ffdfc5477178e5244d4a6aaf78d69 to your computer and use it in GitHub Desktop.
Save t0chas/9a2ffdfc5477178e5244d4a6aaf78d69 to your computer and use it in GitHub Desktop.
A kOS script for KSP. Deploy a CommSat to low Kerbin Orbit (LKO) using a kOS script to handle launch, staging and circularization, without having a proper signal relay network (RemoteTech only).
clearscreen.
set headingAngle to 90. //normal EAST
set targetApoapsis to 150000. //Target apoapsis in meters
set targetPeriapsis to 150000. //Target periapsis in meters
lock throttle to 0. //Throttle is a decimal from 0.0 to 1.0
gear off.
set runmode to 0. //Safety in case we start mid-flight
if SHIP:STATUS = "PRELAUNCH"{
set runmode to 1.
}
print "Launch!".
set lfStageStart to 0.
set sfStageStart to 0.
set stageHasSF to false.
set stageHasLF to false.
set doStage to false.
set currentThrust to 0.
//
until runmode = 0 {
if doStage AND STAGE:READY AND STAGE:NUMBER > 0{
if SHIP:STATUS <> "PRELAUNCH"{
lock throttle to 0.
wait 1.
}
PRINT "Staging...".
stage. //Same thing as pressing Space-bar
wait 2.
set lfStageStart to STAGE:LIQUIDFUEL.
set sfStageStart to STAGE:SOLIDFUEL.
PRINT "Staged! (lf: " + lfStageStart + "; sf: " + sfStageStart + ")".
if sfStageStart > 0 {
set stageHasSF to true.
PRINT "Stage has SolidFuel".
}else{
set stageHasSF to false.
}
SET stageHasLF TO (lfStageStart > 0).
lock throttle to TVAL.
set doStage to false.
set currentThrust to SHIP:AVAILABLETHRUST.
}
if runmode = 1 {//Ship is on the launchpad
lock steering to UP + R(0, 0, 180). //Point the rocket straight up
set TVAL to 1. //Throttle up to 100%
set runmode to 2. //Go to the next runmode
set doStage to true.
}
else if runmode = 2 { // Fly UP to 10,000m
lock steering to UP + R(0, 0, 180). //Point the rocket straight up
set TVAL to 1.
if SHIP:VERTICALSPEED > 100 {
set runmode to 3.
}
}
else if runmode = 3 { //Gravity turn
//Pitch over gradually until levelling out to 5 degrees at 50km
//set targetPitch to max( 5, 90 * (1 - ALT:RADAR / 50000)).
set targetPitch to max( 5, 90 * (1 - max(0, LN(ALT:RADAR / 1000) ) / 4.6 ) ).
set headingVar to heading( headingAngle, targetPitch).
set headingVar to headingVar + r(0, 0, -90).
lock steering to headingVar.
set TVAL to 1.
if SHIP:APOAPSIS > targetApoapsis {
set runmode to 4.
}
}
else if runmode = 4 { //Coast to Ap
//lock steering to heading ( 90, 3). //Stay pointing 3 degrees above horizon
lock steering to SHIP:PROGRADE.
set TVAL to 0. //Engines off.
if (SHIP:ALTITUDE > 70000) and (ETA:APOAPSIS > 60) and (VERTICALSPEED > 0) {
if WARP = 0 { // If we are not time warping
wait 1. //Wait to make sure the ship is stable
SET WARP TO 3. //Be really careful about warping
}
}.
else if ETA:APOAPSIS < 60 {
SET WARP to 0.
lock steering to SHIP:PROGRADE.
wait 3.
set runmode to 5.
}
}
else if runmode = 5 { //Burn to raise Periapsis
lock steering to SHIP:PROGRADE.
if ETA:APOAPSIS < 5 or VERTICALSPEED < 0 { //If we're less 5 seconds from Ap or loosing altitude
set TVAL to 1.
}
if (SHIP:PERIAPSIS > targetPeriapsis) or (SHIP:PERIAPSIS > targetApoapsis * 0.95) {
//If the periapsis is high enough or getting close to the apoapsis
set TVAL to 0.
set runmode to 10.
}
}
else if runmode = 10 { //Final touches
set TVAL to 0. //Shutdown engine.
panels on. //Deploy solar panels
lights on.
unlock steering.
print "SHIP SHOULD NOW BE IN SPACE!".
set runmode to 0.
}
if STAGE:LIQUIDFUEL = 0 AND stageHasLF = true {
set doStage to true.
}
if STAGE:SOLIDFUEL = 0 AND stageHasSF = true {
set doStage to true.
}
if currentThrust > SHIP:AVAILABLETHRUST{
set doStage to true.
}
set currentThrust to SHIP:AVAILABLETHRUST.
lock throttle to TVAL.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment