Advertisement
Guest User

AutoRendezvous.lua

a guest
Sep 18th, 2012
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.58 KB | None | 0 0
  1. -- Several portions of this code are ports from MuMechLib, and as such, this code is licensed under GPL.
  2. -- Thanks to braeunig.us/space for much of the math.
  3. print("Loading AutoRendezvous.lua")
  4. print("Loading OrbitCalculations.lua")
  5. dofile("OrbitCalculations")
  6. print("OrbitCalculations.lua loaded")
  7. -- DEBUG
  8. GetParamsOfVessel = nil
  9. GetParamsOfTarget = nil
  10. WarpToTAnomaly = nil
  11. AlignPlanes = nil
  12. CatchUp = nil
  13. Transfer = nil
  14. MatchVelocity = nil
  15. RendezvousDriver = nil
  16. Rendezvous = nil
  17. RendezvousParams = nil
  18. co = nil
  19. coz = nil
  20. --
  21.  
  22. -- RETURNS: vessel orbital parameter table
  23. -- semi-major axis (meters) - SMA
  24. -- periapsis (radius) (meters) - PE
  25. -- apoapsis (radius) (meters) - AP
  26. -- period (seconds) - P
  27. -- eccentricity - E
  28. -- inclination (degrees) - I
  29. -- argument of periapsis (degrees) - AoP
  30. -- Longitude of Ascending Node (degrees) - LAN
  31. -- time to periapsis (seconds) - PeM
  32. function GetParamsOfVessel()
  33.     print("getting vessel params")
  34.     local params = {}
  35.     params.SMA = vessel.orbitSemiMajorAxis
  36.     params.E = vessel.orbitEccentricity
  37.     params.P = vessel.orbitPeriod
  38.     params.Pe = GetPe(params.SMA, params.E)
  39.     params.Ap = GetAp(params.SMA, params.E)
  40.     params.I = vessel.orbitInclination
  41.     params.AoP = vessel.orbitArgumentOfPeriapsis
  42.     params.LAN = vessel.orbitLAN
  43.     params.PeM = vessel.orbitTimeToPe
  44.     return params
  45. end
  46.  
  47. -- RETURNS: target orbital parameter table
  48. -- semi-major axis (meters) - SMA
  49. -- periapsis (radius) (meters) - PE
  50. -- apoapsis (radius) (meters) - AP
  51. -- period (seconds) - P
  52. -- eccentricity - E
  53. -- inclination (degrees) - I
  54. -- argument of periapsis (degrees) - AoP
  55. -- Longitude of Ascending Node (degrees) - LAN
  56. -- time to periapsis (seconds) - PeM
  57. function GetParamsOfTargetVessel()
  58.     print("getting target params")
  59.     local params = {}
  60.     params.SMA = mechjeb.core.targetVessel.orbit.semiMajorAxis
  61.     params.E = mechjeb.core.targetVessel.orbit.eccentricity
  62.     params.P = mechjeb.core.targetVessel.orbit.period
  63.     params.Pe = GetPe(params.SMA, params.E)
  64.     params.Ae = GetAp(params.SMA, params.E)
  65.     params.I = mechjeb.core.targetVessel.orbit.inclination
  66.     params.AoP = mechjeb.core.targetVessel.orbit.argumentOfPeriapsis
  67.     params.LAN = mechjeb.core.targetVessel.orbit.LAN
  68.     params.PeM = mechjeb.core.targetVessel.orbit.timeToPe
  69.     return params
  70. end
  71.  
  72. -- IGNORE MY PRECIOUS BAD CODING PRACTICES
  73. function fooBagle()
  74.     if (now == nil) then now = vessel.orbitTimeToPe end
  75.     return vessel.orbitTimeToPe > now
  76. end
  77.  
  78. -- Warp craft until vessel.orbitTimeToPe <= (peMinus + leadTime)
  79. function WarpToPeMinus(peMinus, leadTime)
  80.     adjustment = 0
  81.     if (vessel.orbitTimeToPe > peMinus) then
  82.         adjustment = 0
  83.     else
  84.         adjustment = vessel.orbitPeriod
  85.     end
  86. --  print("Now: " .. vessel.orbitTimeToPe)
  87. --  print("Warping to: " .. peMinus + leadTime)
  88. --  print("Maximum warp")
  89.     inc = 0
  90.     while (((adjustment + vessel.orbitTimeToPe) > (peMinus + leadTime + 120)) and (inc < 5)) do
  91.         inc = inc + 1
  92.         wait(0.25)
  93.         mechjeb.warpIncrease()
  94. --      print(inc)
  95.     end
  96. --  print("herp" .. adjustment)
  97.     if (adjustment > 0) then
  98.         wait(fooBagle) -- STOP NOT IGNORING THEM!
  99.         wait(0.1)
  100. --      print("derp1")
  101.     end
  102.     now = nil
  103. --  print("derp2")
  104. --  while ((vessel.orbitTimeToPe) > (peMinus + leadTime + 240)) do
  105. --      wait(0.25)
  106. --  end
  107. --  mechjeb.warpDecrease()
  108.     while ((vessel.orbitTimeToPe) > (peMinus + leadTime + 120)) do
  109.         wait(0.25)
  110.     end
  111.     mechjeb.warpDecrease()
  112.     while ((vessel.orbitTimeToPe) > (peMinus + leadTime + 60)) do
  113.         wait(0.25)
  114.     end
  115.     mechjeb.warpDecrease()
  116.     while ((vessel.orbitTimeToPe) > (peMinus + leadTime + 5)) do
  117.         wait(0.25)
  118.     end
  119.     mechjeb.warpDecrease()
  120.     while ((vessel.orbitTimeToPe) > (peMinus + leadTime)) do
  121.         wait(0.1)
  122.     end
  123.     adjustment = nil
  124.     inc = nil
  125.     mechjeb.warpMinimum()
  126. end
  127.  
  128. -- Align craft with target vessel
  129. function AlignPlanes(target)
  130.     print("Aligning Planes")
  131.     local rads = 180/math.pi
  132.     local vesselParams = GetParamsOfVessel()
  133.     local targetParams = GetParamsOfTargetVessel()
  134. --  print("Setting attitude for planar alignment.")
  135. --  mechjeb.attitudeTo("left", "ORBIT")
  136.     print("Aligning planes with target.")
  137.     local AN = FindAN(vesselParams, targetParams)
  138.     print("AN (true anomaly): " .. math.deg(AN))
  139.     local dAngle = AngleChangeRequired(vesselParams, targetParams)
  140.     print("Angle change required: " .. math.deg(dAngle))
  141.     local meanMotion = 1 / math.abs(GetPeMinusAtMeanAnomaly(vesselParams, 1) - GetPeMinusAtMeanAnomaly(vesselParams, 2))
  142.     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)))
  143.     print("Estimated dV required: " .. dVEstimate)
  144.     local burnTimeEstimate = dVEstimate / vessel.maxThrustAccel
  145.     print("Estimated burn time: " .. burnTimeEstimate)
  146.     local leadTime = 0
  147.     print("Coasting to AN")
  148.     WarpToPeMinus(GetPeMinusAtTruAnomaly(vesselParams, AN), leadTime)--, ((burnTimeEstimate*2/3) + leadTime))
  149.     print("Burn in " .. leadTime .. " seconds. Waiting for mark...")
  150.     wait(leadTime)
  151.     print("Mark.")
  152. --  mechjeb.thrustActivate(100)
  153. --  while (math.abs(vessel.orbitInclination - target.orbit.inclination) > 0.2) do
  154. --      wait(0.25)
  155. --  end
  156. --  mechjeb.thrustActivate(10)
  157. --  while (math.abs(vessel.orbitInclination - target.orbit.inclination) > 0.01) do
  158. --      wait(0.1)
  159. --  end
  160. --  mechjeb.thrustDeactivate()
  161. --  mechjeb.attitudeDeactivate()
  162.     print("Planes aligned.")
  163. end
  164.  
  165. function CatchUp(target)
  166. end
  167.  
  168. function Transfer(target)
  169. end
  170.  
  171. function MatchVelocity(target)
  172. end
  173.  
  174. function RendezvousDriver(target)
  175.     print("Beginning attempt to rendezvous with " .. mechjeb.targetName())
  176.     AlignPlanes(target)
  177.     print("Catching up to target")
  178.     CatchUp(target)
  179.     print("Caught up. Transferring to target orbit.")
  180.     Transfer(target)
  181.     print("Transferred to target orbit. Matching velocity")
  182.     MatchVelocity(target)
  183.     print("Congratulations, rendezvous made.")
  184. end
  185.  
  186. function Rendezvous()
  187.     local co = coroutine.create(RendezvousDriver)
  188.     coroutine.resume(co, mechjeb.core.targetVessel)
  189. end
  190.  
  191. function testAlign()
  192.     testCO = coroutine.create(AlignPlanes)
  193.     coroutine.resume(testCO, mechjeb.core.targetVessel)
  194. end
  195.  
  196. --function RendezvousParams()
  197. --  print(vessel.time .. ", " .. vessel.orbitApA .. ", " .. vessel.orbitPeA .. ", " .. vessel.orbitPeriod .. ", " .. vessel.orbitTimeToPe .. ", " .. vessel.orbitLAN .. ", " .. vessel.orbitInclination .. ")")
  198. --  print("Copy the above line and paste it into the other craft.")
  199. --end
  200.  
  201. print("AutoRendezvous.lua loaded.")
  202. -- DOCUMENTATION
  203. 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.")
  204. --
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement