Advertisement
DOGGYWOOF

Untitled

Aug 24th, 2024 (edited)
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. -- GPS Client Program to Show Its Own Location Based on Satellite Data
  2.  
  3. -- Define the satellite IDs
  4. local satellites = {
  5. 536, -- Example satellite 1
  6. 537, -- Example satellite 2
  7. 538 -- 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 _, satelliteID in ipairs(satellites) do
  25. rednet.send(satelliteID, "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 == satelliteID 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: " .. satelliteID)
  36. end
  37. end
  38.  
  39. return satelliteData
  40. end
  41.  
  42. -- Function to calculate the position of the client based on received satellite data
  43. local function calculatePosition(satelliteData)
  44. if #satelliteData < 3 then
  45. return nil
  46. end
  47.  
  48. -- Simple triangulation to estimate position based on received satellites
  49. -- This example uses an average of coordinates from satellites
  50. local avgX, avgY, avgZ = 0, 0, 0
  51. for _, data in ipairs(satelliteData) do
  52. avgX = avgX + data.x
  53. avgY = avgY + data.y
  54. avgZ = avgZ + data.z
  55. end
  56.  
  57. avgX = avgX / #satelliteData
  58. avgY = avgY / #satelliteData
  59. avgZ = avgZ / #satelliteData
  60.  
  61. return avgX, avgY, avgZ
  62. end
  63.  
  64. -- Main function to continuously update the client status
  65. local function main()
  66. while true do
  67. displayStatus("Locating position...")
  68.  
  69. local satelliteData = getSatelliteData()
  70. local x, y, z = calculatePosition(satelliteData)
  71.  
  72. if x and y and z then
  73. displayStatus("Client Position:\nX: " .. x .. "\nY: " .. y .. "\nZ: " .. z)
  74. else
  75. displayStatus("Unable to determine position.\n\n" ..
  76. "Satellite Data:\n" ..
  77. "Received: " .. #satelliteData .. "/" .. #satellites .. "\n" ..
  78. "Waiting for more data...")
  79. end
  80.  
  81. sleep(10) -- Update every 10 seconds, adjust as needed
  82. end
  83. end
  84.  
  85. main()
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement