Advertisement
Guest User

Untitled

a guest
May 26th, 2015
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. function Clamp {
  2. PARAMETER value.
  3. PARAMETER lower.
  4. PARAMETER upper.
  5.  
  6. return Max(lower, Min(upper, value)).
  7. }
  8.  
  9. function GetOrbitalSpeed {
  10. PARAMETER altitude.
  11. PARAMETER apo.
  12. PARAMETER peri.
  13.  
  14. local a to Body:Radius + (apo + peri)/2.
  15. return Sqrt(Body:Mu(2/(Body:Radius + altitude) - 1/a)).
  16.  
  17. }
  18.  
  19. function GetLaunchAzimuthInertial {
  20. PARAMETER targetInclination.
  21. PARAMETER launchLatitude.
  22.  
  23. return Arcsin(Clamp(Cos(targetInclination)/Cos(launchLatitude), -1, 1)).
  24. }
  25.  
  26. function GetLaunchAzimuthRotating {
  27. PARAMETER targetInclination.
  28. PARAMETER launchLatitude.
  29. PARAMETER targetAltitude.
  30.  
  31. local azimuth_inertial to GetLaunchAzimuthInertial(targetInclination, launchLatitude).
  32.  
  33. local v_equatorial to 2*Constant():Pi*Body:Radius / Body:RotationPeriod.
  34.  
  35. local v_orbit to Sqrt(Body:Mu / (Body:Radius + targetAltitude)).
  36. local v_xrot to v_orbit * sin(azimuth_inertial) - v_equatorial * cos(launchLatitude).
  37. local v_yrot to v_orbit * cos(azimuth_inertial).
  38.  
  39. return Arctan(v_xrot/v_yrot).
  40. }
  41.  
  42.  
  43. // Declare parameters and consants.
  44. PARAMETER targetAltitude.
  45. PARAMETER targetInclination.
  46.  
  47. set launchLatitude to Ship:Latitude.
  48. set launchAzimuth to GetLaunchAzimuthRotating(targetInclination, Ship:Latitude, targetAltitude).
  49.  
  50. // Link control inputs to their respective buffers.
  51. set throttleBuf to 0.
  52. lock THROTTLE to Clamp(throttleBuf, 0, 1).
  53.  
  54. set directionBuf to 90.
  55. set pitchBuf to 90.
  56. lock STEERING to heading(directionBuf, pitchBuf):Vector.
  57.  
  58.  
  59. set runmode to 1.
  60. until (runmode = 0) {
  61.  
  62. // Liftoff
  63. if (runmode = 1) {
  64. SAS off.
  65. RCS off.
  66. LIGHTS on.
  67.  
  68. set throttleBuf to 1.
  69. set directionBuf to 90.
  70. set pitchBuf to 90.
  71.  
  72. stage.
  73.  
  74. set runmode to 2.
  75. }
  76.  
  77. // Vertical Ascent
  78. else if (runmode = 2) {
  79. if (Ship:Altitude > 2000) {
  80. set runmode to 3.
  81. }
  82. }
  83.  
  84. // Gravity Turn
  85. else if (runmode = 3) {
  86. set pitchBuf to 90 * (1 - ALT:RADAR / targetAltitude).
  87. set directionBuf to launchAzimuth.
  88.  
  89. if (Ship:Obt:Apoapsis > 0.95*targetAltitude) {
  90. set runmode to 4.
  91. }
  92. }
  93.  
  94. // Deceleration
  95. else if (runmode = 4) {
  96. set throttleBuf to 1 - (Ship:Apoapsis - 0.95*targetAltitude)/(0.05*targetAltitude).
  97.  
  98. if (Ship:Altitude > 70000) {
  99. set runmode to 5.
  100. }
  101. }
  102.  
  103. // Coasting
  104. else if (runmode = 5) {
  105. set throttleBuf to 0.
  106. }
  107.  
  108.  
  109. // Illegal Runmode
  110. else {
  111. set runmode to 0.
  112. }
  113.  
  114. clearscreen.
  115.  
  116. print "r: " at (44, 1).
  117. print runmode at (48, 1).
  118.  
  119. print "Direction: " + directionBuf at (5,5).
  120. print "Pitch: " + pitchBuf at (5,6).
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement