Improved status response.

This commit is contained in:
Harrison Deng 2021-05-03 14:49:44 -05:00
parent 9f6a4e5e1c
commit 7e7aa9febc
2 changed files with 15 additions and 9 deletions

View File

@ -30,7 +30,6 @@ local FAILED = "failed"
running = true
local modem = peripheral.find("modem")
print("Checking for modem.")
if (modem == nil) then
error("Modem not found.")
@ -100,7 +99,12 @@ end
local function handleMessage(sender, type, data)
if type == LOG then
print("[" .. sender .. "]: " .. data["log"] .. " : " .. (data["content"]["n"] >= 1 and tostring(data["content"][1]) or ""))
print("[" .. sender .. "]: " .. data["log"] .. ": " .. (data["content"]["n"] >= 1 and tostring(data["content"][1]) or ""))
if data["log"] == STATUS then
print("Relative position: " .. tostring(data["content"][1]["relative_position"]["coordinates"]))
print("Position" .. tostring(data["content"][1]["position"]["coordinates"]))
print("fuel: " .. tostring(data["content"][1]["fuel"]))
print("current task: " .. data["content"][1]["task"])
elseif type == REQUEST then
handleRequest(sender, data["request"], data["content"])
end

View File

@ -221,13 +221,13 @@ local function orient(v, forward, right)
print("Orientation complete.")
end
local function attemptStartTask(f, ...)
local function attemptStartTask(name, f, ...)
sendLog(TASK, "Attempting to start task.")
if task ~= nil then
sendLog(FAILED, "Unable to start task due to another task running.")
return
end
task = f
task = {name=name, func=f, args=arg}
taskArgs = arg
end
@ -314,7 +314,8 @@ end
local function executeTasks()
while running do
if task ~= nil then
task(unpack(taskArgs))
sendLog(TASK, "Executing task: " .. task["name"])
task["func"](unpack(task["args"]))
task = nil
end
sleep(5)
@ -349,11 +350,11 @@ local function listen()
if heading ~= nil then
print("Using actual coordinates.")
sendLog(MOVETO, "Using actual coordinates to move.")
attemptStartTask(moveTo, vector.new(x,y,z), heading, rightAxis)
attemptStartTask(MOVETO, moveTo, vector.new(x,y,z), heading, rightAxis)
else
print("Using relative coordinates.")
sendLog(MOVETO, "Using relative coordinates to move.")
attemptStartTask(moveTo, vector.new(x,y,z), relHeading, relRightAxis)
attemptStartTask(MOVETO, moveTo, vector.new(x,y,z), relHeading, relRightAxis)
end
elseif data["request"] == MOVEBY then
local x = data["content"][1]
@ -362,11 +363,11 @@ local function listen()
if heading ~= nil then
print("Using actual coordinates.")
sendLog(MOVEBY, "Using actual coordinates to move.")
attemptStartTask(moveBy, vector.new(x,y,z), heading, rightAxis)
attemptStartTask(MOVEBY, moveBy, vector.new(x,y,z), heading, rightAxis)
else
print("Using relative coordinates.")
sendLog(MOVEBY, "Using relative coordinates to move.")
attemptStartTask(moveBy, vector.new(x,y,z), relHeading, relRightAxis)
attemptStartTask(MOVEBY, moveBy, vector.new(x,y,z), relHeading, relRightAxis)
end
elseif data["request"] == STATUS then
status = {
@ -381,6 +382,7 @@ local function listen()
right=rightAxis
},
fuel=turtle.getFuelLevel(),
task=task["name"]
}
sendLog(STATUS, status)
elseif data["request"] == POSITION then