turtle/behavior.lua
2025-06-27 15:31:34 +01:00

74 lines
1.6 KiB
Lua

local M = {}
local logger = require("libs.logger")
local tutil = require("libs.turtleutils")
local currentStatus = "idle"
local statusCallback = nil
function M.getStatus()
return currentStatus
end
function M.setStatus(value)
currentStatus = value
end
function M.setStatusCallback(fn)
statusCallback = fn
end
local function reportStatusLoop()
while true do
local x, y, z, gpsActive = tutil.getPosition()
local posText = gpsActive and
string.format("GPS (%d,%d,%d)", x, y, z) or
string.format("Manual (%d,%d,%d)", x, y, z)
local message = currentStatus .. " @ " .. posText
if statusCallback then
statusCallback(message)
else
logger.log("Status: " .. message)
end
sleep(5)
end
end
function M.run()
-- So turtleutils can update the status
tutil.setStatusCallback(M.setStatus)
-- Fallback: also log to file
M.setStatusCallback(function(status)
logger.log("Status update: " .. status)
end)
-- Start position + status reporting
local reporter = coroutine.create(reportStatusLoop)
coroutine.resume(reporter)
while true do
currentStatus = "moving backward"
logger.log("Started moving backward")
for i = 1, 10 do
tutil.back()
sleep(0.5)
end
currentStatus = "moving forward"
logger.log("Started moving forward")
for i = 1, 10 do
tutil.forward()
sleep(0.5)
end
if coroutine.status(reporter) == "suspended" then
coroutine.resume(reporter)
end
end
end
return M