turtle gps rework

This commit is contained in:
kepler155c@gmail.com
2019-02-22 03:53:40 -05:00
parent 70a3bd1166
commit f37dac0c79
11 changed files with 195 additions and 139 deletions

View File

@@ -37,10 +37,10 @@ local socket
local page = UI.Page { local page = UI.Page {
coords = UI.Window { coords = UI.Window {
backgroundColor = colors.black, backgroundColor = colors.black,
height = 4, height = 3,
}, },
tabs = UI.Tabs { tabs = UI.Tabs {
x = 1, y = 5, ey = -2, x = 1, y = 4, ey = -2,
scripts = UI.ScrollingGrid { scripts = UI.ScrollingGrid {
tabTitle = 'Run', tabTitle = 'Run',
backgroundColor = colors.cyan, backgroundColor = colors.cyan,
@@ -122,12 +122,19 @@ local page = UI.Page {
fn = 'turtle.turnRight', fn = 'turtle.turnRight',
}, },
info = UI.TextArea { info = UI.TextArea {
x = 2, y = 9, x = 15, y = 2,
inactive = true, inactive = true,
} }
}, },
}, },
statusBar = UI.StatusBar(), statusBar = UI.StatusBar {
values = { },
columns = {
{ key = 'status' },
{ key = 'distance', width = 6 },
{ key = 'fuel', width = 6 },
},
},
notification = UI.Notification(), notification = UI.Notification(),
accelerators = { accelerators = {
q = 'quit', q = 'quit',
@@ -184,8 +191,8 @@ function page.coords:draw()
if not t.point.gps then if not t.point.gps then
ind = 'REL' ind = 'REL'
end end
self:print(string.format('%s : %d,%d,%d\n Fuel: %s\n', self:print(string.format('%s : %d,%d,%d',
ind, t.point.x, t.point.y, t.point.z, Util.toBytes(t.fuel))) ind, t.point.x, t.point.y, t.point.z))
end end
end end
@@ -289,14 +296,14 @@ end
function page.statusBar:draw() function page.statusBar:draw()
local t = self.parent.turtle local t = self.parent.turtle
if t then if t then
local status = string.format('%s [ %s ]', t.status, Util.round(t.distance, 2)) self.values.status = t.status
self:setStatus(status, true) self.values.distance = Util.round(t.distance, 2)
self.values.fuel = Util.toBytes(t.fuel)
end end
UI.StatusBar.draw(self) UI.StatusBar.draw(self)
end end
function page:showBlocks() function page:showBlocks()
local script = [[ local script = [[
local function inspect(direction) local function inspect(direction)
local s,b = turtle['inspect' .. (direction or '')]() local s,b = turtle['inspect' .. (direction or '')]()

View File

@@ -99,4 +99,12 @@ Needs work
\030 \031f\030f\031e\139\030e\128\030f\159\129\0314\130\131\131\129", \030 \031f\030f\031e\139\030e\128\030f\159\129\0314\130\131\131\129",
run = "recorder.lua", run = "recorder.lua",
}, },
[ "4486006f811b88cacd5f211fd579717e29b600cd" ] = {
title = "Swarm",
category = "Turtle",
icon = " \0315\\\030 \031 \
\0304\031f _ \030 \031c/\0315\\\
\0304 ",
run = "multiMiner.lua",
},
} }

View File

@@ -1,3 +1,7 @@
_G.requireInjector(_ENV) _G.requireInjector(_ENV)
local Home = require('turtle.home') local config = require('config').load('gps')
turtle.run(Home.go) if config.home then
if turtle.enableGPS() then
return turtle.pathfind(config.home)
end
end

View File

@@ -1,3 +1,8 @@
_G.requireInjector(_ENV) _G.requireInjector(_ENV)
local Home = require('turtle.home') local Config = require('config')
turtle.run(Home.set) local pt = turtle.enableGPS()
if pt then
local config = Config.load('gps', { })
config.home = pt
Config.update('gps', config)
end

View File

@@ -17,12 +17,4 @@
run = "scanningMiner.lua", run = "scanningMiner.lua",
requires = 'turtle', requires = 'turtle',
}, },
[ "4486006f811b88cacd5f211fd579717e29b600cd" ] = {
title = "Multi",
category = "Turtle",
icon = " \0315\\\030 \031 \
\0304\031f _ \030 \031c/\0315\\\
\0304 ",
run = "multiMiner.lua",
},
} }

View File

@@ -72,6 +72,9 @@ local function getNextPoint(member)
chunkIndex = chunkIndex + 1 chunkIndex = chunkIndex + 1
while paused do while paused do
if abort then
return
end
os.sleep(3) os.sleep(3)
end end

View File

@@ -1,42 +0,0 @@
local Config = require('config')
local GPS = require('gps')
local turtle = _G.turtle
local Home = { }
function Home.go()
local config = { }
Config.load('gps', config)
if config.home then
if turtle.enableGPS() then
return turtle.pathfind(config.home)
end
end
end
function Home.set()
local config = { }
Config.load('gps', config)
local pt = GPS.getPoint()
if pt then
local originalHeading = turtle.point.heading
local heading = GPS.getHeading()
if heading then
local turns = (turtle.point.heading - originalHeading) % 4
pt.heading = (heading - turns) % 4
config.home = pt
Config.update('gps', config)
pt = GPS.getPoint()
pt.heading = heading
turtle.setPoint(pt, true)
turtle.go(config.home)
return config.home
end
end
end
return Home

View File

@@ -1,43 +0,0 @@
local modem = _G.device.wireless_modem
local turtle = _G.turtle
if turtle and modem then
local s, m = turtle.run(function()
_G.requireInjector(_ENV)
local Config = require('config')
local config = {
destructive = false,
}
Config.load('gps', config)
if config.home then
local s = turtle.enableGPS(2)
if not s then
s = turtle.enableGPS(2)
end
if not s and config.destructive then
turtle.set({ attackPolicy = 'attack', digPolicy = 'turtleSafe' })
s = turtle.enableGPS(2)
end
if not s then
error('Unable to get GPS position')
end
if config.destructive then
turtle.set({ attackPolicy = 'attack', digPolicy = 'turtleSafe' })
end
if not turtle.pathfind(config.home) then
error('Failed to return home')
end
end
end)
if not s and m then
error(m)
end
end

View File

@@ -1,31 +1,138 @@
local Util = require('util') if not _G.turtle then
return
end
local device = _G.device local Config = require('config')
local fs = _G.fs local GPS = require('gps')
local turtle = _G.turtle local Point = require('point')
local Util = require('util')
if turtle then local device = _G.device
fs.mount('sys/apps/system/turtle.lua', 'linkfs', 'packages/turtle/system/turtle.lua') local fs = _G.fs
local peripheral = _G.peripheral
local turtle = _G.turtle
function turtle.scan(blocks) -- add a System setup tab
local pt = turtle.point fs.mount('sys/apps/system/turtle.lua', 'linkfs', 'packages/turtle/system/turtle.lua')
local scanner = device['plethora:scanner'] or error('Scanner not equipped')
if not blocks then -- provide a turtle function for scanning
return Util.each(scanner:scan(), function(b) function turtle.scan(blocks)
b.x = pt.x + b.x local pt = turtle.point
b.y = pt.y + b.y local scanner = device['plethora:scanner'] or error('Scanner not equipped')
b.z = pt.z + b.z
end)
end
return Util.filter(scanner:scan(), function(b) if not blocks then
if blocks[b.name] then return Util.each(scanner:scan(), function(b)
b.x = pt.x + b.x b.x = pt.x + b.x
b.y = pt.y + b.y b.y = pt.y + b.y
b.z = pt.z + b.z b.z = pt.z + b.z
return true
end
end) end)
end end
return Util.filter(scanner:scan(), function(b)
if blocks[b.name] then
b.x = pt.x + b.x
b.y = pt.y + b.y
b.z = pt.z + b.z
return true
end
end)
end
local function getHeading(apt)
if not turtle then
return
end
local heading = turtle.point.heading
local bpt
repeat
if not turtle.inspect() and turtle.forward() then
bpt = GPS.locate()
break
end
turtle.turnRight()
until turtle.getHeading() == heading
if not bpt then
repeat
if not peripheral.getType('front') then
turtle.dig()
if turtle.forward() then
bpt = GPS.locate()
break
end
end
turtle.turnRight()
until turtle.point.heading == heading
end
if not bpt then
return false
end
local turns = (turtle.point.heading - heading) % 4
turtle.back()
turtle.setHeading(heading)
if apt.x < bpt.x then
return (0 - turns) % 4
elseif apt.z < bpt.z then
return (1 - turns) % 4
elseif apt.x > bpt.x then
return (2 - turns) % 4
end
return (3 - turns) % 4
end
local function getScannedHeading()
local facing = device['plethora:scanner'].getBlockMeta(0, 0, 0).state.facing
return Point.facings[facing].heading
end
-- [[ GPS ]] --
function turtle.enableGPS(timeout)
local pt = GPS.getPoint(timeout or 2) or error('GPS not found')
if device['plethora:scanner'] then
pt.heading = getScannedHeading()
elseif turtle.select('plethora:module:2') then
-- never swap out modem
local equip = turtle.isEquipped('modem') == 'right' and turtle.equipLeft or turtle.equipRight
if equip() then
pt.heading = getScannedHeading()
equip()
end
end
if not pt.heading then
pt.heading = getHeading(pt)
end
if pt.heading then
turtle.setPoint(pt, true)
return turtle.point
end
end
-- return to home location if configured to do so
if _G.device.wireless_modem then
local config = Config.load('gps')
if config.home then
if not turtle.enableGPS(2) then
error('Unable to get GPS position')
end
if config.destructive then
turtle.set({ attackPolicy = 'attack', digPolicy = 'turtleSafe' })
end
if not turtle.pathfind(config.home) then
error('Failed to return home')
end
end
end end

