From ab4fd29d16fb737cad0c7b7e36c33e04595a4481 Mon Sep 17 00:00:00 2001 From: "kepler155c@gmail.com" Date: Wed, 20 Feb 2019 08:52:27 -0500 Subject: [PATCH] turtle follow --- sys/apis/gps.lua | 25 ++++++++++++++++--------- sys/apis/trace.lua | 12 +++++++++++- sys/apps/system/kiosk.lua | 4 ++-- sys/extensions/6.tl3.lua | 4 ++-- 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/sys/apis/gps.lua b/sys/apis/gps.lua index cf53d61..c7f24e7 100644 --- a/sys/apis/gps.lua +++ b/sys/apis/gps.lua @@ -34,30 +34,37 @@ function GPS.getPoint(timeout, debug) return pt end -function GPS.getHeading(timeout) - +function GPS.getHeading(timeout, destructive) if not turtle then return end local apt = GPS.locate(timeout) if not apt then - return + return false, 'GPS not available' end local heading = turtle.point.heading - while not turtle.forward() do + while true do + if not turtle.inspect() and turtle.forward() then + break + end turtle.turnRight() if turtle.getHeading() == heading then - _G.printError('GPS.getPoint: Unable to move forward') - return + if destructive then + turtle.dig() + if turtle.forward() then + break + end + end + return false, 'GPS.getPoint: Unable to move forward' end end local bpt = GPS.locate() if not bpt then - return + return false, 'GPS not available' end if apt.x < bpt.x then @@ -70,8 +77,8 @@ function GPS.getHeading(timeout) return 3 end -function GPS.getPointAndHeading(timeout) - local heading = GPS.getHeading(timeout) +function GPS.getPointAndHeading(timeout, destructive) + local heading = GPS.getHeading(timeout, destructive) if heading then local pt = GPS.getPoint() if pt then diff --git a/sys/apis/trace.lua b/sys/apis/trace.lua index 6a0a2cb..ddb023c 100644 --- a/sys/apis/trace.lua +++ b/sys/apis/trace.lua @@ -84,15 +84,25 @@ return function (fn, ...) if trace[i] == "stack traceback:" then trace_starts = i; break end end + for _, line in pairs(trace) do + _G._debug(line) + end + -- If this traceback is more than 15 elements long, keep the first 9, last 5 -- and put an ellipsis between the rest local max = 10 if trace_starts and #trace - trace_starts > max then local keep_starts = trace_starts + 7 - for i = #trace - trace_starts - max, 0, -1 do table.remove(trace, keep_starts + i) end + for i = #trace - trace_starts - max, 0, -1 do + table.remove(trace, keep_starts + i) + end table.insert(trace, keep_starts, " ...") end + for k, line in pairs(trace) do + trace[k] = line:gsub("in function", " in") + end + return false, table.remove(trace, 1), table.concat(trace, "\n") end diff --git a/sys/apps/system/kiosk.lua b/sys/apps/system/kiosk.lua index 0eda77b..d75bec2 100644 --- a/sys/apps/system/kiosk.lua +++ b/sys/apps/system/kiosk.lua @@ -28,9 +28,9 @@ local tab = UI.Tab { function tab:enable() local choices = { } - for k,v in pairs(device) do + for _,v in pairs(device) do if v.type == 'monitor' then - table.insert(choices, { name = k, value = v.side }) + table.insert(choices, { name = v.side, value = v.side }) end end diff --git a/sys/extensions/6.tl3.lua b/sys/extensions/6.tl3.lua index 23fee77..d3649ba 100644 --- a/sys/extensions/6.tl3.lua +++ b/sys/extensions/6.tl3.lua @@ -1239,8 +1239,8 @@ function turtle.inspectForwardAt(pt) return _actionForwardAt(actionsAt.inspe function turtle.inspectUpAt(pt) return _actionUpAt(actionsAt.inspect, pt) end -- [[ GPS ]] -- -function turtle.enableGPS(timeout) - local pt = GPS.getPointAndHeading(timeout) +function turtle.enableGPS(timeout, destructive) + local pt = GPS.getPointAndHeading(timeout, destructive) if pt then turtle.setPoint(pt, true) return turtle.point