Worked on heading stuff.

This commit is contained in:
Harrison Deng 2021-05-03 16:11:41 -05:00
parent 308b6a7fe2
commit 2e23715e48

View File

@ -134,6 +134,13 @@ local function attemptMove(movef)
return true return true
end end
local function locatePosition()
local x, y, z = gps.locate()
if x == nil then return false end
position = vector.new(x, y, z)
return true
end
local function attemptMoveUp() local function attemptMoveUp()
if attemptMove(turtle.up) then if attemptMove(turtle.up) then
relativePos.y = relativePos.y + 1 relativePos.y = relativePos.y + 1
@ -155,8 +162,12 @@ end
local function attemptMoveForward() local function attemptMoveForward()
if attemptMove(turtle.forward) then if attemptMove(turtle.forward) then
relativePos = relativePos + relHeading relativePos = relativePos + relHeading
if (position ~= nil and heading ~= nil) then if (position ~= nil) then
if ( heading ~= nil and heading:length() ~= 0) then
position = position + heading position = position + heading
else
locatePosition()
end
end end
return true return true
end end
@ -166,8 +177,12 @@ end
local function attemptMoveBackward() local function attemptMoveBackward()
if attemptMove(turtle.back) then if attemptMove(turtle.back) then
relativePos = relativePos - relHeading relativePos = relativePos - relHeading
if (position ~= nil and heading ~= nil) then if (position ~= nil) then
if (heading ~= nil and heading:length() ~= 0) then
position = position - heading position = position - heading
else
locatePosition()
end
end end
return true return true
end end
@ -180,7 +195,7 @@ local function attemptMoveLeft()
relHeading = -relRightAxis relHeading = -relRightAxis
relRightAxis = previousHeading relRightAxis = previousHeading
if heading ~= nil then if heading ~= nil and heading:length() ~= 0 then
local previousHeading = heading local previousHeading = heading
heading = -rightAxis heading = -rightAxis
rightAxis = previousHeading rightAxis = previousHeading
@ -196,7 +211,7 @@ local function attemptMoveRight()
relHeading = relRightAxis relHeading = relRightAxis
relRightAxis = -previousHeading relRightAxis = -previousHeading
if heading ~= nil then if heading ~= nil and heading:length() ~= 0 then
local previousHeading = heading local previousHeading = heading
heading = rightAxis heading = rightAxis
rightAxis = -previousHeading rightAxis = -previousHeading
@ -231,20 +246,21 @@ local function attemptStartTask(name, f, ...)
taskArgs = arg taskArgs = arg
end end
-- actions
local function locatePosition() -- Actions
sendLog(POSITION, "Using GPS to determine position.")
local x, y, z = gps.locate() local function determinePosition()
if x == nil then sendLog(POSITION, "could not locate.") end if not locatePosition() then
position = vector.new(x, y, z) sendLog(FAILED, "Unable to obtain GPS based position.")
return
end
sendLog(POSITION, "Position obtained: " .. tostring(position)) sendLog(POSITION, "Position obtained: " .. tostring(position))
end end
local function determineHeading() local function determineHeading()
sendLog(HEADING, "Calculating heading.") sendLog(HEADING, "Calculating heading.")
if position == nil then if position == nil && locatePosition() then
sendLog(FAILED, "No current position") sendLog(FAILED, "Unable to get current position.")
return return
end end
local oldPos = vector.new(position.x, position.y, position.z) local oldPos = vector.new(position.x, position.y, position.z)
@ -252,28 +268,34 @@ local function determineHeading()
sendLog(FAILED, "Could not move to get delta.") sendLog(FAILED, "Could not move to get delta.")
return return
end end
if position == nil then
sendLog(FAILED, "Could not locate.")
return
end
local fDelta = position - oldPos local fDelta = position - oldPos
if not attemptMoveBackward() then
sendLog(FAILED, "Could not return to original position after first axis.")
return
end
if not attemptMoveRight() then if not attemptMoveRight() then
sendLog(FAILED, "Unable to rotate right.") sendLog(FAILED, "Unable to rotate right.")
return return
end end
local oldPos = vector.new(position.x, position.y, position.z)
if not attemptMoveForward() then if not attemptMoveForward() then
sendLog(FAILED, "Unable to move forward after rotation.") sendLog(FAILED, "Unable to move forward after rotation.")
return return
end end
local rDelta = position - oldPos
if not attemptMoveBackward() then
sendLog(FAILED, "Could not return to original position after second axis.")
return
end
if not attemptMoveLeft() then if not attemptMoveLeft() then
sendLog(FAILED, "Unable to rotate left.") sendLog(FAILED, "Unable to rotate left.")
return return
end end
heading = fDelta heading = fDelta
rightAxis = position - oldPos rightAxis = rDelta
sendLog(HEADING, "Heading obtained. \nForward: " .. textutils.serialiseJSON(heading) .. "\nRight direction: " .. textutils.serialiseJSON(rightAxis)) sendLog(HEADING, "Heading obtained. \nForward: " .. textutils.serialiseJSON(heading) .. "\nRight direction: " .. textutils.serialiseJSON(rightAxis))
end end
@ -389,7 +411,7 @@ local function listen()
"\nfuel: " .. tostring(status["fuel"]) .. "\nfuel: " .. tostring(status["fuel"]) ..
"\ncurrent task: " .. status["task"], status) "\ncurrent task: " .. status["task"], status)
elseif data["request"] == POSITION then elseif data["request"] == POSITION then
locatePosition() determinePosition()
elseif data["request"] == HEADING then elseif data["request"] == HEADING then
determineHeading() determineHeading()
elseif data["request"] == QUARRY then elseif data["request"] == QUARRY then