fix: Optimize GPS position retrieval with reduced timeout and background retry logic
This commit is contained in:
40
turtle.lua
40
turtle.lua
@@ -54,14 +54,12 @@ print("ID: " .. os.getComputerID())
|
||||
|
||||
-- GPS Functions
|
||||
local function updatePosition()
|
||||
print("Requesting GPS position...")
|
||||
local x, y, z = gps.locate(10) -- Increased timeout to 10 seconds
|
||||
-- Quick GPS check with short timeout
|
||||
local x, y, z = gps.locate(2) -- 2 second timeout
|
||||
if x then
|
||||
state.position = {x = math.floor(x), y = math.floor(y), z = math.floor(z)}
|
||||
print("GPS position: " .. x .. ", " .. y .. ", " .. z)
|
||||
return true
|
||||
end
|
||||
print("GPS timeout - no position received")
|
||||
return false
|
||||
end
|
||||
|
||||
@@ -357,7 +355,8 @@ end
|
||||
function broadcastStatus()
|
||||
updateFuel()
|
||||
updateInventory()
|
||||
updatePosition()
|
||||
-- Don't update position on every broadcast to avoid GPS delays
|
||||
-- Position will be updated by movement functions
|
||||
|
||||
modem.transmit(STATUS_CHANNEL, CHANNEL_RECEIVE, {
|
||||
type = "status",
|
||||
@@ -562,32 +561,23 @@ else
|
||||
print("Wireless modem: OK")
|
||||
end
|
||||
|
||||
print("Attempting to get GPS position (this may take 10 seconds)...")
|
||||
local gpsAttempts = 0
|
||||
local maxGpsAttempts = 3
|
||||
|
||||
while not state.position and gpsAttempts < maxGpsAttempts do
|
||||
gpsAttempts = gpsAttempts + 1
|
||||
print("GPS attempt " .. gpsAttempts .. " of " .. maxGpsAttempts .. "...")
|
||||
updatePosition()
|
||||
if not state.position then
|
||||
print("Waiting 2 seconds before retry...")
|
||||
sleep(2)
|
||||
end
|
||||
print("Attempting quick GPS check...")
|
||||
-- Single quick GPS attempt (5 second timeout instead of 10)
|
||||
local x, y, z = gps.locate(5)
|
||||
if x then
|
||||
state.position = {x = math.floor(x), y = math.floor(y), z = math.floor(z)}
|
||||
print("GPS: OK - Position acquired!")
|
||||
else
|
||||
print("GPS: Not available - will retry in background")
|
||||
print("Position tracking disabled for now.")
|
||||
end
|
||||
|
||||
updateFuel()
|
||||
|
||||
if not state.position then
|
||||
print("WARNING: No GPS signal after " .. maxGpsAttempts .. " attempts!")
|
||||
print("Make sure GPS hosts are set up correctly.")
|
||||
print("Limited functionality available - position tracking disabled.")
|
||||
else
|
||||
print("GPS: OK - Position acquired!")
|
||||
end
|
||||
|
||||
print("Ready! Waiting for commands...")
|
||||
print("Broadcasting initial status...")
|
||||
broadcastStatus()
|
||||
print("Turtle " .. os.getComputerID() .. " is online!")
|
||||
|
||||
-- Main execution loop
|
||||
parallel.waitForAny(
|
||||
|
||||
Reference in New Issue
Block a user