View File

@@ -5,13 +5,11 @@ local fs = _G.fs
local turtle = _G.turtle local turtle = _G.turtle
if turtle then if turtle then
local Home = require('turtle.home') local config = Config.load('gps')
local values = { }
Config.load('gps', values.home and { values.home } or { })
local gpsTab = UI.Tab { local gpsTab = UI.Tab {
tabTitle = 'GPS', tabTitle = 'Home',
description = 'Turtle home location',
labelText = UI.Text { labelText = UI.Text {
x = 3, y = 2, x = 3, y = 2,
value = 'On restart, return to this location' value = 'On restart, return to this location'
@@ -19,7 +17,7 @@ if turtle then
grid = UI.Grid { grid = UI.Grid {
x = 3, ex = -3, y = 4, x = 3, ex = -3, y = 4,
height = 2, height = 2,
values = values, values = { config.home or { } },
inactive = true, inactive = true,
columns = { columns = {
{ heading = 'x', key = 'x' }, { heading = 'x', key = 'x' },
@@ -37,24 +35,41 @@ if turtle then
text = 'Clear', text = 'Clear',
event = 'gps_clear', event = 'gps_clear',
}, },
breakingText = UI.Text {
x = 3, y = 9,
value = 'Can break blocks',
},
breaking = UI.Checkbox {
x = 20, y = 9,
value = config.destructive,
},
} }
function gpsTab:eventHandler(event) function gpsTab:eventHandler(event)
if event.type == 'gps_set' then if event.type == 'gps_set' then
self:emit({ type = 'info_message', message = 'Determining location' }) self:emit({ type = 'info_message', message = 'Determining location' })
self:sync() self:sync()
if Home.set() then local pt = turtle.enableGPS()
Config.load('gps', values) if pt then
self.grid:setValues(values.home and { values.home } or { }) config.home = pt
Config.update('gps', config)
self.grid:setValues({ config.home })
self.grid:draw() self.grid:draw()
self:emit({ type = 'success_message', message = 'Location set' }) self:emit({ type = 'success_message', message = 'Location set' })
else else
self:emit({ type = 'error_message', message = 'Unable to determine location' }) self:emit({ type = 'error_message', message = 'Unable to determine location' })
end end
return true return true
elseif event.type == 'checkbox_change' then
config.destructive = event.checked
Config.update('gps', config)
elseif event.type == 'gps_clear' then elseif event.type == 'gps_clear' then
fs.delete('usr/config/gps') fs.delete('usr/config/gps')
self.grid:setValues({ }) self.grid:setValues({ })
self.grid:draw() self.breaking:reset()
self:draw()
return true return true
end end
end end