Advertisement
space_is_hard

CMLSsburn 0.2.0

Apr 1st, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.98 KB | None | 0 0
  1. //==CMLS SUICIDE BURN SCRIPT==
  2.  
  3. SET scriptVersion TO "v0.2.0".
  4.  
  5. //0.1.0
  6. // - First stable version
  7. //
  8. //0.1.1
  9. //--Major improvements--
  10. // - Experimental PI controller
  11. // - Automatic engine activation and deactivation handling
  12. // - Automatic Kp adjustment based on TWR
  13. //--Minor improvements--
  14. // - Exported initial engine setup to shutdown script
  15. // - Added PILOTMAINTHROTTLE control to ensure that the
  16. //   booster doesn't revert to half or full throttle once
  17. //   the script ends
  18. //
  19. //0.2.0
  20. //--Major improvements--
  21. // - Automatic opMode checking to handle the different
  22. //   types of cores (side booster vs S1)
  23. // - Reverted to P controller for simplicity; PI controller
  24. //   did not improve results and was difficult to tune
  25. // - Automatic handling of occasionally-necessary
  26. //   re-entry burn based on speed and remaining delta-v
  27. //--Minor improvements--
  28. // - Replaced shutdown script with the same CMLSeng script
  29. //   that handles the engine tagging and shutdown for the
  30. //   launch sequence. Updated launch scripts to
  31. //   automatically copy that script to the core/booster
  32. //   computers
  33. // - Added remaining delta-v and booster type readouts
  34. // - Moved altitude triggers outside of control loop
  35.  
  36. //~~~~~SPLASHSCREEN~~~~~
  37.  
  38. SET TERMINAL:WIDTH TO 50.
  39. SET TERMINAL:HEIGHT TO 25.
  40. CLEARSCREEN.
  41.  
  42. PRINT "==========COYOTE MLS SUICIDE BURN SCRIPT==========".
  43. PRINT "----------------------" + scriptVersion + "----------------------".
  44. WAIT 1.
  45.  
  46. PRINT "Initializing...".
  47.  
  48. RUN CMLSeng.
  49.  
  50. //~~~~~Variable Declaration~~~~~
  51. SET nextLine TO 7.      //Tracks next line that events should print at in the event log
  52.  
  53.  
  54. IF SHIP:PARTSTAGGED("S1Alpha"):LENGTH > 0 {
  55.     SET opMode TO "Core".
  56.    
  57. } ELSE IF SHIP:PARTSTAGGED("LBAlpha"):LENGTH > 0 {
  58.     SET opMode TO "LeftBooster".
  59.  
  60. } ELSE IF SHIP:PARTSTAGGED("RBAlpha"):LENGTH > 0 {
  61.     SET opMode TO "RightBooster".
  62.  
  63. }. //IF-ELSE opMode
  64.  
  65. FOR eng IN engAll {
  66.     IF eng:GETMODULE("ModuleEnginesFX"):HASEVENT("Shutdown Engine") {       //Shutdown all engines
  67.         eng:GETMODULE("ModuleEnginesFX"):DOEVENT("Shutdown Engine").
  68.     }.
  69. }.
  70.  
  71. IF opMode = "Core" {
  72.     S1Alpha:GETMODULE("ModuleEnginesFX"):DOEVENT("Activate Engine").        //Activate alpha-group engines
  73.     FOR eng IN S1Bravo {
  74.         eng:GETMODULE("ModuleEnginesFX"):DOEVENT("Activate Engine").        //Activate bravo-group engines
  75.     }. //FOR eng
  76.    
  77. } ELSE IF opMode = "LeftBooster" {
  78.     LBAlpha:GETMODULE("ModuleEnginesFX"):DOEVENT("Activate Engine").        //Activate alpha-group engines
  79.     FOR eng IN LBBravo {
  80.         eng:GETMODULE("ModuleEnginesFX"):DOEVENT("Activate Engine").        //Activate bravo-group engines
  81.     }. //FOR eng
  82.    
  83. } ELSE IF opMode = "RightBooster" {
  84.     RBAlpha:GETMODULE("ModuleEnginesFX"):DOEVENT("Activate Engine").        //Activate alpha-group engines
  85.     FOR eng IN RBBravo {
  86.         eng:GETMODULE("ModuleEnginesFX"):DOEVENT("Activate Engine").        //Activate bravo-group engines
  87.     }. //FOR eng
  88.    
  89. }. //IF opMode
  90.  
  91. RCS ON.
  92. GEAR OFF.
  93.  
  94. SET thrott TO 0.
  95. SET I TO 0.
  96.  
  97. SET SBSAmargin TO 10.       //Margin to allow for burns that start a little too late
  98.  
  99. LOCK dvRemain TO (engAll[0]:ISP * CONSTANT():G) * LN(SHIP:MASS / SHIP:DRYMASS).     //Keeps track of remaining delta-v
  100. LOCK TWR TO SHIP:MAXTHRUST / (SHIP:MASS * SHIP:SENSORS:GRAV:MAG).       //Keeps track of TWR
  101.  
  102. LOCK vsActual TO SHIP:VERTICALSPEED.
  103. SET radaltOffset TO 18.4.       //Fixed offset amount from probe core to landing legs
  104. LOCK radaltActual TO (ALT:RADAR - radaltOffset).        //Uses offset amount to track distance from bottom of landing legs to ground.
  105.  
  106. SET vsSetpoint TO -10000.       //Starts out really low so that the PI doesn't try to adjust the vertical speed whilst falling back to the launch site
  107.  
  108. WAIT 2.5.
  109.  
  110. //Readout header
  111. CLEARSCREEN.
  112. PRINT "==========COYOTE MLS MK1R SUICIDE BURN============" AT(0,0).
  113. PRINT "TWR: " AT(0,2).          //Readout at 6,2
  114. PRINT "BSA: " AT(16,2).         //Readout at 22,2
  115. PRINT "ARA: " AT(33,2).         //Readout at 39,2
  116.  
  117. PRINT "OPM: " AT(0,3).          //Readout at 6,3
  118. PRINT opMode AT(6,3).           //opMode readout; static from script start
  119. PRINT "DVR: " AT(33,3).         //Readout at 39,3
  120. PRINT "--------------------EVENT LOG---------------------" AT(0,5).
  121. PRINT "Stage ready for boostback burn" AT(0,nextLine).
  122. SET nextLine to nextLine + 1.
  123.  
  124. //~~~~~Boostback completion~~~~~
  125. WAIT UNTIL vsActual < 0.        //Wait until apogee
  126. WAIT 10.        //Wait another 10 seconds to allow for late/extended boostback burns
  127.  
  128. FOR eng IN S1Bravo {
  129.         eng:GETMODULE("ModuleEnginesFX"):DOEVENT("Shutdown Engine").        //Shutdown bravo-group engines, this leaves the single alpha engine running
  130. }.
  131.  
  132. FOR eng IN LBBravo {
  133.         eng:GETMODULE("ModuleEnginesFX"):DOEVENT("Shutdown Engine").
  134. }.
  135.  
  136. FOR eng IN RBBravo {
  137.         eng:GETMODULE("ModuleEnginesFX"):DOEVENT("Shutdown Engine").
  138. }.
  139.  
  140. PRINT "Stage prepared for re-entry." AT(0,nextLine).
  141. SET nextLine TO nextLine + 1.
  142.  
  143. WAIT UNTIL ALTITUDE < 15000.
  144.  
  145. IF VELOCITY:SURFACE:MAG > 1400 AND dvRemain >= 550 {
  146.     PRINT "Re-entry velocity high;" AT(0,nextLine).
  147.     SET nextLine TO nextLine + 1.
  148.     PRINT "Performing re-entry burn" AT(0,nextLine).
  149.     SET nextLine TO nextLine + 1.
  150.    
  151.     SET SHIP:CONTROL:PILOTMAINTHROTTLE TO 0.5.
  152.     WAIT UNTIL dvRemain < 500.
  153.     SET SHIP:CONTROL:PILOTMAINTHROTTLE TO 0.
  154.     PRINT "Re-entry burn complete" AT(0,nextLine).
  155.     SET nextLine TO nextLine + 1.
  156.    
  157. } ELSE IF VELOCITY:SURFACE:MAG > 1400 AND dvRemain < 550 {
  158.     PRINT "Re-entry velocity high;" AT(0,nextLine).
  159.     SET nextLine TO nextLine + 1.
  160.     PRINT "Delta-V margin below threshold; no re-entry burn" AT(0,nextLine).
  161.     SET nextLine TO nextLine + 1.
  162.  
  163. }. //IF-ELSE surf velocity
  164.  
  165. SAS OFF.
  166.  
  167. LOCK Kp TO (TWR * -0.3) + 1.3.      //Adjusts the gain based on the TWR
  168. LOCK P to (vsSetpoint - vsActual).      //Error - difference between desired VS and actual VS
  169. LOCK THROTTLE TO thrott.
  170. LOCK dthrott TO Kp * P.
  171.  
  172. LOCK SBSA TO (((vsActual^2) / (2 * (SHIP:MAXTHRUST / SHIP:MASS) - SHIP:SENSORS:GRAV:MAG)) + SBSAmargin).        //Determines suicide burn start altitude
  173.  
  174. //~~~~~Burn Control~~~~~
  175.  
  176. WHEN radaltActual < (SBSA + 250) THEN {     //Waits until 250m above SBSA
  177.     SAS ON.
  178.     PRINT "Prepare for suicide burn." AT(0,nextLine).
  179.     SET nextLine TO nextLine + 1.
  180.  
  181.     WHEN radaltActual < SBSA THEN {     //Waits until SBSA
  182.         LOCK vsSetpoint TO -3.      //Stage will burn fully until 3m/s descent
  183.         GEAR ON.
  184.         PRINT "Suicide burn initiated. Gear down." AT(0,nextLine).
  185.         SET nextLine TO nextLine + 1.
  186.        
  187.         WHEN radaltActual < 10 THEN {       //Below 10 meters, it switches to 1m/s descent
  188.             LOCK vsSetpoint TO -1.
  189.  
  190.         }. //WHEN < 10
  191.     }. //WHEN < SBSA
  192. }. //WHEN < 250 + SBSA
  193.  
  194. //~~~~~P Loop/Readouts~~~~~
  195.  
  196. UNTIL STATUS = "LANDED" {       //Loops until touchdown
  197.  
  198.     SET thrott TO thrott + dthrott.
  199.  
  200.     PRINT ROUND(TWR,1) + "    " AT(6,2).        //Prints TWR
  201.     PRINT ROUND(SBSA,1) + "    " AT(22,2).      //Prints suicide burn start alt
  202.     PRINT ROUND(radaltActual,1) + "    " AT(39,2).      //Prints the radar altitude adjusted for the landing legs
  203.    
  204.     PRINT dvRemain + "    " AT(39,3).       //Prints remaining DV
  205.     WAIT 0.001.
  206.  
  207. }. //UNTIL
  208.  
  209. PRINT "Touchdown!" at (0,nextLine).
  210. SET nextLine TO nextLine + 1.
  211. SAS OFF.
  212. RCS OFF.
  213. SET SHIP:CONTROL:PILOTMAINTHROTTLE TO 0.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement