Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////////////////////////////
- //// Lantz's LKO script. v2.0 5/16/2015 ////
- ////////////////////////////////////////////
- // Synopsis: Attempts to put your rocket into an almost circular 70-75k orbit using a reactive method.
- // Where proactive means using equations for drag, mass, cross-sectional area, and so on, this script assumes
- // that all of those values are unknown. In other words, it assumes that KSP's drag system is in a black box.
- // Drag is minimized by calculating the rocket's jerk (change in acceleration) and throttling UP when jerk is
- // upwards, DOWN when jerk is downwards (drag fighting the ascent). Throttle adjustment is a flat value of 0.01
- // every time period, but in future versions will scale as needed (tldr; future PID).
- // This script assumes that your rocket's first stage has solid boosters (attached radially to a liquid engine).
- // Gravity turn is based on the curve of sqrt(x). Pitch will be 0 degrees right as the apoapsis reaches 75k.
- // Too many control elements (moveable fins, reaction wheels) may cause SAS to fight this script's steering.
- // If you must use fins, set them to control pitch only.
- CLEARSCREEN.
- SET time_then TO TIME:SECONDS.
- SET vel_then TO 0.
- SET acc_then TO 0.
- SET jerk TO 0. // change in acceleration, known as "jerk."
- SET time_period TO 0.1.
- SET thrott TO 1.0.
- SET turn_pitch TO 90.
- SET SHIP:CONTROL:PILOTMAINTHROTTLE TO 0. // This makes sure that throttle remains at zero when the controls are unlocked.
- SET seventyfive_k_achieved TO FALSE. // flag for when minimum trajectory is reached.
- LOCK STEERING TO HEADING(90,turn_pitch).
- SAS ON.
- LOCK THROTTLE TO thrott. // Lock THROTTLE to a variable once and change that variable to prevent trigger overflow.
- STAGE.
- PRINT "LAUNCHING.".
- // detach boosters.
- WHEN (STAGE:SOLIDFUEL < 0.01) THEN {
- PRINT "Boosters detaching.".
- STAGE.
- }
- // keep staging liquids until the rocket is dry, or orbit is achieved.
- WHEN (STAGE:LIQUIDFUEL < 0.01 AND SHIP:LIQUIDFUEL > 0 AND STAGE:READY) THEN {
- PRINT "Staging.".
- STAGE.
- PRESERVE. // preserved triggers will be checked over and over again.
- }
- // Calculate jerk each time period.
- WHEN (seventyfive_k_achieved = FALSE AND TIME:SECONDS - time_then > time_period) THEN {
- // calculate instantaneous acceleration since the last time period.
- SET vel_now TO SQRT((SHIP:VERTICALSPEED * SHIP:VERTICALSPEED) + (SHIP:SURFACESPEED * SHIP:SURFACESPEED)).
- SET acc_now TO (vel_now - vel_then)/(TIME:SECONDS - time_then).
- // calculate jerk by subtracting the previous acceleration from the current acceleration.
- SET jerk TO (acc_now - acc_then).
- //PRINT "CHANGE IN ACCELERATION: " + jerk.
- // save current time, speed, and acceleration for our next round of calculations.
- SET time_then TO TIME:SECONDS.
- SET vel_then TO vel_now.
- SET acc_then TO acc_now.
- // A little bit of negative jerk is necessary, otherwise the rocket will stall in some cases.
- // Too much drag, throttle down.
- IF (jerk < -0.1 AND thrott > 0) {
- //PRINT "THROTTLING DOWN.".
- SET thrott TO (thrott - 0.01).
- }
- // Otherwise, room to throttle up.
- IF (jerk > -0.1 AND thrott < 1.0) {
- //PRINT "THROTTLING UP.".
- SET thrott TO (thrott + 0.01).
- }
- // set pitch according to inverse of sqrt(x).
- SET turn_pitch TO (90 - (((SHIP:APOAPSIS/75000)^2) * 90)).
- // Keep checking this trigger.
- PRESERVE.
- }
- // Execute all previous triggers ("preserved" triggers can be executed repeatedly) until an apoapsis of 75k is reached.
- WAIT UNTIL SHIP:APOAPSIS > 75000.
- PRINT "Minimum trajectory achieved.".
- SET thrott TO 0.
- SET turn_pitch TO 0.
- SET seventyfive_k_achieved TO TRUE.
- // now wait until within 1000m of apoapsis. May need adjusting, see below.
- WAIT UNTIL ABS(SHIP:APOAPSIS - SHIP:ALTITUDE) < 1000.
- PRINT "Executing orbital burn...".
- SET thrott TO 1.0. // punch it.
- // keep burning until minimum orbit is reached, or until out of fuel.
- // If your rocket keeps burning because your *original* apoapsis dropped below 70k,
- // (i.e. engines too weak, rocket too heavy, needs more burn time to complete),
- // then you may want to adjust the burn window (Make it bigger than 1000m).
- WAIT UNTIL (SHIP:APOAPSIS > 70000 AND SHIP:PERIAPSIS > 70000).
- PRINT "Orbit achieved. Program quitting...".
- SET thrott TO 0.
- WAIT 1.
Advertisement
Add Comment
Please, Sign In to add comment