Advertisement
DOGGYWOOF

GPS

Aug 23rd, 2024 (edited)
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. -- Enhanced GPS Client Program with Fixed Satellite IDs and Continuous Live Feedback
  2.  
  3. -- Define the satellite IDs and their coordinates
  4. local satellites = {
  5. {id = 544, x = 100, y = 64, z = 100}, -- Example satellite 1
  6. {id = 546, x = 200, y = 64, z = 200}, -- Example satellite 2
  7. {id = 547, x = 300, y = 64, z = 300} -- Example satellite 3
  8. }
  9.  
  10. -- Function to display status messages on the screen
  11. local function displayStatus(message)
  12. term.clear()
  13. term.setCursorPos(1, 1)
  14. print(message)
  15. end
  16.  
  17. -- Function to request data from fixed satellite IDs
  18. local function getSatelliteData()
  19. rednet.open("right") -- Ensure modem is on the correct side
  20. local satelliteData = {}
  21.  
  22. displayStatus("Requesting data from satellites...")
  23.  
  24. for _, satellite in ipairs(satellites) do
  25. rednet.send(satellite.id, "request", "GPS")
  26. local id, message = rednet.receive(5) -- Wait for 5 seconds for response
  27.  
  28. if message then
  29. local data = textutils.unserializeJSON(message)
  30. if data and data.id == satellite.id then
  31. table.insert(satelliteData, data)
  32. displayStatus("Received data from satellite ID: " .. data.id)
  33. end
  34. else
  35. displayStatus("No response from satellite ID: " .. satellite.id)
  36. end
  37. end
  38.  
  39. return satelliteData
  40. end
  41.  
  42. -- Function to calculate the position based on received satellite data
  43. local function calculatePosition(satelliteData)
  44. if #satelliteData < 3 then
  45. return nil
  46. end
  47.  
  48. -- Average positions (simple method)
  49. local avgX, avgY, avgZ = 0, 0, 0
  50. for _, data in ipairs(satelliteData) do
  51. avgX = avgX + data.x
  52. avgY = avgY + data.y
  53. avgZ = avgZ + data.z
  54. end
  55.  
  56. avgX = avgX / #satelliteData
  57. avgY = avgY / #satelliteData
  58. avgZ = avgZ / #satelliteData
  59.  
  60. return avgX, avgY, avgZ
  61. end
  62.  
  63. -- Main function to continuously update the client status
  64. local function main()
  65. while true do
  66. displayStatus("Locating position...")
  67.  
  68. local satelliteData = getSatelliteData()
  69. local x, y, z = calculatePosition(satelliteData)
  70.  
  71. if x and y and z then
  72. displayStatus("Position determined:\nX: " .. x .. "\nY: " .. y .. "\nZ: " .. z)
  73. else
  74. displayStatus("Unable to determine position.\n\n" ..
  75. "Satellite Data:\n" ..
  76. "Received: " .. #satelliteData .. "/" .. #satellites .. "\n" ..
  77. "Waiting for more data...")
  78. end
  79.  
  80. sleep(10) -- Update every 10 seconds, adjust as needed
  81. end
  82. end
  83.  
  84. main()
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement