Advertisement
space_is_hard

CMLS1.ks - v0.3

Apr 10th, 2015
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.78 KB | None | 0 0
  1. //=====CMLS Mk1/Mk1R LAUNCH CONTROL SCRIPT=====
  2. //~~CMLS1run.ks~~
  3.  
  4. @LAZYGLOBAL OFF.
  5.  
  6. LOCAL scriptVersion TO "v0.3.0".
  7.  
  8. //Changelog located in CMLSlaunch script
  9.  
  10. //~~~~~Staging Control~~~~~ #open
  11.  
  12. WHEN loxRemaining <= (S1SepLevel / 100) THEN {      //Checks to see if first stage is at sep level, shutdown engines and extend fins if true
  13.     CMLSeng_Shutdown(S1All).
  14.    
  15.     IF opMode = "1R" {
  16.         FOR part IN S1finHinges {
  17.             part:GETMODULE("MuMechToggle"):DOACTION("move -",TRUE).
  18.         }. //FOR finhinge move
  19.         S1Ant:GETMODULE("ModuleRTAntenna"):DOEVENT("Activate").
  20.        
  21.     }. //IF opMode
  22.    
  23.     RCS ON.
  24.     PRINT "T+" + missionClock + " Stage 1 at sep level, staging" AT(0,nextLine).
  25.     SET nextLine TO nextLine + 1.
  26.     SET S1ECOtimer TO missionClock.     //Marks the time at which S1ECO occurred
  27.    
  28.     WHEN missionClock >= (S1ECOtimer + 1.5) THEN {      //Checks to see if 1.5 seconds has passed since S1ECO, quit extending fins if true
  29.         IF opMode = "1R" {
  30.             FOR part IN S1finHinges {
  31.                 part:GETMODULE("MuMechToggle"):DOACTION("move -",FALSE).
  32.             }. //FOR finhinge stop
  33.            
  34.             PRINT "T+" + missionClock + " Fins extended" AT(0,nextLine).
  35.             SET nextLine TO nextLine + 1.
  36.            
  37.         }. //IF opMode
  38.        
  39.         WHEN missionClock >= (S1ECOtimer + 2.5) THEN {      //Checks to see if 2.5 seconds have passed since S1ECO, stage sep if true
  40.             interStage:GETMODULE("ModuleDecouple"):DOEVENT("Decouple").
  41.             PRINT "T+" + missionClock + " Staging confirmed" AT(0,nextLine).
  42.             SET nextLine TO nextLine + 1.
  43.             SET activeStage TO 2.       //Denotes that stage 2 is now the active stage
  44.             SET loxCap TO S2Tank:RESOURCES[1]:CAPACITY.
  45.             LOCK loxAmount TO S2Tank:RESOURCES[1]:AMOUNT.
  46.        
  47.             WHEN missionClock >= (S1ECOtimer + 7.5) THEN {      //Checks to see if 5 seconds have passed since stage sep, MEI if true
  48.                 CMLSeng_Ignite(S2Alpha).        //Ignite stage 2 engine            
  49.                 PRINT "T+" + missionClock + " MEI" AT(0,nextLine).
  50.                 SET nextLine TO nextLine + 1.
  51.    
  52.                 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
  53.                         FOR PART IN fairingShell {
  54.                             PART:GETMODULE("ProceduralFairingDecoupler"):DOEVENT("Jettison").
  55.                         }. //FOR loop
  56.                         SET fairingSep TO TRUE.     //Readout will update to confirm fairing sep status
  57.                         S2Ant:GETMODULE("ModuleRTAntenna"):DOEVENT("Activate").     //Activate small omni on second stage
  58.                         PRINT "T+" + missionClock + " Fairing separation confirmed" AT(0,nextLine).
  59.                         SET nextLine TO nextLine + 1.
  60.                    
  61.                 }. //WHEN SHIP:ALTITUDE
  62.             }. //WHEN stage2StartTimer
  63.         }. //WHEN BECOtimer decouple
  64.     }. //WHEN BECOtimer finHinges
  65. }. //WHEN loxRemaining
  66.  
  67. //staging #close
  68.  
  69. //~~~~~Directional Control~~~~~ #open
  70.  
  71. SET pitchPID TO PID_Init(0.05, 0, 0).       //TEST: Pitch PID gains
  72. SET rollPID TO PID_Init(0.05, 0, 0).        //TEST: Roll PID gains
  73. SET yawPID TO PID_Init(0.02, 0, 0).     //TEST: Yaw PID gains
  74.  
  75. WHEN VELOCITY:SURFACE:MAG > 10 {        //Once we get going
  76.     SAS OFF.
  77.     SET pitchLock TO 88.        //Pulls the ship slightly off of directly up to have the roll make sense
  78.     SET rollLock TO 180.        //Rolls heads down; will remain so throughout launch
  79.     SET yawLock TO launchAzimuth.       //Yaws towards the launch azimuth
  80.    
  81.     WHEN ABS(currentYaw - launchAzimuth) < 5 {      //Once yaw maneuver is nearly complete
  82.         SET pitchLock TO  80.       //Pitch kick; pulls ten degrees down to get the gravity turn started
  83.    
  84.         WHEN VELOCITY:SURFACE:MAG > 120 {
  85.             LOCK pitchLock TO SHIP:SRFPROGRADE:PITCH.       //Locks pitch to surface prograde
  86.        
  87.             WHEN activeStage = "2" {        //Once staging is confirmed
  88.                 LOCK pitchLock TO SHIP:PROGRADE:PITCH.      //Locks pitch to orbit prograde
  89.                 LOCK yawLock TO SHIP:SRFPROGRADE:YAW.       //Locks yaw to surface prograde; should roughly hold sidereal launch azimuth
  90.                 //TODO: Implement updated PID gains for second stage
  91.                
  92.                 WHEN ETA:APOAPSIS < 5 {
  93.                     LOCK pitchLock TO  SHIP:VERTICALSPEED.      //TEST: See if this maintains vertical speed
  94.                    
  95.                 }. //WHEN eta:apoapsis
  96.             }. //WHEN activeStage
  97.         }. //WHEN surfVel
  98.     }. //WHEN surfVel
  99. }. //WHEN surfVel
  100.  
  101. //directional control #close
  102.  
  103. //~~~~~Acceleration Control~~~~~ #open
  104.  
  105. WHEN TWR > 3.0 AND activeStage = "1" THEN {
  106.     FOR eng IN S1Charlie {
  107.         eng:GETMODULE("ModuleEngines"):SETFIELD("Thrust Limiter",50).
  108.     }. //FOR eng
  109.     PRINT "T+" + missionClock + " Throttling back charlie-group engines" AT(0,nextLine).
  110.     SET nextLine TO nextLine + 1.
  111. }. //WHEN acc
  112.  
  113. //acceleration control #close
  114.  
  115. //~~~~~Readout Header~~~~~ #open
  116.  
  117. CLEARSCREEN.
  118. PRINT "============COYOTE MEDIUM LAUNCH SYSTEM===========" AT(0,2).
  119. PRINT "ASL: " AT(0,3).      //Altitude above sea level readout at 6,3
  120. PRINT "VEL: " AT(16,3).     //Surface or orbital velocity readout at 22,3
  121. PRINT "CLK: " AT(33,3).     //Mission clock readout at 39,3
  122.  
  123. PRINT "STG: " AT(0,4).      //Current stage readout at 6,4
  124. PRINT "SFL: " AT(16,4).     //Stage fuel level readout at 22,4
  125. PRINT "FRG: " AT(33,4).     //Fairing status readout at 39,4
  126. PRINT "-------------------EVENT LOG----------------------" AT(0,5).
  127.  
  128. PRINT "T-" + ROUND(ABS(BEItime - launchTime),2) + " BEI" AT(0,nextLine).        //Time of BEI, will be negative
  129. SET nextLine TO nextLine + 1.
  130. PRINT "T+0.0 Liftoff" AT(0,nextLine).
  131. SET nextLine TO nextLine + 1.
  132.  
  133. //readout header #close
  134.  
  135. //~~~~~Run Loop~~~~~ #open
  136.  
  137. UNTIL SHIP:APOAPSIS >= (targetAlt * 1000) {
  138.    
  139.     //~~Directional Control~~ #open
  140.    
  141.     //Pitch Control
  142.     SET currentPitch TO 90 - VECTORANGLE(UP:VECTOR, SHIP:FACING:FOREVECTOR).
  143.     SET SHIP:CONTROL:PITCH TO PID_Seek(pitchPID, pitchLock, currentPitch).
  144.    
  145.     //Roll Control
  146.     SET currentroll TO VECTORANGLE(UP:VECTOR, SHIP:FACING:STARVECTOR).
  147.     SET SHIP:CONTROL:ROLL TO PID_Seek(rollPID, rollLock, currentRoll).
  148.    
  149.     //Yaw Control
  150.     SET rawyaw TO (-1 * SHIP:BEARING).
  151.     IF rawyaw < 0 {
  152.         SET currentyaw TO (rawyaw + 360).
  153.     } ELSE {
  154.         SET currentyaw TO rawyaw.
  155.     }.
  156.     SET SHIP:CONTROL:YAW TO PID_Seek(yawPID, yawLock, currentYaw).
  157.    
  158.     //directional control #close
  159.    
  160.     //~~Readouts~~ #open
  161.    
  162.     //Mission info
  163.     PRINT FLOOR(SHIP:ALTITUDE) + "   " AT(6,3).
  164.     IF ALTITUDE <=50000 {       //Prints surface velocity below 50km, orbital above
  165.         PRINT ROUND(VELOCITY:SURFACE:MAG,1) + "   " AT(22,3).
  166.     } ELSE {
  167.         PRINT ROUND(VELOCITY:ORBIT:MAG,1) + "   " AT(22,3).
  168.     }. //IF-ELSE
  169.     PRINT missionClock + "   " AT(39,3).
  170.    
  171.     //Systems info
  172.     PRINT activeStage + "   " AT(6,4).
  173.     PRINT ROUND((loxRemaining * 100), 1) + "%  " AT(22,4).
  174.     IF fairingSep = FALSE {         //Prints ATCHD or SPRTD depending on whether the fairings are still on
  175.         PRINT "ATCHD" + "   " AT(39,4).
  176.     } ELSE {
  177.         PRINT "SPRTD" + "   " AT(39,4).
  178.     }. //IF-ELSE
  179.    
  180.     //readouts #close
  181.  
  182.     WAIT updateRes.
  183.    
  184. }. //UNTIL
  185.  
  186. //run loop #close
  187.  
  188. //~~~~~Exit instructions~~~~~ #open
  189.  
  190. CLEARSCREEN.
  191. SET SHIP:CONTROL:PILOTMAINTHROTTLE TO 0.
  192. PRINT "Target apoapsis reached. Manual control restored.".
  193. WAIT 1.
  194. CLEARSCREEN.
  195. //exit #close
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement