relative-gps coord system

This commit is contained in:
kepler155c@gmail.com 2017-05-24 19:48:48 -04:00
parent 64146f8625
commit e6ee432997
11 changed files with 62 additions and 57 deletions

View File

@ -31,13 +31,13 @@ function GPS.getPoint(timeout, debug)
return pt
end
function GPS.getHeading()
function GPS.getHeading(timeout)
if not turtle then
return
end
local apt = GPS.locate()
local apt = GPS.locate(timeout)
if not apt then
return
end
@ -67,8 +67,8 @@ function GPS.getHeading()
return 3
end
function GPS.getPointAndHeading()
local heading = GPS.getHeading()
function GPS.getPointAndHeading(timeout)
local heading = GPS.getHeading(timeout)
if heading then
local pt = GPS.getPoint()
if pt then

View File

@ -1260,6 +1260,8 @@ function UI.Device:runTransitions(transitions, canvas)
canvas:blitClipped(self.device) -- and blit the remainder
canvas:reset()
local queue = { } -- don't miss events while transition is running
-- especially timers
while true do
for _,k in ipairs(Util.keys(transitions)) do
local transition = transitions[k]
@ -1270,7 +1272,17 @@ function UI.Device:runTransitions(transitions, canvas)
if Util.empty(transitions) then
break
end
os.sleep() -- ?
local timerId = os.startTimer(0)
while true do
local e = { os.pullEvent() }
if e[1] == 'timer' and e[2] == timerId then
break
end
table.insert(queue, e)
end
end
for _, e in ipairs(queue) do
Event.processEvent(e)
end
end

View File

@ -2143,13 +2143,14 @@ UI:setPages({
})
UI:setPage('start')
turtle.setPolicy(turtle.policies.digAttack)
turtle.setPoint({ x = -1, z = -1, y = 0, heading = 0 })
turtle.saveLocation('supplies')
turtle.status = 'idle'
turtle.abort = false
Event.pullEvents()
turtle.run(function()
turtle.setPolicy(turtle.policies.digAttack)
turtle.setPoint({ x = -1, z = -1, y = 0, heading = 0 })
turtle.getState().coordSystem = 'relative'
turtle.saveLocation('supplies')
Event.pullEvents()
end)
UI.term:reset()
turtle.status = 'idle'
--turtle.status = 'idle'

View File

@ -387,9 +387,6 @@ __BUILDER_ID = tonumber(args[1])
maxStackDB:load()
turtle.setPoint({ x = -1, z = -2, y = 0, heading = 0 })
turtle.saveLocation('supplies')
Builder.itemProvider = MEProvider()
if not Builder.itemProvider:isValid() then
Builder.itemProvider = ChestProvider()
@ -399,5 +396,10 @@ if not Builder.itemProvider:isValid() then
end
turtle.run(function()
turtle.setPoint({ x = -1, z = -2, y = 0, heading = 0 })
turtle.getState().coordSystem = 'relative'
turtle.saveLocation('supplies')
Event.pullEvents(onTheWay)
end)

View File

@ -12,21 +12,16 @@ if turtle and device.wireless_modem then
}
Config.load('gps', config)
local GPS = require('gps')
local pt
for i = 1, 3 do
pt = GPS.getPointAndHeading(2)
if pt then
break
end
local s = turtle.enableGPS(2)
if not s then
s = turtle.enableGPS(2)
end
if not pt and config.destructive then
if not s and config.destructive then
turtle.setPolicy('turtleSafe')
pt = GPS.getPointAndHeading(2)
s = turtle.enableGPS(2)
end
if not pt then
if not s then
error('Unable to get GPS position')
end
@ -34,18 +29,12 @@ if turtle and device.wireless_modem then
turtle.setPolicy('turtleSafe')
end
Util.print('Setting turtle point to %d %d %d', pt.x, pt.y, pt.z)
turtle.setPoint(pt)
turtle.getState().coordSystem = 'GPS'
if not turtle.pathfind(homePt) then
error('Failed to return home')
end
end
end)
turtle.setPolicy('none')
if not s and m then
error(m)
end

View File

@ -1,20 +1,16 @@
local function follow(id)
require = requireInjector(getfenv(1))
local GPS = require('gps')
local Socket = require('socket')
local Point = require('point')
local process = require('process')
turtle.status = 'follow ' .. id
local pt = GPS.getPointAndHeading()
if not pt or not pt.heading then
if not turtle.enableGPS() then
error('turtle: No GPS found')
end
turtle.setPoint(pt)
local socket = Socket.connect(id, 161)
if not socket then
error('turtle: Unable to connect to ' .. id)

View File

@ -5,13 +5,10 @@ turtle.run(function()
local id = {COMPUTER_ID}
local pt = GPS.getPointAndHeading()
if not pt or not pt.heading then
if not turtle.enableGPS() then
error('turtle: No GPS found')
end
turtle.setPoint(pt)
local socket = Socket.connect(id, 161)
if not socket then
error('turtle: Unable to connect to ' .. id)

View File

@ -7,6 +7,7 @@ local function summon(id)
turtle.status = 'GPSing'
turtle.setPoint({ x = 0, y = 0, z = 0, heading = 0 })
turtle.getState().coordSystem = 'relative'
local pts = {
[ 1 ] = { x = 0, z = 0, y = 0 },

View File

@ -67,6 +67,7 @@ function turtle.run(fn, ...)
end
if id == ticketId then
turtle.abort = false
turtle.resetState()
local args = { ... }
local s, m = pcall(function() fn(unpack(args)) end)
turtle.abort = false

View File

@ -5,10 +5,15 @@ end
require = requireInjector(getfenv(1))
local GPS = require('gps')
function turtle.enableGPS()
local pt = GPS.getPointAndHeading()
function turtle.enableGPS(timeout)
if turtle.getState().coordSystem == 'GPS' then
return turtle.point
end
local pt = GPS.getPointAndHeading(timeout)
if pt then
turtle.setPoint(pt)
turtle.getState().coordSystem = 'GPS'
return true
end
end
@ -16,25 +21,15 @@ end
function turtle.gotoGPSHome()
local homePt = turtle.loadLocation('gpsHome')
if homePt then
local pt = GPS.getPointAndHeading()
if pt then
turtle.setPoint(pt)
if turtle.enableGPS() then
turtle.pathfind(homePt)
end
end
end
function turtle.setGPSHome()
local GPS = require('gps')
local pt = GPS.getPoint()
if pt then
turtle.setPoint(pt)
pt.heading = GPS.getHeading()
if pt.heading then
turtle.point.heading = pt.heading
turtle.storeLocation('gpsHome', pt)
turtle.gotoPoint(pt)
end
if turtle.enableGPS() then
turtle.storeLocation('gpsHome', turtle.point)
turtle.gotoPoint(tturtle.point)
end
end

View File

@ -50,6 +50,17 @@ function turtle.reset()
return true
end
function turtle.resetState()
--turtle.abort = false -- should be part of state
--turtle.status = 'idle' -- should be part of state
state.moveAttack = noop
state.moveDig = noop
state.moveCallback = noop
state.locations = {}
return true
end
local actions = {
up = {
detect = turtle.native.detectUp,