Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Enhanced GPS Client Program with Fixed Satellite IDs and Continuous Live Feedback
- -- Define the satellite IDs and their coordinates
- local satellites = {
- {id = 544, x = 100, y = 64, z = 100}, -- Example satellite 1
- {id = 546, x = 200, y = 64, z = 200}, -- Example satellite 2
- {id = 547, x = 300, y = 64, z = 300} -- Example satellite 3
- }
- -- Function to display status messages on the screen
- local function displayStatus(message)
- term.clear()
- term.setCursorPos(1, 1)
- print(message)
- end
- -- Function to request data from fixed satellite IDs
- local function getSatelliteData()
- rednet.open("right") -- Ensure modem is on the correct side
- local satelliteData = {}
- displayStatus("Requesting data from satellites...")
- for _, satellite in ipairs(satellites) do
- rednet.send(satellite.id, "request", "GPS")
- local id, message = rednet.receive(5) -- Wait for 5 seconds for response
- if message then
- local data = textutils.unserializeJSON(message)
- if data and data.id == satellite.id then
- table.insert(satelliteData, data)
- displayStatus("Received data from satellite ID: " .. data.id)
- end
- else
- displayStatus("No response from satellite ID: " .. satellite.id)
- end
- end
- return satelliteData
- end
- -- Function to calculate the position based on received satellite data
- local function calculatePosition(satelliteData)
- if #satelliteData < 3 then
- return nil
- end
- -- Average positions (simple method)
- local avgX, avgY, avgZ = 0, 0, 0
- for _, data in ipairs(satelliteData) do
- avgX = avgX + data.x
- avgY = avgY + data.y
- avgZ = avgZ + data.z
- end
- avgX = avgX / #satelliteData
- avgY = avgY / #satelliteData
- avgZ = avgZ / #satelliteData
- return avgX, avgY, avgZ
- end
- -- Main function to continuously update the client status
- local function main()
- while true do
- displayStatus("Locating position...")
- local satelliteData = getSatelliteData()
- local x, y, z = calculatePosition(satelliteData)
- if x and y and z then
- displayStatus("Position determined:\nX: " .. x .. "\nY: " .. y .. "\nZ: " .. z)
- else
- displayStatus("Unable to determine position.\n\n" ..
- "Satellite Data:\n" ..
- "Received: " .. #satelliteData .. "/" .. #satellites .. "\n" ..
- "Waiting for more data...")
- end
- sleep(10) -- Update every 10 seconds, adjust as needed
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement