Skip to content

Instantly share code, notes, and snippets.

@agoodman
Created April 30, 2020 05:36
Show Gist options
  • Select an option

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

Select an option

Save agoodman/ba459cb3c6eb0b80df8a3fbb405e0f57 to your computer and use it in GitHub Desktop.
// aubranium minmus lander mk1
clearscreen.
set gravity to (constant():G * body:mass) / body:radius^2.
set targetBody to minmus.
set targetAltitude to 80000.
set munarAltitude to 25000.
set targetLng to -30.
set transferDV to 915.//922.
set maxTWR to 3.
set targetTWR to 1.3. //25.
set circularThreshold to 0.05.
set landingOffset to 3.416.
set targetInclination to 0.
set ascentVelocity to 150.
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 = targetBody {
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!".
function orbit_normal {
parameter orbit_in.
return VCRS(orbit_in:body:position - orbit_in:position,
orbit_in:velocity:orbit):normalized.
}
function relative_inclination {
parameter orbit_a, orbit_b.
return abs(vang(orbit_normal(orbit_a), orbit_normal(orbit_b))).
}
until runmode = 0 {
if runmode = 1 { // liftoff
lock steering to heading(90+targetInclination, 90).
when alt:radar > 10 then { gear off. }
// available thrust / (m*g)
lock throttle to ship:availablethrust / (ship:mass * gravity * maxTWR).
wait until ship:airspeed > ascentVelocity.
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+targetInclination, 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 Adjust inclination".
set runmode to 5.
}
if runmode = 5 { // adjust inclination
set target to "Minmus".
lock ra to relative_inclination(ship:orbit, target:orbit).
set td to 0.
set dv to 100.
set newnode to node(time:seconds+td, 0, dv, 0).
add newnode.
set lastRA to relative_inclination(newnode:orbit, target:orbit).
set shouldContinue to true.
set isConverging to false.
until shouldContinue = false {
remove newnode.
set newnode to node(time:seconds+td, 0, dv, 0).
add newnode.
set nextRA to relative_inclination(newnode:orbit, target:orbit).
if isConverging = false and nextRA < lastRA {
set isConverging to true.
}
else if isConverging = true and nextRA > lastRA {
set shouldContinue to false.
}
print lastRA.
set lastRA to nextRA.
set td to td + 10.
wait 0.1. //0.005.
}
set shouldContinue to true.
until shouldContinue = false {
remove newnode.
set newnode to node(time:seconds+td, 0, dv, 0).
add newnode.
set nextRA to relative_inclination(newnode:orbit, target:orbit).
if nextRA < 0.5 {
set shouldContinue to false.
}
else {
set dv to dv + 10.
}
print lastRA.
set lastRA to nextRA.
wait 0.1. //0.005.
}
lock steering to nextnode:deltav.
wait 5.
kuniverse:timewarp:cancelwarp().
wait 1.
kuniverse:timewarp:warpto(time:seconds + nextnode:eta).
wait nextnode:eta.
set initialRA to ra.
lock throttleParam to abs(ra / initialRA).
lock throttle to max(0.75*throttleParam, 0.05).
set initialDV to nextnode:deltav:mag.
set endVelocity to ship:orbit:velocity:orbit:mag + initialDV.
lock maneuverCondition to nextnode:deltav:mag < initialDV *.05.
wait until maneuverCondition.
set throttle to 0.
remove nextnode.
print "5 -> 6 Compute Minmus transfer".
set runmode to 6.
}
if runmode = 6 { // compute minmus transfer maneuver
set td to 0.
set dv to transferDV.
set validTransfer to false.
until validTransfer = true {
// check for mun encounter
set newnode to node(time:seconds+td, 0, 0, dv).
add newnode.
wait 0.005.
set isEncounter to hasnode and
nextnode:orbit:hasnextpatch and
nextnode:orbit:nextpatch:body = targetBody.
set validPeriapsis to hasnode and
nextnode:orbit:hasnextpatch and
nextnode:orbit:nextpatch:periapsis > munarAltitude and
nextnode:orbit:nextpatch:periapsis < munarAltitude * 10.
set validTransfer to isEncounter and validPeriapsis.
if validTransfer = false {
wait 0.005.
remove newnode.
set td to td + 10.
}
}
print "6 -> 7 Execute Minmus transfer".
set runmode to 7.
}
if runmode = 7 { // execute minmus transfer maneuver
// warp to maneuver
print "wait for maneuver".
lock steering to nextnode:deltav:direction.
wait 3.
kuniverse:timewarp:cancelwarp().
wait 1.
kuniverse:timewarp:warpto(time:seconds + nextnode:eta).
wait nextnode:eta.
// execute maneuver
print "execute maneuver".
set startVelocity to ship:velocity:orbit:mag.
set endVelocity to startVelocity + transferDV.
lock throttleParam to (endVelocity - ship:velocity:orbit:mag) / (endVelocity - startVelocity).
lock throttle to max(0.75*throttleParam, 0.05).
lock encounterCondition to ship:orbit:hasnextpatch and ship:orbit:nextpatch:body = targetBody.
wait until encounterCondition.
set throttle to 0.
set steering to retrograde.
remove nextnode.
print "7 -> 8 Wait for Minmus encounter".
set runmode to 8.
}
if runmode = 8 { // wait for minmus encounter
kuniverse:timewarp:cancelwarp().
wait 1.
kuniverse:timewarp:warpto(time:seconds + eta:transition).
wait eta:transition.
wait 3.
print "8 -> 9 Drop periapsis".
set runmode to 9.
}
if runmode = 9 { // drop periapsis
wait until ship:orbit:body = targetBody.
// wait for periapsis
print "wait for periapsis".
kuniverse:timewarp:cancelwarp().
wait 1.
kuniverse:timewarp:warpto(time:seconds + eta:periapsis).
wait eta:periapsis.
wait 3.
// drop periapsis to specified altitude
lock steering to retrograde.
lock targetPeriapsisCondition to ship:orbit:periapsis < munarAltitude.
wait 3.
set throttle to 0.1.
wait until targetPeriapsisCondition.
set throttle to 0.
wait 3.
print "9 -> 10 Circularize".
set runmode to 10.
}
if runmode = 10 { // minmus circularize
// wait for periapsis
print "wait for periapsis".
kuniverse:timewarp:cancelwarp().
wait 1.
kuniverse:timewarp:warpto(time:seconds + eta:periapsis - 5).
wait eta:periapsis - 5.
lock steering to retrograde.
wait 10.
// drop apoapsis to circularize
print "circularize".
lock circularCondition to ship:orbit:apoapsis > 0 and ship:orbit:apoapsis <= munarAltitude.
set throttle to 0.1.
wait until circularCondition.
set throttle to 0.
print "10 -> 11 Deorbit".
set runmode to 11.
}
if runmode = 11 { // deorbit
kuniverse:timewarp:cancelwarp().
wait 1.
kuniverse:timewarp:warpto(time:seconds + eta:apoapsis).
wait eta:apoapsis.
// wait for equatorial positioning
// lock landingCondition to abs(ship:geoposition:lng-targetLng) < 0.1.
// wait until landingCondition.
lock steering to srfretrograde.
wait 10.
lock horizontalCondition to ship:groundspeed < 50.
lock verticalCondition to ship:verticalspeed < -0.1.
set throttle to 0.5.
wait until horizontalCondition and verticalCondition.
set throttle to 0.
print "11 -> 12 Hoverslam".
set runmode to 12.
}
if runmode = 12 { // 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.
wait 3.
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 "12 -> 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