Advertisement
Guest User

nav.lua

a guest
Feb 22nd, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.05 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.     if a == 'self' then x1, z1 = getGPS()
  25.     else x1, z1 = getPoint(a) end
  26.     if check(x1) then return 'error' end
  27.     x2, z2 = getPoint(b)
  28.     x = 0 + (x1 - x2)
  29.     z = 0 - (z1 - z2)
  30.     h = math.floor(((x*x) + (z*z))^0.5+0.5)
  31.     return h
  32. end
  33.  
  34. function getAngle(a, b)
  35.     if a == 'self' then x1, z1 = getGPS()
  36.     else x1, z1 = getPoint(a) end
  37.     if check(x1) then return 'error' end
  38.     x2, z2 = getPoint(b)
  39.     x = 0 + (x1 - x2)
  40.     z = 0 - (z1 - z2)
  41.     local num = math.floor((conv * math.atan(x/z) + 0.5))
  42.     if z > 0 then num = num + 180 end
  43.     if num < 1 then num = num + 360
  44.     elseif num > 360 then num = num - 360 end
  45.     return num
  46. end
  47.  
  48. waypoints = {}
  49. wp = ""
  50. while wp ~= "END" do
  51.     wp = read()
  52.     if wp ~= "END" then
  53.         table.insert(waypoints, wp)
  54.     end
  55. end
  56.  
  57. for i=1, #waypoints do
  58.     if i ~= 1 then
  59.         angle = getAngle(waypoints[i-1], waypoints[i])
  60.         distance = getDistance(waypoints[i-1], waypoints[i])
  61.         if string.len(waypoints[i]) == 7 then
  62.             str = '  | '..distance..'m, '..angle..'DEG'
  63.         else
  64.             str = ' | '..distance..'m, '..angle..'DEG'
  65.         end
  66.         print(i..': '..waypoints[i]..str)
  67.     else
  68.         print(i..': '..waypoints[i])
  69.     end
  70. end
  71. read()
  72.  
  73.  
  74. i = 1
  75. while i <= #waypoints do
  76.     a = getAngle('self', waypoints[i])
  77.     d = getDistance('self', waypoints[i])
  78.     term.clear()
  79.     term.setCursorPos(1, 1)
  80.     print('NEXT WPT: '..waypoints[i])
  81.     if a == 'error' or d == 'error' then
  82.         print('Unable to locate GPS')
  83.         sleep(1)
  84.     else
  85.         print(d..'m, '..a..'DEG')
  86.         if d <= tolerance then i = i+1 end
  87.     end
  88. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement