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