Jump to content

MechJeb Autom8 Scripts Megathread


r4m0n

Recommended Posts

Umm I cant get this to work .. I'm typing ... dofile("tour")

and it says...

ParseException: Code has syntax errors:

Line 1, Col 1 'f': Failed to parse Name of VarName.

Line 1, Col 20 '(': Failed to parse '0'..

I've named the file tour.lua

and I've looked over the code but cant see anything that's obvious .........HELP lol

First, next time, please include more of the lua error messages. SharpLua (the version used in Autom8) has an interesting habit of putting the _actual_ line number farther down - in this case, line 31.

Second, I see the problem - I had the same issues with anonymous functions and eventually abandoned the practice.

No guarantees, but try replacing line 31,

wait(function() return vessel.altitudeASL < 5000 end)

with

local Function1 = function() return vessel.altitudeASL < 5000 end

wait(Function1)

Link to comment
Share on other sites

As awesome as this megathread is...it'd be a lot more useful if we had an index for these on the mechjeb wiki. Good stuff inevitably gets buried in a thread.

Edit: After further reading, I realize that I hadn't realized how young Autom8 is. Well...kudos on the hard math work peoples. I look forward to being able to run a script that sets-up a rendezvous in a single burn at some point. :P

...of course at least we can all be thankful that we're dealing with simplified two-body math, not n-body math. :huh:

Edited by phoenix_ca
Link to comment
Share on other sites

Are there any scripts yet for getting to the new planets? I'm really enjoying KSP but I gotta say Im crap at trying to actually steer ships myself.

We're working on it. Doing it right (efficient launch windows and vectors) is a lot of math.

Link to comment
Share on other sites

I created one with 3 functions called Kerbin to Mun and Back, "kerbintomun.lua". Kerbin to Mun (Credit to R4m0n), Mun to Kerbin (Credit to Olex), and Round Trip, that lands you on the mun, waits 30 seconds, and brings you back to Kerbin.

Functions are:

GoMun(lat, lon) (leaving them blank lands you at (0, 14))

GoKerbin(lat, lon) (leaving them blank land you at the KSC)

RoundTrip(munlat, munlon, kerblat, kerblon) (leaving them blank uses the defaults listed above)

I'm currently working on a ship that works perfectly with the Round Trip function. Should I post that here or in the ship exchange?

http://pastebin.com/GzrWa82t


--
-- Kerbin to Mun and Back by B25Mich
-- Version 1.0
--
-- for MechJeb 1.9.1
--
--------------------------------------------------------
-- Usage: * save to KSP/PluginData/mumechlib/kerbintomun.lua
-- * in game, open autom8
-- * enter: dofile("kerbintomun.lua")
-- * and follow the instructions.
--------------------------------------------------------

function GoMunDriver(lat, lon)

print "Launching..."
mechjeb.launchTo(125000, 0)
wait(mechjeb.free)
mechjeb.autoStageActivate()
print "TMI..."
mechjeb.transfer("Mun", 200000)
wait(mechjeb.free)
print "Warping to SoI..."
mechjeb.warpToEvent("soi")
wait(mechjeb.free)
print "Warping to Pe..."
mechjeb.warpToEvent("pe", 30)
wait(mechjeb.free)
print "Circularizing orbit..."
mechjeb.circularize()
wait(mechjeb.free)
print "Landing..."
if (lat == nil or lon == nil) then
mechjeb.landAt(0, 14)
else
mechjeb.landAt(lat, lon)
end
wait(mechjeb.free)
mechjeb.autoStageDeactivate()
print "Landed!"
end

function GoKerbinDriver(lat, lon)

print "Lifting off..."
mechjeb.attitudeTo("up", "SURFACE_NORTH") -- vertical ascent
mechjeb.thrustActivate(100)
while vessel.altitudeTrue < 300 do
wait(0.25)
end

print "Escaping the Mun..."
-- this needs to be optimized. figure out an escape vector
-- that is as close as possible to the opposite of Mun's orbital velocity
-- but not lower than 0° to the surface
mechjeb.attitudeTo("right", "SURFACE_NORTH") -- heading east
while vessel.orbitEccentricity < 1 do
wait(0.25)
end
mechjeb.thrustDeactivate()

print "Coasting to Mun escape, hang on."
mechjeb.warpToEvent("soi")
wait(mechjeb.free)

print("Lowering Kerbin periapsis to 100 km...")
mechjeb.changePe(100 * 1000)
wait(mechjeb.free)

print "Warping to Pe..."
mechjeb.warpToEvent("pe", 30)
wait(mechjeb.free)

print "Circularizing orbit..."
mechjeb.circularize()
wait(mechjeb.free)

print "Re-entry... hold onto something."
mechjeb.landAt(-0.103, -74.575)
wait(mechjeb.free)
mechjeb.autoStageDeactivate()
print "Landed!"
end

function WaitForLandingDriver(lat, lon)
wait(30)
while vessel.altitudeBottom > 5 do
wait(5)
end
print "Prepare for launch."
wait(30)
local co = coroutine.create(GoKerbinDriver)
coroutine.resume(co, lat, lon)
end

function GoKerbin(lat, lon)
local co = coroutine.create(GoKerbinDriver)
coroutine.resume(co, lat, lon)
end

function GoMun(lat, lon)
local co = coroutine.create(GoMunDriver)
coroutine.resume(co, lat, lon)
end

function RoundTrip(munlat, munlon, kerblat, kerblon)
local co1 = coroutine.create(GoMunDriver)
coroutine.resume(co1, munlat, munlon)
local co2 = coroutine.create(WaitForLandingDriver)
coroutine.resume(co2, kerblat, kerblon)
end

print "Usage: GoMun(lat, lon)"
print "Usage: GoKerbin(lat, lon)"
print "Usage: RoundTrip(munlat, munlon, kerbinlat, kerbinlon)"

Link to comment
Share on other sites

I absolutely love this Autom8 stuff. Here's a thrown together script to get you to Duna. Not sure if it will work...


--
-- To Duna by B25Mitch
-- Version 1.0
--
-- for MechJeb 1.9.3
--
--------------------------------------------------------
-- Usage: * save to KSP/PluginData/mumechlib/toduna.lua
-- * in game, open autom8
-- * enter: dofile("toduna.lua")
-- * and follow the instructions.
--------------------------------------------------------

function ToDunaDriver(lat, lon)

print "Launching..."
mechjeb.launchTo(125000, 0)
wait(mechjeb.free)
mechjeb.autoStageActivate()

print "Changing Apoapsis to outside Kerbin SoI"
mechjeb.changeAp(90000000)
wait(mechjeb.free)

print "Warping to SoI"
mechjeb.warpToEvent("SoI", 30)
wait(mechjeb.free)

print "Circularizing orbit."
mechjeb.circularize()
wait(mechjeb.free)

print "Transferring to Duna"
mechjeb.transfer("Duna", 200000)
wait(mechjeb.free)

print "Circularizing Orbit."
mechjeb.circularize()
wait(mechjeb.free)

print "Landing..."
if (lat == nil or lon == nil) then
mechjeb.landAt(0, 0)
else
mechjeb.landAt(lat, lon)
end
wait(mechjeb.free)
mechjeb.autoStageDeactivate()
print "Landed!"
end

function ToDuna(lat, lon)
local co = coroutine.create(ToDunaDriver)
coroutine.resume(co, lat, lon)
end

print "Usage: ToDuna(lat, lon)"

Edited by B25Mitch
Link to comment
Share on other sites

I absolutely love this Autom8 stuff. Here's a thrown together script to get you to Duna. Not sure if it will work...

It won't work as is, here is what I can see will fail:

After the "Transferring to Duna", it will still be in Kerbol orbit, you need to warp to SoI switch, then probably correct the Pe, warp to Pe and THEN circularize.

Else than that, the landing AP will probably crash the ship, as it isn't ready to account for the atmosphere of Duna yet, so you could start it, wait until you are at a safer altitude, say 15km, and engage the plain mechjeb.land() function, which ignores the atmosphere but is more reliable. You can't properly pick where it will land, but it won't crash you.

Link to comment
Share on other sites

Three thoughts. If we improve a script, how should we deal with that? Just update the PasteBin and tell R4m0n that it's now v2.0 or whatever, or make an entirely new file?

Secondly, I'd love to see a mechjeb.warp(int factor) or something similar, so we can automatically induce time warp.

Lastly, I'd also like to see a way to modify the ascent path for the Ascent Autopilot.

Link to comment
Share on other sites

Not sure if I should post it here or in general MechJeb, but sometimes when running Autom8 programs, I do something else in another window. For some reason, the auto-warp on "coasting to apoapsis" on the Ascent Autopilot does not activate when you are not focused on the window. Then you click on Kerbal and it ramps up the warp like normal. Every other auto-warp works fine, just not Ascent Autopilot. Does anybody else have/can recreate this problem?

Link to comment
Share on other sites

