Advertisement
wirawafiy1

Mun landing

Oct 26th, 2023 (edited)
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.15 KB | None | 0 0
  1. // aubranium mun lander mk1
  2.  
  3. clearscreen.
  4.  
  5. set gravity to (constant():G * body:mass) / body:radius^2.
  6.  
  7. set targetAltitude to 80000.
  8. set munarAltitude to 25000.
  9. set targetLng to -30.
  10. set maxTWR to 3.
  11. set targetTWR to 1.25.
  12. set circularThreshold to 0.05.
  13. set landingOffset to 3.416.
  14.  
  15. set iTWR to 0.
  16.  
  17. set steering to up.
  18. set throttle to 0.
  19. rcs on.
  20. sas off.
  21.  
  22. until ship:maxthrust > 0 {
  23. wait 0.5.
  24. stage.
  25. }
  26.  
  27. if ship:body = mun {
  28. set runmode to 8.
  29. }
  30. else if ship:orbit:hasnextpatch {
  31. set runmode to 6.
  32. }
  33. else if ship:periapsis > 70000 and ship:apoapsis > 70000 {
  34. set runmode to 5.
  35. }
  36. else {
  37. set runmode to 1.
  38. }
  39. print "Go baby go!".
  40.  
  41. until runmode = 0 {
  42.  
  43. if runmode = 1 { // liftoff
  44. when alt:radar > 10 then { gear off. }
  45. // available thrust / (m*g)
  46. lock throttle to ship:availablethrust / (ship:mass * gravity * maxTWR).
  47. wait until ship:airspeed > 140.
  48. print "1 -> 2 Constant TWR ascent".
  49. set runmode to 2.
  50. }
  51.  
  52. if runmode = 2 { // constant TWR ascent
  53. until ship:apoapsis > targetAltitude {
  54. set targetThrottle to throttle.
  55. // maintain constant TWR
  56. set TWR to max(0.001, ship:maxthrust / (ship:mass * gravity)).
  57. set throttle to min(1, max(0, targetTWR / TWR)).
  58. set elevation to min(1, max(0, 1 - ship:altitude / (0.8*targetAltitude))).
  59. set steering to heading(90, max(5, 90*elevation)).
  60. }
  61. print "2 -> 3 Coast to apoapsis".
  62. lock steering to prograde.
  63. set throttle to 0.
  64. set runmode to 3.
  65. }
  66.  
  67. if runmode = 3 { // coast to apoapsis
  68. lock apoapsisCondition to ship:verticalspeed < 0.
  69. wait until apoapsisCondition.
  70. print "3 -> 4 Raise periapsis".
  71. set runmode to 4.
  72. }
  73.  
  74. if runmode = 4 { // raise periapsis
  75. lock steering to prograde.
  76. wait 1.0.
  77. set throttle to 0.1.
  78. wait until abs(targetAltitude - ship:periapsis)/targetAltitude < circularThreshold.
  79. set throttle to 0.
  80. print "4 -> 5 Compute Mun transfer".
  81. set runmode to 5.
  82. }
  83.  
  84. if runmode = 5 { // compute mun transfer maneuver
  85. set td to 0.
  86. set validTransfer to false.
  87. until validTransfer = true {
  88. // check for mun encounter
  89. set newnode to node(time:seconds+td, 0, 0, 860).
  90. add newnode.
  91. set isMunEncounter to hasnode and nextnode:orbit:hasnextpatch and nextnode:orbit:nextpatch:body = mun.
  92. set validMunPeriapsis to hasnode and nextnode:orbit:hasnextpatch and nextnode:orbit:nextpatch:periapsis > 10000.
  93. set validTransfer to isMunEncounter and validMunPeriapsis.
  94. if validTransfer = false {
  95. wait 0.1.
  96. remove newnode.
  97. set td to td + 10.
  98. }
  99. }
  100. print "5 -> 6 Execute Mun transfer".
  101. set runmode to 6.
  102. }
  103.  
  104. if runmode = 6 { // execute mun transfer maneuver
  105. // warp to maneuver
  106. print "wait for maneuver".
  107. lock steering to nextnode:deltav:direction.
  108. wait nextnode:eta.
  109. // execute maneuver
  110. print "execute maneuver".
  111. set startVelocity to ship:velocity:orbit:mag.
  112. set endVelocity to startVelocity + 860.
  113. lock velocityCondition to ship:velocity:orbit:mag > endVelocity.
  114. set throttle to 0.5.
  115. wait until velocityCondition.
  116. set throttle to 0.
  117. print "6 -> 7 Wait for Mun encounter".
  118. set runmode to 7.
  119. }
  120.  
  121. if runmode = 7 { // wait for mun encounter
  122. kuniverse:timewarp:cancelwarp().
  123. wait 1.
  124. kuniverse:timewarp:warpto(time:seconds + eta:transition).
  125. wait eta:transition.
  126. remove nextnode.
  127. print "7 -> 8 Mun circularize".
  128. set runmode to 8.
  129. }
  130.  
  131. if runmode = 8 { // mun circularize
  132. if ship:orbit:periapsis < 0 {
  133. print "rescue periapsis".
  134. lock steering to up.
  135. lock minimumPeriapsisCondition to ship:orbit:periapsis > munarAltitude.
  136. set throttle to 0.5.
  137. wait until minimumPeriapsisCondition.
  138. set throttle to 0.
  139. wait 1.
  140. }
  141.  
  142. // wait for periapsis
  143. print "wait for periapsis".
  144. lock steering to retrograde.
  145. wait until ship:verticalspeed < 0.
  146. kuniverse:timewarp:cancelwarp().
  147. kuniverse:timewarp:warpto(time:seconds + eta:periapsis).
  148. wait eta:periapsis.
  149.  
  150. // drop periapsis to specified altitude
  151. print "lower periapsis".
  152. lock targetPeriapsisCondition to ship:orbit:periapsis < munarAltitude.
  153. set throttle to 0.5.
  154. wait until targetPeriapsisCondition.
  155. set throttle to 0.
  156. wait 1.
  157.  
  158. // wait for periapsis
  159. print "wait for periapsis".
  160. kuniverse:timewarp:cancelwarp().
  161. kuniverse:timewarp:warpto(time:seconds + eta:periapsis).
  162. wait eta:periapsis.
  163.  
  164. // drop apoapsis to circularize
  165. print "circularize".
  166. lock circularCondition to ship:orbit:apoapsis < munarAltitude and ship:orbit:periapsis < munarAltitude.
  167. set throttle to 0.5.
  168. wait until circularCondition.
  169. set throttle to 0.
  170. print "8 -> 9 Deorbit".
  171. set runmode to 9.
  172. }
  173.  
  174. if runmode = 9 { // deorbit
  175. // wait for equatorial positioning
  176. lock landingCondition to abs(ship:geoposition:lng-targetLng) < 0.1.
  177. wait until landingCondition.
  178. lock steering to srfretrograde.
  179. lock velocityCondition to ship:groundspeed < 50.
  180. set throttle to 0.5.
  181. wait until velocityCondition.
  182. set throttle to 0.
  183. print "9 -> 10 Landing".
  184. set runmode to 10.
  185. }
  186.  
  187. if runmode = 10 { // hoverslam
  188. lock g to body:mu / (alt:radar + body:radius)^2.
  189. lock maxDecel to (ship:availablethrust / ship:mass) - g. // Maximum deceleration possible (m/s^2)
  190. lock stopDist to ship:verticalspeed^2 / (2 * maxDecel). // The distance the burn will require
  191. lock idealThrottle to stopDist / alt:radar. // Throttle required for perfect hoverslam
  192. lock impactTime to alt:radar / abs(ship:verticalspeed). // Time until impact, used for landing gear
  193. lock steering to srfretrograde.
  194. lock throttle to idealThrottle.
  195. lock altitudeCondition to alt:radar < landingOffset.
  196. lock verticalSpeedCondition to verticalspeed > -0.1.
  197. when impactTime < 5 then { gear on. }
  198. wait until altitudeCondition or verticalSpeedCondition.
  199. set throttle to 0.
  200. print "10 -> 0 Landed".
  201. set runmode to 0.
  202. }
  203.  
  204. }
  205.  
  206. print "Mission complete.".
  207.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement