Advertisement
Guest User

KSP Launch Script

a guest
May 27th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.55 KB | None | 0 0
  1. //================ Launch Script ===================
  2. //
  3. // This script launches a rocket from the Kerbal Space Center to a circular orbit of the desired height and inclination:
  4. //
  5. // Suggested first stage atmospheric TWR and second stage vacuum TWR of around 1.25.
  6. //
  7. // Parameters:
  8. // targetaltitude - altitude of target orbit (km)
  9. // targetincl - inclination of target orbit (deg)
  10. //
  11. // To run: e.g. runpath("0:/launch.ks,100,0"). to launch to altitude of 100 km in 0 deg orbit.
  12. //
  13. //==================================================
  14.  
  15.  
  16. // Define Parameters
  17. parameter targetaltitude.
  18. parameter targetincl.
  19.  
  20. set targetaltitude to targetaltitude*1000.
  21.  
  22. //Define Initial Conditions
  23. SAS off.
  24. RCS off.
  25. lights off.
  26. set throttle to 0.
  27. gear off.
  28.  
  29. // Define Constants
  30. set launchlatitude to LATITUDE.
  31. set launchlongitude to LONGITUDE.
  32. set MUkerbin to BODY:MU/(1000*1000*1000). //km^3/s^2
  33. set Rkerbin to BODY:RADIUS/1000. //km
  34. set Vlaunchsite to VELOCITY:ORBIT:MAG/1000. //kilometers per second
  35.  
  36. // Set initial values for loops
  37. set eventlogline to 12.
  38. set enginecutoff to 1.
  39. set apoapsischeck to 0.
  40. set circburnflag to 0.
  41. set MaxQflag to 0.
  42. set gamma to 90.
  43. set steeringlock to 0.
  44.  
  45. // Reset inclination
  46. if abs(launchlatitude)>abs(targetincl) {set targetincl to launchlatitude.}
  47.  
  48. // Calculate Launch Azimuth
  49. if targetincl>=0 {set betaI to arcsin(cos(targetincl)/cos(launchlatitude)).}
  50. else if targetincl<0 {set betaI to 180-arcsin(cos(targetincl)/cos(launchlatitude)).}
  51. set Vtargetorbit to sqrt(MUkerbin/(Rkerbin+targetaltitude/1000)). //kilometers per second
  52. if targetincl>=0 {lock beta to arctan((Vtargetorbit*sin(betaI)-Vlaunchsite*cos(launchlatitude))/(Vtargetorbit*abs(cos(betaI)))).}
  53. else if targetincl<0 {lock beta to 180-arctan((Vtargetorbit*sin(betaI)-Vlaunchsite*cos(launchlatitude))/(Vtargetorbit*abs(cos(betaI)))).}
  54. lock flightpathang to 90-VANG(UP:VECTOR,SHIP:PROGRADE:VECTOR).
  55.  
  56. //Launch Countdown
  57. CLEARSCREEN.
  58. FROM {local countdown is 5.} UNTIL countdown=0 STEP {SET countdown to countdown-1.} DO {
  59. print "Launch Countdown Initiated" at (0,1).
  60. print "T-"+countdown.
  61. wait 1.
  62. CLEARSCREEN.
  63. }
  64.  
  65. //Begin Launch Process
  66. print "Launch Process Initiated" at (0,1).
  67. set runmode to 1.
  68. set throt to 1.
  69. lock throttle to throt.
  70. stage.
  71. wait 2.
  72. until verticalspeed>1 { //stage through any prelaunch stages
  73. stage.
  74. wait 1.
  75. }
  76. print "Launch Status: Liftoff! " at (0,1).
  77.  
  78. // Initiate Runmode Loop
  79. UNTIL runmode=0 {
  80.  
  81.  
  82. // -------------------- Repeated Code --------------------
  83.  
  84. wait 0.1.
  85.  
  86. // Telemetry Display
  87. print "Launching to a " + targetaltitude/1000 + " km circular orbit" at (0,3).
  88. print "with inclination of " + round(targetincl,3) + " degrees." at (0,4).
  89. print "Commanded Azimuth (degrees): " + round(beta,4) at (0,6).
  90. print "Commanded Flight Path Angle (degrees): " + round(gamma,4) + " " at (0,7).
  91. print "Orbital Flight Path Angle (degrees): " + round(flightpathang,4) at (0,8).
  92.  
  93.  
  94. // Lock steering
  95. if runmode=1 {
  96. SAS on.
  97. print " Vertical Lock Active " at (10,10).
  98. }
  99. else if runmode = 2 {
  100. SAS off.
  101. lock steering to heading(beta,gamma).
  102. print " Vertical Lock Active " at (10,10).
  103. }
  104. else if SHIP:altitude > 15000 and abs(ORBIT:inclination-abs(targetincl))<0.01 and steeringlock = 0 {
  105. lock steering to SHIP:PROGRADE*R(flightpathang-gamma,0,0):VECTOR.
  106. print "Inclination Lock Active" at (10,10).
  107. set steeringlock to 1.
  108. }
  109. else if steeringlock = 0 {
  110. lock steering to heading(beta,gamma).
  111. print " Azimuth Lock Active " at (10,10).
  112. }
  113. else if steeringlock = 1 {
  114. lock steering to SHIP:PROGRADE*R(flightpathang-gamma,0,0):VECTOR.
  115. print "Inclination Lock Active" at (10,10).
  116. }
  117.  
  118. // Automatic Staging
  119. until maxthrust>0 {
  120. if stage:number > 0 {
  121. if maxthrust = 0 {
  122. wait 0.5.
  123. stage.
  124. }
  125. SET numOut to 0.
  126. LIST ENGINES IN engines.
  127. FOR eng IN engines {
  128. IF eng:FLAMEOUT {
  129. SET numOut TO numOut + 1.
  130. }
  131. }
  132. if numOut > 0 {
  133. wait 0.5.
  134. stage.
  135. }
  136. }
  137. if SHIP:ALTITUDE > 150 {
  138. if runmode = 3 {
  139. wait 1.5.
  140. }
  141. }
  142. }
  143.  
  144. // Run Apoapsis Check
  145. if ALT:APOAPSIS > targetaltitude*0.90 and apoapsischeck=0 {
  146. set runmode to 5.
  147. print "Launch Status: Apoapsis Approach " at (0,1).
  148. set apoapsischeck to 1.
  149. }
  150.  
  151.  
  152. // -------------------- Runmodes -------------------------
  153.  
  154. // Leaving the Launchpad
  155. if runmode=1 {
  156. set gamma to 90.
  157. set throt to 1.
  158. wait 6.
  159. set runmode to 2.
  160. print "Launch Status: Vertical Ascent" at (0,1).
  161. }
  162.  
  163. // Vertical Ascent to 500m
  164. else if runmode=2 {
  165. set gamma to 90.
  166. set throt to 1.
  167. if SHIP:ALTITUDE >500{
  168. set runmode to 3.
  169. print "Launch Status: Gravity Turn " at (0,1).
  170. }
  171. }
  172.  
  173. // Gravity Turn from 500m to 65km
  174. else if runmode=3 {
  175. set gamma to 8.75513e-9*(SHIP:ALTITUDE)^2-0.0019223*(SHIP:ALTITUDE)+90.95896.
  176. set TWR to 1.6.
  177. set throt to (MASS*9.81*TWR)/max(0.01,MAXTHRUST).
  178. if SHIP:ALTITUDE>65000{
  179. set runmode to 4.
  180. print "Launch Status: Thrust to Apoapsis" at (0,1).
  181. }
  182. }
  183.  
  184. // Prograde Thrust to Raise Apoapsis
  185. else if runmode=4 {
  186. set gamma to 3.
  187. set TWR to 1.5.
  188. set throt to (MASS*9.81*TWR)/max(0.01,MAXTHRUST).
  189. }
  190.  
  191. // Apoapsis Approach
  192. else if runmode=5 {
  193. set gamma to 1.
  194. set TWR to 0.5.
  195. set throt to min(1,(MASS*9.81*TWR)/max(0.01,MAXTHRUST)).
  196. if ALT:APOAPSIS > targetaltitude*0.998 {
  197. set runmode to 10.
  198. print "Launch Status: Circularization " at (0,1).
  199. }
  200. }
  201.  
  202. // Circularization Burn
  203. else if runmode=6 {
  204. set gamma to 0.
  205. // First circularization burn increases periapsis above sea level
  206. if circburnflag=0 {
  207. if ALT:PERIAPSIS>0 { //cut throttle and wait when periapsis increases above sea level
  208. set circburnflag to 1.
  209. set runmode to 10.
  210. }
  211. else {
  212. set TWR to 0.75.
  213. set throt to min(1,(MASS*9.81*TWR)/max(0.01,MAXTHRUST)).
  214. }
  215. }
  216. // Second circularization burn fully circularizes orbit
  217. else if circburnflag=1 {
  218. if ALT:PERIAPSIS > targetaltitude*0.995 or ALT:APOAPSIS > 1.01*targetaltitude {
  219. set circburnflag to 3.
  220. set throt to 0.
  221. set pilotmainthrottle to 0.
  222. set runmode to 0.
  223. }
  224. else if VERTICALSPEED<0 {set TWR to 1.}
  225. else {
  226. set TWR to 0.75.
  227. set throt to min(1,(MASS*9.81*TWR)/max(0.01,MAXTHRUST)).
  228. }
  229. }
  230. }
  231.  
  232. // Wait call
  233. else if runmode=10 {
  234. set throt to 0.
  235. if circburnflag = 0 and ETA:APOAPSIS<10 {
  236. set runmode to 6.
  237. }
  238. if circburnflag = 1 and (ETA:APOAPSIS<5 or VERTICALSPEED<0) {
  239. set runmode to 6.
  240. }
  241. }
  242.  
  243. }
  244.  
  245. CLEARSCREEN.
  246. print "Launch Complete!" at (0,1).
  247. wait 5.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement