Advertisement
space_is_hard

CMLSsburn 0.2.1

Apr 2nd, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.03 KB | None | 0 0
  1. //==CMLS SUICIDE BURN SCRIPT==
  2.  
  3. SET scriptVersion TO "v0.2.1".
  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. //0.2.1
  37. //--Major improvements--
  38. // - Integrated automatic re-entry burn into the terminal
  39. //   guidance control block; each action in the sequence of
  40. //   events is now part of one overall trigger block
  41. //--Minor improvements--
  42. // - Changed delta-v remaining readout to always show sea-
  43. //   level delta-v
  44. // - Further tuning of P controller
  45.  
  46. //~~~~~SPLASHSCREEN~~~~~
  47.  
  48. SET TERMINAL:WIDTH TO 50.
  49. SET TERMINAL:HEIGHT TO 25.
  50. CLEARSCREEN.
  51.  
  52. PRINT "==========COYOTE MLS SUICIDE BURN SCRIPT==========".
  53. PRINT "----------------------" + scriptVersion + "----------------------".
  54. WAIT 1.
  55.  
  56. PRINT "Initializing...".
  57.  
  58. RUN CMLSeng.
  59.  
  60. //~~~~~Variable Declaration~~~~~
  61. SET nextLine TO 7.      //Tracks next line that events should print at in the event log
  62.  
  63.  
  64. IF SHIP:PARTSTAGGED("S1Alpha"):LENGTH > 0 {
  65.     SET opMode TO "Core".
  66.    
  67. } ELSE IF SHIP:PARTSTAGGED("LBAlpha"):LENGTH > 0 {
  68.     SET opMode TO "LeftBooster".
  69.  
  70. } ELSE IF SHIP:PARTSTAGGED("RBAlpha"):LENGTH > 0 {
  71.     SET opMode TO "RightBooster".
  72.  
  73. }. //IF-ELSE opMode
  74.  
  75. FOR eng IN engAll {
  76.     IF eng:GETMODULE("ModuleEnginesFX"):HASEVENT("Shutdown Engine") {       //Shutdown all engines
  77.         eng:GETMODULE("ModuleEnginesFX"):DOEVENT("Shutdown Engine").
  78.     }.
  79. }.
  80.  
  81. IF opMode = "Core" {
  82.     S1Alpha[0]:GETMODULE("ModuleEnginesFX"):DOEVENT("Activate Engine").     //Activate alpha-group engines
  83.     FOR eng IN S1Bravo {
  84.         eng:GETMODULE("ModuleEnginesFX"):DOEVENT("Activate Engine").        //Activate bravo-group engines
  85.     }. //FOR eng
  86.    
  87. } ELSE IF opMode = "LeftBooster" {
  88.     LBAlpha[0]:GETMODULE("ModuleEnginesFX"):DOEVENT("Activate Engine").     //Activate alpha-group engines
  89.     FOR eng IN LBBravo {
  90.         eng:GETMODULE("ModuleEnginesFX"):DOEVENT("Activate Engine").        //Activate bravo-group engines
  91.     }. //FOR eng
  92.    
  93. } ELSE IF opMode = "RightBooster" {
  94.     RBAlpha[0]:GETMODULE("ModuleEnginesFX"):DOEVENT("Activate Engine").     //Activate alpha-group engines
  95.     FOR eng IN RBBravo {
  96.         eng:GETMODULE("ModuleEnginesFX"):DOEVENT("Activate Engine").        //Activate bravo-group engines
  97.     }. //FOR eng
  98.    
  99. }. //IF opMode
  100.  
  101. RCS ON.
  102. GEAR OFF.
  103.  
  104. SET thrott TO 0.
  105. SET I TO 0.
  106.  
  107. SET SBSA TO -10000.
  108. SET SBSAmargin TO 10.       //Margin to allow for burns that start a little too late
  109.  
  110. LOCK dvRemain TO (285 * 9.82) * LN(SHIP:MASS / SHIP:DRYMASS).       //Keeps track of remaining delta-v
  111. LOCK TWR TO SHIP:MAXTHRUST / (SHIP:MASS * SHIP:SENSORS:GRAV:MAG).       //Keeps track of TWR
  112.  
  113. LOCK vsActual TO SHIP:VERTICALSPEED.
  114. SET radaltOffset TO 18.4.       //Fixed offset amount from probe core to landing legs
  115. LOCK radaltActual TO (ALT:RADAR - radaltOffset).        //Uses offset amount to track distance from bottom of landing legs to ground.
  116.  
  117. 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
  118.  
  119. WAIT 0.5.
  120.  
  121. //Readout header
  122. CLEARSCREEN.
  123. PRINT "==========COYOTE MLS MK1R SUICIDE BURN============" AT(0,0).
  124. PRINT "TWR: " AT(0,2).          //Readout at 6,2
  125. PRINT "BSA: " AT(16,2).         //Readout at 22,2
  126. PRINT "ARA: " AT(33,2).         //Readout at 39,2
  127.  
  128. PRINT "OPM: " AT(0,3).          //Readout at 6,3
  129. PRINT opMode AT(6,3).           //opMode readout; static from script start
  130. PRINT "DVR: " AT(33,3).         //Readout at 39,3
  131. PRINT "--------------------EVENT LOG---------------------" AT(0,5).
  132. PRINT "Stage ready for boostback burn" AT(0,nextLine).
  133. SET nextLine to nextLine + 1.
  134.  
  135. //~~~~~Boostback completion~~~~~
  136. WAIT UNTIL vsActual < 0.        //Wait until apogee
  137. WAIT 30.        //Wait another 10 seconds to allow for late/extended boostback burns
  138.  
  139. FOR eng IN S1Bravo {
  140.         eng:GETMODULE("ModuleEnginesFX"):DOEVENT("Shutdown Engine").        //Shutdown bravo-group engines, this leaves the single alpha engine running
  141. }.
  142.  
  143. FOR eng IN LBBravo {
  144.         eng:GETMODULE("ModuleEnginesFX"):DOEVENT("Shutdown Engine").
  145. }.
  146.  
  147. FOR eng IN RBBravo {
  148.         eng:GETMODULE("ModuleEnginesFX"):DOEVENT("Shutdown Engine").
  149. }.
  150.  
  151. PRINT "Stage prepared for re-entry." AT(0,nextLine).
  152. SET nextLine TO nextLine + 1.
  153.  
  154. //~~~~~Burn control~~~~~
  155.  
  156. WHEN ALTITUDE < 40000 THEN {        //Check performed at 40km
  157.     IF dvRemain >= 500 AND VELOCITY:SURFACE:MAG > 1400 {        //If the velocity is high and enough dv remains...
  158.         PRINT "Re-entry velocity high;" AT(0,nextLine).
  159.         SET nextLine TO nextLine + 1.
  160.         PRINT "Performing re-entry burn" AT(0,nextLine).
  161.         SET nextLine TO nextLine + 1.
  162.         SET SHIP:CONTROL:PILOTMAINTHROTTLE TO 0.5.      //...set the throttle to half...
  163.        
  164.         WHEN dvRemain < 500 THEN {
  165.             SET SHIP:CONTROL:PILOTMAINTHROTTLE TO 0.        //...and burn until 500dv remains
  166.             PRINT "Re-entry burn complete" AT(0,nextLine).
  167.             SET nextLine TO nextLine + 1.
  168.         }. //WHEN dvRemain
  169.        
  170.     } ELSE IF dvRemain < 500 AND VELOCITY:SURFACE:MAG > 1400        //If there's not enough dv left, don't burn. Just pray.
  171.         PRINT "Re-entry velocity high;" AT(0,nextLine).
  172.         SET nextLine TO nextLine + 1.
  173.         PRINT "Delta-V margin below threshold; no re-entry burn" AT(0,nextLine).
  174.         SET nextLine TO nextLine + 1.
  175.        
  176.     }. //IF-ELSE
  177.    
  178.     WHEN ALTITUDE < 10000 THEN {
  179.         LOCK SBSA TO (((vsActual^2) / (2 * (SHIP:MAXTHRUST / SHIP:MASS) - SHIP:SENSORS:GRAV:MAG)) + SBSAmargin).        //Determines suicide burn start altitude
  180.         SET SHIP:CONTROL:PILOTMAINTHROTTLE TO 0.        //Stops re-entry burn if it's still going
  181.         PRINT "Atmospheric re-entry complete" AT(0,nextLine).
  182.         SET nextLine TO nextLine + 1.
  183.        
  184.             WHEN radaltActual < (SBSA + 250) THEN {     //Waits until 250m above SBSA
  185.             SAS ON.     //Vehicle should be completely vertical and with no horizontal velocity at this point
  186.             PRINT "Prepare for suicide burn." AT(0,nextLine).
  187.             SET nextLine TO nextLine + 1.
  188.  
  189.                 WHEN radaltActual < SBSA THEN {     //Waits until SBSA
  190.                     LOCK vsSetpoint TO -5.      //Stage will burn fully until 5m/s descent
  191.                     LOCK THROTTLE TO thrott.
  192.                     GEAR ON.
  193.                     PRINT "Suicide burn initiated. Gear down." AT(0,nextLine).
  194.                     SET nextLine TO nextLine + 1.
  195.                
  196.                     WHEN radaltActual < 3 THEN {        //Below 3 meters, it switches to 1m/s descent
  197.                         LOCK vsSetpoint TO -1.
  198.                
  199.                     }. //WHEN < 3
  200.                 }. //WHEN < SBSA
  201.             }. //WHEN < 250 + SBSA
  202.     }. //WHEN altitude
  203. }. //WHEN surf velocity
  204.  
  205. SAS OFF.
  206. LOCK Kp TO MAX(0.001,((TWR * -0.1) + 1.1)).     //Adjusts the gain based on the TWR
  207. LOCK P to (vsSetpoint - vsActual).      //Error - difference between desired VS and actual VS
  208. LOCK dthrott TO Kp * P.
  209.  
  210. //~~~~~P Loop/Readouts~~~~~
  211.  
  212. UNTIL STATUS = "LANDED" {       //Loops until touchdown
  213.  
  214.     SET thrott TO MIN(1, MAX(0,thrott + dthrott)).
  215.  
  216.     PRINT ROUND(TWR,1) + "    " AT(6,2).        //Prints TWR
  217.     PRINT ROUND(SBSA,1) + "    " AT(22,2).      //Prints suicide burn start alt
  218.     PRINT ROUND(radaltActual,1) + "    " AT(39,2).      //Prints the radar altitude adjusted for the landing legs
  219.    
  220.     PRINT ROUND(dvRemain) + "    " AT(39,3).        //Prints remaining DV
  221.    
  222.     WAIT 0.001.
  223.  
  224. }. //UNTIL
  225.  
  226. PRINT "Touchdown!" at (0,nextLine).
  227. SET nextLine TO nextLine + 1.
  228. UNLOCK THROTTLE.
  229. SAS OFF.
  230. RCS OFF.
  231. SET SHIP:CONTROL:PILOTMAINTHROTTLE TO 0.
  232. WAIT 3.
  233. CLEARSCREEN.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement