Advertisement
Guest User

Mun to Kerbin Return Script by olex

a guest
Aug 8th, 2012
3,736
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.22 KB | None | 0 0
  1. --
  2. --         Mun to Kerbin Return Script by olex
  3. --                    Version 1.0
  4. --
  5. --                 for MechJeb 1.9.1
  6. --
  7. --------------------------------------------------------
  8. -- Usage: * save to KSP/PluginData/mumechlib/GoHome.lua
  9. --        * in game, open autom8
  10. --        * enter: dofile("GoHome.lua")
  11. --        * and follow the instructions.
  12. --
  13. -- Quicksave before using.
  14. -- If an "coroutine out of sync" error pops up, load the save and try again.
  15. --
  16. -- No guarantees, but it should work :) Have fun!
  17. --------------------------------------------------------
  18.  
  19. function KerbinReturnDriver(pe)
  20.     if pe == nil then
  21.         pe = 20.0
  22.     end
  23.     if pe > 70 then
  24.         print "Final periapsis not in atmosphere, no automatic landing."
  25.     end
  26.        
  27.     print "Lifting off..."
  28.     mechjeb.attitudeTo("up", "SURFACE_NORTH") -- vertical ascent
  29.     mechjeb.thrustActivate(100)
  30.     while vessel.altitudeTrue < 300 do
  31.         wait(0.25)
  32.     end
  33.    
  34.     print "Escaping the Mun..."
  35.     -- this needs to be optimized. figure out an escape vector
  36.     -- that is as close as possible to the opposite of Mun's orbital velocity
  37.     -- but not lower than 0° to the surface
  38.     mechjeb.attitudeTo("right", "SURFACE_NORTH") -- heading east
  39.     while vessel.orbitEccentricity < 1 do
  40.         wait(0.25)
  41.     end
  42.     mechjeb.thrustDeactivate()
  43.    
  44.     print "Coasting to Mun escape, hang on."
  45.     mechjeb.warpToEvent("soi")
  46.     wait(mechjeb.free)
  47.    
  48.     print("Lowering Kerbin periapsis to "..pe.."km...")
  49.     mechjeb.changePe(pe * 1000)
  50.     wait(mechjeb.free)
  51.    
  52.     print "Coasting to Kerbin, stand by."
  53.     mechjeb.warpToEvent("pe")
  54.     while vessel.atmosphericDensity <= 0 and (not mechjeb.free()) do
  55.         wait(0.25)
  56.     end
  57.     mechjeb.controlRelease()
  58.    
  59.     if vessel.atmosphericDensity > 0 then
  60.         print "Re-entry... hold onto something."
  61.         mechjeb.attitudeTo("back", "ORBIT")
  62.         while vessel.speedSurface > 250 do
  63.             wait(0.1)
  64.         end
  65.         print "Landing."
  66.         mechjeb.autoStageActivate()
  67.         mechjeb.land()
  68.         wait(mechjeb.free)
  69.         mechjeb.autoStageDeactivate()
  70.         print "Welcome home!"
  71.     else
  72.         print "Not in atmosphere at periapsis. We're done here."
  73.     end
  74. end
  75.  
  76. function GoHome(pe)
  77.     local co = coroutine.create(KerbinReturnDriver)
  78.     coroutine.resume(co, pe)
  79. end
  80.  
  81. print "Usage: GoHome(<desired Kerbin periapsis in km, default 20>)"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement