Guest User

KOS Launch Script

a guest
Jun 19th, 2016
924
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ###############
  2. // ---DISCLAIMER--
  3. // ###############
  4. // THIS IS BY NO MEANS A PRECISE ORBIT PLACEMENT SCRIPT
  5. // IT WILL USUALLY PUT YOUR CRAFT IN A 200 BY 140 ORBIT WITH NO POSSIBILITY TO SPECIFY HIGHER / LOWER ORBIT
  6. // YOU _COULD_ ACHIEVE HIGHER ORBIT FIDDLING WITH THE waitpitch AND targetapoeta PARAMETERS THOUGH
  7.  
  8. // ###############
  9. // -- IMPORTANT --
  10. // ###############
  11. // STAGING IS LEFT IN CONTROL OF THE PLAYER
  12. // STAGE YOUR SECOND STAGE WHEN [CURRENT APOAPSIS] ETA IS EQUAL (OR A BIT MORE) TO [TARGET APOAPSIS] * 2
  13.  
  14. // -- EXAMPLE --
  15. // TARGET APOPAPSIS ETA: 40
  16. // CURRENT APOAPSIS ETA: 80 (OR A BIT MORE THAN THAT JUST TO BE SURE)  
  17.  
  18. // FAILING TO DO SO AND STAGING TO EARLY WILL PROBABLY RESULT IN A HIGHLY ECCENTRIC ORBIT
  19. // WHICH THE SCRIPT CAN'T COMPENSATE FOR
  20.  
  21. // ###############
  22. // -- WARNING ---
  23. // ###############
  24. // * SHIP MUST HAVE A PRESMAT BAROMERER
  25. // * SECOND STAGE TWR SHOULD BE AROUND 1 FOR OPTIMAL RESULTS
  26.  
  27. //@lazyglobal off.
  28. clearscreen.
  29.  
  30. // in degrees, pitch inclination where craft will sit while waiting to match specified APOAPSIS ETA
  31. // by increasing this value the script will apply a steeper climb
  32. // allowing low TWR craft to reach orbit
  33. // should be modified together with the targetapoeta parameter
  34. parameter waitpitch is 40.
  35. // in degrees, defaulted for Cape Canaveral
  36. parameter targetinclination is 28.5.
  37. // in seconds, APOAPSIS ETA the script will try to mantain (scaling down as orbital speed climbs)
  38. // the default value somehow works for most crafts with a second stage TWR around 1
  39. // increase this if your second stage has troubles to reach orbit precisely at apoapsis
  40. parameter targetapoeta is 90.
  41. // in degrees, Maximum correction the script will apply to surface prograde vector
  42. // this is dampened when craft is in atmosphere to avoid overheating and structural failure
  43. parameter maxcorrection is 30.
  44.  
  45.  
  46. // inclination parameters accounting for launch site location
  47. // stolen from another script I don't remember
  48. if abs(targetinclination) < floor(abs(latitude)) or abs(targetinclination) > (180 - ceiling(abs(latitude))) {
  49.     print "desired inclination impossible. ".
  50.     print "magnitude must be larger than or equal to the ".
  51.     print "current latitude of "+round(latitude,1)+" deg".
  52.     print "and less than or equal to "+round(90+latitude)+" deg".
  53.     print "use ctrl+c to cancel program and try again".
  54.     print " ".
  55.     wait until false.
  56. }.
  57.  
  58. set launchloc to ship:geoposition.
  59. set inertialazimuth to arcsin(max(min(cos(targetinclination) / cos(launchloc:lat),1),-1)).
  60. set targetorbitspeed to sqrt(ship:body:mu / (200000+ship:body:radius)).
  61. set rotvelx to targetorbitspeed*sin(inertialazimuth) - (6.2832*ship:body:radius/ship:body:rotationperiod).
  62. set rotvely to targetorbitspeed*cos(inertialazimuth).
  63. set azimuth to arctan(rotvelx / rotvely).
  64. if targetinclination < 0 {set azimuth to 180-azimuth.}.
  65.  
  66.  
  67. // program start
  68. lock throttle to 1.
  69.  
  70. // clear tower
  71. rcs off.
  72. sas on.
  73. wait until alt:radar > 100.
  74. sas off.
  75.  
  76.  
  77. // turn phase, craft will sit at [waitpitch] waiting to reach targetapoeta
  78. set fullysteeredangle to 90 - waitpitch.
  79. set atmp_ground to ship:sensors:pres.
  80. set atmp_end to 0.
  81.  
  82. lock altitude to alt:radar.
  83. lock atmp to ship:sensors:pres.
  84. lock atmodensity to atmp / atmp_ground.
  85. lock apoeta to max(0,eta:apoapsis).
  86.  
  87. lock first_phase_pitch to fullysteeredangle - (fullysteeredangle * atmodensity).
  88. lock steering to heading(azimuth, 90 - first_phase_pitch).
  89. // wait for specified apoapsis ETA
  90. until apoeta >= targetapoeta {
  91.     // store some variables for later use
  92.     set endturn_altitude to altitude.
  93.     set endturn_orbitspeed to ship:velocity:orbit:mag.
  94.     set second_phase_pitch to first_phase_pitch.
  95.     print "Target Apoapsis ETA (seconds): "+targetapoeta at (0,0).
  96.     print "Current Apoapsis ETA (seconds): "+apoeta at (0,1).
  97. }
  98.  
  99. clearscreen.
  100.  
  101.  
  102. // APOAPSIS ETA PHASE
  103. unlock first_phase_pitch.
  104. unlock steering.
  105. unlock atmodensity.
  106. unlock atmp.
  107.  
  108. set atmoendaltitude to 110000.
  109. set tolerance to targetapoeta * 0.5.
  110.  
  111. // calculate ship pitch based on surface prograde vector
  112. lock shipangle to vang(ship:up:vector, ship:srfprograde:vector).
  113.  
  114. // this damps the correction based on the distance to the end of the atmosphere
  115. // to prevent oversteering in atmosphere
  116. lock correctiondamp to (altitude - endturn_altitude) / (atmoendaltitude - endturn_altitude).
  117. lock mx to shipangle + (maxcorrection * correctiondamp).
  118. lock mi to shipangle - (maxcorrection * correctiondamp).
  119.  
  120.  
  121. // this decreases expected APO eta as orbital speed increases
  122. lock orbitSpeedFactor to ((targetorbitspeed - ship:velocity:orbit:mag) / (targetorbitspeed - endturn_orbitspeed)).
  123. lock tApoEta to targetapoeta * orbitSpeedFactor.
  124.  
  125. set ae to 0.
  126. // calculate pitch correction
  127. // apply a limit to correction down
  128. // because overshooting is better than undershooting
  129. lock correction to max(-maxcorrection*0.3,((tApoEta - ae) / tolerance) * maxcorrection).
  130. // apply correction dampening
  131. lock second_phase_pitch to max(mi,min(mx, shipangle - correction )).
  132. // apply steering
  133. lock steering to heading(azimuth, 90 - second_phase_pitch).
  134.  
  135. until alt:periapsis >= 140000 {
  136.     // prevent program to fail if ship is falling down
  137.     // by overriding apoeta
  138.     if ship:verticalspeed > 0 {
  139.         set ae to apoeta.
  140.     } else {
  141.         set ae to 0.
  142.     }
  143.     print "Target Apoapsis ETA (seconds): "+tApoEta at (0,0).
  144.     print "Current Apoapsis ETA (seconds): "+apoeta at (0,1).
  145. }
  146.  
  147. unlock throttle.
  148. set ship:control:pilotmainthrottle to 0.
  149. clearscreen.
  150. rcs on.
  151. sas on.
Advertisement
Add Comment
Please, Sign In to add comment