Advertisement
blorgon

Mun Mission main program

Jul 17th, 2017
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // MAIN_PROGRAM
  2.  
  3. // a script to launch to and land at a predesignated target on the Mun, Apollo style
  4. // by /u/supreme_blorgon, AKA kickpuncher on Discord
  5.  
  6. // DOES NOT INCLUDE ASCENT, RENDEZVOUS, TRANS-KERBIN INJECTION, OR REENTRY
  7. // also requires a lot of specific action groups, name tags, and craft construction
  8. // if you plan on using the script as-is, you will need to build a similar craft, or get the .craft file from me
  9.  
  10. //to parking orbit
  11. clearscreen.
  12. lock twr to (0.84/(1+1.01^(apoapsis - 81000))) + (0.35/(1+1.01^(apoapsis - 100000))+0.25).          // stair stepping sigmoid function of my own creation
  13. on twr {
  14.     set throt to (twr * (mass * (9.81*(600000/(600000 + altitude))^2)) / availablethrust).          // sure, it's not really practical, but it's awesome
  15.     if twr = 0.25 return false.
  16.     else return true.
  17. }
  18. when stage:liquidfuel < 0.1 then {
  19.     stage. wait 5. stage.
  20.     set throt to (0.6 * (mass * (9.81*(600000/(600000 + altitude))^2)) / availablethrust).
  21.     toggle AG1. return false.
  22. }
  23. stage.
  24. set gain to 1.
  25. set throt to 1.
  26. lock throttle to throt*gain.
  27. set pitch to 90.
  28. lock steering to heading(-90,pitch).
  29.  
  30. set throt to (twr * (mass * 9.81)) / availablethrust.          // sets the throttle to meet an exact TWR, also useful for hovering, and slow, constant velocity descents
  31. until apoapsis >= 149999.925 {
  32.     set pitch to min(180,(max(90,(sqrt(0.170283 * altitude) + 85)))).          // basic square root function that puts me totally horizontal at about 53 km
  33.     set gain to -(1/149999.95 * round(apoapsis,4))^128 + 1.          // kills the throttle smoothly as apo approaches 150 km, for precision
  34.     wait 0.
  35. }
  36. set throt to 0.
  37. set gain to 1.
  38. wait 5.
  39. warpto(time:seconds + (eta:apoapsis - 60)).
  40. until ship:orbit:eccentricity < 0.00001 {
  41.     set throt to (availablethrust/(6.2784*mass))*constant:e^(-0.0025*constant:​e^(((availablethrust/(6.2784*mass))^1.4696)*​min(30,eta:apoapsis))).          // black magic, sorry
  42.     wait 0.
  43. }
  44. set throt to 0.
  45. set gain to 1.
  46. set pitch to 180.
  47. wait 5.
  48. warpto(time:seconds + ship:orbit:period).
  49.  
  50. //trans-Munar injection
  51.  
  52. until false {
  53.     set mun_0 to (1445625 - (mun:position:mag / 10000)^2)/180000.
  54.     set mun_1 to (1445625 - (positionat(mun, time:seconds + 1):mag / 10000)^2)/180000.
  55.     set p_0 to arccos(min(max(mun_0, -1), 1)).          // current phase angle
  56.     set p_1 to arccos(min(max(mun_1, -1), 1)).          // phase angle one second into the future
  57.  
  58.     // the range of arccos(x) is [0°,180°]. In order to convert to [0°,360°] I have to find if my phase angle is increasing or decreasing
  59.  
  60.     if p_1 > p_0 set phase to p_0.          // I compare the two here, and if the future angle is smaller, the phase angle is decreasing from 180°
  61.     else set phase to 360 - p_0.          // if increasing, I subtract the value from 360, so that the phase angle is always decreasing from 360° to 0° and then resetting at 360° again, instead of counting up to 180°, and then counting back down to 0° and then back up.
  62.  
  63.     print "Phase angle: " + round(phase,0) at (0,0).
  64.  
  65.     if round(phase,0) = 135 kuniverse:timewarp:cancelwarp().          // kills the warp command from line 39, since waiting for the proper phase angle can take a while
  66.     if round(phase,0) = 128 break.          // proper phase angle, quite a bit higher than usual; compensating for the ~45 second trans-Munar injection burn
  67.     wait 0.
  68. }
  69. // burning full throttle here until my orbit has a "next patch"
  70. set throt to 1.
  71. until orbit:hasnextpatch = true wait 0.
  72. until false {
  73.     set gain to max(-constant:e^(-0.0000125*orbit:nextpatch:periapsis + 0.375) + 1, 0).          // now I'm controlling my throttle based on my desired "closest approach"
  74.     if gain = 0 break.
  75.     wait 0.
  76. }
  77. set throt to 0.
  78. wait 10.
  79.  
  80. //Munar module extraction
  81. set mm to processor("MMcpu").
  82. set mm:bootfilename to f.ks.          // sends the CPU on the Munar Module it's own little program to run. See AUX_PROGRAM.
  83. mm:activate.          // wake up, yo
  84.  
  85. wait 5. toggle AG3.
  86. RCS on. set ship:control:fore to 0.25. wait 1.
  87. set ship:control:fore to 0. RCS off. wait 3.
  88. set target to "Saturn K Lander".
  89. lock steering to target:position.
  90. wait 15.
  91. RCS on.
  92.  
  93. list dockingports in my_dp.
  94. set originDP to my_dp[0].
  95. originDP:controlfrom().
  96. set tar_dp to target:partstagged("MMdock").
  97. set targetDP to tar_dp[0].
  98.  
  99. until false {
  100.     set rel_pos to ship:facing:inverse * (originDP:nodeposition - targetDP:nodeposition).          // thanks again hvacengi, my own coordinate system worked okay but this is much better
  101.     set rel_vel to ship:facing:inverse * (ship:velocity:orbit - target:velocity:orbit).
  102.  
  103.     set x_dist to rel_pos:x.          // getting my distance from my target in all three axes
  104.     set y_dist to rel_pos:y.
  105.     set z_dist to rel_pos:z.
  106.  
  107.     set x_vec to rel_vel:x.           // getting my velocity relative to my target in all three axes
  108.     set y_vec to rel_vel:y.
  109.     set z_vec to rel_vel:z.
  110.  
  111.     set st to 2/(1 + constant:e^(5*x_vec + x_dist)) - 1.          // more black magic
  112.     set tp to 2/(1 + constant:e^(5*y_vec + y_dist)) - 1.          // I *really* like sigmoid functions
  113.     set fr to 2/(1 + constant:e^(5*z_vec + z_dist)) - 1.          // okay fine, so these three functions control how fast I'm allowed to go based on my distance to my target in all three axes
  114.  
  115.     set ship:control:translation to v(st, tp, fr).          // so if I'm going too fast, the RCS thrusters burn in the opposite direction to slow me down. NO RUNNING BY THE POOL
  116.  
  117.     if z_dist > -0.5 {
  118.         set ship:control:neutralize to true.          // once I'm close enough, the magnets in the docking ports take over. I'm killing RCS here so that I'm not wasting fuel fighting that
  119.         if z_dist > -0.1 break.          // breaking from the loop because the crafts are about to join, and some important stuff needs to happen before that
  120.     }
  121.     else wait 0.
  122. }
  123. unlock steering. wait 1. RCS off.
  124. set target to "".
  125. set mm:bootfilename to "".
  126. toggle AG4.
  127. wait 5.
  128.  
  129. warpto(time:seconds + eta:transition).          // this bit's annoying. I'm warping to the transition from Kerbin orbit to Munar fly-by here,
  130. wait 1.
  131. wait until ship:unpacked.                       // but as far as I can tell, you can't get your eta to your next patch's periapsis
  132. wait 1.
  133. warpto(time:seconds + (eta:periapsis - 60)).    // so I have to get to my next patch, and then warp to the periapsis from there
  134. wait 1.
  135. wait until ship:unpacked.                       // ¯\_(ツ)_/¯
  136.  
  137. //orbital insertion and circularization
  138. lock throttle to throt.
  139. lock steering to retrograde.
  140. wait until eta:periapsis <= 25.
  141. stage. wait 5.
  142. until false {
  143.     if apoapsis < 0 set throt to 1.          // orbital insertion, full throttle until my orbit closes
  144.     else set throt to -constant:e^(-0.0005*apoapsis + 20) + 1.          // still full throttle until I approach my target apoapsis, where I smoothly kill it, for precision
  145.     if throt < 0.000001 break.
  146.     wait 0.
  147. }
  148. set throt to 0.
  149. warpto(time:seconds + (eta:apoapsis - 30)).
  150. lock steering to prograde.
  151. until round(periapsis,0) >= 39999 {
  152.     set eta_ap to min(eta:apoapsis, 1.5).
  153.     set throt to 1/(1 + constant:e^(20*eta_ap - 15)).          // just circularizing to a nice clean 40 km orbit
  154.     wait 0.
  155. }
  156. set throt to 0.
  157.  
  158. //inclination change
  159. lock steering to prograde * r(0, 90, 0).
  160. set done to false.
  161. warpto(time:seconds + orbit:period).
  162. wait 1.
  163. clearscreen.
  164. until done {
  165.     set eta_AN to (360 - (orbit:argumentofperiapsis + orbit:trueanomaly)) / (360/orbit:period).          // the lazy way to do orbital mechanics. DOES NOT WORK SUPER GREAT FOR ELLIPTICAL ORBITS, but can be made to do so. I'll leave the exercise to the reader. The proof is trivial.
  166.  
  167.     if eta_AN < 0 set t_AN to eta_AN + orbit:period.          // eta_AN returns a negative number when your true anomaly + arg of periapsis is > 360, so I add the orbit's period in order to sate the Kraken.
  168.     else set t_AN to eta_AN.
  169.  
  170.     print "time to ascending node: " + t_AN at (0,0).
  171.  
  172.     set t to min(t_AN, 3).
  173.     if t_AN < 35 kuniverse:timewarp:cancelwarp().
  174.  
  175.     set throt to 1/(1 + constant:e^(10*t - 20)).          // yet another sigmoid function, my god. They're so useful!
  176.  
  177.     if orbit:inclination < 0.00005 set done to true.          // this honestly could be zero, sigmoid functions are that boss, but it'd take a little bit more time than is really worth it. 0.00005° inclination is pretty dang nice.
  178.     else wait 0.
  179. }
  180. set throt to 0.
  181. lock steering to retrograde.
  182. wait 20.
  183.  
  184. //Munar module separation
  185. unlock steering.
  186. set mm to processor("MMcpu").          // okay, we're in a 0° inclination orbit, we're ready to separate the Command Module from the Munar Module
  187. set mm:bootfilename to sep.ks.          // tells the Munar Module's CPU the next program it needs to run. see DESCENT_PROGRAM.
  188. mm:activate.          // wake up again, yo
  189.  
  190. wait 5.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement