Advertisement
Guest User

SimpleKSPOrbitalMechanics0.002

a guest
Aug 27th, 2012
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.12 KB | None | 0 0
  1. -- SimpleKSPOrbitalMechanics is a purely calculation based lua library, with some constants hardcoded for Kerbal Space Program
  2. -- v0.002
  3. -- v0.002 changes: added printSkomKSPCurrentVesselInfo, added elements to printSkomKSPCurrentOrbitalElements
  4. -- to use, put
  5. -- require "SimpleKSPOrbitalMechanics"
  6. -- in your lua script, or run it with
  7. -- dofile("SimpleKSPOrbitalMechanics")
  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.  
  13. -- This script requires SimpleOrbitalMechanics to do the heavy mathematical lifting.
  14. -- This script contains KSP specific functions and calls useful to and reliant on Kerbal Space Program, MechJeb, and MechJeb's Autom8 module.
  15.  
  16. require "SimpleOrbitalMechanics"
  17.  
  18. function globalSkomKSPMainBodyResetBasedOnGuessing()
  19.   -- prints body it guessed
  20.   -- also sets the globalMainBody... variables to that body (or all to -1
  21.   --   if the body is UNKNOWN)
  22.   -- Essentially, this function, if called while in orbit around a given body (like the Mun, or Kerbin)
  23.   --   sets some "background" variables so the rest of the functions know how to act based on what
  24.   --   you're orbiting; you use different values for the Min and for Kerbin, because Kerbin is bigger,
  25.   --   has more gravity, has an atmosphere, and has a much larger Hill sphere of influence.
  26.   -- NOTE: Landed, on the launchpad, in ascent, in descent all count as being "in orbit"!
  27.   -- no return value
  28.   local GuessedMainBody = guessSkomKSPWhatBodyIsTheMainBodyCurrentlyBeingOrbited()
  29.   globalSomMainBodyReset(GuessedMainBody)
  30.   print GuessedMainBody
  31. end
  32.  
  33.  
  34. function guessSkomKSPWhatBodyIsTheMainBodyCurrentlyBeingOrbited()
  35.   -- returns the SimpleOrbitalMechanics name of the body we think we're orbiting.
  36.   --   suitable to pass to globalSomMainBodyReset, or returns UNKNOWN if it cannot tell
  37.   --[[ Ex:
  38.     print(guessSkomKSPWhatBodyIsTheMainBodyCurrentlyBeingOrbited())
  39.     ]]
  40.   -- NOTE: All warnings and notes for guessSomWhatBodyIsTheMainBodyFromaOvAltaslMoe apply.
  41.   return guessSomWhatBodyIsTheMainBodyFromaOvAltaslMoe(vessel.orbitSemiMajorAxis, vessel.speedOrbital, vessel.altitudeASL, 0.01)
  42. end
  43.  
  44.  
  45. function calcSkomMolniyaApPeFromMainBodyCurrentlyBeingOrbitedFromMoe(MarginOfError)
  46.   -- MarginOfError 0.001 means allow 0.1% for a "safety zone" above the minimum stable altitude, or below the Hill sphere of influence.
  47.   --   Do this if you want to just barely scrape by!
  48.   --   2 means allow a 200% "safety zone" above the minimum stable altitude, or below the Hill sphere of influence.
  49.   --   Do this if you want to have a much less eccentric orbit - perhaps you want to keep certain orbits clear for other uses!
  50.   --   If in doubt, try setting MarginOfError to 0.1
  51.   -- Returns Ap and Pe of the best possible Molniya orbit for a given body
  52.   --   Returns -1, -1 if no stable Molniya orbit is possible
  53.   -- REQUIRES globalSomMainBodyReset to have been called properly
  54.   --   the simple way is to run globalSkomKSPMainBodyResetBasedOnGuessing() first!
  55.   --[[ Ex:
  56.     do
  57.     local localAp, localPe = calcSkomMolniyaApPeFromMainBodyCurrentlyBeingOrbitedFromMoe(0.1)
  58.     print("Your Apoapsis should be " .. localAp .. " meters and your Periapsis should be " .. localPe .. " meters")
  59.     print("  For a classical Molniya orbit, use an inclination of 63.4, 116.6, -63.4, or -116.6")
  60.     print("  These values are chosen because oblate planets (like the Earth's fat equatorial bulge")
  61.     print("  would change your orbit with other inclinations, since the planet isn't a sphere")
  62.     end
  63.     ]]
  64.   -- 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.
  65.  
  66.   -- Now we call our standard Molniya calculator, and pass in numbers based on the celestial body we claim we're currently in orbit of!
  67.   return calcSomMolniyaApPeFromHsoiMsaMbrGmSrpMoe(globalSomMainBodyHillSphereOfInfluence, globalSomMainBodyMinStableAltitude, globalSomMainBodyEquatorialRadius, globalSomMainBodyGM, globalSomMainBodySiderealRotationalPeriod, MarginOfError)
  68.  
  69. end
  70.  
  71.  
  72.  
  73. function printSkomKSPCurrentOrbitalElements()
  74.   print("orbitApA " .. vessel.orbitApA)
  75.   print("orbitPeA " .. vessel.orbitPeA)
  76.   print("orbitPeriod " .. vessel.orbitPeriod)
  77.   print("orbitTimeToAp " .. vessel.orbitTimeToAp)
  78.   print("orbitTimeToPe " .. vessel.orbitTimeToPe)
  79.   print("orbitLAN (RA/Long of Ascending Node) " .. vessel.orbitLAN)
  80.   print("orbitArgumentOfPeriapsis " .. vessel.orbitArgumentOfPeriapsis)
  81.   print("orbitInclination " .. vessel.orbitInclination)
  82.   print("orbitEccentricity " .. vessel.orbitEccentricity)
  83.   print("orbitSemiMajorAxis (a) " .. vessel.orbitSemiMajorAxis)
  84.   print("speedOrbital " .. vessel.speedOrbital)
  85.   print("speedSurface " .. vessel.speedSurface)
  86.   print("speedVertical " .. vessel.speedVertical)
  87.   print("speedHorizontal " .. vessel.speedHorizontal)
  88.   print("vesselHeading " .. vessel.vesselHeading)
  89.   print("vesselPitch " .. vessel.vesselPitch)
  90.   print("vesselRoll " .. vessel.vesselRoll)
  91.   print("altitudeASL " .. vessel.altitudeASL)
  92.   print("altitudeTrue " .. vessel.altitudeTrue)
  93.   print("altitudeBottom " .. vessel.altitudeBottom)
  94.   print("latitude " .. vessel.latitude)
  95.   print("longitude " .. vessel.longitude)
  96.   print("radius " .. vessel.radius)
  97.   print("localg " .. vessel.localg)
  98.   print("atmosphericDensity " .. vessel.atmosphericDensity)
  99.   print("time " .. vessel.time)
  100. end
  101.  
  102.  
  103. function printSkomKSPCurrentVesselInfo()
  104.   print("CoM[1] " .. vessel.CoM[1])
  105.   print("CoM[2] " .. vessel.CoM[2])
  106.   print("CoM[3] " .. vessel.CoM[3])
  107.   print("up[1] " .. vessel.up[1])
  108.   print("up[2] " .. vessel.up[2])
  109.   print("up[3] " .. vessel.up[3])
  110.   print("north[1] " .. vessel.north[1])
  111.   print("north[2] " .. vessel.north[2])
  112.   print("north[3] " .. vessel.north[3])
  113.   print("east[1] " .. vessel.east[1])
  114.   print("east[2] " .. vessel.east[2])
  115.   print("east[3] " .. vessel.east[3])
  116.   print("forward[1] " .. vessel.forward[1])
  117.   print("forward[2] " .. vessel.forward[2])
  118.   print("forward[3] " .. vessel.forward[3])
  119.   print("rotationSurface[1] " .. vessel.rotationSurface[1])
  120.   print("rotationSurface[2] " .. vessel.rotationSurface[2])
  121.   print("rotationSurface[3] " .. vessel.rotationSurface[3])
  122.   print("rotationSurface[4] " .. vessel.rotationSurface[4])
  123.   print("rotationVesselSurface[1] " .. vessel.rotationVesselSurface[1])
  124.   print("rotationVesselSurface[2] " .. vessel.rotationVesselSurface[2])
  125.   print("rotationVesselSurface[3] " .. vessel.rotationVesselSurface[3])
  126.   print("rotationVesselSurface[4] " .. vessel.rotationVesselSurface[4])
  127.   print("velocityMainBodySurface[1] " .. vessel.velocityMainBodySurface[1])
  128.   print("velocityMainBodySurface[2] " .. vessel.velocityMainBodySurface[2])
  129.   print("velocityMainBodySurface[3] " .. vessel.velocityMainBodySurface[3])
  130.   print("velocityVesselSurface[1] " .. vessel.velocityVesselSurface[1])
  131.   print("velocityVesselSurface[2] " .. vessel.velocityVesselSurface[2])
  132.   print("velocityVesselSurface[3] " .. vessel.velocityVesselSurface[3])
  133.   print("velocityVesselSurfaceUnit[1] " .. vessel.velocityVesselSurfaceUnit[1])
  134.   print("velocityVesselSurfaceUnit[2] " .. vessel.velocityVesselSurfaceUnit[2])
  135.   print("velocityVesselSurfaceUnit[3] " .. vessel.velocityVesselSurfaceUnit[3])
  136.   print("velocityVesselOrbit[1] " .. vessel.velocityVesselOrbit[1])
  137.   print("velocityVesselOrbit[2] " .. vessel.velocityVesselOrbit[2])
  138.   print("velocityVesselOrbit[3] " .. vessel.velocityVesselOrbit[3])
  139.   print("velocityVesselOrbitUnit[1] " .. vessel.velocityVesselOrbitUnit[1])
  140.   print("velocityVesselOrbitUnit[2] " .. vessel.velocityVesselOrbitUnit[2])
  141.   print("velocityVesselOrbitUnit[3] " .. vessel.velocityVesselOrbitUnit[3])
  142.   print("angularVelocity[1] " .. vessel.angularVelocity[1])
  143.   print("angularVelocity[2] " .. vessel.angularVelocity[2])
  144.   print("angularVelocity[3] " .. vessel.angularVelocity[3])
  145.   print("angularMomentum[1] " .. vessel.angularMomentum[1])
  146.   print("angularMomentum[2] " .. vessel.angularMomentum[2])
  147.   print("angularMomentum[3] " .. vessel.angularMomentum[3])
  148.   print("upNormalToVelSurface[1] " .. vessel.upNormalToVelSurface[1])
  149.   print("upNormalToVelSurface[2] " .. vessel.upNormalToVelSurface[2])
  150.   print("upNormalToVelSurface[3] " .. vessel.upNormalToVelSurface[3])
  151.   print("leftSurface[1] " .. vessel.leftSurface[1])
  152.   print("leftSurface[2] " .. vessel.leftSurface[2])
  153.   print("leftSurface[3] " .. vessel.leftSurface[3])
  154.   print("leftOrbit[1] " .. vessel.leftOrbit[1])
  155.   print("leftOrbit[2] " .. vessel.leftOrbit[2])
  156.   print("leftOrbit[3] " .. vessel.leftOrbit[3])
  157.   print("gravityForce[1] " .. vessel.gravityForce[1])
  158.   print("gravityForce[2] " .. vessel.gravityForce[2])
  159.   print("gravityForce[3] " .. vessel.gravityForce[3])
  160.   print("mass " .. vessel.mass)
  161.   print("thrustAvailable " .. vessel.thrustAvailable)
  162.   print("thrustMinimum " .. vessel.thrustMinimum)
  163.   print("maxThrustAccel  " .. vessel.maxThrustAccel )
  164.   print("minThrustAccel " .. vessel.minThrustAccel)
  165.   print("torqueRAvailable " .. vessel.torqueRAvailable)
  166.   print("torquePYAvailable " .. vessel.torquePYAvailable)
  167.   print("torqueThrustPYAvailable " .. vessel.torqueThrustPYAvailable)
  168.   print("massDrag " .. vessel.massDrag)
  169. end
  170.  
  171. function helpSkomSimpleKSPOrbitalMechanics()
  172.   print "Usage: globalSkomKSPMainBodyResetBasedOnGuessing()"
  173.   print "   prints body it guessed"
  174.   print "   also sets the globalMainBody... variables to that body (or all to -1"
  175.   print "     if the body is UNKNOWN)"
  176.   print "   Essentially, this function, if called while in orbit around a given body (like the Mun, or Kerbin)"
  177.   print "     sets some \"background\" variables so the rest of the functions know how to act based on what"
  178.   print "     you're orbiting; you use different values for the Min and for Kerbin, because Kerbin is bigger,"
  179.   print "     has more gravity, has an atmosphere, and has a much larger Hill sphere of influence."
  180.   print "   NOTE: Landed, on the launchpad, in ascent, in descent, and crashing all count as being \"in orbit\"!"
  181.   print "   no return value"
  182.   print "Usage: guessSkomKSPWhatBodyIsTheMainBodyCurrentlyBeingOrbited()"
  183.   print "   returns the SimpleOrbitalMechanics name of the body we think we're orbiting."
  184.   print "   suitable to pass to globalSomMainBodyReset, or returns UNKNOWN if it cannot tell."
  185.   print "   Ex:"
  186.   print "     print(guessSkomKSPWhatBodyIsTheMainBodyCurrentlyBeingOrbited())"
  187.   print "Usage: calcSkomMolniyaApPeFromMainBodyCurrentlyBeingOrbitedFromMoe(MarginOfError)"
  188.   print "   MarginOfError 0.001 means allow 0.1% for a \"safety zone\" above the minimum stable altitude, or below the Hill sphere of influence."
  189.   print "     Do this if you want to just barely scrape by!"
  190.   print "     2 means allow a 200% \"safety zone\" above the minimum stable altitude, or below the Hill sphere of influence."
  191.   print "     Do this if you want to have a much less eccentric orbit - perhaps you want to keep certain orbits clear for other uses!"
  192.   print "     If in doubt, try setting MarginOfError to 0.1"
  193.   print "   Returns Ap and Pe of the best possible Molniya orbit for a given body"
  194.   print "     Returns -1, -1 if no stable Molniya orbit is possible"
  195.   print "   REQUIRES globalSomMainBodyReset to have been called properly"
  196.   print "     the simple way is to run globalSkomKSPMainBodyResetBasedOnGuessing() first!"
  197.   print "   Ex:"
  198.   print "     do"
  199.   print "     local localAp, localPe = calcSkomMolniyaApPeFromMainBodyCurrentlyBeingOrbitedFromMoe(0.1)"
  200.   print "     print(\"Your Apoapsis should be \" .. localAp .. \" meters and your Periapsis should be \" .. localPe .. \" meters\")"
  201.   print "     print(\"  For a classical Molniya orbit, use an inclination of 63.4, 116.6, -63.4, or -116.6\")"
  202.   print "     print(\"  These values are chosen because oblate planets (like the Earth's fat equatorial bulge\")"
  203.   print "     print(\"  would change your orbit with other inclinations, since the planet isn't a sphere\")"
  204.   print "     end"
  205.   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."
  206.   print "Usage: printSkomKSPCurrentOrbitalElements()"
  207.   print "   Prints important KSP elements for human use or records"
  208.   print "Usage: printSkomKSPCurrentVesselInfo()"
  209.   print "   Prints important KSP vessel information for human use or records"
  210. end
  211.  
  212. globalSkomKSPMainBodyResetBasedOnGuessing() -- initalize main body dependent variables based on guessing what body we're orbiting.
  213.  
  214. print "Usage: helpSkomSimpleKSPOrbitalMechanics() gives detailed help on all Skom functions."
  215. print "   Skom functions relate to Kerbal Space Program (KSP) specific functionality."
  216. print("If it helps, I'm guessing you are currently orbiting " .. globalSomMainBodyName)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement