Advertisement
Guest User

nav.lua

a guest
Feb 22nd, 2020
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.08 KB | None | 0 0
  1. rednet.open('back')
  2. x = 0
  3. y = 0
  4. tolerance = 25
  5. conv = 57.2958
  6.  
  7. function getGPS()
  8.     local x, y, z = gps.locate(5)
  9.     return x, z
  10. end
  11.  
  12. function check(x)
  13.     x = x + 5
  14. end
  15.  
  16. function getPoint(str)
  17.     rednet.broadcast(str, 'navSys')
  18.     local ip, tempX = rednet.receive('navSys', 3)
  19.     local ip, tempZ = rednet.receive('navSys', 3)
  20.     return tempX, tempZ
  21. end
  22.  
  23. function getDistance(a, b)
  24.     x1, z1 = 0
  25.     if a == 'self' then x1, z1 = getGPS()
  26.     else x1, z1 = getPoint(a) end
  27.     if type(x1) == "nil" then return 'error' end
  28.     x2, z2 = getPoint(b)
  29.     x = 0 + (x1 - x2)
  30.     z = 0 - (z1 - z2)
  31.     h = math.floor(((x*x) + (z*z))^0.5+0.5)
  32.     return h
  33. end
  34.  
  35. function getAngle(a, b)
  36.     x1, z1 = 0
  37.     if a == 'self' then x1, z1 = getGPS()
  38.     else x1, z1 = getPoint(a) end
  39.     if type(x1) == "nil" then return 'error' end
  40.     x2, z2 = getPoint(b)
  41.     x = 0 + (x1 - x2)
  42.     z = 0 - (z1 - z2)
  43.     local num = math.floor((conv * math.atan(x/z) + 0.5))
  44.     if z > 0 then num = num + 180 end
  45.     if num < 1 then num = num + 360
  46.     elseif num > 360 then num = num - 360 end
  47.     return num
  48. end
  49.  
  50. waypoints = {}
  51. wp = ""
  52. while wp ~= "END" do
  53.     wp = read()
  54.     if wp ~= "END" then
  55.         table.insert(waypoints, wp)
  56.     end
  57. end
  58.  
  59. for i=1, #waypoints do
  60.     if i ~= 1 then
  61.         angle = getAngle(waypoints[i-1], waypoints[i])
  62.         distance = getDistance(waypoints[i-1], waypoints[i])
  63.         if string.len(waypoints[i]) == 7 then
  64.             str = '  | '..distance..'m, '..angle..'DEG'
  65.         else
  66.             str = ' | '..distance..'m, '..angle..'DEG'
  67.         end
  68.         print(i..': '..waypoints[i]..str)
  69.     else
  70.         print(i..': '..waypoints[i])
  71.     end
  72. end
  73. read()
  74.  
  75.  
  76. i = 1
  77. while i <= #waypoints do
  78.     a = getAngle('self', waypoints[i])
  79.     d = getDistance('self', waypoints[i])
  80.     term.clear()
  81.     term.setCursorPos(1, 1)
  82.     print('NEXT WPT: '..waypoints[i])
  83.     if a == 'error' or d == 'error' then
  84.         print('Unable to locate GPS')
  85.     else
  86.         print(d..'m, '..a..'DEG')
  87.         if d <= tolerance then i = i+1 end
  88.     end
  89. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement