Advertisement
Guest User

SimpleKSPOrbitalOperations0.001

a guest
Aug 16th, 2012
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.12 KB | None | 0 0
  1.  
  2. -- SimpleKSPOrbitalOperations is a purely calculation based lua library, with some constants hardcoded for Kerbal Space Program
  3. -- v0.001
  4. -- to use, put
  5. -- require "SimpleKSPOrbitalOperations"
  6. -- in your lua script, or run it with
  7. -- dofile("SimpleKSPOrbitalOperations")
  8. -- Original Work Copyright 2012, Nadrek, insofar as any elements that aren't obvious, trivial, or mathematics are concerned (none of which are copyrightable)
  9. -- licensed under your choice of GPLv2, GPLv3, or Creative Commons Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0)  http://www.gnu.org/licenses/gpl-2.0.html  or  http://www.gnu.org/licenses/gpl.html  or  http://creativecommons.org/licenses/by-sa/3.0/
  10. -- KSP specific functions will have KSP in the name; other functions will not, and can be used for real life calculations as well.
  11.  
  12. -- This script requires SimpleKSPOrbitalMechanics to do the math, which in turn requires SimpleOrbitalMechanics to do the heavy mathematical lifting.
  13. -- This script contains KSP specific functions and calls useful to and reliant on Kerbal Space Program, MechJeb, and MechJeb's Autom8 module.
  14.  
  15.  
  16. require "SimpleKSPOrbitalMechanics"
  17.  
  18.  
  19.  
  20. local function launchSkooFromApPeInc(Ap, Pe, Inclination)
  21.   -- Ap is the Apoapsis in meters
  22.   -- Pe is the Periapsis in meters
  23.   -- Inclination is the desired orbital inclination in degrees
  24.   -- FUTURE include wait(time) or warp(time), but right now do print(vessel.time) wait(2) print(vessel.time) end    freezes the game for 2 seconds and then displays two identical times.
  25.  
  26.   -- REQUIRES globalSomMainBodyReset to have been called properly
  27.   --   the simple way is to run globalSkomKSPMainBodyResetBasedOnGuessing() first!
  28.  
  29.   -- First, let's reset our main body; perhaps we've switch orbits recently!
  30.   globalSkomKSPMainBodyResetBasedOnGuessing()
  31.  
  32.   -- Check to see if the Ap and Pe is obviously bad
  33.   if Ap == (-1) or Pe == (-1) then
  34.     print("ERROR: Ap or Pe is -1, which indicates an inability to get a prior calculation to come out right when orbiting " .. globalSomMainBodyName)
  35.     return
  36.   end
  37.    
  38.  
  39.   print("Launching to a Periapsis of " .. Pe .. " meters with Inclination " .. Inclination .. " degrees at " .. vessel.time)
  40.   mechjeb.launchTo(Pe, Inclination)
  41.   wait(mechjeb.free)
  42.   mechjeb.autoStageActivate()
  43.   local StartTime = vessel.time -- in seconds, with a massive offset.  Maybe for use later.
  44.   print("Followon launch circularization at " .. StartTime)
  45.   mechjeb.circularize()
  46.   wait(mechjeb.free)
  47.   print("Changing Apoapsis to " .. Ap .. " meters starting at " .. vessel.time)
  48.   mechjeb.changeAp(Ap)
  49.   wait(mechjeb.free)
  50.   print("Tweaking orbit Pe starting at " .. vessel.time)
  51.   mechjeb.changePe(Pe)
  52.   wait(mechjeb.free)
  53.   print("Tweaking orbit Ap starting at " .. vessel.time)
  54.   mechjeb.changeAp(Ap)
  55.   wait(mechjeb.free)
  56.   mechjeb.autoStageDeactivate()
  57.   print("Launch complete at " .. vessel.time)
  58.   printSkomKSPCurrentOrbitalElements()
  59. end
  60.  
  61.  
  62. local function launchSkooMolniyaFromIncMoe(Inclination, MarginOfError)
  63.   -- Inclination is the desired orbital inclination in degrees, and should be 63.4, -63.4, 116.6, or -116.6 degrees
  64.   --   to correctly counteract the gravitational effect of an oblate body, like Earth's fat equatorial bulge
  65.   -- MarginOfError 0.001 means allow 0.1% for a "safety zone" above the minimum stable altitude, or below the Hill sphere of influence.
  66.   --   Do this if you want to just barely scrape by!
  67.   --   2 means allow a 200% "safety zone" above the minimum stable altitude, or below the Hill sphere of influence.
  68.   --   Do this if you want to have a much less eccentric orbit - perhaps you want to keep certain orbits clear for other uses!
  69.   --   If in doubt, try setting MarginOfError to 0.1
  70.   -- REQUIRES globalSomMainBodyReset to have been called properly
  71.   --   the simple way is to run globalSkomKSPMainBodyResetBasedOnGuessing() first!
  72.  
  73.  
  74.     -- First, let's reset our main body; perhaps we've switch orbits recently!
  75.   globalSkomKSPMainBodyResetBasedOnGuessing()
  76.  
  77.   -- Now, did we get a good Inclination?  If not, give a polite warning.
  78.   if Inclination ~= (63.4) and Inclination ~= (-63.4) and Inclination ~= (116.6) and Inclination ~= (-116.6) then
  79.     print "WARNING: Classic Molniya orbits must have an inclination of 63.4, -63.4, 116.6, or -116.6 degrees"
  80.     print "   to correctly counteract the gravitational effect of an oblate body, like Earth's fat equatorial bulge"
  81.     print "   Continuing to try anyway.  You have been politely warned!"
  82.   end
  83.  
  84.     -- Now, calculate a Molniya orbit.
  85.   local localAp, localPe = calcSkomMolniyaApPeFromMainBodyCurrentlyBeingOrbitedFromMoe(MarginOfError)
  86.  
  87.     -- Did we get one?  If we got -1 back, we didn't.
  88.   if localAp == (-1) or localPe == (-1) then
  89.     print("ERROR: Cannot calculate a stable Molniya orbit with this MarginOfError around " .. globalSomMainBodyName)
  90.     return
  91.   end
  92.  
  93.  
  94.   launchSkooFromApPeInc(localAp, localPe, Inclination)
  95. end
  96.  
  97. function colaunchSkooFromApPeInc(Ap, Pe, Inclination)
  98.     local coToLaunchWith = coroutine.create(launchSkooFromApPeInc)
  99.     coroutine.resume(coToLaunchWith, Ap, Pe, Inclination)
  100. end
  101.  
  102.  
  103. function colaunchSkooMolniyaFromIncMoe(Inclination, MarginOfError)
  104.     local coToLaunchWith = coroutine.create(launchSkooMolniyaFromIncMoe)
  105.     coroutine.resume(coToLaunchWith, Inclination, MarginOfError)
  106. end
  107.  
  108. print "Usage: colaunchSkooMolniyaFromIncMoe(Inclination, MarginOfError)"
  109. print "   If you're currently landed in launch position, this will either launch you into a Molniya orbit, or"
  110. print "   give you a message stating that no stable Molniya orbit exists for this body and Margin of Error."
  111. print "   Inclination should be 63.4, -63.4, 116.6, or -116.6 degrees"
  112. print "   Inclination is the desired orbital inclination in degrees, and should be 63.4, -63.4, 116.6, or -116.6 degrees"
  113. print "     to correctly counteract the gravitational effect of an oblate body, like Earth's fat equatorial bulge"
  114. print "   MarginOfError 0.001 means allow 0.1% for a \"safety zone\" above the minimum stable altitude, or below the Hill sphere of influence."
  115. print "     Do this if you want to just barely scrape by!"
  116. print "     2 means allow a 200% \"safety zone\" above the minimum stable altitude, or below the Hill sphere of influence."
  117. print "     Do this if you want to have a much less eccentric orbit - perhaps you want to keep certain orbits clear for other uses!"
  118. print "     If in doubt, try setting MarginOfError to 0.1"
  119. print "   Ex: "
  120. print "     colaunchSkooMolniyaFromIncMoe(-63.4, 0.01)"
  121. print "   REQUIRES globalSomMainBodyReset to have been called properly"
  122. print "     the simple way is to run globalSkomKSPMainBodyResetBasedOnGuessing() first!"
  123. print "Usage: colaunchSkooFromApPeInc(Ap, Pe, Inclination)"
  124. print "   Ap is the Apoapsis in meters"
  125. print "   Pe is the Periapsis in meters"
  126. print "   Inclination is the desired orbital inclination in degrees"
  127. print "   Ex: 125km circular orbit, 0 degree inclination"
  128. print "     colaunchSkooFromApPeInc(125000, 125000, 0)"
  129. print "   REQUIRES globalSomMainBodyReset to have been called properly"
  130. print "     the simple way is to run globalSkomKSPMainBodyResetBasedOnGuessing() first!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement