Advertisement
Guest User

SimpleKSPOrbitalMechanics0.001

a guest
Aug 16th, 2012
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.87 KB | None | 0 0
  1. -- SimpleKSPOrbitalMechanics is a purely calculation based lua library, with some constants hardcoded for Kerbal Space Program
  2. -- v0.001
  3. -- to use, put
  4. -- require "SimpleKSPOrbitalMechanics"
  5. -- in your lua script, or run it with
  6. -- dofile("SimpleKSPOrbitalMechanics")
  7. -- Original Work Copyright 2012, Nadrek, insofar as any elements that aren't obvious, trivial, or mathematics are concerned (none of which are copyrightable)
  8. -- 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/
  9. -- KSP specific functions will have KSP in the name; other functions will not, and can be used for real life calculations as well.
  10.  
  11.  
  12. -- This script 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. require "SimpleOrbitalMechanics"
  16.  
  17. function globalSkomKSPMainBodyResetBasedOnGuessing()
  18.   -- prints body it guessed
  19.   -- also sets the globalMainBody... variables to that body (or all to -1
  20.   --   if the body is UNKNOWN)
  21.   -- Essentially, this function, if called while in orbit around a given body (like the Mun, or Kerbin)
  22.   --   sets some "background" variables so the rest of the functions know how to act based on what
  23.   --   you're orbiting; you use different values for the Min and for Kerbin, because Kerbin is bigger,
  24.   --   has more gravity, has an atmosphere, and has a much larger Hill sphere of influence.
  25.   -- NOTE: Landed, on the launchpad, in ascent, in descent all count as being "in orbit"!
  26.   -- no return value
  27.   local GuessedMainBody = guessSkomKSPWhatBodyIsTheMainBodyCurrentlyBeingOrbited()
  28.   globalSomMainBodyReset(GuessedMainBody)
  29.   print GuessedMainBody
  30. end
  31.  
  32.  
  33. function guessSkomKSPWhatBodyIsTheMainBodyCurrentlyBeingOrbited()
  34.   -- returns the SimpleOrbitalMechanics name of the body we think we're orbiting.
  35.   --   suitable to pass to globalSomMainBodyReset, or returns UNKNOWN if it cannot tell
  36.   --[[ Ex:
  37.     print(guessSkomKSPWhatBodyIsTheMainBodyCurrentlyBeingOrbited())
  38.     ]]
  39.   -- NOTE: All warnings and notes for guessSomWhatBodyIsTheMainBodyFromaOvAltaslMoe apply.
  40.   return guessSomWhatBodyIsTheMainBodyFromaOvAltaslMoe(vessel.orbitSemiMajorAxis, vessel.speedOrbital, vessel.altitudeASL, 0.01)
  41. end
  42.  
  43.  
  44. function calcSkomMolniyaApPeFromMainBodyCurrentlyBeingOrbitedFromMoe(MarginOfError)
  45.   -- MarginOfError 0.001 means allow 0.1% for a "safety zone" above the minimum stable altitude, or below the Hill sphere of influence.
  46.   --   Do this if you want to just barely scrape by!
  47.   --   2 means allow a 200% "safety zone" above the minimum stable altitude, or below the Hill sphere of influence.
  48.   --   Do this if you want to have a much less eccentric orbit - perhaps you want to keep certain orbits clear for other uses!
  49.   --   If in doubt, try setting MarginOfError to 0.1
  50.   -- Returns Ap and Pe of the best possible Molniya orbit for a given body
  51.   --   Returns -1, -1 if no stable Molniya orbit is possible
  52.   -- REQUIRES globalSomMainBodyReset to have been called properly
  53.   --   the simple way is to run globalSkomKSPMainBodyResetBasedOnGuessing() first!
  54.   --[[ Ex:
  55.     do
  56.     local localAp, localPe = calcSkomMolniyaApPeFromMainBodyCurrentlyBeingOrbitedFromMoe(0.1)
  57.     print("Your Apoapsis should be " .. localAp .. " meters and your Periapsis should be " .. localPe .. " meters")
  58.     print("  For a classical Molniya orbit, use an inclination of 63.4, 116.6, -63.4, or -116.6")
  59.     print("  These values are chosen because oblate planets (like the Earth's fat equatorial bulge")
  60.     print("  would change your orbit with other inclinations, since the planet isn't a sphere")
  61.     end
  62.     ]]
  63.   -- For oblate celestial bodies, of course, the inclination of the actual orbit must be 63.4 or 116.6 degrees, so the effect of the oblate equitorial belt is nullified.
  64.  
  65.   -- Now we call our standard Molniya calculator, and pass in numbers based on the celestial body we claim we're currently in orbit of!
  66.   return calcSomMolniyaApPeFromHsoiMsaMbrGmSrpMoe(globalSomMainBodyHillSphereOfInfluence, globalSomMainBodyMinStableAltitude, globalSomMainBodyEquatorialRadius, globalSomMainBodyGM, globalSomMainBodySiderealRotationalPeriod, MarginOfError)
  67.  
  68. end
  69.  
  70.  
  71.  
  72. function printSkomKSPCurrentOrbitalElements()
  73.   print("orbitApA " .. vessel.orbitApA)
  74.   print("orbitPeA " .. vessel.orbitPeA)
  75.   print("orbitPeriod " .. vessel.orbitPeriod)
  76.   print("orbitTimeToAp " .. vessel.orbitTimeToAp)
  77.   print("orbitTimeToPe " .. vessel.orbitTimeToPe)
  78.   print("orbitLAN (RA/Long of Ascending Node) " .. vessel.orbitLAN)
  79.   print("orbitArgumentOfPeriapsis " .. vessel.orbitArgumentOfPeriapsis)
  80.   print("orbitInclination " .. vessel.orbitInclination)
  81.   print("orbitEccentricity " .. vessel.orbitEccentricity)
  82.   print("orbitSemiMajorAxis (a) " .. vessel.orbitSemiMajorAxis)
  83.   print("speedOrbital " .. vessel.speedOrbital)
  84.   print("speedSurface " .. vessel.speedSurface)
  85.   print("speedVertical " .. vessel.speedVertical)
  86.   print("speedHorizontal " .. vessel.speedHorizontal)
  87.   print("altitudeASL " .. vessel.altitudeASL)
  88.   print("altitudeTrue " .. vessel.altitudeTrue)
  89.   print("altitudeBottom " .. vessel.altitudeBottom)
  90.   print("latitude " .. vessel.latitude)
  91.   print("longitude " .. vessel.longitude)
  92.   print("time " .. vessel.time)
  93. end
  94.  
  95.  
  96. function helpSkomSimpleKSPOrbitalMechanics()
  97.   print "Usage: globalSkomKSPMainBodyResetBasedOnGuessing()"
  98.   print "   prints body it guessed"
  99.   print "   also sets the globalMainBody... variables to that body (or all to -1"
  100.   print "     if the body is UNKNOWN)"
  101.   print "   Essentially, this function, if called while in orbit around a given body (like the Mun, or Kerbin)"
  102.   print "     sets some \"background\" variables so the rest of the functions know how to act based on what"
  103.   print "     you're orbiting; you use different values for the Min and for Kerbin, because Kerbin is bigger,"
  104.   print "     has more gravity, has an atmosphere, and has a much larger Hill sphere of influence."
  105.   print "   NOTE: Landed, on the launchpad, in ascent, in descent, and crashing all count as being \"in orbit\"!"
  106.   print "   no return value"
  107.   print "Usage: guessSkomKSPWhatBodyIsTheMainBodyCurrentlyBeingOrbited()"
  108.   print "   returns the SimpleOrbitalMechanics name of the body we think we're orbiting."
  109.   print "   suitable to pass to globalSomMainBodyReset, or returns UNKNOWN if it cannot tell."
  110.   print "   Ex:"
  111.   print "     print(guessSkomKSPWhatBodyIsTheMainBodyCurrentlyBeingOrbited())"
  112.   print "Usage: calcSkomMolniyaApPeFromMainBodyCurrentlyBeingOrbitedFromMoe(MarginOfError)"
  113.   print "   MarginOfError 0.001 means allow 0.1% for a \"safety zone\" above the minimum stable altitude, or below the Hill sphere of influence."
  114.   print "     Do this if you want to just barely scrape by!"
  115.   print "     2 means allow a 200% \"safety zone\" above the minimum stable altitude, or below the Hill sphere of influence."
  116.   print "     Do this if you want to have a much less eccentric orbit - perhaps you want to keep certain orbits clear for other uses!"
  117.   print "     If in doubt, try setting MarginOfError to 0.1"
  118.   print "   Returns Ap and Pe of the best possible Molniya orbit for a given body"
  119.   print "     Returns -1, -1 if no stable Molniya orbit is possible"
  120.   print "   REQUIRES globalSomMainBodyReset to have been called properly"
  121.   print "     the simple way is to run globalSkomKSPMainBodyResetBasedOnGuessing() first!"
  122.   print "   Ex:"
  123.   print "     do"
  124.   print "     local localAp, localPe = calcSkomMolniyaApPeFromMainBodyCurrentlyBeingOrbitedFromMoe(0.1)"
  125.   print "     print(\"Your Apoapsis should be \" .. localAp .. \" meters and your Periapsis should be \" .. localPe .. \" meters\")"
  126.   print "     print(\"  For a classical Molniya orbit, use an inclination of 63.4, 116.6, -63.4, or -116.6\")"
  127.   print "     print(\"  These values are chosen because oblate planets (like the Earth's fat equatorial bulge\")"
  128.   print "     print(\"  would change your orbit with other inclinations, since the planet isn't a sphere\")"
  129.   print "     end"
  130.   print "   For oblate celestial bodies, of course, the inclination of the actual orbit must be 63.4 or 116.6 degrees, so the effect of the oblate equitorial belt is nullified."
  131.   print "Usage: printSkomKSPCurrentOrbitalElements()"
  132.   print "   Prints important KSP elements for human use or records"
  133.  
  134. end
  135.  
  136. globalSkomKSPMainBodyResetBasedOnGuessing() -- initalize main body dependent variables based on guessing what body we're orbiting.
  137.  
  138. print "Usage: helpSkomSimpleKSPOrbitalMechanics() gives detailed help on all Skom functions."
  139. print "   Skom functions relate to Kerbal Space Program (KSP) specific functionality."
  140. print("If it helps, I'm guessing you are currently orbiting " .. globalSomMainBodyName)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement