Advertisement
PHOBOSS

GPS_lib.lua

Apr 14th, 2022 (edited)
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. local s_utils = require 'swarm_utilities'
  2. local GPS ={}
  3.  
  4.  
  5. local floor, sqrt, abs = math.floor, math.sqrt, math.abs
  6. local arr_length = s_utils.arr_length
  7. local trunc,vec_trunc = s_utils.trunc,s_utils.vec_trunc
  8. local cross,len,dot,add,sub,mul,norm = s_utils.cross,s_utils.len,s_utils.dot,s_utils.add,s_utils.sub,s_utils.mul,s_utils.norm
  9.  
  10.  
  11. local function trilaterate(A, B, C)
  12. local a2b = {x=B.x-A.x, y=B.y-A.y, z=B.z-A.z}
  13. local a2c = {x=C.x-A.x, y=C.y-A.y, z=C.z-A.z}
  14. if abs(dot(norm(a2b), norm(a2c))) > 0.999 then return nil end
  15. local d, ex = len(a2b), norm(a2b)
  16. local i = dot(ex, a2c)
  17. local exMi = mul(ex, i)
  18. local ey = norm(sub(a2c, mul(ex, i)))
  19. local j, ez = dot(ey, a2c), cross(ex, ey)
  20. local r1, r2, r3 = A.d, B.d, C.d
  21. local x = (r1^2 - r2^2 + d^2) / (2*d)
  22. local y = (r1^2 - r3^2 - x^2 + (x-i)^2 + j^2) / (2*j)
  23. local result = add(A, add(mul(ex, x), mul(ey, y)))
  24. local zSquared = r1^2 - x^2 - y^2
  25. if zSquared > 0 then
  26. local z = sqrt( zSquared )
  27. local result1 = add(result, mul(ez, z))
  28. local result2 = sub(result, mul(ez, z))
  29. local rnd1, rnd2 = result1,result2
  30. if rnd1.x ~= rnd2.x or rnd1.y ~= rnd2.y or rnd1.z ~= rnd2.z then
  31. --print("rnd1: ",rnd1.x,rnd1.y,rnd1.z)
  32. --print("rnd2: ",rnd2.x,rnd2.y,rnd2.z)
  33. return rnd1, rnd2
  34. else
  35. --print("rnd1: ",rnd1.x,rnd1.y,rnd1.z)
  36. return rnd1
  37. end
  38. end
  39. --print("result: ",result.x,result.y,result.z)
  40. return result
  41. end
  42.  
  43. local function narrow(p1, p2, fix)
  44. local d1 = abs(len(sub(p1, fix))-fix.d)
  45. local d2 = abs(len(sub(p2, fix))-fix.d)
  46. if abs(d1-d2) < 0.01 then
  47. return p1, p2
  48. elseif d1 < d2 then
  49. --print("p1: ",p1.x,p1.y,p1.z)
  50. return p1,nil
  51. else
  52. --print("p2: ",p2.x,p2.y,p2.z)
  53. return p2,nil
  54. end
  55. end
  56.  
  57. function GPS.getGPSPos(gpsT) --**********************--
  58. local fixes = {}
  59. local pos1, pos2 = nil, nil
  60. for addr,fix in pairs(gpsT) do
  61. if fix.d == 0 then
  62. pos1, pos2 = {x=fix.x, y=fix.y, z=fix.z}, nil
  63. else
  64. table.insert(fixes, fix)
  65. --print(addr,fix.x,fix.y,fix.z,fix.d)
  66. end
  67. end
  68. if #fixes >= 4 then
  69. if not pos1 then
  70. pos1, pos2 = trilaterate(fixes[1], fixes[2], fixes[3])
  71. end
  72. if pos1 and pos2 then
  73. for f=4,#fixes do
  74. pos1, pos2 = narrow(pos1, pos2, fixes[f])
  75. if pos1 and not pos2 then break end
  76. end
  77. end
  78. end
  79.  
  80. if pos1 and pos2 then
  81. return nil
  82. elseif pos1 then
  83. local c = pos1
  84. return {x=c.x,y=c.y,z=c.z}
  85. else
  86. return nil
  87. end
  88. end
  89.  
  90.  
  91.  
  92. function GPS.refreshGPSTable(gpsT,refreshCounter,refreshInterval) --************--
  93. --print("refreshCounter: ",refreshCounter,"refreshInterval: ",refreshInterval)
  94. if refreshCounter >= refreshInterval then return 0,{} end
  95. return refreshCounter + 1,gpsT
  96. end
  97.  
  98.  
  99. function GPS.add2GPSTable(r_addr,x,y,z,dist,gpsT) --************--
  100. --print("add2GPSTable")
  101. if arr_length(gpsT) < 7 then gpsT[r_addr] = {x=x,y=y,z=z,d=dist} end
  102. end
  103.  
  104. return GPS
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement