Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- SimpleKSPOrbitalOperations is a purely calculation based lua library, with some constants hardcoded for Kerbal Space Program
- -- v0.001
- -- to use, put
- -- require "SimpleKSPOrbitalOperations"
- -- in your lua script, or run it with
- -- dofile("SimpleKSPOrbitalOperations")
- -- Original Work Copyright 2012, Nadrek, insofar as any elements that aren't obvious, trivial, or mathematics are concerned (none of which are copyrightable)
- -- 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/
- -- KSP specific functions will have KSP in the name; other functions will not, and can be used for real life calculations as well.
- -- This script requires SimpleKSPOrbitalMechanics to do the math, which in turn requires SimpleOrbitalMechanics to do the heavy mathematical lifting.
- -- This script contains KSP specific functions and calls useful to and reliant on Kerbal Space Program, MechJeb, and MechJeb's Autom8 module.
- require "SimpleKSPOrbitalMechanics"
- local function launchSkooFromApPeInc(Ap, Pe, Inclination)
- -- Ap is the Apoapsis in meters
- -- Pe is the Periapsis in meters
- -- Inclination is the desired orbital inclination in degrees
- -- 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.
- -- REQUIRES globalSomMainBodyReset to have been called properly
- -- the simple way is to run globalSkomKSPMainBodyResetBasedOnGuessing() first!
- -- First, let's reset our main body; perhaps we've switch orbits recently!
- globalSkomKSPMainBodyResetBasedOnGuessing()
- -- Check to see if the Ap and Pe is obviously bad
- if Ap == (-1) or Pe == (-1) then
- print("ERROR: Ap or Pe is -1, which indicates an inability to get a prior calculation to come out right when orbiting " .. globalSomMainBodyName)
- return
- end
- print("Launching to a Periapsis of " .. Pe .. " meters with Inclination " .. Inclination .. " degrees at " .. vessel.time)
- mechjeb.launchTo(Pe, Inclination)
- wait(mechjeb.free)
- mechjeb.autoStageActivate()
- local StartTime = vessel.time -- in seconds, with a massive offset. Maybe for use later.
- print("Followon launch circularization at " .. StartTime)
- mechjeb.circularize()
- wait(mechjeb.free)
- print("Changing Apoapsis to " .. Ap .. " meters starting at " .. vessel.time)
- mechjeb.changeAp(Ap)
- wait(mechjeb.free)
- print("Tweaking orbit Pe starting at " .. vessel.time)
- mechjeb.changePe(Pe)
- wait(mechjeb.free)
- print("Tweaking orbit Ap starting at " .. vessel.time)
- mechjeb.changeAp(Ap)
- wait(mechjeb.free)
- mechjeb.autoStageDeactivate()
- print("Launch complete at " .. vessel.time)
- printSkomKSPCurrentOrbitalElements()
- end
- local function launchSkooMolniyaFromIncMoe(Inclination, MarginOfError)
- -- Inclination is the desired orbital inclination in degrees, and should be 63.4, -63.4, 116.6, or -116.6 degrees
- -- to correctly counteract the gravitational effect of an oblate body, like Earth's fat equatorial bulge
- -- MarginOfError 0.001 means allow 0.1% for a "safety zone" above the minimum stable altitude, or below the Hill sphere of influence.
- -- Do this if you want to just barely scrape by!
- -- 2 means allow a 200% "safety zone" above the minimum stable altitude, or below the Hill sphere of influence.
- -- Do this if you want to have a much less eccentric orbit - perhaps you want to keep certain orbits clear for other uses!
- -- If in doubt, try setting MarginOfError to 0.1
- -- REQUIRES globalSomMainBodyReset to have been called properly
- -- the simple way is to run globalSkomKSPMainBodyResetBasedOnGuessing() first!
- -- First, let's reset our main body; perhaps we've switch orbits recently!
- globalSkomKSPMainBodyResetBasedOnGuessing()
- -- Now, did we get a good Inclination? If not, give a polite warning.
- if Inclination ~= (63.4) and Inclination ~= (-63.4) and Inclination ~= (116.6) and Inclination ~= (-116.6) then
- print "WARNING: Classic Molniya orbits must have an inclination of 63.4, -63.4, 116.6, or -116.6 degrees"
- print " to correctly counteract the gravitational effect of an oblate body, like Earth's fat equatorial bulge"
- print " Continuing to try anyway. You have been politely warned!"
- end
- -- Now, calculate a Molniya orbit.
- local localAp, localPe = calcSkomMolniyaApPeFromMainBodyCurrentlyBeingOrbitedFromMoe(MarginOfError)
- -- Did we get one? If we got -1 back, we didn't.
- if localAp == (-1) or localPe == (-1) then
- print("ERROR: Cannot calculate a stable Molniya orbit with this MarginOfError around " .. globalSomMainBodyName)
- return
- end
- launchSkooFromApPeInc(localAp, localPe, Inclination)
- end
- function colaunchSkooFromApPeInc(Ap, Pe, Inclination)
- local coToLaunchWith = coroutine.create(launchSkooFromApPeInc)
- coroutine.resume(coToLaunchWith, Ap, Pe, Inclination)
- end
- function colaunchSkooMolniyaFromIncMoe(Inclination, MarginOfError)
- local coToLaunchWith = coroutine.create(launchSkooMolniyaFromIncMoe)
- coroutine.resume(coToLaunchWith, Inclination, MarginOfError)
- end
- print "Usage: colaunchSkooMolniyaFromIncMoe(Inclination, MarginOfError)"
- print " If you're currently landed in launch position, this will either launch you into a Molniya orbit, or"
- print " give you a message stating that no stable Molniya orbit exists for this body and Margin of Error."
- print " Inclination should be 63.4, -63.4, 116.6, or -116.6 degrees"
- print " Inclination is the desired orbital inclination in degrees, and should be 63.4, -63.4, 116.6, or -116.6 degrees"
- print " to correctly counteract the gravitational effect of an oblate body, like Earth's fat equatorial bulge"
- print " MarginOfError 0.001 means allow 0.1% for a \"safety zone\" above the minimum stable altitude, or below the Hill sphere of influence."
- print " Do this if you want to just barely scrape by!"
- print " 2 means allow a 200% \"safety zone\" above the minimum stable altitude, or below the Hill sphere of influence."
- print " Do this if you want to have a much less eccentric orbit - perhaps you want to keep certain orbits clear for other uses!"
- print " If in doubt, try setting MarginOfError to 0.1"
- print " Ex: "
- print " colaunchSkooMolniyaFromIncMoe(-63.4, 0.01)"
- print " REQUIRES globalSomMainBodyReset to have been called properly"
- print " the simple way is to run globalSkomKSPMainBodyResetBasedOnGuessing() first!"
- print "Usage: colaunchSkooFromApPeInc(Ap, Pe, Inclination)"
- print " Ap is the Apoapsis in meters"
- print " Pe is the Periapsis in meters"
- print " Inclination is the desired orbital inclination in degrees"
- print " Ex: 125km circular orbit, 0 degree inclination"
- print " colaunchSkooFromApPeInc(125000, 125000, 0)"
- print " REQUIRES globalSomMainBodyReset to have been called properly"
- print " the simple way is to run globalSkomKSPMainBodyResetBasedOnGuessing() first!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement