This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
DePAK's profile picture

If statement appears to not be working

Started by DePAK, 14 November 2015 - 09:51 AM
DePAK #1
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.

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.
valithor #2
Posted 14 November 2015 - 03:32 PM
I must admit you came up with a unique way of accomplishing this. :P/>

From what I can tell your problem is this line:

elseif(diff == -2 or diff == 0 or diff == 2)then

More specifically the "diff == 0" part. If the difference is 0, then the turtle is already facing the correct direction and there is no reason to turn.
DePAK #3
Posted 14 November 2015 - 05:09 PM
It appears i messed up the truth table on a simple subtraction -1 - (1) = - 2 and not 0 wich probably contributed to this mess, thx for pointing it out.

But even then i got the debug message as follows:
frontOritentation : 2
toOritentation : 0
originalOrientation : 0

i miscalculated the transitions from -1 to 1 and from 1 to -1, but the debug message tell me this ocurred on a transition from 0 to 0 which should have
been caught by this "if(not (toOrientation == ttl.frontOritentation))" and not executed any of the turn****() anyway

I usually don't seek help, but i seem to be missing something really obvious here, and cant seem to understand it.

Thx for the help.
Bomb Bloke #4
Posted 15 November 2015 - 09:45 PM
Seems to me that you're mixing up east and west. Your basic compass looks like this:

 N
W E
 S

Say ttl.frontOritentation is 1 (east) and toOrientation is 0 (north). The turtle should turn left once, but diff == 1 - 0 == 1, which will lead to it turning right instead. It'll then end up thinking that it's pointing north, when really the new facing is south.

If you're still having trouble after sorting that out, please post the whole script.
DePAK #5
Posted 16 November 2015 - 11:42 AM
I did mix up east and west but i did so in translation (sorry about that), while thinking through it i used a used a compass like the one bellow but in my native language and hence the mess up.

There might be some errors of logic in the code, but the reason that i came here for help is the debug message that followed that saves the comparison made by that if and tells me that the variables were the same and despite that the contents of that if were executed.

What im trying to get at is being that toOrientation and ttl.frontOrientation were the same (i think? cuz of the debug message) how did it change orientation in the first place in a manner consistent with whats inside that if.

The rest of the script is 600 lines and counting, all calls that can be made from this function are posted in the second spoiler tag in the original post.

N(0)
W(1) E(-1)
S(2)


The debug message:
frontOritentation : 2
toOritentation : 0
originalOrientation : 0


Thx for your help.
Bomb Bloke #6
Posted 17 November 2015 - 04:40 AM
That'll be down to some miss-spellings of "orientation". "toOrientation" and "toOritentation" aren't the same variable.
DePAK #7
Posted 17 November 2015 - 10:25 AM
Unbelivable… i spent hours looking a this….

Thx for spotting it.

On a related note, do you have any tips on how to prevent this kind of things from happening?