Skip to content

Instantly share code, notes, and snippets.

@random-person-001
Created July 17, 2019 21:55
Show Gist options
  • Save random-person-001/1f77294f9f23fef4dd454330b39d2de5 to your computer and use it in GitHub Desktop.
Save random-person-001/1f77294f9f23fef4dd454330b39d2de5 to your computer and use it in GitHub Desktop.
KerboScript program to automatically run a launch abort sequence in Kerbal Space Program if it detects Bad Things
//
// aborter.ks
//
// A passive script to run an abort sequence under bad circumstances.
//
// This works well as a boot script (put it under
// Kerbal Space Program/Ships/Script/boot/ and set the boot script in the vehicle
// editor)
//
// Vehicle configuration:
// Capsule should be passively aerodynamically stable in the prograde direction
// while the abort system is attached. That's the only real requirement for
// construction config.
// Abort action group: detach the capsule from everything except for the abort
// system, and activate abort motors
// Action group 1: activate abort motors and detach them (for if nothing bad
// happens)
//
// Written 17 July 2019 on kOS 1.1.8 for KSP 1.7.3
//
print "LAS armed!".
set initialpartcount to ship:parts:length.
// automated sequence of events, from right after trouble is detected to touchdown.
declare function abortsequence {
abort on.
// We're passively stabilized by the wings, so we don't need to actively
// steer towards "up" here. We're also within about 20 degrees of the
// azimuth, so firing the engines gets us upward which is what we want.
// when the engines run out, eject them. We're in a free trajectory now.
wait 5. // Sepratrons burn for 5 seconds
AG1 on.
unlock steering.
sas off.
// wait until the highest point in our arc, then deploy all chutes
set max_alt to ship:altitude.
until ship:altitude < max_alt {
set max_alt to ship:altitude.
wait 0.1.
}
chutes on.
}
// Evaluate whether we're within mission tolerances, or whether we'll have to abort.
// This checks pointing direction, velocity direction, and whether we've lost parts.
declare function check_abort {
// tolerance is maximum degrees from azimuth we can be before triggering abort.
declare parameter tolerance.
set p to ship:facing - UP.
// anything outside of [0, tolerance]U[360-tolerance, 360] causes abort.
// thus (pitch + tolerance) % 360 - tolerance should map that to [0, tolerance] causing abort
if abs(mod(p:pitch + tolerance, 360) - tolerance) > tolerance {
print "pointing direction pitch outside tolerance".
return true.
}
// anything outside of [0, tolerance]U[360-tolerance, 360] causes abort.
if abs(mod(p:yaw + tolerance, 360) - tolerance) > tolerance {
print "pointing direction yaw outside tolerance".
return true.
}
if ship:airspeed > 1 {
set vel to ship:prograde - UP.
// anything outside of [0, tolerance]U[360-tolerance, 360] causes abort.
if abs(mod(vel:pitch + tolerance, 360) - tolerance) > tolerance {
print "airspeed pitch outside tolerance".
return true.
}
}
// if we're missing parts, we're probably blowing up.
if ship:parts:length < initialpartcount {
print "it appears that a rapid unscheduled disassembly is occuring.".
return true.
}
return false.
}
// abort system is safed above this altitude (in meters)
until ship:altitude > 30000 {
print ship:facing - UP.
print ship:prograde - UP.
if check_abort(20) { // check whether we should abort, with 20 degree tolerance.
abortsequence().
break.
}
wait 0.1.
}
// Eject the abort system.
AG1 on.
print "LAS safed.".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment