Posted 14 November 2015 - 10:51 AM
Hello.
I was writing a mining script for fun, i was aiming for it to able to go from point A to B by itself.
To handle the changes in orientation as required i wrote the following code.
*I could probably make this in a less contrived way, but i was figuring it out as i was going and this is how it turned out.
Code debrief
This function was intended to receive a desired orientation and align the turtle with it.
I used 0 as North, 1 as East, -1 as West and 2 as South.
I used a truth table based on (ttl.frontOritentation - toOritentation) to get at what should the movement be to get to the intended orientation.
The second if(toOrientation ~= ttl.frontOritentation) is for debugging purposes and prints the error message letting me know that the current orientation and target orientation at the time of the error.
The error
Basically the turtle was in the right orientation (North) and turned back (South), i don't know how if(toOrientation ~= ttl.frontOritentation) could allow this to happen
The variables at the time of error were :
frontOrientation = 2 (South)
originalOrientation = 0 (North)
toOrientation = 0 (North)
I visually confirmed the reported orientations are correct, the turtle was turned north and by the end of it turned South
Thx for your help.
I dont think the helper functions have anything to do with this but here they are.
P.s : My writing skills in English are fairly limited, if you need something clarified i will make my best to explain.
I was writing a mining script for fun, i was aiming for it to able to go from point A to B by itself.
To handle the changes in orientation as required i wrote the following code.
*I could probably make this in a less contrived way, but i was figuring it out as i was going and this is how it turned out.
Spoiler
function ttl.alignFront(toOritentation)
local originalOrientation = ttl.frontOritentation
if(not (toOrientation == ttl.frontOritentation))then
local diff = ttl.frontOritentation - toOritentation
if(diff == -1 or diff == 3)then
ttl.turnLeft()
elseif(diff == 1 or diff == -3)then
ttl.turnRight()
elseif(diff == -2 or diff == 0 or diff == 2)then
ttl.turnBack()
end
end
if(ttl.frontOritentation ~= toOritentation)then
local msg = ("frontOritentation : " .. ttl.frontOritentation .."\ntoOritentation : " .. toOritentation ..'\noriginalOrientation : ' .. originalOrientation .."\nposition : " .. ttl.pos[1] .. ' ' .. ttl.pos[2] .. ' ' .. ttl.pos[3])
print(msg)
ttl.createFile('sumtingwong', msg)
iDunGoofed()
end
end
Code debrief
This function was intended to receive a desired orientation and align the turtle with it.
I used 0 as North, 1 as East, -1 as West and 2 as South.
I used a truth table based on (ttl.frontOritentation - toOritentation) to get at what should the movement be to get to the intended orientation.
The second if(toOrientation ~= ttl.frontOritentation) is for debugging purposes and prints the error message letting me know that the current orientation and target orientation at the time of the error.
The error
Basically the turtle was in the right orientation (North) and turned back (South), i don't know how if(toOrientation ~= ttl.frontOritentation) could allow this to happen
The variables at the time of error were :
frontOrientation = 2 (South)
originalOrientation = 0 (North)
toOrientation = 0 (North)
I visually confirmed the reported orientations are correct, the turtle was turned north and by the end of it turned South
Thx for your help.
I dont think the helper functions have anything to do with this but here they are.
Spoiler
function ttl.updateOrientation(turnDirection) -- +1 to turn left -1 to turn right
ttl.frontOritentation = ttl.frontOritentation + turnDirection
if(ttl.frontOritentation == -2 )then
ttl.frontOritentation = 2
elseif(ttl.frontOritentation == 3)then
ttl.frontOritentation = -1
end
end
function ttl.turnBack()
ttl.turnLeft()
ttl.turnLeft()
end
function ttl.turnLeft()
--print('my orientation is : ' ,ttl.frontOritentation)
turtle.turnLeft()
ttl.updateOrientation(1)
--print('i turned left and now ornt is : ' ,ttl.frontOritentation)
end
function ttl.turnRight()
--print('my orientation is' ,ttl.frontOritentation)
turtle.turnRight()
ttl.updateOrientation(-1)
--print('i turned right and now ornt is : ' ,ttl.frontOritentation)
end
P.s : My writing skills in English are fairly limited, if you need something clarified i will make my best to explain.