Advertisement
space_is_hard

CMLSlaunch.ks - v0.3

Apr 10th, 2015
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.44 KB | None | 0 0
  1. //=====CMLS PRE-LAUNCH CONTROLLER=====
  2. //~~CMLSlaunch.ks~~
  3.  
  4. @LAZYGLOBAL OFF.
  5.  
  6. LOCAL scriptVersion TO "v0.3.0".
  7.  
  8. //~~~~~Changelog~~~~~ #open
  9. //0.3.0
  10. //--Major Improvements--
  11. // - Migrated all of the pre-launch work to pre-launch
  12. //   script, which will run from the archive. The staging,
  13. //   readout, and control work will be done from the
  14. //   second stage computer.
  15. // - Created boot script that compiles all CMLS scripts and
  16. //   provides instructions for launch script utilization
  17. // - Implemented preliminary versions of pitch, roll, and
  18. //   yaw PID controllers.
  19. // - Integrated boot script that compiles all required
  20. //   scripts and builds the function library
  21. //--Minor Improvements--
  22. // - General reorganization of code
  23. // - All variables now declared prior to use per LAZYGLOBAL
  24. // - Changed all "ModuleEnginesFX" to "ModuleEngines" to
  25. //   facilitate the uninstallation of HotRockets and
  26. //   CoolRockets
  27. // - Changed all DOEVENTs to DOACTIONs
  28. //
  29. //--TODO: This subversion--
  30. // - Apply all 0.3 changes to the Mk2 launch script
  31. // - Test and tune PID gains for all stages
  32. // - Clean up and integrate RTLS script into 0.3 framework
  33. // - Test, test, test!!!
  34. //
  35. //changelog #close
  36.  
  37. //~~~~~Splashscreen~~~~~ #open
  38.  
  39. CLEARSCREEN.
  40. PRINT "==========COYOTE MLS LAUNCH SETUP SCRIPT==========".
  41. PRINT "----------------------" + scriptVersion + "----------------------".
  42. WAIT 0.25.
  43. PRINT "Initializing...".
  44.  
  45. //splashscreen #close
  46.  
  47. //~~~~~Parameters~~~~~ #open
  48.  
  49. PARAMETER targetAlt.
  50. PARAMETER targetInc.
  51. PARAMETER launchNode.
  52.  
  53. PRINT "Target altitude for initial orbit: " + targetAlt + " kilometers".
  54. PRINT "Target inclination for initial orbit: " + targetInc + " degrees".
  55. PRINT "Calculating launch azimuth...".
  56.  
  57. GLOBAL launchAzimuth TO LAZcalc(targetAlt, targetInc, launchNode).      //Determine launch azimuth
  58.  
  59. IF launchNode = "A" {
  60.     PRINT "Launching from the (A)scending node".
  61.    
  62. } ELSE IF launchNode = "D" {
  63.     PRINT "Launching from the (D)escending node".
  64.    
  65. }.
  66.  
  67. //parameters #close
  68.  
  69. //~~~~~Variable declaration~~~~~ #open
  70.  
  71. LOCAL countdown TO 10.      //Length of countdown. Minimum of five seconds required.
  72. LOCAL CDmessage TO " ".     //Holds messages for countdown loop
  73. GLOBAL nextLine TO 7.       //Handles event log printing after liftoff
  74. GLOBAL fairingSep TO FALSE.     //Variable to view fairing separation status; false = still attached
  75. GLOBAL BECOtimer TO 10000.      //Marks BECO time
  76. GLOBAL S1ECOtimer TO 10000.     //Marks S1ECO time
  77. GLOBAL SBSepLevel TO 0.     //Percent of fuel desired for side booster RTLS.
  78. GLOBAL S1SepLevel TO 0.     //Percent of fuel desired for first stage RTLS.
  79. GLOBAL activeStage TO 0.        //Variable to view active stage, Mk2 versions start at 1B (booster)
  80. GLOBAL updateRes TO 10.     //Determines how many refreshes per second for the readout and PID controllers
  81. SET updateRes TO 1 / updateRes.     //Turns updateRes into decimal
  82.  
  83. GLOBAL S1LOX TO 0.      //Stage 1 liquid oxygen tank
  84. GLOBAL S2LOX TO 0.      //Stage 2 liquid oxygen tank
  85. GLOBAL LBLOX TO LIST().     //Left booster liquid oxygen tank
  86. GLOBAL RBLOX TO LIST().     //Right booster liquid oxygen tank
  87. GLOBAL loxCap TO 0.     //Variable to hold capacity of active stage's LOX tank
  88. GLOBAL loxAmount TO 0.      //Variable to hold amount of LOX in active stage's LOX tank
  89.  
  90. GLOBAL res TO 0.        //Variable to hold resources in FOR loops
  91. GLOBAL part TO 0.       //Variable to hold parts in FOR loops
  92. GLOBAL eng TO 0.        //Variable to hold engines in FOR loops
  93.  
  94. GLOBAL rollLock TO 0.       //Desired roll
  95. GLOBAL yawLock TO 0.        //Desired yaw
  96. GLOBAL pitchLock TO 0.      //Desired pitch
  97.  
  98. //variable declaration #close
  99.  
  100. //~~~~~OpMode Management~~~~~ #open
  101.  
  102. IF SHIP:PARTSTAGGED("Mk1"):LENGTH > 0 {
  103.     SET opMode TO "1".
  104.     SET activeStage TO "1".
  105.    
  106. } ELSE IF SHIP:PARTSTAGGED("Mk1R"):LENGTH > 0 {
  107.     SET opMode TO "1R".
  108.     SET activeStage TO "1".
  109.     SET S1SepLevel TO 15.
  110.     COPY CMLSsburn.ksm TO 2.
  111.    
  112. } ELSE IF SHIP:PARTSTAGGED("Mk2"):LENGTH > 0 {
  113.     SET opMode TO "2".
  114.     SET activeStage TO "1B".
  115.  
  116. } ELSE IF SHIP:PARTSTAGGED("Mk2RB"):LENGTH > 0 {
  117.     SET opMode TO "2RB".
  118.     SET activeStage TO "1B".
  119.     SET SBSepLevel TO 15.
  120.     COPY CMLSsburn.ksm TO 2.
  121.     COPY CMLSsburn.ksm TO 3.
  122.  
  123.  
  124. } ELSE IF SHIP:PARTSTAGGED("Mk2R"):LENGTH > 0 {
  125.     SET opMode TO "2R".
  126.     SET activeStage TO "1B".
  127.     SET SBSepLevel TO 15.
  128.     SET S1SepLevel TO 20.
  129.     COPY CMLSsburn.ksm TO 2.
  130.     COPY CMLSsburn.ksm TO 3.
  131.     COPY CMLSsburn.ksm TO 4.
  132.  
  133. }. //IF
  134.  
  135. CMLSeng_Tag().      //Places all tagged engines into approprite lists
  136. CMLSeng_Shutdown(engAll).       //Shuts down all engines for good measure.
  137. CMLSeng_Limit(engAll, 100).     //Sets all thrust limiter at 100 for good measure.
  138.  
  139. //opmode management #close
  140.  
  141. //~~~~~Parts~~~~~ #open
  142.  
  143. //~~Parts common to all variants~~ #open
  144.  
  145. SET lClamps TO SHIP:PARTSDUBBED("launchclamp1").        //Launch clamps holding first stage/boosters down
  146. SET interStage TO SHIP:PARTSTAGGED("intstage")[0].      //Interstage fairing and decoupler between first and second stage
  147. SET fairingShell TO SHIP:PARTSTAGGED("fairinghalf").        //Both fairing halves
  148. SET S2Tank TO SHIP:PARTSTAGGED("S2Tank")[0].
  149. SET S1Tank TO SHIP:PARTSTAGGED("S1Tank")[0].
  150. SET S2Ant TO SHIP:PARTSTAGGED("S2Ant")[0].      //Small omnidirectional antenna for second stage communication
  151.  
  152. FOR part IN lClamps {
  153.     part:GETMODULE("RefuelingPump"):DOEVENT("Toggle Pump").     //Turn on fuel pump to keep stages topped off
  154. }. //FOR lClamps pump
  155.  
  156. FOR res IN S2Tank:RESOURCES {
  157.     IF res:NAME = "LQDOXYGEN" {
  158.         SET S2LOX TO res.       //Sets LOX in S1 to S1LOX
  159.     }. //IF res
  160. }. //FOR res
  161.  
  162. FOR res IN S1Tank:RESOURCES {
  163.     IF res:NAME = "LQDOXYGEN" {
  164.         SET S1LOX TO res.       //Sets LOX in S2 to S2LOX
  165.     }. //IF res
  166. }. //FOR res
  167.  
  168. //common parts #close
  169.  
  170. //~~Parts dependent on variant~~ #open
  171.  
  172. IF opMode = "1" OR opMode = "1R" {      //Mk1 Variants
  173.  
  174.     IF opMode = "1R" {
  175.         SET S1Ant TO SHIP:PARTSTAGGED("S1Ant")[0].      //Small omnidirectional antenna for first stage communication
  176.         SET S1finHinges TO SHIP:PARTSTAGGED("S1FinHinge").      //Stage 1 fin hinges
  177.     }. //IF
  178.  
  179.     SET loxCap TO S1LOX:CAPACITY.
  180.     LOCK loxAmount TO S1LOX:AMOUNT.
  181.    
  182. } ELSE IF opMode = "2" OR opMode = "2RB" OR opMode = "2R" {     //Mk2 Variants
  183.    
  184.     IF opMode = "2RB" OR opMode = "2R" {
  185.         SET SBAnt TO SHIP:PARTSTAGGED("SBAnt").         //Small omnidirectional antennae for booster communication
  186.         SET SBfinHinges TO SHIP:PARTSTAGGED("SBFinHinge").      //Side booster fin hinges, only on 2RB or 2R
  187.     }. //IF opMode
  188.    
  189.     IF opMode = "2R" {
  190.         SET S1Ant TO SHIP:PARTSTAGGED("S1Ant")[0].      //Small omnidirectional antenna for first stage communication
  191.         SET S1finHinges TO SHIP:PARTSTAGGED("S1FinHinge").      //Stage 1 fin hinges
  192.     }. //IF opMode
  193.    
  194.     SET SBDecouplers TO SHIP:PARTSTAGGED("SBDecoup").
  195.     SET LBTank[0] TO SHIP:PARTSTAGGED("LBTankA")[0].        //Left booster main tank
  196.     SET LBTank[1] TO SHIP:PARTSTAGGED("LBTankB")[0].        //Left booster extension tank
  197.     SET RBTank[0] TO SHIP:PARTSTAGGED("RBTankA")[0].        //Right booster main tank
  198.     SET RBTank[1] TO SHIP:PARTSTAGGED("RBTankB")[0].        //Right booster extension tank
  199.    
  200.     FOR res IN LBTank[0]:RESOURCES {
  201.         IF res:NAME = "LQDOXYGEN" {
  202.             SET LBLOX[0] TO res.
  203.         }. //IF res
  204.     }. //FOR res
  205.    
  206.     FOR res IN LBTank[1]:RESOURCES {
  207.         IF res:NAME = "LQDOXYGEN" {
  208.             SET LBLOX[1] TO res.
  209.         }. //IF res
  210.     }. //FOR res
  211.    
  212.     FOR res IN RBTank[0]:RESOURCES {
  213.         IF res:NAME = "LQDOXYGEN" {
  214.             SET RBLOX[0] TO res.
  215.         }. //IF res
  216.     }. //FOR res
  217.    
  218.     FOR res IN RBTank[1]:RESOURCES {
  219.         IF res:NAME = "LQDOXYGEN" {
  220.             SET RBLOX[1] TO res.
  221.         }. //IF res
  222.     }. //FOR res
  223.    
  224.     SET loxCap TO (LBLOX[0]:CAPACITY + LBLOX[1]:CAPACITY + RBLOX[0]:CAPACITY + RBLOX[1]:CAPACITY).
  225.     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)
  226.    
  227. }. //IF-ELSE opmode parts
  228.  
  229. //dependent parts #close
  230.  
  231. LOCK loxRemaining TO (loxAmount / (loxCap + 0.01)).     //Locks percentage of fuel left in stage +0.01 to avoid divide by zero/NaN
  232.  
  233. //parts #close
  234.  
  235. //~~~~~Countdown~~~~~ #open
  236.  
  237. PRINT "Awaiting launch. Press Action Group 0 to".
  238. PRINT "start " + countdown + "-second countdown".
  239. AG10 OFF.
  240. WAIT UNTIL AG10.
  241. PRINT "Countdown sequence started".
  242.  
  243. UNTIL countdown = 0 {       //Countdown loop
  244.     IF countdown = 3 {      //Engines activate at T-3
  245.         CMLSeng_Ignite(engAll).     //Activates all first stage and booster engines
  246.         SET CDmessage TO " Main Engine Start".
  247.         SET BEItime to TIME:SECONDS.        //Records BEI time
  248.         SET SHIP:CONTROL:PILOTMAINTHROTTLE TO 1.        //Set throttle to 100%
  249.  
  250.     } ELSE IF countdown = 2 {
  251.         SET CDmessage TO " All engines healthy".
  252.         SAS ON.
  253.  
  254.     } ELSE IF countdown = 1 {
  255.         SET CDmessage TO " Releasing clamps".
  256.  
  257.     } ELSE {
  258.         SET CDmessage TO " ".       //Clears message variable if no event is happening that tick
  259.    
  260.     }. //IF countdown message      
  261.  
  262.     PRINT "T-" + countdown + CDmessage.     //Prints any message handed off by countdown
  263.     SET countdown TO countdown - 1.
  264.     WAIT 1.
  265.    
  266. }. //UNTIL countdown
  267.  
  268. LOCK TWR TO SHIP:MAXTHRUST / (SHIP:MASS * SHIP:SENSORS:GRAV:MAG).       //Keeps track of TWR
  269.  
  270. FOR PART IN lClamps {       //Loop to release launch clamps
  271.     PART:GETMODULE("launchclamp"):DOACTION("release clamp").        //TEST: DOACTION
  272. }. //FOR lClamps release
  273.  
  274. SET launchTime TO TIME:SECONDS.     //Records liftoff time
  275. LOCK missionClock TO ROUND((TIME:SECONDS - launchTime), 1).     //Sets missionClock to T-0 and starts counting, rounded to tenths of a second
  276.  
  277. SWITCH TO 1.
  278.  
  279. IF opMode = "1" OR opMode = "1R" {
  280.     RUN CMLS1run.
  281.    
  282. } ELSE IF opMode = "2" OR opMode = "2RB" OR opMode = "2R" {
  283.     RUN CMLS2run.
  284.    
  285. }. //IF-ELSE opMode run
  286.  
  287. //countdown #close
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement