Advertisement
space_is_hard

CMLS2.ks - v0.3

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