Advertisement
space_is_hard

CMLS1 0.2.2

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