gps server record positions

This commit is contained in:
kepler155c@gmail.com
2019-01-05 01:01:23 -05:00
parent 2539fce815
commit f148bee303

View File

@@ -6,6 +6,7 @@ local args = { ... }
local gps = _G.gps
local os = _G.os
local peripheral = _G.peripheral
local term = _G.term
local turtle = _G.turtle
local vector = _G.vector
@@ -65,6 +66,10 @@ local function server()
local modems = Config.load('gpsServer')
local computers = { }
if #modems == 0 then
error('Missing usr/config/gpsServer configuration file')
end
for k, modem in pairs(modems) do
Util.merge(modem, peripheral.wrap(k))
modem.open(gps.CHANNEL_GPS)
@@ -74,27 +79,29 @@ local function server()
local e, p1, p2, p3, p4, p5 = os.pullEvent( "modem_message" )
if e == "modem_message" then
-- We received a message from a modem
local sSide, channel, computerId, sMessage, distance = p1, p2, p3, p4, p5
local side, channel, computerId, sMessage, distance = p1, p2, p3, p4, p5
if channel == gps.CHANNEL_GPS and sMessage == "PING" then
-- We received a ping message on the GPS channel, send a response
local computer = memoize(computers, computerId, function() return { } end)
local modem = modems[sSide]
table.insert(computer, {
position = vector.new(modem.x, modem.y, modem.z), distance = distance
})
if #computer == 4 then
local pt = GPS.trilaterate(computer)
if pt then
positions[computerId] = pt
term.clear()
for k,v in pairs(positions) do
Util.print('ID: %d: %s %s %s', k, v.x, v.y, v.z)
local modem = modems[side]
if modem then
local computer = memoize(computers, computerId, function() return { } end)
table.insert(computer, {
position = vector.new(modem.x, modem.y, modem.z), distance = distance
})
if #computer == 4 then
local pt = GPS.trilaterate(computer)
if pt then
positions[computerId] = pt
term.clear()
for k,v in pairs(positions) do
Util.print('ID: %d: %s %s %s', k, v.x, v.y, v.z)
end
end
computers[computerId] = nil
end
for _, modem in pairs(modems) do
modem.transmit( computerId, gps.CHANNEL_GPS, { modem.x, modem.y, modem.z })
end
computers[computerId] = nil
end
for _, modem in pairs(modems) do
modem.transmit( computerId, gps.CHANNEL_GPS, { modem.x, modem.y, modem.z })
end
end
end