Advertisement
space_is_hard

CMLS2 0.2.2

Mar 31st, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 15.15 KB | None | 0 0
  1. //==Coyote Medium LS Launch script 2, 2RB and 2R==
  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 BECOtimer TO 10000.     //Marks BECO time
  78. SET S1ECOtimer TO 10000.        //Marks S1ECO time
  79. SET SBSepLevel TO 0.        //Percent of fuel desired for side booster RTLS.
  80. SET S1SepLevel TO 0.        //Percent of fuel desired for first stage RTLS.
  81. SET activeStage TO "1B".        //Variable to view active stage, Mk2 versions start at 1B (booster)
  82. SET updateRes TO 10.        //Determines how many refreshes per second for the readout script
  83.  
  84. //~~~~~Script management and more variables~~~~~
  85.  
  86. IF SHIP:PARTSTAGGED("Mk2"):LENGTH > 0 {
  87.     SET opMode TO "2".
  88.  
  89. } ELSE IF SHIP:PARTSTAGGED("Mk2RB"):LENGTH > 0 {
  90.     SET opMode TO "2RB".
  91.    
  92.     COPY CMLSsburn.ksm FROM 0.
  93.     COPY CMLSsburn.ksm TO 2.
  94.     COPY CMLSsburn.ksm TO 3.
  95.     DELETE CMLSsburn.ksm FROM 1.
  96.  
  97.     COPY CMLSeng.ksm FROM 0.
  98.     COPY CMLSeng.ksm TO 2.
  99.     COPY CMLSeng.ksm TO 3.
  100.     DELETE CMLSeng.ksm FROM 1.
  101.  
  102.     SET SBSepLevel TO 15.
  103.  
  104. } ELSE IF SHIP:PARTSTAGGED("Mk2R"):LENGTH > 0 {
  105.     SET opMode TO "2R".
  106.    
  107.     COPY CMLSsburn.ksm FROM 0.
  108.     COPY CMLSsburn.ksm TO 2.
  109.     COPY CMLSsburn.ksm TO 3.
  110.     COPY CMLSsburn.ksm TO 4.
  111.     DELETE CMLSsburn.ksm FROM 1.
  112.  
  113.     COPY CMLSeng.ksm FROM 0.
  114.     COPY CMLSeng.ksm TO 2.
  115.     COPY CMLSeng.ksm TO 3.
  116.     COPY CMLSeng.ksm TO 4.
  117.     DELETE CMLSeng.ksm FROM 1.
  118.  
  119.     SET SBSepLevel TO 15.
  120.     SET S1SepLevel TO 20.
  121.  
  122. }. //IF
  123.  
  124. SWITCH TO 0.
  125. RUN CMLSeng.        //Tags all engines and shuts down any running
  126. SWITCH TO 1.
  127.  
  128. //~~~~~Parts~~~~~
  129.  
  130. SET interStage TO SHIP:PARTSTAGGED("intstage")[0].      //Interstage fairing and decoupler between first and second stage
  131. SET fairingShell TO SHIP:PARTSTAGGED("fairinghalf").        //Both fairing halves
  132.  
  133. IF opMode = "2RB" OR opMode = "2R" {
  134.     SET SBAnt TO SHIP:PARTSTAGGED("SBAnt").         //Small omnidirectional antennae for booster communication
  135.     SET SBfinHinges TO SHIP:PARTSTAGGED("SBFinHinge").      //Side booster fin hinges, only on 2RB or 2R
  136. }. //IF opMode
  137.  
  138. IF opMode = "2R" {
  139.     SET S1Ant TO SHIP:PARTSTAGGED("S1Ant")[0].      //Small omnidirectional antenna for first stage communication
  140.     SET S1finHinges TO SHIP:PARTSTAGGED("S1FinHinge").      //Stage 1 fin hinges, only on 2R
  141. }. //IF opMode
  142.  
  143. SET S2Ant TO SHIP:PARTSTAGGED("S2Ant")[0].      //Small omnidirectional antenna for second stage communication
  144.  
  145. SET lClamps TO SHIP:PARTSDUBBED("launchclamp1").        //Launch clamps holding first stage/boosters down
  146. FOR part IN lClamps {
  147.     part:GETMODULE("RefuelingPump"):DOEVENT("Toggle Pump").     //Turn on fuel pump to keep stage topped off
  148. }. //FOR lClamps pump
  149.  
  150. SET S2Tank TO SHIP:PARTSTAGGED("S2Tank")[0].        //Stage 2 fuel tank
  151. SET S1Tank TO SHIP:PARTSTAGGED("S1Tank")[0].        //Stage 2 fuel tank
  152.  
  153. FOR res IN S1Tank:RESOURCES {
  154.     IF res:NAME = "LQDOXYGEN" {
  155.         SET S1LOX TO res.
  156.         SET loxCap TO S1LOX:CAPACITY.       //Sets lox capacity for stage 1 tank
  157.        
  158.     }. //IF res
  159. }. //FOR res
  160.  
  161. SET SBDecouplers TO SHIP:PARTSTAGGED("SBDecoup").
  162. SET LBTank[0] TO SHIP:PARTSTAGGED("LBTankA")[0].        //Left booster main tank
  163. SET LBTank[1] TO SHIP:PARTSTAGGED("LBTankB")[0].        //Left booster extension tank
  164. SET RBTank[0] TO SHIP:PARTSTAGGED("RBTankA")[0].        //Right booster main tank
  165. SET RBTank[1] TO SHIP:PARTSTAGGED("RBTankB")[0].        //Right booster extension tank
  166.  
  167. FOR res IN LBTank[0]:RESOURCES {
  168.     IF res:NAME = "LQDOXYGEN" {
  169.         SET LBLOX[0] TO res.
  170.     }. //IF res
  171. }. //FOR res
  172.  
  173. FOR res IN LBTank[1]:RESOURCES {
  174.     IF res:NAME = "LQDOXYGEN" {
  175.         SET LBLOX[1] TO res.
  176.     }. //IF res
  177. }. //FOR res
  178.  
  179. FOR res IN RBTank[0]:RESOURCES {
  180.     IF res:NAME = "LQDOXYGEN" {
  181.         SET RBLOX[0] TO res.
  182.     }. //IF res
  183. }. //FOR res
  184.  
  185. FOR res IN RBTank[1]:RESOURCES {
  186.     IF res:NAME = "LQDOXYGEN" {
  187.         SET RBLOX[1] TO res.
  188.     }. //IF res
  189. }. //FOR res
  190.  
  191. SET loxCap TO (LBLOX[0]:CAPACITY + LBLOX[1]:CAPACITY + RBLOX[0]:CAPACITY + RBLOX[1]:CAPACITY).
  192. LOCK loxAmount TO MIN((LBLOX[0]:AMOUNT + LBLOX[1]:AMOUNT),(RBLOX[0]:AMOUNT + RBLOX[1]:AMOUNT)).     //Locks remaining LOX amount to the lesser of the two side booster tanks (two tanks on each booster)
  193. LOCK loxRemaining TO (loxAmount / (loxCap + 0.01)).     //Locks percentage of fuel left in stage +0.01 to avoid divide by zero/NaN
  194.  
  195. //~~~~~Countdown sequence~~~~~
  196.  
  197. PRINT "Awaiting launch. Press Action Group 0 to start " + countdown + "-second countdown".
  198. AG10 OFF.
  199. WAIT UNTIL AG10.
  200. PRINT "Countdown sequence started".
  201.  
  202. UNTIL countdown = 0 {       //Countdown loop
  203.     IF countdown = 3 {      //Engines activate at T-3
  204.         FOR eng IN engAll {
  205.             eng:GETMODULE("ModuleEnginesFX"):DOEVENT("Activate Engine").
  206.         }. //FOR eng
  207.         SET message TO " Main Engine Start".
  208.         SET BEItime to TIME:SECONDS.        //Records BEI time
  209.         SET SHIP:CONTROL:PILOTMAINTHROTTLE TO 1.        //Set throttle to 100%
  210.  
  211.     } ELSE IF countdown = 2 {
  212.         SET message TO " All engines healthy".
  213.         SAS ON.
  214.  
  215.     } ELSE IF countdown = 1 {
  216.         SET message TO " Releasing clamps".
  217.  
  218.     } ELSE {
  219.         SET message TO " ".     //Clears message variable if no event is happening that tick
  220.    
  221.     }. //IF countdown message      
  222.  
  223.     PRINT "T-" + countdown + message.       //Prints any message handed off by countdown
  224.     SET countdown TO countdown - 1.
  225.     WAIT 1.
  226.    
  227. }. //UNTIL countdown
  228.  
  229. LOCK TWR TO SHIP:MAXTHRUST / (SHIP:MASS * SHIP:SENSORS:GRAV:MAG).       //Keeps track of TWR
  230.  
  231. FOR PART IN lClamps {       //Loop to release launch clamps
  232.     PART:GETMODULE("launchclamp"):DOEVENT("release clamp").
  233. }. //FOR lClamps release
  234.  
  235. SET launchTime TO TIME:SECONDS.     //Records liftoff time
  236. LOCK missionClock TO ROUND((TIME:SECONDS - launchTime), 1).     //Sets missionClock to T-0 and starts counting, rounded to tenths of a second
  237.  
  238. //~~~~~Staging control~~~~~
  239.  
  240. WHEN loxRemaining <= (SBSepLevel / 100) THEN {      //Checks to see if side boosters are at sep level
  241.     FOR eng IN LBAll {
  242.         eng:GETMODULE("ModuleEnginesFX"):DOEVENT("Shutdown Engine").        //Shutdown right booster engines
  243.     }. //FOR eng
  244.  
  245.     FOR eng IN RBAll {
  246.         eng:GETMODULE("ModuleEnginesFX"):DOEVENT("Shutdown Engine").        //Shutdown left booster engines
  247.     }. //FOR eng
  248.  
  249.     IF opMode = "2RB" OR opMode = "2R" {
  250.         FOR part IN SBfinHinges {
  251.             PART:GETMODULE("MuMechToggle"):DOACTION("move -",TRUE).     //Extend side booster fins
  252.         }. //FOR finhinge move
  253.  
  254.         FOR part IN SBAnt {
  255.             SBAnt:GETMODULE("ModuleRTAntenna"):DOEVENT("Activate").     //Activate side booster antennas
  256.         }. //FOR SBant
  257.     }. //IF opMode
  258.  
  259.     PRINT "T+" + missionClock + " Boosters at sep level. Staging" AT(0,nextLine).
  260.     SET nextLine TO nextLine + 1.
  261.     SET BECOtimer to missionClock.      //Sets BECOtimer
  262.  
  263.     WHEN missionClock >= (BECOtimer + 1.5) THEN {
  264.         IF opMode = "Mk2RB" OR opMode = "Mk2R" {
  265.             FOR part IN S2finHinges {
  266.                 PART:GETMODULE("MuMechToggle"):DOACTION("move -",FALSE).        //Stop moving side booster fin hinges
  267.             }. //FOR finhinge stop
  268.         }. //IF
  269.  
  270.         WHEN missionClock >= (BECOtimer + 2.5) THEN {
  271.             FOR part IN SBDecouplers {
  272.                 part:GETMODULE("ModuleDecouple"):DOEVENT("Decouple").       //Decouple side boosters &*&*&*VERIFY&*&*&*
  273.             }. //FOR part
  274.             SET activeStage TO "1".
  275.  
  276.             WHEN loxRemaining <= (S1SepLevel / 100) THEN {      //Checks to see if first stage is at sep level
  277.                 FOR eng IN S1All {
  278.                     eng:GETMODULE("ModuleEnginesFX"):DOEVENT("Shutdown Engine").        //Shutdown S1 engines
  279.                 }. //FOR eng shutdown
  280.  
  281.                 IF opMode = "2R" {
  282.                     FOR part IN S1finHinges {
  283.                         PART:GETMODULE("MuMechToggle"):DOACTION("move -",TRUE).     //Extend fin hinges for reuseable core
  284.                     }. //FOR finhinge move
  285.                     S1Ant:GETMODULE("ModuleRTAntenna"):DOEVENT("Activate").     //Activate antenna for reuseable core
  286.                 }. //IF opMode
  287.  
  288.                 RCS ON.
  289.                 SAS ON.
  290.                 PRINT "T+" + missionClock + " Stage 1 at sep level, staging" AT(0,nextLine).
  291.                 SET nextLine TO nextLine + 1.
  292.                 SET S1ECOtimer to missionClock.     //Marks the time at which S1ECO occurred
  293.  
  294.                 WHEN missionClock >= (S1ECOtimer + 1.5) THEN {      //Checks to see if 1.5 seconds has passed since S1ECO
  295.                     IF opMode = "2R" {
  296.                         FOR part IN S1finHinges {
  297.                             PART:GETMODULE("MuMechToggle"):DOACTION("move -",FALSE).        //Quit extending fin hinges
  298.                         }. //FOR finhinge stop
  299.                         PRINT "T+" + missionClock + " Fins extended" AT(0,nextLine).
  300.                         SET nextLine TO nextLine + 1.
  301.                     }. //IF opMode
  302.  
  303.                     WHEN missionClock >= (S1ECOtimer + 2.5) THEN {      //Checks to see if 2.5 seconds have passed since S1ECO
  304.                         interStage:GETMODULE("ModuleDecouple"):DOEVENT("Decouple").     //Separate core and second stage
  305.                         PRINT "T+" + missionClock + " Staging confirmed" AT(0,nextLine).
  306.                         SET nextLine TO nextLine + 1.
  307.                         SET activeStage TO 2.       //Denotes that stage 2 is now the active stage
  308.                         SET loxCap TO S2Tank:RESOURCES[1]:CAPACITY.     //Sets loxCap to stage 2 lox capacity
  309.                         LOCK loxAmount TO S2Tank:RESOURCES[1]:AMOUNT.       //Locks loxAmount to stage 2 lox amount
  310.  
  311.                         WHEN missionClock >= (S1ECOtimer + 7.5) THEN {      //Checks to see if 5 seconds have passed since stage sep,
  312.                             FOR eng IN S2Alpha {
  313.                                 eng:GETMODULE("ModuleEnginesFX"):DOEVENT("Activate Engine").        //Main engine ignition
  314.                             }.
  315.                             PRINT "T+" + missionClock + " MEI" AT(0,nextLine).
  316.                             SET nextLine TO nextLine + 1.
  317.  
  318.                             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
  319.                                 FOR PART IN fairingShell {
  320.                                     PART:GETMODULE("ProceduralFairingDecoupler"):DOEVENT("Jettison").       //Separate fairings
  321.                                 }. //FOR loop
  322.                                 SET fairingSep TO TRUE.     //Readout will update to confirm fairing sep status
  323.                                 S2Ant:GETMODULE("ModuleRTAntenna"):DOEVENT("Activate").     //Activate small omni on second stage
  324.                                 PRINT "T+" + missionClock + " Fairing separation confirmed" AT(0,nextLine).
  325.                                 SET nextLine TO nextLine + 1.
  326.  
  327.                             }. //WHEN SHIP:ALTITUDE
  328.                         }. //WHEN stage2StartTimer
  329.                     }. //WHEN BECOtimer decouple
  330.                 }. //WHEN BECOtimer finHinges
  331.             }. //WHEN loxRemaining
  332.         }. //WHEN missionClock decouple
  333.     }.//WHEN missionClock fins stop
  334. }. //WHEN loxRemaining
  335.  
  336. //~~~~~Acceleration Control~~~~~
  337.  
  338. WHEN TWR > 3.0 AND activeStage = "1B" THEN {
  339.     FOR eng IN LBCharlie {
  340.         eng:GETMODULE("ModuleEnginesFX"):SETFIELD("Thrust Limiter",50).     //Drops thrust limiter on LB charlie-group engines to 50%
  341.     }. //FOR eng
  342.    
  343.     FOR eng IN RBCharlie {
  344.         eng:GETMODULE("ModuleEnginesFX"):SETFIELD("Thrust Limiter",50).     //Drops thrust limiter on RB charlie-group engines to 50%
  345.     }. //FOR eng
  346.    
  347.     FOR eng IN S1Charlie {
  348.         eng:GETMODULE("ModuleEnginesFX"):SETFIELD("Thrust Limiter", 50).        //Drops thrust limiter on 1S charlie-group engines to 50%
  349.     }. //FOR eng
  350.    
  351.     PRINT "T+" + missionClock + " Throttling back charlie-group engines" AT(0,nextLine).
  352.     SET nextLine TO nextLine + 1.
  353.    
  354.     WHEN activeStage = "1" THEN {       //Once booster sep is complete, full throttle on S1 charlie-group engines
  355.         FOR eng IN S1Charlie {
  356.             eng:GETMODULE("ModuleEnginesFX"):SETFIELD("Thrust Limiter", 100).       //Resets thrust limiter on 1S charlie-group engines to 100%
  357.         }. //FOR eng
  358.        
  359.         WHEN SHIP:SENSORS:ACC:MAG > 3.0 AND activeStage = "1" THEN {
  360.             FOR eng IN S1Charlie {
  361.                 eng:GETMODULE("ModuleEnginesFX"):SETFIELD("Thrust Limiter", 50).        //Drops thrust limiter on 1S charlie-group engines to 50%
  362.             }.
  363.            
  364.             PRINT "T+" + missionClock + " Throttling back charlie-group engines" AT(0,nextLine).
  365.             SET nextLine TO nextLine + 1.
  366.            
  367.         }. //WHEN acc
  368.     }. //WHEN activeStage
  369. }. //WHEN acc
  370.  
  371. //~~~~~Readouts~~~~~
  372.  
  373. CLEARSCREEN.
  374. PRINT "============COYOTE MEDIUM LAUNCH SYSTEM===========" AT(0,0).
  375.  
  376. PRINT "ASL: " AT(0,2).      //Altitude above sea level readout at 6,2
  377. PRINT "VEL: " AT(16,2).     //Surface or orbital velocity readout at 22,2
  378. PRINT "CLK: " AT(33,2).     //Mission clock readout at 39,2
  379.  
  380. PRINT "STG: " AT(0,3).      //Current stage readout at 6,3
  381. PRINT "SFL: " AT(16,3).     //Stage fuel level readout at 22,3
  382. PRINT "FRG: " AT(33,3).     //Fairing status readout at 39,3
  383.  
  384. PRINT "-------------------EVENT LOG----------------------" AT(0,5).
  385.  
  386. PRINT "T-" + ROUND(ABS(BEItime - launchTime),2) + " BEI" AT(0,nextLine).        //Time of BEI, will be negative
  387. SET nextLine TO nextLine + 1.
  388. PRINT "T+0.0 Liftoff" AT(0,nextLine).
  389. SET nextLine TO nextLine + 1.
  390.  
  391. UNTIL SHIP:APOAPSIS >= (targetAlt * 1000) {
  392.     PRINT FLOOR(SHIP:ALTITUDE) + "   " AT(6,2).
  393.  
  394.     IF ALTITUDE <=50000 {       //Prints surface velocity below 50km, orbital above
  395.         PRINT ROUND(VELOCITY:SURFACE:MAG,1) + "   " AT(22,2).
  396.     } ELSE {
  397.         PRINT ROUND(VELOCITY:ORBIT:MAG,1) + "   " AT(22,2).
  398.     }. //IF-ELSE
  399.  
  400.     PRINT missionClock + "   " AT(39,2).
  401.     PRINT activeStage + "   " AT(6,3).
  402.     PRINT ROUND((loxRemaining * 100), 1) + "%  " AT(22,3).
  403.  
  404.     IF fairingSep = FALSE {         //Prints ATCHD or SPRTD depending on whether the fairings are still on
  405.         PRINT "ATCHD" + "   " AT(39,3).
  406.     } ELSE {
  407.         PRINT "SPRTD" + "   " AT(39,3).
  408.     }. //IF-ELSE
  409.  
  410.     WAIT (1 / updateRes).       //Update resolution turned into fraction
  411.    
  412. }. //UNTIL
  413.  
  414. CLEARSCREEN.
  415. SET SHIP:CONTROL:PILOTMAINTHROTTLE TO 0.
  416. PRINT "Target apoapsis reached. Manual control restored.".
  417. WAIT 1.
  418. CLEARSCREEN.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement