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