Advertisement
space_is_hard

atlasLaunch.ks

Apr 28th, 2015
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.83 KB | None | 0 0
  1. //Atlas Launch Controller
  2.  
  3. @LAZYGLOBAL OFF.
  4.  
  5. LOCAL scriptVersion TO "v0.1".
  6.  
  7. //=========================================================
  8. //Script Initialization #open
  9.  
  10. CLEARSCREEN.
  11. SET TERMINAL:WIDTH TO 50.
  12. SET TERMINAL:HEIGHT TO 25.
  13. PRINT "==============ATLAS LAUNCH CONTROLLER=============".
  14. PRINT "----------------Script Version " + scriptVersion + "---------------".
  15. PRINT "Initializing...".
  16.  
  17. DECLARE PARAMETER
  18.     targetAlt,      //Target altitude of final circular orbit
  19.     targetInc.      //Target inclination of final circular orbit; negative for southerly launch
  20.  
  21. SWITCH TO 0.
  22. RUN LIB_LAZcalc.ks.     //Launch azimuth calculator
  23. RUN LIB_PID.ks.             //Generic PID controller
  24. RUN LIB_navball.ks.     //Navball reading translator
  25. SWITCH TO 1.
  26.  
  27. //initialization #close
  28. //=========================================================
  29. //Variable Declaration #open
  30.  
  31. LOCAL opMode TO -1.                     //Pre-launch start value
  32. LOCAL typeFairing TO "3".       //Fairing diameter
  33. LOCAL typeBooster TO "0".       //Number of SRBs
  34. LOCAL typeEngines TO "1".       //Number of upper stage engines
  35. LOCAL vesselType TO typeFairing + typeBooster + typeEngines.        //Puts it all together
  36. LOCAL launchAzimuth TO LAZcalc(targetAlt, targetInc).                   //Determines launch azimuth
  37. LOCAL controlReady TO FALSE.            //Whether to implement PID controller
  38. LOCAL controlMode TO "Modified".    //Modified has roll as compass heading for initial upwards launch; Standard has roll as normal
  39. LOCAL readoutReady TO FALSE.            //Whether to display readouts
  40. LOCAL logReady TO FALSE.                    //Whether to log flight data
  41. LOCAL countdown TO 10.                          //Countdown start value
  42. LOCAL countdownMessage TO " ".      //Countdown message holder
  43. LOCAL activeStage TO 1.                     //Stores active stage
  44. LOCAL missionClock TO -1.                   //Stores seconds since launch
  45. LOCAL launchTime TO -1.                     //Stores time of launch clamp release
  46.  
  47. LOCAL pitchInit TO PID_init(0.05,0.02,0,-1,1)//Initial values for first stage flight
  48. LOCAL rollInit TO PID_init(0.05,0.02,0,-1,1).       //Initial values for first stage flight
  49. LOCAL yawInit TO PID_init(0.05,0.02,0,-1,1).            //Initial values for first stage flight
  50. LOCAL pitchDesired TO 0.        //Desired pitch value (ship orientation)
  51. LOCAL rollDesired TO 0.         //Desired roll value (ship orientation)
  52. LOCAL yawDesired TO 0.              //Desired yaw value (ship orientation)
  53. LOCAL pitchCurrent TO pitch_for(SHIP).      //Starts at 90 on launchpad
  54. LOCAL rollCurrent TO compass_for(SHIP).     //Starts as compass heading of top of ship
  55. LOCAL yawCurrent TO 0.                                              //Starts as 0
  56. LOCAL throttInit TO PID_init(0.05,0.02,0,-1,1).     //Initial values for first stage flight
  57. LOCAL throttDesired TO 0.       //Desired throttle value
  58.  
  59. LOCAL TWR TO 0.
  60.  
  61. //declaration #close
  62. //=========================================================
  63. //Parts declaration #open
  64.  
  65. LOCAL LOXTanks TO LIST(SHIP:PARTSTAGGED("S1Tank"), SHIP:PARTSTAGGED("S2Tank")).                                                     //Array holding both stage's LOX tanks
  66. LOCAL stageOxidizerCapacity TO LIST(LOXTanks[0]:LQDOXYGEN:CAPACITY, LOXTanks[1]:LQDOXYGEN:CAPACITY).    //Array holding capacity of both stage's LOX tanks
  67. LOCAL S2RCSTank TO SHIP:PARTSTAGGED("S2RCSTank").   //Stage 2 RCS tank
  68. LOCAL S2RCSCapacity TO S2RCSTank:A50:CAPACITY.      //Stage 2 RCS tank capacity
  69.  
  70. //parts declaration #close
  71. //=========================================================
  72. //Locks #open
  73.  
  74. LOCK missionClock TO launchTime - TIME:SECONDS.     //Keeps track of time since launch clamp release
  75. LOCK TWR TO SHIP:MAXTHRUST / (SHIP:MASS * SHIP:SENSORS:GRAV:MAG).                                   //Keeps track of TWR
  76. LOCK stageOxidizerAmount TO LOXTanks[activeStage + 1]:LQDOXYGEN:AMOUNT + 0.001. //Keeps track of LOX amount in active stage
  77. LOCK stageOxidizerLevel TO (stageOxidizerAmount / stageOxidizerCapacity) * 100. //Keeps track of percent LOX remaining in active stage
  78. LOCK S2RCSAmount TO S2RCSTank:A50:AMOUNT + 0.001.                   //Keeps track of Stage 2 RCS amount
  79. LOCK S2RCSLevel TO (S2RCSAmount / S2RCSCapacity) * 100.     //Keeps track of percent Stage 2 RCS remaining
  80.  
  81. LOCK SHIP:CONTROL:PITCH TO pitchControl.    //Variable to set pitch
  82. LOCK SHIP:CONTROL:ROLL TO rollControl.      //Variable to set roll
  83. LOCK SHIP:CONTROL:YAW TO yawControl.            //Variable to set yaw
  84. LOCK THROTTLE TO throttControl.                     //Variable to set throttle
  85.  
  86. //locks #close
  87. //=========================================================
  88. //Main Loop #open
  89.  
  90. UNTIL  opMode = 0 {
  91.    
  92.     IF opMode = -1 {        //Pre-countdown
  93.         PRINT "Vehicle type: " + vesselType.
  94.         PRINT "Desired orbit altitude: " + targetAlt.
  95.         PRINT "Desired orbit inclination: " + targetInc.
  96.         PRINT "Launch Azimuth: " + ROUND(launchAzimuth).
  97.        
  98.         IF logReady = TRUE {
  99.             LOG vesselType TO atlasLog_params.txt.
  100.             LOG targetAlt TO atlasLog_params.txt.
  101.             LOG targetInc TO atlasLog_params.txt.
  102.             LOG launchAzimuth TO atlasLog_params.txt.
  103.         }. //IF logReady
  104.        
  105.         PRINT "**Press AG10 to initiate countdown**".
  106.         AG10 OFF.       //Ensures AG10 is off to prevent immediate countdown initiation
  107.         SET opMode TO 100.
  108.        
  109.     } ELSE IF opMode = 100 AND AG10 = TRUE {        //Countdown
  110.         IF countdown = 5 {
  111.             SET throttControl TO 1.
  112.            
  113.         } ELSE IF countdown = 3 {
  114.             STAGE.      //Main Engines
  115.             SET countdownMessage TO "Main Engine Ignition".
  116.            
  117.         } ELSE IF countdown = 1 {
  118.             SET countdownMessage TO "Releasing Launch Clamps".
  119.            
  120.         }. //IF
  121.        
  122.         PRINT "T-" + countdown + " " + countdownMessage.
  123.         SET countdownMessage TO " ".
  124.         WAIT 1.
  125.        
  126.         IF countdown = 0 {
  127.             STAGE.      //Ignite SRBs and release clamps
  128.             CLEARSCREEN.
  129.             PRINT "==============ATLAS LAUNCH CONTROLLER=============" AT(0,0).
  130.             PRINT "ASL: " AT(0,1). PRINT "VEL: " AT(16,1). PRINT "CLK: " AT(33,1)//Readouts at 5, 21, 38
  131.             PRINT "STG: " AT(0,2). PRINT "SFL: " AT(16,2). PRINT "RCS: " AT(33,2)//Readouts at 5, 21, 38
  132.             PRINT "---------------------EVENT LOG--------------------" AT(0,3).
  133.             SET controlReady TO TRUE.
  134.             SET readoutReady TO TRUE.
  135.             SET opMode TO 200.
  136.            
  137.         }. //IF
  138.        
  139.     } ELSE IF opMode = 200 {        //Initial climb and roll to launch azimuth
  140.         SET pitchControl TO 90.     //Straight up
  141.         SET rollControl TO launchAzimuth.       //Roll to heads-up as launch azimuth; modified controlMode requried
  142.         SET yawControl TO 0.
  143.     }. //IF
  144.    
  145.     IF controlReady = TRUE {
  146.        
  147.         IF controlMode = "Standard" {       //For regular flying
  148.             SET currentPitch TO pitch_for(SHIP).        //Positive is above horizon
  149.             SET currentRoll TO roll_for(SHIP).          //Regular roll value; needs trickery for heads-down piloting
  150.             SET currentYaw TO  compass_for(SHIP).       //Compass heading
  151.            
  152.         } ELSE IF controlMode = "Modified" {        //For initial vertical flight
  153.             //TODO: Figure out how to avoid gimbal lock
  154.         }.
  155.        
  156.     }. //IF controlReady
  157.    
  158.     IF readoutReady = TRUE {
  159.         PRINT ROUND(SHIP:ALTITUDE) AT(5,1).
  160.         IF SHIP:ALTITUDE > 50000 {
  161.             PRINT ROUND(VELOCITY:ORBIT:MAG, 1) AT(21,1).
  162.         } ELSE {
  163.             PRINT ROUND(VELOCITY:SURFACE:MAG, 1) AT(21,1).
  164.         }.
  165.         PRINT ROUND(missionClock, 1) AT(38,1).
  166.         PRINT activeStage AT(5,2).
  167.         PRINT ROUND(stageOxidizerLevel, 1) AT(21,2).
  168.         PRINT ROUND(S2RCSLevel, 1) AT(38,2).
  169.        
  170.     }.//IF readoutReady
  171.    
  172.     IF logReady = TRUE {
  173.         LOG missionClock TO atlasLog_Clock.txt.
  174.         LOG TWR TO atlasLog_TWR.txt.
  175.         LOG SHIP:ALTITUDE TO atlasLog_alt.txt.
  176.         LOG VELOCITY:ORBIT:MAG TO atlasLog_orbitVel.txt.
  177.         LOG VELOCITY:SURFACE:MAG TO atlasLog_surfVel.txt.
  178.         LOG stageOxidizerLevel TO atlasLog_stageLOXlevel.txt.
  179.         LOG S2RCSLevel TO atlasLog_S2RCSlevel.txt.
  180.         LOG pitchControl TO atlasLog_pitchControl.txt.
  181.         LOG rollControl TO atlasLog_rollControl.txt.
  182.         LOG yawControl TO atlasLog_yawControl.txt.
  183.         LOG pitchDesired TO atlasLog_pitchDesired.txt.
  184.         LOG rollDesired TO atlasLog_rollDesired.txt.
  185.         LOG yawDesired TO atlasLog_yawDesired.txt.
  186.         LOG throttDesired TO atlasLog_throttDesired.txt.
  187.         LOG pitchCurrent TO atlasLog_pitchCurrent.txt.
  188.         LOG rollCurrent TO atlasLog_rollCurrent.txt.
  189.         LOG yawCurrent TO atlasLog_yawCurrent.txt.
  190.    
  191.     }. //IF logReady
  192.    
  193. }. //UNTIL
  194.  
  195. SET SHIP:CONTROL:PILOTMAINTHROTTLE TO 0.
  196. UNLOCK THROTTLE.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement