Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function getAccessPoints()
- local component = require("component")
- local event = require("event")
- local t = component.list()
- local accessPoints = {}
- local i = 0
- for address,componentType in pairs(t) do
- if(componentType == "access_point") then
- accessPoints[i] = address
- i = i + 1
- end
- end
- return accessPoints
- end
- function turnOffRepeaters(accessPoints)
- local component = require("component")
- for i=0, #accessPoints do
- print(accessPoints[i])
- component.proxy(accessPoints[i]).setRepeater(false)
- component.proxy(accessPoints[i]).setStrength(0)
- print(component.proxy(accessPoints[i]).isRepeater())
- end
- end
- function testPointEquality(p1,p2)
- if(p1[2] == p2[2] and p1[3] == p2[3] and p1[4] == p2[4]) then
- return true
- else return false
- end
- end
- function getGPSCoordinates(timeOut)
- local computer = require("computer")
- local timer = computer.uptime()
- while(computer.uptime() - timer < timeOut*4) do
- local event = require("event")
- local points = {}
- local eventType, recieverAddress, senderAddress, port, distenace, x,y,z = event.pull(timeOut, "modem_message")
- if(eventType ~= nil) then
- points[1] = {distenace, x,y,z}
- --print(points[1][1])
- else
- return nil
- end
- while #points < 4 and computer.uptime() - timer < timeOut*4 do
- local eventType, recieverAddress, senderAddress, port, distenace, x,y,z = event.pull(timeOut, "modem_message")
- local tmp = {distenace, x,y,z}
- local same = false
- for i=1, #points do
- if(testPointEquality(points[i], tmp)) then
- same = true
- --print("Same point!")
- break
- end
- end
- if(not same) then
- points[#points+1] = tmp
- --print("Point added:".. tostring(tmp[1]))
- end
- end
- return points
- end
- end
- function split(inputstr, sep)
- if(inputstr == nil or inputstr == "") then
- return nil
- end
- if sep == nil then
- sep = ","
- end
- local t={} ; i=1
- for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
- t[i] = str
- i = i + 1
- end
- return t
- end
- function addVector(p1,p2)
- --p1 + p2
- return {p1[1]+p2[1], p1[2]+p2[2], p1[3]+p2[3]}
- end
- function subtractVector(p1,p2)
- --p1 - p2
- return {p1[1]-p2[1], p1[2]-p2[2], p1[3]-p2[3]}
- end
- function scaleVector(p1, scalar)
- --p1 * scalar
- return {p1[1]*scalar, p1[2]*scalar, p1[3]*scalar}
- end
- function dotVector(p1,p2)
- return p1[1]*p2[1] + p1[2]*p2[2] + p1[3]*p2[3]
- end
- function crossVector(p1,p2)
- return {p1[2]*p2[3] - p1[3]*p2[2], p1[3]*p2[1] - p1[1]*p2[3], p1[1]*p2[2] - p1[2]*p2[1]}
- end
- function magnitudeVector(p1)
- return math.sqrt(math.pow(p1[1], 2) + math.pow(p1[2], 2) + math.pow(p1[3], 2))
- end
- function normalizeVector(p1)
- return p1/magnitudeVector(p1)
- end
- function rotateVectorZAxis(p1,theta)
- return {p1[1]*math.cos(theta) - p1[2]*math.sin(theta), p1[1]*math.sin(theta) + p1[2]*math.cos(theta), p1[3]}
- end
- function rotateVectorXAxis(p1,theta)
- return {p1[1], p1[2]*math.cos(theta) - p1[3]*math.sin(theta), p1[2]*math.sin(theta) + p1[3]*math.cos(theta)}
- end
- function rotateVectorYAxis(p1,theta)
- return {p1[1]*math.cos(theta) + p1[3]*math.sin(theta), p1[2], p1[3]*math.cos(theta) - p1[1]*math.sin(theta)}
- end
- function tostringVector(p1)
- return tostring(p1[1]) .. "," .. tostring(p1[2]) .. "," .. tostring(p1[3])
- end
- function tonumberVector(p1)
- return {tonumber(p1[1]), tonumber(p1[2]), tonumber(p1[3])}
- end
- function distanceBetweenPoints(p1,p2)
- return math.sqrt(math.pow(p1[1]-p2[1],2) + math.pow(p1[2]-p2[2],2) + math.pow(p1[3]-p2[3],2))
- end
- function findPossiblePoints2D(d1,p1,d2,p2)
- local distance = distanceBetweenPoints(p1,p2)
- local a = (math.pow(d1, 2) - math.pow(d2, 2) + math.pow(distance, 2)) / (2 * distance)
- --print(a)
- local intersect = addVector(p1,scaleVector(scaleVector(subtractVector(p2,p1), a), (1/distance)))
- --print(tostringVector(intersect))
- local h = math.sqrt(math.pow(d1, 2) - math.pow(a, 2))
- --print(h)
- local x_plus = intersect[1] + ((h*(p2[2]-p1[2]))/distance)
- local x_minus = intersect[1] - ((h*(p2[2]-p1[2]))/distance)
- local y_plus = intersect[2] + ((h*(p2[1]-p1[1]))/distance)
- local y_minus = intersect[2] - ((h*(p2[1]-p1[1]))/distance)
- return {x_plus,y_minus,0}, {x_minus,y_plus,0}
- end
- function findPossiblePoints3D(d1,p1,d2,p2,d3,p3)
- --print(tostringVector(p1)..";"..tostringVector(p2)..";"..tostringVector(p3))
- --offset vectors such that p1 is at 0,0,0
- local offset = scaleVector(p1, -1)
- --print("Offset: "..tostringVector(offset))
- local p1p = addVector(p1, offset)
- --print("p1p: "..tostringVector(p1p))
- local p2p = addVector(p2, offset)
- --print("p2p: "..tostringVector(p2p))
- local p3p = addVector(p3, offset)
- --print("p3p: "..tostringVector(p3p))
- --print("Offset vectors: "..tostringVector(p1p).."; "..tostringVector(p2p).."; "..tostringVector(p3p))
- --find the equation of the plane
- local p12 = subtractVector(p2p,p1p)
- --print("p12: "..tostringVector(p12))
- local p13 = subtractVector(p3p,p1p)
- --print("p13: "..tostringVector(p13))
- local plane = crossVector(p12,p13)
- --print("Plane: "..tostringVector(plane))
- -- checked up to here --
- --rotate first about the y axis
- local angle1 = math.atan(plane[1]/plane[3])
- --print("Angle1: "..angle1)
- local plane_ry = rotateVectorYAxis(plane, angle1)
- --print("plane_ry: "..tostringVector(plane_ry))
- local p1p_ry = rotateVectorYAxis(p1p, angle1)
- --print("p1p_ry: "..tostringVector(p1p_ry))
- local p2p_ry = rotateVectorYAxis(p2p, angle1)
- --print("p2p_ry: "..tostringVector(p2p_ry))
- local p3p_ry =rotateVectorYAxis(p3p, angle1)
- --print("p3p_ry: "..tostringVector(p3p_ry))
- --rotate second about the x axis
- local angle2 = math.atan(plane_ry[2]/plane_ry[3])
- --print("Angle2: "..angle2)
- local plane_ryx = rotateVectorXAxis(plane_ry, angle2)
- --print("plane_ryx: "..tostringVector(plane_ryx))
- local p1p_ryx = rotateVectorXAxis(p1p_ry, angle2)
- --print("p1p_ryx: "..tostringVector(p1p_ryx))
- local p2p_ryx = rotateVectorXAxis(p2p_ry, angle2)
- --print("p2p_ryx: "..tostringVector(p2p_ryx))
- local p3p_ryx = rotateVectorXAxis(p3p_ry, angle2)
- --print("p3p_ryx: "..tostringVector(p3p_ryx))
- --at this point, the plane should lie such that all z=0
- --rotate plane about the z axis such that p2 is at x = 0
- local angle3 = (-1) * math.atan(p2p_ryx[2]/p2p_ryx[1])
- --print("Angle3: "..angle3)
- local plane_ryxz = rotateVectorZAxis(plane_ryx, angle3)
- local p1p_ryxz = rotateVectorZAxis(p1p_ryx, angle3)
- local p2p_ryxz = rotateVectorZAxis(p2p_ryx, angle3)
- local p3p_ryxz = rotateVectorZAxis(p3p_ryx, angle3)
- --at this point, p1 should be at 0,0,0; p2 should be such that y=0,z=0; and p3 should be such that z=0
- --calculations for finding rotated, offset points
- local xp_ryxz = (math.pow(d1,2) - math.pow(d2,2) + math.pow(p2p_ryxz[1],2))/(2*p2p_ryxz[1])
- local yp_ryxz = ((math.pow(d1,2) - math.pow(d3,2) + math.pow(p3p_ryxz[1],2) + math.pow(p3p_ryxz[2],2))/(2*p3p_ryxz[2])) - ((p3p_ryxz[1]/p3p_ryxz[2])*xp_ryxz)
- local z1p_ryxz = math.sqrt(math.pow(d1,2)-math.pow(xp_ryxz,2)-math.pow(yp_ryxz,2))
- local z2p_ryxz = (-1) * math.sqrt(math.pow(d1,2)-math.pow(xp_ryxz,2)-math.pow(yp_ryxz,2))
- --possible rotated, offset points
- local point1p_ryxz = {xp_ryxz, yp_ryxz, z1p_ryxz}
- local point2p_ryxz = {xp_ryxz, yp_ryxz, z2p_ryxz}
- --rotate back around the z axis
- plane_ryx = rotateVectorZAxis(plane_ryxz, (-1)*angle3)
- local point1p_ryx = rotateVectorZAxis(point1p_ryxz, (-1)*angle3)
- local point2p_ryx = rotateVectorZAxis(point2p_ryxz, (-1)*angle3)
- --rotate back around the x axis
- plane_ry = rotateVectorXAxis(plane_ryx, (-1)*angle2)
- local point1p_ry = rotateVectorXAxis(point1p_ryx, (-1)*angle2)
- local point2p_ry = rotateVectorXAxis(point2p_ryx, (-1)*angle2)
- --rotate back around the y axis
- plane = rotateVectorYAxis(plane_ry, (-1)*angle1)
- local point1p = rotateVectorYAxis(point1p_ry, (-1)*angle1)
- local point2p = rotateVectorYAxis(point2p_ry, (-1)*angle1)
- --print(tostringVector(plane))
- --remove offset
- local point1 = addVector(point1p, scaleVector(offset,-1))
- local point2 = addVector(point2p, scaleVector(offset,-1))
- --print("Possible points are either: "..tostringVector(point1).." or "..tostringVector(point2))
- return point1, point2
- end
- function narrow(p1,p2,d4,p4)
- if(distanceBetweenPoints(p1,p4) == d4) then
- return p1
- end
- if(distanceBetweenPoints(p2,p4) == d4) then
- return p2
- end
- end
- function getLocation(timeout)
- local timeout = timeout or 2
- local coords = getGPSCoordinates(timeout)
- if(coords == nil) then
- print("could not obtain")
- return nil
- end
- if(#coords < 4) then
- print("Not enough points")
- return nil
- end
- local d1 = coords[1][1]
- local p1 = {coords[1][2],coords[1][3],coords[1][4]}
- local d2 = coords[2][1]
- local p2 = {coords[2][2],coords[2][3],coords[2][4]}
- local d3 = coords[3][1]
- local p3 = {coords[3][2],coords[3][3],coords[3][4]}
- local d4 = coords[4][1]
- local p4 = {coords[4][2],coords[4][3],coords[4][4]}
- local point1, point2 = findPossiblePoints3D(d1,p1,d2,p2,d3,p3)
- local location = narrow(point1,point2,d4,p4)
- return location
- end
- local term = require("term")
- local component = require("component")
- local event = require("event")
- local fileSystem = require("filesystem")
- local shell = require("shell")
- local accessPoints = getAccessPoints()
- local gps_port = 3665
- local signal_strength = 400
- local sleep = 0.001
- local hostCoordinates = {}
- component.modem.open(gps_port)
- if(fileSystem.exists(shell.resolve("gpsHostData"))) then
- local file = io.open(shell.resolve("gpsHostData"), "r")
- local i = 0
- for line in io.lines(shell.resolve("gpsHostData")) do
- hostCoordinates[i] = split(line,",")
- hostCoordinates = tonumberVector({hostCoordinates[i][2],hostCoordinates[i][3],hostCoordinates[i][4]})
- print(line)
- i=i+1
- end
- file:close()
- elseif(getLocation() ~= nil) then
- local location = getLocation()
- print(tostringVector(location))
- local file = io.open(shell.resolve("gpsHostData"), "a")
- local address = component.getPrimary("modem").address
- file:write(tostring(address)..","..tostringVector(location))
- print("File written")
- file:close()
- hostCoordinates = location
- else
- local file = io.open(shell.resolve("gpsHostData"), "a")
- print("Location data could not be obtained, please enter in (x,y,z) coords of modem: ")
- local tmp = term.read()
- hostCoordinates = tonumberVector(split(tmp,","))
- local address = component.getPrimary("modem").address
- file:write(tostring(address)..","..tmp)
- print("File written")
- file:close()
- end
- print(hostCoordinates[1]..type(hostCoordinates[1]))
- print(hostCoordinates[2]..type(hostCoordinates[2]))
- print(hostCoordinates[3]..type(hostCoordinates[3]))
- while true do
- component.modem.setStrength(signal_strength)
- component.modem.broadcast(gps_port, hostCoordinates[1], hostCoordinates[2],hostCoordinates[3])
- os.sleep(sleep)
- end
- --[[
- if(getLocation() == nil) then
- local location = getLocation()
- print("Location of this station has been found to be: "..location)
- local file = io.open(shell.resolve("gpsHostData"), "a")
- file:write(tostring(component.getPrimary("modem").address)..","..tostringVector(location))
- hostCoordinates[0] = location
- else
- local file = io.open(shell.resolve("gpsHostData"), "a")
- print("Enter coordinates (x,y,z) for modem: ")
- local tmp = term.read()
- hostCoordinates[0] = split(tmp,",")
- file:write(tostring(component.getPrimary("modem").address)..","..tmp)
- file:close()
- end
- component.modem.open(gps_port)
- while true do
- io.write(i..": "..hostCoordinates[i][0].." "..hostCoordinates[i][1]..","..hostCoordinates[i][2]..","..hostCoordinates[i][3])
- component.modem.setStrength(signal_strength)
- component.modem.broadcast(gps_port, tonumber(hostCoordinates[0][1]),tonumber(hostCoordinates[0][2]),tonumber(hostCoordinates[0][3]))
- end
- ]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement