1
0
mirror of https://github.com/kepler155c/opus synced 2025-01-19 03:42:51 +00:00

GPS ambiguous position fix

This commit is contained in:
xAnavrins 2019-07-27 00:13:24 -04:00
parent b3efbc7438
commit c1430f6dac
2 changed files with 64 additions and 43 deletions

View File

@ -1,3 +1,5 @@
local Util = require('opus.util')
local GPS = { } local GPS = { }
local device = _G.device local device = _G.device
@ -89,19 +91,17 @@ end
-- end stock gps api -- end stock gps api
function GPS.trilaterate(tFixes) function GPS.trilaterate(tFixes)
local pos1, pos2 = trilaterate(tFixes[1], tFixes[2], tFixes[3]) local attemps = 0
for tFixes in Util.permutation(tFixes) do
if pos2 then attemps = attemps + 1
pos1, pos2 = narrow(pos1, pos2, tFixes[4]) local pos1, pos2 = trilaterate(tFixes[4], tFixes[3], tFixes[2])
if pos2 then
pos1, pos2 = narrow(pos1, pos2, tFixes[1])
end
if not pos2 then
return pos1, attemps
end
end end
if pos1 and pos2 then
print("Ambiguous position")
print("Could be "..pos1.x..","..pos1.y..","..pos1.z.." or "..pos2.x..","..pos2.y..","..pos2.z )
return
end
return pos1
end end
return GPS return GPS

View File

@ -46,10 +46,10 @@ function Util.tryTimes(attempts, f, ...)
end end
function Util.timer() function Util.timer()
local ct = os.clock() local ct = os.clock()
return function() return function()
return os.clock() - ct return os.clock() - ct
end end
end end
Util.Timer = Util.timer -- deprecate Util.Timer = Util.timer -- deprecate
@ -678,33 +678,33 @@ end
-- https://github.com/MightyPirates/OpenComputers -- https://github.com/MightyPirates/OpenComputers
function Util.parse(...) function Util.parse(...)
local params = table.pack(...) local params = table.pack(...)
local args = {} local args = {}
local options = {} local options = {}
local doneWithOptions = false local doneWithOptions = false
for i = 1, params.n do for i = 1, params.n do
local param = params[i] local param = params[i]
if not doneWithOptions and type(param) == "string" then if not doneWithOptions and type(param) == "string" then
if param == "--" then if param == "--" then
doneWithOptions = true -- stop processing options at `--` doneWithOptions = true -- stop processing options at `--`
elseif param:sub(1, 2) == "--" then elseif param:sub(1, 2) == "--" then
local key, value = param:match("%-%-(.-)=(.*)") local key, value = param:match("%-%-(.-)=(.*)")
if not key then if not key then
key, value = param:sub(3), true key, value = param:sub(3), true
end end
options[key] = value options[key] = value
elseif param:sub(1, 1) == "-" and param ~= "-" then elseif param:sub(1, 1) == "-" and param ~= "-" then
for j = 2, string.len(param) do for j = 2, string.len(param) do
options[string.sub(param, j, j)] = true options[string.sub(param, j, j)] = true
end end
else else
table.insert(args, param) table.insert(args, param)
end end
else else
table.insert(args, param) table.insert(args, param)
end end
end end
return args, options return args, options
end end
function Util.args(arg) function Util.args(arg)
@ -799,4 +799,25 @@ function Util.getOptions(options, args, ignoreInvalid)
return true, Util.size(rawOptions) return true, Util.size(rawOptions)
end end
-- https://www.lua.org/pil/9.3.html
function Util.permutation(tbl)
local function permgen(a, n)
if n == 0 then
coroutine.yield(a)
else
for i=1,n do
a[n], a[i] = a[i], a[n]
permgen(a, n - 1)
a[n], a[i] = a[i], a[n]
end
end
end
local co = coroutine.create(function() permgen(tbl, #tbl) end)
return function()
local _, res = coroutine.resume(co)
return res
end
end
return Util return Util