feat: Implement non-blocking return home functionality with improved navigation
This commit is contained in:
116
turtle.lua
116
turtle.lua
@@ -438,63 +438,61 @@ function broadcastStatus()
|
|||||||
})
|
})
|
||||||
end
|
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()
|
local function returnHome()
|
||||||
print("Returning home...")
|
print("Returning home...")
|
||||||
state.mode = "returning"
|
state.mode = "returning"
|
||||||
state.stuckCounter = 0
|
state.stuckCounter = 0
|
||||||
broadcastStatus()
|
returnHomeAttempts = 0
|
||||||
|
|
||||||
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
|
|
||||||
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
|
||||||
|
|||||||
Reference in New Issue
Block a user