space_is_hard

CMLS1 0.2.1

Mar 29th, 2015
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.21 KB | None | 0 0
  1. //==Coyote Medium LS Launch script 1 and 1R==
  2.  
  3. SET scriptVersion TO "v0.2.1".      //Version control
  4.  
  5. //~~CHANGELOG:~~
  6. //0.1.0
  7. // - First stable version
  8. //
  9. //0.2.0
  10. //--Major improvements--
  11. // - Added declared opMode to control vehicle version/operation mode
  12. // - Added staging control for Mk2 versions
  13. //--Minor improvements--
  14. // - Improved handling of engines and activations/shutdowns
  15. // - Improved fuel tank/fuel level handling
  16. // - All fuel pumps will now be turned on before liftoff in order to keep
  17. //   the boosters topped off in addition to the first and second stages
  18. // - Removed redundant timers; antenna activation now occurs on fairing sep
  19. // - Commented out unused "exit loop"; saved for later iteration that
  20. //   utilizes fault checking
  21. // - Switched liftoff handling to action group that starts short countdown
  22. // - Changed script exit parameters; target apoapsis altitude is declared on
  23. //   start and script closes when it is reached. A proper ascent should
  24. //   allow for a healthy periapsis by this time.
  25. //
  26. //0.2.1
  27. //--Major improvements--
  28. // - Separated 1/1R versions from 2/2RB/2R versions to decrease script
  29. //   size.
  30. //--Minor improvements--
  31. // - Automatically determined opMode
  32.  
  33.  
  34. //FUTURE SUBVERSION TODO:
  35. // - Automatically adjust throttle to impose g-force limit
  36. // - Fault checking
  37. // - Abort conditions and abort automation for future manned variant
  38. // - Expanded readouts
  39. // - Launch guidance assist (display vectors to aid manual control)
  40. // - Launch guidance automation (completely automate launch, one button
  41. //   press to orbit)
  42.  
  43. DECLARE PARAMETER targetAlt.
  44.  
  45. SET TERMINAL:WIDTH TO 50.
  46. SET TERMINAL:HEIGHT TO 25.
  47. CLEARSCREEN.
  48.  
  49. PRINT "========COYOTE MLS LAUNCH CONTROLLER SCRIPT=======".
  50. PRINT "----------------------" + scriptVersion + "----------------------".
  51. WAIT 0.25.
  52. PRINT "Initializing...".
  53.  
  54. //~~~~~Variables~~~~~
  55. SET countdown TO 10.        //Length of countdown. Minimum of five seconds required.
  56. SET CDmessage TO " ".       //Holds messages for countdown loop
  57. SET nextLine TO 7.      //Handles event log printing after liftoff
  58. SET fairingSep TO FALSE.        //Variable to view fairing separation status; false = still attached
  59. SET S1ECOtimer TO 10000.        //Marks S1ECO time
  60. SET S1SepLevel TO 0.        //Percent of fuel desired for first stage RTLS.
  61. SET activeStage TO "1".     //Variable to view active stage
  62. SET updateRes TO 10.        //Determines how many refreshes per second for the readout script
  63.  
  64. //~~~~~Script management and more variables~~~~~
  65.  
  66. IF SHIP:PARTSTAGGED("Mk1"):LENGTH > 0 {
  67.     SET opMode TO "1".
  68.    
  69. } ELSE IF SHIP:PARTSTAGGED("Mk1R"):LENGTH > 0 {
  70.     SET opMode TO "1R".
  71. //  COPY CMLSSBURN FROM 0.
  72. //  COPY CMLSSBURN TO 2.
  73. //  DELETE CMLSSBURN FROM 1.
  74.     SET S1SepLevel TO 15.
  75.    
  76. }. //IF
  77.  
  78. SWITCH TO 0.
  79. RUN CMLSeng.        //Tags all engines and shuts down any running
  80. SWITCH TO 1.
  81.  
  82. //~~~~~Parts~~~~~
  83. SET interStage TO SHIP:PARTSTAGGED("intstage")[0].      //Interstage fairing and decoupler between first and second stage
  84. SET fairingShell TO SHIP:PARTSTAGGED("fairinghalf").        //Both fairing halves
  85.  
  86. IF opMode = "1R" {
  87.     SET S1Ant TO SHIP:PARTSTAGGED("S1Ant")[0].      //Small omnidirectional antenna for first stage communication
  88.     SET S1finHinges TO SHIP:PARTSTAGGED("S1FinHinge").
  89. }. //IF opMode antenna
  90. SET S2Ant TO SHIP:PARTSTAGGED("S2Ant")[0].      //Small omnidirectional antenna for second stage communication
  91.  
  92. SET lClamps TO SHIP:PARTSDUBBED("launchclamp1").        //Launch clamps holding first stage/boosters down
  93. FOR part IN lClamps {
  94.     part:GETMODULE("RefuelingPump"):DOEVENT("Toggle Pump").     //Turn on fuel pump to keep stage topped off
  95. }. //FOR lClamps pump
  96.  
  97. SET S2Tank TO SHIP:PARTSTAGGED("S2Tank")[0].
  98. SET S1Tank TO SHIP:PARTSTAGGED("S1Tank")[0].
  99.  
  100. FOR res IN S1Tank:RESOURCES {
  101.     IF res:NAME = "LQDOXYGEN" {
  102.         SET S1LOX TO res.
  103.         SET loxCap TO S1LOX:CAPACITY.
  104.        
  105.     }. //IF res
  106. }. //FOR res
  107.  
  108. LOCK loxAmount TO S1LOX:AMOUNT.
  109. LOCK loxRemaining TO (loxAmount / (loxCap + 0.01)).     //Locks percentage of fuel left in stage +0.01 to avoid divide by zero/NaN
  110.  
  111. //~~~~~Countdown sequence~~~~~
  112.  
  113. PRINT "Awaiting launch. Press Action Group 0 to start " + countdown + "-second countdown".
  114. AG10 OFF.
  115. WAIT UNTIL AG10.
  116. PRINT "Countdown sequence started".
  117.  
  118. UNTIL countdown = 0 {       //Countdown loop
  119.     IF countdown = 3 {
  120.         FOR eng IN engAll {
  121.             eng:GETMODULE("ModuleEnginesFX"):DOEVENT("Activate Engine").
  122.         }. //FOR eng
  123.         SET message TO " Main Engine Start".
  124.         SET BEItime to TIME:SECONDS.        //Records BEI time
  125.  
  126.     } ELSE IF countdown = 2 {
  127.         SET message TO " All engines healthy".
  128.         SAS ON.
  129.  
  130.     } ELSE IF countdown = 1 {
  131.         SET message TO " Releasing clamps".
  132.  
  133.     } ELSE {
  134.         SET message TO " ".
  135.    
  136.     }. //IF countdown message      
  137.  
  138.     PRINT "T-" + countdown + message.
  139.     SET countdown TO countdown - 1.
  140.     WAIT 1.
  141.    
  142. }. //UNTIL countdown
  143.  
  144. FOR PART IN lClamps {       //Loop to release launch clamps
  145.     PART:GETMODULE("launchclamp"):DOEVENT("release clamp").
  146. }. //FOR lClamps release
  147.  
  148. SET launchTime TO TIME:SECONDS.     //Records liftoff time
  149. LOCK missionClock TO ROUND((TIME:SECONDS - launchTime), 1).     //Sets missionClock to T-0 and starts counting, rounded to tenths of a second
  150.  
  151. //~~~~~Staging control~~~~~
  152.  
  153. WHEN loxRemaining <= (S1SepLevel / 100) THEN {      //Checks to see if first stage is at sep level, shutdown engines and extend fins if true
  154.     FOR eng IN S1All {
  155.         eng:GETMODULE("ModuleEnginesFX"):DOEVENT("Shutdown Engine").
  156.     }. //FOR eng shutdown
  157.  
  158.  
  159.    
  160.     IF opMode = "1R" {
  161.         FOR part IN S1finHinges {
  162.             PART:GETMODULE("MuMechToggle"):DOACTION("move -",TRUE).
  163.         }. //FOR finhinge move
  164.         S1Ant:GETMODULE("ModuleRTAntenna"):DOEVENT("Activate").
  165.        
  166.     }. //IF opMode
  167.    
  168.     RCS ON.
  169.     SAS ON.
  170.     PRINT "T+" + missionClock + " Stage 1 at sep level, staging" AT(0,nextLine).
  171.     SET nextLine TO nextLine + 1.
  172.     SET S1ECOtimer to missionClock.     //Marks the time at which S1ECO occurred
  173.    
  174.     WHEN missionClock >= (S1ECOtimer + 1.5) THEN {      //Checks to see if 1.5 seconds has passed since S1ECO, quit extending fins if true
  175.         IF opMode = "1R" {
  176.             FOR part IN S1finHinges {
  177.                 PART:GETMODULE("MuMechToggle"):DOACTION("move -",FALSE).
  178.             }. //FOR finhinge stop
  179.            
  180.             PRINT "T+" + missionClock + " Fins extended" AT(0,nextLine).
  181.             SET nextLine TO nextLine + 1.
  182.            
  183.         }. //IF opMode
  184.        
  185.         WHEN missionClock >= (S1ECOtimer + 2.5) THEN {      //Checks to see if 2.5 seconds have passed since S1ECO, stage sep if true
  186.             interStage:GETMODULE("ModuleDecouple"):DOEVENT("Decouple").
  187.             PRINT "T+" + missionClock + " Staging confirmed" AT(0,nextLine).
  188.             SET nextLine TO nextLine + 1.
  189.             SET activeStage TO 2.       //Denotes that stage 2 is now the active stage
  190.             SET loxCap TO S2Tank:RESOURCES[1]:CAPACITY.
  191.             LOCK loxAmount TO S2Tank:RESOURCES[1]:AMOUNT.
  192.        
  193.             WHEN missionClock >= (S1ECOtimer + 7.5) THEN {      //Checks to see if 5 seconds have passed since stage sep, MEI if true
  194.                 FOR eng IN S2Alpha {
  195.                     eng:GETMODULE("ModuleEnginesFX"):DOEVENT("Activate Engine").
  196.                 }.
  197.                
  198.                 PRINT "T+" + missionClock + " MEI" AT(0,nextLine).
  199.                 SET nextLine TO nextLine + 1.
  200.    
  201.                 WHEN SHIP:ALTITUDE > 60000 AND missionClock >= (S1ECOtimer + 10.5) THEN {       //Fairing sep at 60km or 3 seconds after stage 2 ignition, both conditions need satisfied
  202.                         FOR PART IN fairingShell {
  203.                             PART:GETMODULE("ProceduralFairingDecoupler"):DOEVENT("Jettison").
  204.                         }. //FOR loop
  205.                         SET fairingSep TO TRUE.     //Readout will update to confirm fairing sep status
  206.                         S2Ant:GETMODULE("ModuleRTAntenna"):DOEVENT("Activate").     //Activate small omni on second stage
  207.                         PRINT "T+" + missionClock + " Fairing separation confirmed" AT(0,nextLine).
  208.                         SET nextLine TO nextLine + 1.
  209.                    
  210.                 }. //WHEN SHIP:ALTITUDE
  211.             }. //WHEN stage2StartTimer
  212.         }. //WHEN BECOtimer decouple
  213.     }. //WHEN BECOtimer finHinges
  214. }. //WHEN loxRemaining
  215.  
  216. //~~~~~Readouts~~~~~
  217. CLEARSCREEN.
  218. PRINT "============COYOTE MEDIUM LAUNCH SYSTEM===========" AT(0,0).
  219.  
  220. PRINT "ASL: " AT(0,2).      //Altitude above sea level readout at 6,2
  221. PRINT "VEL: " AT(16,2).     //Surface or orbital velocity readout at 22,2
  222. PRINT "CLK: " AT(33,2).     //Mission clock readout at 39,2
  223.  
  224. PRINT "STG: " AT(0,3).      //Current stage readout at 6,3
  225. PRINT "SFL: " AT(16,3).     //Stage fuel level readout at 22,3
  226. PRINT "FRG: " AT(33,3).     //Fairing status readout at 39,3
  227.  
  228. PRINT "-------------------EVENT LOG----------------------" AT(0,5).
  229.  
  230. PRINT "T-" + ROUND(ABS(BEItime - launchTime),2) + " BEI" AT(0,nextLine).        //Time of BEI, will be negative
  231. SET nextLine TO nextLine + 1.
  232. PRINT "T+0.0 Liftoff" AT(0,nextLine).
  233. SET nextLine TO nextLine + 1.
  234.  
  235. UNTIL SHIP:APOAPSIS >= (targetAlt * 1000) {
  236.     PRINT FLOOR(SHIP:ALTITUDE) + "   " AT(6,2).
  237.  
  238.     IF ALTITUDE <=50000 {       //Prints surface velocity below 50km, orbital above
  239.         PRINT ROUND(VELOCITY:SURFACE:MAG,1) + "   " AT(22,2).
  240.     } ELSE {
  241.         PRINT ROUND(VELOCITY:ORBIT:MAG,1) + "   " AT(22,2).
  242.     }. //IF-ELSE
  243.  
  244.     PRINT missionClock AT(39,2).
  245.     PRINT activeStage AT(6,3).
  246.     PRINT ROUND((loxRemaining * 100), 1) + "%  " AT(22,3).
  247.  
  248.     IF fairingSep = FALSE {         //Prints ATCHD or SPRTD depending on whether the fairings are still on
  249.         PRINT "ATCHD" + "   " AT(39,3).
  250.     } ELSE {
  251.         PRINT "SPRTD" + "   " AT(39,3).
  252.     }. //IF-ELSE
  253.  
  254.     WAIT (1 / updateRes).       //Update resolution turned into fraction
  255.    
  256. }. //UNTIL
  257.  
  258. CLEARSCREEN.
  259. PRINT "Target apoapsis reached. Manual control restored.".
  260. WAIT 1.
  261. CLEARSCREEN.
Advertisement
Add Comment
Please, Sign In to add comment