Advertisement
Alexr360

GPS Mobile

Feb 24th, 2024 (edited)
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.88 KB | None | 0 0
  1. local function clearScreen()
  2.   term.clear()
  3.   term.setCursorPos(1, 1)
  4. end
  5.  
  6. local function calculateVelocity(x1, y1, z1, x2, y2, z2, time)
  7.   -- Calculate the change in distance
  8.   local dx = x2 - x1
  9.   local dy = y2 - y1
  10.   local dz = z2 - z1
  11.  
  12.   -- Calculate total distance traveled
  13.   local distance = math.sqrt(dx*dx + dy*dy + dz*dz)
  14.  
  15.   -- Calculate velocity (distance / time)
  16.   local velocity = distance / time
  17.  
  18.   return velocity
  19. end
  20.  
  21. -- Initialize previous coordinates and time
  22. local prevX, prevY, prevZ = gps.locate()
  23. local prevTime = os.clock()
  24. local prevVelocities = {}
  25.  
  26. while true do
  27.   -- Get the current GPS location
  28.   local x, y, z = gps.locate()
  29.  
  30.   -- Clear the terminal
  31.   clearScreen()
  32.  
  33.   -- Print the coordinates
  34.   print("X: " .. x)
  35.   print("Y: " .. y)
  36.   print("Z: " .. z)
  37.  
  38.   -- Calculate time difference
  39.   local currentTime = os.clock()
  40.   local timeDifference = currentTime - prevTime
  41.  
  42.   -- Calculate velocity
  43.   local velocity = calculateVelocity(prevX, prevY, prevZ, x, y, z, timeDifference)
  44.  
  45.   -- Update previous coordinates and time
  46.   prevX, prevY, prevZ = x, y, z
  47.   prevTime = currentTime
  48.  
  49.   -- Add current velocity to the list of previous velocities
  50.   table.insert(prevVelocities, velocity)
  51.  
  52.   -- Keep only the last 5 velocities
  53.   if #prevVelocities > 5 then
  54.     table.remove(prevVelocities, 1)
  55.   end
  56.  
  57.   -- Calculate the average velocity
  58.   local totalVelocities = 0
  59.   for _, v in ipairs(prevVelocities) do
  60.     totalVelocities = totalVelocities + v
  61.   end
  62.   local averageVelocity = totalVelocities / #prevVelocities
  63.  
  64.   -- Round the current velocity to 3 decimals
  65.   local roundedVelocity = string.format("%.3f", velocity)
  66.  
  67.   -- Print velocity and average velocity
  68.   print("Velocity: " .. roundedVelocity)
  69.   print("Average Velocity (last 5): " .. averageVelocity)
  70.  
  71.   -- Wait for a second before updating
  72.   os.sleep(0.1)
  73. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement