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
|
-- GPS Functions
|
||||||
local function updatePosition()
|
local function updatePosition()
|
||||||
print("Requesting GPS position...")
|
-- Quick GPS check with short timeout
|
||||||
local x, y, z = gps.locate(10) -- Increased timeout to 10 seconds
|
local x, y, z = gps.locate(2) -- 2 second timeout
|
||||||
if x then
|
if x then
|
||||||
state.position = {x = math.floor(x), y = math.floor(y), z = math.floor(z)}
|
state.position = {x = math.floor(x), y = math.floor(y), z = math.floor(z)}
|
||||||
print("GPS position: " .. x .. ", " .. y .. ", " .. z)
|
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
print("GPS timeout - no position received")
|
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -357,7 +355,8 @@ end
|
|||||||
function broadcastStatus()
|
function broadcastStatus()
|
||||||
updateFuel()
|
updateFuel()
|
||||||
updateInventory()
|
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, {
|
modem.transmit(STATUS_CHANNEL, CHANNEL_RECEIVE, {
|
||||||
type = "status",
|
type = "status",
|
||||||
@@ -562,32 +561,23 @@ else
|
|||||||
print("Wireless modem: OK")
|
print("Wireless modem: OK")
|
||||||
end
|
end
|
||||||
|
|
||||||
print("Attempting to get GPS position (this may take 10 seconds)...")
|
print("Attempting quick GPS check...")
|
||||||
local gpsAttempts = 0
|
-- Single quick GPS attempt (5 second timeout instead of 10)
|
||||||
local maxGpsAttempts = 3
|
local x, y, z = gps.locate(5)
|
||||||
|
if x then
|
||||||
while not state.position and gpsAttempts < maxGpsAttempts do
|
state.position = {x = math.floor(x), y = math.floor(y), z = math.floor(z)}
|
||||||
gpsAttempts = gpsAttempts + 1
|
print("GPS: OK - Position acquired!")
|
||||||
print("GPS attempt " .. gpsAttempts .. " of " .. maxGpsAttempts .. "...")
|
else
|
||||||
updatePosition()
|
print("GPS: Not available - will retry in background")
|
||||||
if not state.position then
|
print("Position tracking disabled for now.")
|
||||||
print("Waiting 2 seconds before retry...")
|
|
||||||
sleep(2)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
updateFuel()
|
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("Ready! Waiting for commands...")
|
||||||
|
print("Broadcasting initial status...")
|
||||||
broadcastStatus()
|
broadcastStatus()
|
||||||
|
print("Turtle " .. os.getComputerID() .. " is online!")
|
||||||
|
|
||||||
-- Main execution loop
|
-- Main execution loop
|
||||||
parallel.waitForAny(
|
parallel.waitForAny(
|
||||||
|
|||||||
Reference in New Issue
Block a user