Guest User

LantzKO

a guest
May 16th, 2015
474
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 KB | None | 0 0
  1. ////////////////////////////////////////////
  2. //// Lantz's LKO script. v2.0 5/16/2015 ////
  3. ////////////////////////////////////////////
  4. // Synopsis: Attempts to put your rocket into an almost circular 70-75k orbit using a reactive method.
  5. // Where proactive means using equations for drag, mass, cross-sectional area, and so on, this script assumes
  6. // that all of those values are unknown. In other words, it assumes that KSP's drag system is in a black box.
  7. // Drag is minimized by calculating the rocket's jerk (change in acceleration) and throttling UP when jerk is
  8. // upwards, DOWN when jerk is downwards (drag fighting the ascent). Throttle adjustment is a flat value of 0.01
  9. // every time period, but in future versions will scale as needed (tldr; future PID).
  10. // This script assumes that your rocket's first stage has solid boosters (attached radially to a liquid engine).
  11. // Gravity turn is based on the curve of sqrt(x). Pitch will be 0 degrees right as the apoapsis reaches 75k.
  12. // Too many control elements (moveable fins, reaction wheels) may cause SAS to fight this script's steering.
  13. // If you must use fins, set them to control pitch only.
  14. CLEARSCREEN.
  15.  
  16. SET time_then TO TIME:SECONDS.
  17. SET vel_then TO 0.
  18. SET acc_then TO 0.
  19. SET jerk TO 0. // change in acceleration, known as "jerk."
  20. SET time_period TO 0.1.
  21. SET thrott TO 1.0.
  22. SET turn_pitch TO 90.
  23. SET SHIP:CONTROL:PILOTMAINTHROTTLE TO 0. // This makes sure that throttle remains at zero when the controls are unlocked.
  24. SET seventyfive_k_achieved TO FALSE. // flag for when minimum trajectory is reached.
  25.  
  26. LOCK STEERING TO HEADING(90,turn_pitch).
  27. SAS ON.
  28. LOCK THROTTLE TO thrott. // Lock THROTTLE to a variable once and change that variable to prevent trigger overflow.
  29. STAGE.
  30. PRINT "LAUNCHING.".
  31.  
  32.  
  33. // detach boosters.
  34. WHEN (STAGE:SOLIDFUEL < 0.01) THEN {
  35. PRINT "Boosters detaching.".
  36. STAGE.
  37. }
  38.  
  39. // keep staging liquids until the rocket is dry, or orbit is achieved.
  40. WHEN (STAGE:LIQUIDFUEL < 0.01 AND SHIP:LIQUIDFUEL > 0 AND STAGE:READY) THEN {
  41. PRINT "Staging.".
  42. STAGE.
  43. PRESERVE. // preserved triggers will be checked over and over again.
  44. }
  45.  
  46. // Calculate jerk each time period.
  47. WHEN (seventyfive_k_achieved = FALSE AND TIME:SECONDS - time_then > time_period) THEN {
  48.  
  49. // calculate instantaneous acceleration since the last time period.
  50. SET vel_now TO SQRT((SHIP:VERTICALSPEED * SHIP:VERTICALSPEED) + (SHIP:SURFACESPEED * SHIP:SURFACESPEED)).
  51. SET acc_now TO (vel_now - vel_then)/(TIME:SECONDS - time_then).
  52.  
  53. // calculate jerk by subtracting the previous acceleration from the current acceleration.
  54. SET jerk TO (acc_now - acc_then).
  55. //PRINT "CHANGE IN ACCELERATION: " + jerk.
  56.  
  57. // save current time, speed, and acceleration for our next round of calculations.
  58. SET time_then TO TIME:SECONDS.
  59. SET vel_then TO vel_now.
  60. SET acc_then TO acc_now.
  61.  
  62. // A little bit of negative jerk is necessary, otherwise the rocket will stall in some cases.
  63. // Too much drag, throttle down.
  64. IF (jerk < -0.1 AND thrott > 0) {
  65. //PRINT "THROTTLING DOWN.".
  66. SET thrott TO (thrott - 0.01).
  67. }
  68.  
  69. // Otherwise, room to throttle up.
  70. IF (jerk > -0.1 AND thrott < 1.0) {
  71. //PRINT "THROTTLING UP.".
  72. SET thrott TO (thrott + 0.01).
  73. }
  74.  
  75. // set pitch according to inverse of sqrt(x).
  76. SET turn_pitch TO (90 - (((SHIP:APOAPSIS/75000)^2) * 90)).
  77.  
  78. // Keep checking this trigger.
  79. PRESERVE.
  80. }
  81.  
  82. // Execute all previous triggers ("preserved" triggers can be executed repeatedly) until an apoapsis of 75k is reached.
  83. WAIT UNTIL SHIP:APOAPSIS > 75000.
  84. PRINT "Minimum trajectory achieved.".
  85. SET thrott TO 0.
  86. SET turn_pitch TO 0.
  87. SET seventyfive_k_achieved TO TRUE.
  88.  
  89. // now wait until within 1000m of apoapsis. May need adjusting, see below.
  90. WAIT UNTIL ABS(SHIP:APOAPSIS - SHIP:ALTITUDE) < 1000.
  91. PRINT "Executing orbital burn...".
  92. SET thrott TO 1.0. // punch it.
  93.  
  94. // keep burning until minimum orbit is reached, or until out of fuel.
  95. // If your rocket keeps burning because your *original* apoapsis dropped below 70k,
  96. // (i.e. engines too weak, rocket too heavy, needs more burn time to complete),
  97. // then you may want to adjust the burn window (Make it bigger than 1000m).
  98. WAIT UNTIL (SHIP:APOAPSIS > 70000 AND SHIP:PERIAPSIS > 70000).
  99. PRINT "Orbit achieved. Program quitting...".
  100. SET thrott TO 0.
  101. WAIT 1.
Advertisement
Add Comment
Please, Sign In to add comment