Skip to content

Instantly share code, notes, and snippets.

@agoodman
Created February 23, 2020 18:03
Show Gist options
  • Select an option

  • Save agoodman/90dd9733697fd27d9f5e64682f320ce0 to your computer and use it in GitHub Desktop.

Select an option

Save agoodman/90dd9733697fd27d9f5e64682f320ce0 to your computer and use it in GitHub Desktop.
// aubranium mun lander mk1
clearscreen.
set gravity to (constant():G * body:mass) / body:radius^2.
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 landingOffset to 3.416.
set iTWR to 0.
set steering to up.
set throttle to 0.
rcs on.
sas off.
until ship:maxthrust > 0 {
wait 0.5.
stage.
}
if ship:body = mun {
set runmode to 8.
}
else if ship:orbit:hasnextpatch {
set runmode to 6.
}
else if ship:periapsis > 70000 and ship:apoapsis > 70000 {
set runmode to 5.
}
else {
set runmode to 1.
}
print "Go baby go!".
until runmode = 0 {
if runmode = 1 { // liftoff
when alt:radar > 10 then { gear off. }
// available thrust / (m*g)
lock throttle to ship:availablethrust / (ship:mass * gravity * maxTWR).
wait until ship:airspeed > 140.
print "1 -> 2 Constant TWR ascent".
set runmode to 2.
}
if runmode = 2 { // constant TWR ascent
until ship:apoapsis > targetAltitude {
set targetThrottle to throttle.
// maintain constant TWR
set TWR to max(0.001, ship:maxthrust / (ship:mass * gravity)).
set throttle to min(1, max(0, targetTWR / TWR)).
set elevation to min(1, max(0, 1 - ship:altitude / (0.8*targetAltitude))).
set steering to heading(90, max(5, 90*elevation)).
}
print "2 -> 3 Coast to apoapsis".
lock steering to prograde.
set throttle to 0.
set runmode to 3.
}
if runmode = 3 { // coast to apoapsis
lock apoapsisCondition to ship:verticalspeed < 0.
wait until apoapsisCondition.
print "3 -> 4 Raise periapsis".
set runmode to 4.
}
if runmode = 4 { // raise periapsis
lock steering to prograde.
wait 1.0.
set throttle to 0.1.
wait until abs(targetAltitude - ship:periapsis)/targetAltitude < circularThreshold.
set throttle to 0.
print "4 -> 5 Compute Mun transfer".
set runmode to 5.
}
if runmode = 5 { // compute mun transfer maneuver
set td to 0.
set validTransfer to false.
until validTransfer = true {
// check for mun encounter
set newnode to node(time:seconds+td, 0, 0, 860).
add newnode.
set isMunEncounter to hasnode and nextnode:orbit:hasnextpatch and nextnode:orbit:nextpatch:body = mun.
set validMunPeriapsis to hasnode and nextnode:orbit:hasnextpatch and nextnode:orbit:nextpatch:periapsis > 10000.
set validTransfer to isMunEncounter and validMunPeriapsis.
if validTransfer = false {
wait 0.1.
remove newnode.
set td to td + 10.
}
}
print "5 -> 6 Execute Mun transfer".
set runmode to 6.
}
if runmode = 6 { // execute mun transfer maneuver
// warp to maneuver
print "wait for maneuver".
lock steering to nextnode:deltav:direction.
wait nextnode:eta.
// execute maneuver
print "execute maneuver".
set startVelocity to ship:velocity:orbit:mag.
set endVelocity to startVelocity + 860.
lock velocityCondition to ship:velocity:orbit:mag > endVelocity.
set throttle to 0.5.
wait until velocityCondition.
set throttle to 0.
print "6 -> 7 Wait for Mun encounter".
set runmode to 7.
}
if runmode = 7 { // wait for mun encounter
kuniverse:timewarp:cancelwarp().
wait 1.
kuniverse:timewarp:warpto(time:seconds + eta:transition).
wait eta:transition.
remove nextnode.
print "7 -> 8 Mun circularize".
set runmode to 8.
}
if runmode = 8 { // mun circularize
if ship:orbit:periapsis < 0 {
print "rescue periapsis".
lock steering to up.
lock minimumPeriapsisCondition to ship:orbit:periapsis > munarAltitude.
set throttle to 0.5.
wait until minimumPeriapsisCondition.
set throttle to 0.
wait 1.
}
// wait for periapsis
print "wait for periapsis".
lock steering to retrograde.
wait until ship:verticalspeed < 0.
kuniverse:timewarp:cancelwarp().
kuniverse:timewarp:warpto(time:seconds + eta:periapsis).
wait eta:periapsis.
// drop periapsis to specified altitude
print "lower periapsis".
lock targetPeriapsisCondition to ship:orbit:periapsis < munarAltitude.
set throttle to 0.5.
wait until targetPeriapsisCondition.
set throttle to 0.
wait 1.
// wait for periapsis
print "wait for periapsis".
kuniverse:timewarp:cancelwarp().
kuniverse:timewarp:warpto(time:seconds + eta:periapsis).
wait eta:periapsis.
// drop apoapsis to circularize
print "circularize".
lock circularCondition to ship:orbit:apoapsis < munarAltitude and ship:orbit:periapsis < munarAltitude.
set throttle to 0.5.
wait until circularCondition.
set throttle to 0.
print "8 -> 9 Deorbit".
set runmode to 9.
}
if runmode = 9 { // deorbit
// wait for equatorial positioning
lock landingCondition to abs(ship:geoposition:lng-targetLng) < 0.1.
wait until landingCondition.
lock steering to srfretrograde.
lock velocityCondition to ship:groundspeed < 50.
set throttle to 0.5.
wait until velocityCondition.
set throttle to 0.
print "9 -> 10 Landing".
set runmode to 10.
}
if runmode = 10 { // hoverslam
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). // 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.
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.
set throttle to 0.
print "10 -> 0 Landed".
set runmode to 0.
}
}
print "Mission complete.".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment