Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Several portions of this code are ports from MuMechLib, and as such, this code is licensed under GPL.
- -- Thanks to braeunig.us/space for much of the math.
- print("Loading AutoRendezvous.lua")
- print("Loading OrbitCalculations.lua")
- dofile("OrbitCalculations")
- print("OrbitCalculations.lua loaded")
- -- DEBUG
- GetParamsOfVessel = nil
- GetParamsOfTarget = nil
- WarpToTAnomaly = nil
- AlignPlanes = nil
- CatchUp = nil
- Transfer = nil
- MatchVelocity = nil
- RendezvousDriver = nil
- Rendezvous = nil
- RendezvousParams = nil
- co = nil
- coz = nil
- --
- -- RETURNS: vessel orbital parameter table
- -- semi-major axis (meters) - SMA
- -- periapsis (radius) (meters) - PE
- -- apoapsis (radius) (meters) - AP
- -- period (seconds) - P
- -- eccentricity - E
- -- inclination (degrees) - I
- -- argument of periapsis (degrees) - AoP
- -- Longitude of Ascending Node (degrees) - LAN
- -- time to periapsis (seconds) - PeM
- function GetParamsOfVessel()
- print("getting vessel params")
- local params = {}
- params.SMA = vessel.orbitSemiMajorAxis
- params.E = vessel.orbitEccentricity
- params.P = vessel.orbitPeriod
- params.Pe = GetPe(params.SMA, params.E)
- params.Ap = GetAp(params.SMA, params.E)
- params.I = vessel.orbitInclination
- params.AoP = vessel.orbitArgumentOfPeriapsis
- params.LAN = vessel.orbitLAN
- params.PeM = vessel.orbitTimeToPe
- return params
- end
- -- RETURNS: target orbital parameter table
- -- semi-major axis (meters) - SMA
- -- periapsis (radius) (meters) - PE
- -- apoapsis (radius) (meters) - AP
- -- period (seconds) - P
- -- eccentricity - E
- -- inclination (degrees) - I
- -- argument of periapsis (degrees) - AoP
- -- Longitude of Ascending Node (degrees) - LAN
- -- time to periapsis (seconds) - PeM
- function GetParamsOfTargetVessel()
- print("getting target params")
- local params = {}
- params.SMA = mechjeb.core.targetVessel.orbit.semiMajorAxis
- params.E = mechjeb.core.targetVessel.orbit.eccentricity
- params.P = mechjeb.core.targetVessel.orbit.period
- params.Pe = GetPe(params.SMA, params.E)
- params.Ae = GetAp(params.SMA, params.E)
- params.I = mechjeb.core.targetVessel.orbit.inclination
- params.AoP = mechjeb.core.targetVessel.orbit.argumentOfPeriapsis
- params.LAN = mechjeb.core.targetVessel.orbit.LAN
- params.PeM = mechjeb.core.targetVessel.orbit.timeToPe
- return params
- end
- -- IGNORE MY PRECIOUS BAD CODING PRACTICES
- function fooBagle()
- if (now == nil) then now = vessel.orbitTimeToPe end
- return vessel.orbitTimeToPe > now
- end
- -- Warp craft until vessel.orbitTimeToPe <= (peMinus + leadTime)
- function WarpToPeMinus(peMinus, leadTime)
- adjustment = 0
- if (vessel.orbitTimeToPe > peMinus) then
- adjustment = 0
- else
- adjustment = vessel.orbitPeriod
- end
- -- print("Now: " .. vessel.orbitTimeToPe)
- -- print("Warping to: " .. peMinus + leadTime)
- -- print("Maximum warp")
- inc = 0
- while (((adjustment + vessel.orbitTimeToPe) > (peMinus + leadTime + 120)) and (inc < 5)) do
- inc = inc + 1
- wait(0.25)
- mechjeb.warpIncrease()
- -- print(inc)
- end
- -- print("herp" .. adjustment)
- if (adjustment > 0) then
- wait(fooBagle) -- STOP NOT IGNORING THEM!
- wait(0.1)
- -- print("derp1")
- end
- now = nil
- -- print("derp2")
- -- while ((vessel.orbitTimeToPe) > (peMinus + leadTime + 240)) do
- -- wait(0.25)
- -- end
- -- mechjeb.warpDecrease()
- while ((vessel.orbitTimeToPe) > (peMinus + leadTime + 120)) do
- wait(0.25)
- end
- mechjeb.warpDecrease()
- while ((vessel.orbitTimeToPe) > (peMinus + leadTime + 60)) do
- wait(0.25)
- end
- mechjeb.warpDecrease()
- while ((vessel.orbitTimeToPe) > (peMinus + leadTime + 5)) do
- wait(0.25)
- end
- mechjeb.warpDecrease()
- while ((vessel.orbitTimeToPe) > (peMinus + leadTime)) do
- wait(0.1)
- end
- adjustment = nil
- inc = nil
- mechjeb.warpMinimum()
- end
- -- Align craft with target vessel
- function AlignPlanes(target)
- print("Aligning Planes")
- local rads = 180/math.pi
- local vesselParams = GetParamsOfVessel()
- local targetParams = GetParamsOfTargetVessel()
- -- print("Setting attitude for planar alignment.")
- -- mechjeb.attitudeTo("left", "ORBIT")
- print("Aligning planes with target.")
- local AN = FindAN(vesselParams, targetParams)
- print("AN (true anomaly): " .. math.deg(AN))
- local dAngle = AngleChangeRequired(vesselParams, targetParams)
- print("Angle change required: " .. math.deg(dAngle))
- local meanMotion = 1 / math.abs(GetPeMinusAtMeanAnomaly(vesselParams, 1) - GetPeMinusAtMeanAnomaly(vesselParams, 2))
- local dVEstimate = math.abs((2 * math.sin(dAngle / 2) * math.sqrt(1 - vesselParams.E ^ 2) * math.cos(vesselParams.AoP + AN) * meanMotion * vesselParams.SMA) / (1 + vesselParams.E * math.cos(AN)))
- print("Estimated dV required: " .. dVEstimate)
- local burnTimeEstimate = dVEstimate / vessel.maxThrustAccel
- print("Estimated burn time: " .. burnTimeEstimate)
- local leadTime = 0
- print("Coasting to AN")
- WarpToPeMinus(GetPeMinusAtTruAnomaly(vesselParams, AN), leadTime)--, ((burnTimeEstimate*2/3) + leadTime))
- print("Burn in " .. leadTime .. " seconds. Waiting for mark...")
- wait(leadTime)
- print("Mark.")
- -- mechjeb.thrustActivate(100)
- -- while (math.abs(vessel.orbitInclination - target.orbit.inclination) > 0.2) do
- -- wait(0.25)
- -- end
- -- mechjeb.thrustActivate(10)
- -- while (math.abs(vessel.orbitInclination - target.orbit.inclination) > 0.01) do
- -- wait(0.1)
- -- end
- -- mechjeb.thrustDeactivate()
- -- mechjeb.attitudeDeactivate()
- print("Planes aligned.")
- end
- function CatchUp(target)
- end
- function Transfer(target)
- end
- function MatchVelocity(target)
- end
- function RendezvousDriver(target)
- print("Beginning attempt to rendezvous with " .. mechjeb.targetName())
- AlignPlanes(target)
- print("Catching up to target")
- CatchUp(target)
- print("Caught up. Transferring to target orbit.")
- Transfer(target)
- print("Transferred to target orbit. Matching velocity")
- MatchVelocity(target)
- print("Congratulations, rendezvous made.")
- end
- function Rendezvous()
- local co = coroutine.create(RendezvousDriver)
- coroutine.resume(co, mechjeb.core.targetVessel)
- end
- function testAlign()
- testCO = coroutine.create(AlignPlanes)
- coroutine.resume(testCO, mechjeb.core.targetVessel)
- end
- --function RendezvousParams()
- -- print(vessel.time .. ", " .. vessel.orbitApA .. ", " .. vessel.orbitPeA .. ", " .. vessel.orbitPeriod .. ", " .. vessel.orbitTimeToPe .. ", " .. vessel.orbitLAN .. ", " .. vessel.orbitInclination .. ")")
- -- print("Copy the above line and paste it into the other craft.")
- --end
- print("AutoRendezvous.lua loaded.")
- -- DOCUMENTATION
- print("Usage: First, get into a circular orbit lower than the target (SAME SOI). Select target vessel in Rendezvous module, type 'Rendezvous()' into autom8, watch the magic happen.")
- --
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement