Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- GPS Client Program to Show Its Own Location Based on Satellite Data
- -- Define the satellite IDs
- local satellites = {
- 536, -- Example satellite 1
- 537, -- Example satellite 2
- 538 -- 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 _, satelliteID in ipairs(satellites) do
- rednet.send(satelliteID, "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 == satelliteID then
- table.insert(satelliteData, data)
- displayStatus("Received data from satellite ID: " .. data.id)
- end
- else
- displayStatus("No response from satellite ID: " .. satelliteID)
- end
- end
- return satelliteData
- end
- -- Function to calculate the position of the client based on received satellite data
- local function calculatePosition(satelliteData)
- if #satelliteData < 3 then
- return nil
- end
- -- Simple triangulation to estimate position based on received satellites
- -- This example uses an average of coordinates from satellites
- 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("Client Position:\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