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