1
0
mirror of https://github.com/kepler155c/opus synced 2024-06-14 09:26:54 +00:00

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 return pt
end end
function GPS.getHeading() function GPS.getHeading(timeout)
if not turtle then if not turtle then
return return
end end
local apt = GPS.locate() local apt = GPS.locate(timeout)
if not apt then if not apt then
return return
end end
@ -67,8 +67,8 @@ function GPS.getHeading()
return 3 return 3
end end
function GPS.getPointAndHeading() function GPS.getPointAndHeading(timeout)
local heading = GPS.getHeading() local heading = GPS.getHeading(timeout)
if heading then if heading then
local pt = GPS.getPoint() local pt = GPS.getPoint()
if pt then if pt then

View File

@ -1260,6 +1260,8 @@ function UI.Device:runTransitions(transitions, canvas)
canvas:blitClipped(self.device) -- and blit the remainder canvas:blitClipped(self.device) -- and blit the remainder
canvas:reset() canvas:reset()
local queue = { } -- don't miss events while transition is running
-- especially timers
while true do while true do
for _,k in ipairs(Util.keys(transitions)) do for _,k in ipairs(Util.keys(transitions)) do
local transition = transitions[k] local transition = transitions[k]
@ -1270,7 +1272,17 @@ function UI.Device:runTransitions(transitions, canvas)
if Util.empty(transitions) then if Util.empty(transitions) then
break break
end 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
end end

View File

@ -2143,13 +2143,14 @@ UI:setPages({
}) })
UI:setPage('start') 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() UI.term:reset()
turtle.status = 'idle' --turtle.status = 'idle'

View File

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

View File

@ -12,21 +12,16 @@ if turtle and device.wireless_modem then
} }
Config.load('gps', config) Config.load('gps', config)
local GPS = require('gps') local s = turtle.enableGPS(2)
local pt if not s then
for i = 1, 3 do s = turtle.enableGPS(2)
pt = GPS.getPointAndHeading(2)
if pt then
break
end
end end
if not s and config.destructive then
if not pt and config.destructive then
turtle.setPolicy('turtleSafe') turtle.setPolicy('turtleSafe')
pt = GPS.getPointAndHeading(2) s = turtle.enableGPS(2)
end end
if not pt then if not s then
error('Unable to get GPS position') error('Unable to get GPS position')
end end
@ -34,18 +29,12 @@ if turtle and device.wireless_modem then
turtle.setPolicy('turtleSafe') turtle.setPolicy('turtleSafe')
end 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 if not turtle.pathfind(homePt) then
error('Failed to return home') error('Failed to return home')
end end
end end
end) end)
turtle.setPolicy('none')
if not s and m then if not s and m then
error(m) error(m)
end end

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -50,6 +50,17 @@ function turtle.reset()
return true return true
end 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 = { local actions = {
up = { up = {
detect = turtle.native.detectUp, detect = turtle.native.detectUp,