feat: Implement command acknowledgment and improve command polling reliability for turtles

This commit is contained in:
MayaTheShy
2026-02-19 23:27:23 -05:00
parent 6e50a109dc
commit 4bda9c536b
2 changed files with 104 additions and 20 deletions

View File

@@ -320,6 +320,25 @@ local function pollCommands(turtleID)
end
end
-- Function to acknowledge commands were sent
local function acknowledgeCommands(turtleID)
local success = pcall(function()
local response = http.post(
SERVER_URL .. "/api/turtle/" .. turtleID .. "/commands/ack",
"{}",
{["Content-Type"] = "application/json"}
)
if response then
response.close()
return true
end
return false
end)
return success
end
-- Initial setup
if hasMonitor then
drawDashboard()
@@ -333,9 +352,12 @@ end
addLog("Listening on channels " .. STATUS_CHANNEL .. " and " .. CHANNEL_RECEIVE, colors.lightBlue)
-- Start polling timer
local POLL_INTERVAL = 1 -- Poll every 1 second
local POLL_INTERVAL = 2 -- Poll every 2 seconds (reduced frequency for better reliability)
os.startTimer(POLL_INTERVAL)
-- Track which turtles we've sent commands to recently
local recentCommandSends = {}
-- Main loop
local lastRefresh = os.epoch("utc")
while true do
@@ -346,23 +368,51 @@ while true do
for turtleID, turtleData in pairs(turtles) do
local commands = pollCommands(turtleID)
-- Forward commands back to turtle
for _, cmd in ipairs(commands) do
stats.commandsSent = stats.commandsSent + 1
addLog("CMD: " .. cmd.command .. " -> Turtle #" .. turtleID, colors.yellow)
-- Only send commands if we got some
if #commands > 0 then
addLog("Received " .. #commands .. " command(s) for Turtle #" .. turtleID, colors.cyan)
local commandPacket = {
command = cmd.command,
param = cmd.param,
target = turtleID
}
modem.transmit(COMMAND_CHANNEL, CHANNEL_RECEIVE, commandPacket)
-- Debug: show what we're sending
if not hasMonitor then
print(" Sent to channel " .. COMMAND_CHANNEL .. ": target=" .. turtleID)
-- Forward commands back to turtle
for _, cmd in ipairs(commands) do
stats.commandsSent = stats.commandsSent + 1
addLog(" CMD: " .. cmd.command .. " -> Turtle #" .. turtleID, colors.yellow)
local commandPacket = {
command = cmd.command,
param = cmd.param,
target = turtleID
}
-- Send command multiple times for reliability
for i = 1, 3 do
modem.transmit(COMMAND_CHANNEL, CHANNEL_RECEIVE, commandPacket)
os.sleep(0.05) -- Small delay between retransmissions
end
-- Debug: show what we're sending
if not hasMonitor then
print(" Sent to channel " .. COMMAND_CHANNEL .. ": target=" .. turtleID)
end
end
-- Acknowledge that we sent the commands
os.sleep(0.5) -- Give turtle time to receive
if acknowledgeCommands(turtleID) then
addLog(" ACK: Commands acknowledged", colors.lime)
else
addLog(" WARN: Failed to acknowledge", colors.orange)
end
-- Mark that we sent commands to this turtle
recentCommandSends[turtleID] = os.epoch("utc")
end
end
-- Clean up old command send timestamps (older than 10 seconds)
local now = os.epoch("utc")
for turtleID, timestamp in pairs(recentCommandSends) do
if now - timestamp > 10000 then
recentCommandSends[turtleID] = nil
end
end