r4m0n, would you like a new report on the Userecho/whatever new place?

MechJeb 1.9.3 is still vulnerable to the out of sync errors on wait(), which makes any precision control very difficult, to say the least.


0.187329864501953 loop 166
Coroutine exception: InvalidOperationException - out of sync
at System.Collections.Generic.Dictionary`2+Enumerator[SharpLua.LuaTypes.LuaValue,SharpLua.LuaTypes.LuaValue].VerifyState () [0x00000] in <filename unknown>:0
at System.Collections.Generic.Dictionary`2+Enumerator[SharpLua.LuaTypes.LuaValue,SharpLua.LuaTypes.LuaValue].MoveNext () [0x00000] in <filename unknown>:0
at System.Collections.Generic.Dictionary`2+KeyCollection+Enumerator[SharpLua.LuaTypes.LuaValue,SharpLua.LuaTypes.LuaValue].MoveNext () [0x00000] in <filename unknown>:0
at SharpLua.LuaTypes.LuaTable.GetKey (System.String key) [0x00000] in <filename unknown>:0
at SharpLua.LuaTypes.LuaTable.GetValue (System.String name) [0x00000] in <filename unknown>:0
at SharpLua.LuaTypes.LuaTable.GetValueFromMetaTable (System.String name) [0x00000] in <filename unknown>:0
at SharpLua.LuaTypes.LuaTable.GetValue (System.String name) [0x00000] in <filename unknown>:0
at SharpLua.LuaTypes.LuaTable.GetValueFromMetaTable (System.String name) [0x00000] in <filename unknown>:0
at SharpLua.LuaTypes.LuaTable.GetValue (System.String name) [0x00000] in <filename unknown>:0
at SharpLua.AST.VarName.Evaluate (SharpLua.LuaTypes.LuaTable enviroment) [0x00000] in <filename unknown>:0
at SharpLua.AST.Operation.InfixBinaryOperation (SharpLua.AST.Term LeftOperand, System.String Operator, SharpLua.AST.Term RightOperand, SharpLua.LuaTypes.LuaTable enviroment) [0x00000] in <filename unknown>:0
at SharpLua.AST.Operation.Evaluate (SharpLua.LuaTypes.LuaTable enviroment) [0x00000] in <filename unknown>:0
at SharpLua.AST.FunctionCall+<Evaluate>c__AnonStorey9.<>m__B (SharpLua.AST.Expr arg) [0x00000] in <filename unknown>:0
at System.Collections.Generic.List`1[SharpLua.AST.Expr].ConvertAll[LuaValue] (System.Converter`2 converter) [0x00000] in <filename unknown>:0
at SharpLua.AST.FunctionCall.Evaluate (SharpLua.LuaTypes.LuaValue baseValue, SharpLua.LuaTypes.LuaTable enviroment) [0x00000] in <filename unknown>:0
at SharpLua.AST.PrimaryExpr.Evaluate (SharpLua.LuaTypes.LuaTable enviroment) [0x00000] in <filename unknown>:0
at SharpLua.AST.Operation.InfixBinaryOperation (SharpLua.AST.Term LeftOperand, System.String Operator, SharpLua.AST.Term RightOperand, SharpLua.LuaTypes.LuaTable enviroment) [0x00000] in <filename unknown>:0
at SharpLua.AST.Operation.Evaluate (SharpLua.LuaTypes.LuaTable enviroment) [0x00000] in <filename unknown>:0
at SharpLua.AST.Assignment+<Execute>c__AnonStorey4.<>m__5 (SharpLua.AST.Expr expr) [0x00000] in <filename unknown>:0
at System.Collections.Generic.List`1[SharpLua.AST.Expr].ConvertAll[LuaValue] (System.Converter`2 converter) [0x00000] in <filename unknown>:0
at SharpLua.AST.Assignment.Execute (SharpLua.LuaTypes.LuaTable enviroment, System.Boolean& isBreak) [0x00000] in <filename unknown>:0
at SharpLua.AST.Chunk.Execute (System.Boolean& isBreak) [0x00000] in <filename unknown>:0
at SharpLua.AST.Chunk.Execute (SharpLua.LuaTypes.LuaTable enviroment, System.Boolean& isBreak) [0x00000] in <filename unknown>:0
at SharpLua.AST.WhileStmt.Execute (SharpLua.LuaTypes.LuaTable enviroment, System.Boolean& isBreak) [0x00000] in <filename unknown>:0
at SharpLua.AST.Chunk.Execute (System.Boolean& isBreak) [0x00000] in <filename unknown>:0
at SharpLua.AST.Chunk.Execute () [0x00000] in <filename unknown>:0
at SharpLua.AST.FunctionBody+<Evaluate>c__AnonStorey8.<>m__A (SharpLua.LuaTypes.LuaValue[] args) [0x00000] in <filename unknown>:0
at SharpLua.AST.FunctionCall.Evaluate (SharpLua.LuaTypes.LuaValue baseValue, SharpLua.LuaTypes.LuaTable enviroment) [0x00000] in <filename unknown>:0
at SharpLua.AST.PrimaryExpr.Evaluate (SharpLua.LuaTypes.LuaTable enviroment) [0x00000] in <filename unknown>:0
at SharpLua.AST.LocalVar+<Execute>c__AnonStorey7.<>m__9 (SharpLua.AST.Expr expr) [0x00000] in <filename unknown>:0
at System.Collections.Generic.List`1[SharpLua.AST.Expr].ConvertAll[LuaValue] (System.Converter`2 converter) [0x00000] in <filename unknown>:0
at SharpLua.AST.LocalVar.Execute (SharpLua.LuaTypes.LuaTable enviroment, System.Boolean& isBreak) [0x00000] in <filename unknown>:0
at SharpLua.AST.Chunk.Execute (System.Boolean& isBreak) [0x00000] in <filename unknown>:0
at SharpLua.AST.Chunk.Execute () [0x00000] in <filename unknown>:0
at SharpLua.AST.FunctionBody

Link to comment
Share on other sites

r4m0n, would you like a new report on the Userecho/whatever new place?

MechJeb 1.9.3 is still vulnerable to the out of sync errors on wait(), which makes any precision control very difficult, to say the least.

My fix was mainly on the wait() function, which doesn't appear you were using. De-syncs can happen anytime, so running heavy computing code is really likely to trigger it. I'll see if I can do something about it, but Unity doesn't support multi-threading, so it's possible that I won't be able to totally eliminate those.

If you want to run heavier code, break it in pieces you can run through wait() and make them able to resume if the previous triggered the error in the middle.

Update: I've checked around a bit, and this particular error happens when you are iterating through an table and change one of it's elements, were you doing it? It would help a lot if you could share the code that caused this exception.

Edited by r4m0n
Link to comment
Share on other sites

My apologies - I didn't know the errors could happen at any time, as I'd only seen them on the wait() loops and I hadn't delved deeper. I was running my Isp test routine; a much newer version which I'll release once I've finished adding Delta-V vector burn code.

In-progress beta:

Som http://pastebin.com/dt7wsq2Z

Skom http://pastebin.com/AnDhqQvP

Skoo http://pastebin.com/FiDgeSZy


require "SimpleKSPOrbitalOperations"
cotestSkooIspFromTmbiTmbtwnNsMpcsMnottbsMnst(0.08, 0.15, 250, 0.01, 20000, 12)

With clumsier ships, it'll typically die during the "figure out if we're done pointing yet" phase. If it gets past that, then it's likely to due during the very fine-grained throttle settings.

Note that "make them able to resume" is not nearly as easy as it sounds, and it's even more difficult because of the limited amount of output before the Autom8 windows stops showing output altogether. Combined with being able to create files, but not write to them (in any of my tests, at least), this provides several challenges, even for work that's not timing sensitive.

Regardless, thank you for Autom8 and for reducing the frequency of sync errors! Is there any kind of error trapping we can do in lua?

Link to comment
Share on other sites

Body.maxAtmosphereAltitude

as in

local Orbits = script.call("Planetarium.Orbits")

if PlanetIndex == nil then

-- Kerbol

print("Nameindex nil")

Body = Orbits[2].referenceBody

elseif MoonIndex1 == nil then

-- Planet

print("Nameindex " .. PlanetIndex)

Body = Orbits[2].referenceBody.orbitingBodies[PlanetIndex]

elseif MoonIndex2 == nil then

-- Moon of a Planet

print("Nameindex " .. PlanetIndex .. "," .. MoonIndex1)

Body = Orbits[2].referenceBody.orbitingBodies[PlanetIndex].orbitingBodies[MoonIndex1]

else

-- Moon of a Moon of a Planet

print("Nameindex " .. PlanetIndex .. "," .. MoonIndex1 .. "," .. MoonIndex2)

Body = Orbits[2].referenceBody.orbitingBodies[PlanetIndex].orbitingBodies[MoonIndex1].orbitingBodies[MoonIndex2]

end

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...