feat: Implement non-blocking return home functionality with improved navigation

This commit is contained in:
MayaTheShy
2026-02-16 03:10:47 -05:00
parent 1435a7ac55
commit 0d2fe5b8b1

View File

@@ -438,63 +438,61 @@ function broadcastStatus()
})
end
-- Return to home with better navigation
-- Return to home step (non-blocking)
local returnHomeAttempts = 0
local function returnHomeStep()
if not state.homePosition or not state.position then
print("No home or position set!")
state.mode = "idle"
broadcastStatus()
return true -- Done (failed)
end
if getDistance(state.position, state.homePosition) <= 1 then
print("Arrived at home!")
state.mode = "idle"
state.stuckCounter = 0
broadcastStatus()
return true -- Done (success)
end
if returnHomeAttempts >= 1000 then
print("Could not reach home - gave up after 1000 attempts")
state.mode = "idle"
broadcastStatus()
return true -- Done (failed)
end
updateFuel()
if needsRefuel() then
print("Low fuel, trying to refuel...")
if not tryRefuel() then
print("WARNING: Out of fuel!")
state.mode = "idle"
broadcastStatus()
return true -- Done (failed)
end
print("Refueled!")
end
navigateTowards(state.homePosition)
returnHomeAttempts = returnHomeAttempts + 1
-- Update GPS position every few steps
if returnHomeAttempts % 10 == 0 then
updatePosition()
print("Distance to home: " .. getDistance(state.position, state.homePosition))
end
return false -- Not done yet
end
-- Start return home (just initialize)
local function returnHome()
print("Returning home...")
state.mode = "returning"
state.stuckCounter = 0
broadcastStatus()
local maxAttempts = 1000
local attempts = 0
while state.homePosition and state.position and
getDistance(state.position, state.homePosition) > 1 and
attempts < maxAttempts do
-- Check for stop command
if state.mode ~= "returning" then
print("Return cancelled")
return
end
updateFuel()
if needsRefuel() then
print("Low fuel, trying to refuel...")
if tryRefuel() then
print("Refueled!")
else
print("WARNING: Out of fuel!")
state.mode = "idle"
broadcastStatus()
return
end
end
local arrived = navigateTowards(state.homePosition)
if arrived then
break
end
attempts = attempts + 1
-- Update GPS position every few steps
if attempts % 10 == 0 then
updatePosition()
print("Distance to home: " .. getDistance(state.position, state.homePosition))
end
sleep(0.1)
end
if attempts >= maxAttempts then
print("Could not reach home - gave up after " .. maxAttempts .. " attempts")
else
print("Arrived at home!")
end
state.mode = "idle"
state.stuckCounter = 0
returnHomeAttempts = 0
broadcastStatus()
end
@@ -745,8 +743,7 @@ parallel.waitForAny(
-- Safety check: too far from home
if isTooFarFromHome() then
print("Too far from home! Returning...")
state.mode = "returning"
broadcastStatus()
returnHome() -- Initialize return home
-- Check if we need to pause (chunk loading concern)
elseif shouldPauseOperations() then
print("Pausing operations (safety limit)")
@@ -755,15 +752,18 @@ parallel.waitForAny(
-- Check if we should stop
elseif needsRefuel() or inventoryFull() then
print("Need to return home")
state.mode = "returning"
broadcastStatus()
returnHome() -- Initialize return home
else
exploreStep()
sleep(0.2)
end
elseif state.mode == "returning" then
returnHome()
-- Non-blocking return home step
local done = returnHomeStep()
if not done then
sleep(0.1)
end
else
-- Idle or manual mode