Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function clearScreen()
- term.clear()
- term.setCursorPos(1, 1)
- end
- local function calculateVelocity(x1, y1, z1, x2, y2, z2, time)
- -- Calculate the change in distance
- local dx = x2 - x1
- local dy = y2 - y1
- local dz = z2 - z1
- -- Calculate total distance traveled
- local distance = math.sqrt(dx*dx + dy*dy + dz*dz)
- -- Calculate velocity (distance / time)
- local velocity = distance / time
- return velocity
- end
- -- Initialize previous coordinates and time
- local prevX, prevY, prevZ = gps.locate()
- local prevTime = os.clock()
- local prevVelocities = {}
- while true do
- -- Get the current GPS location
- local x, y, z = gps.locate()
- -- Clear the terminal
- clearScreen()
- -- Print the coordinates
- print("X: " .. x)
- print("Y: " .. y)
- print("Z: " .. z)
- -- Calculate time difference
- local currentTime = os.clock()
- local timeDifference = currentTime - prevTime
- -- Calculate velocity
- local velocity = calculateVelocity(prevX, prevY, prevZ, x, y, z, timeDifference)
- -- Update previous coordinates and time
- prevX, prevY, prevZ = x, y, z
- prevTime = currentTime
- -- Add current velocity to the list of previous velocities
- table.insert(prevVelocities, velocity)
- -- Keep only the last 5 velocities
- if #prevVelocities > 5 then
- table.remove(prevVelocities, 1)
- end
- -- Calculate the average velocity
- local totalVelocities = 0
- for _, v in ipairs(prevVelocities) do
- totalVelocities = totalVelocities + v
- end
- local averageVelocity = totalVelocities / #prevVelocities
- -- Round the current velocity to 3 decimals
- local roundedVelocity = string.format("%.3f", velocity)
- -- Print velocity and average velocity
- print("Velocity: " .. roundedVelocity)
- print("Average Velocity (last 5): " .. averageVelocity)
- -- Wait for a second before updating
- os.sleep(0.1)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement