Jump to content




If statement appears to not be working


  • You cannot reply to this topic
6 replies to this topic

#1 DePAK

  • Members
  • 4 posts

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


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

P.s : My writing skills in English are fairly limited, if you need something clarified i will make my best to explain.

#2 valithor

  • Members
  • 1,053 posts

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.

#3 DePAK

  • Members
  • 4 posts

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.

#4 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

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.

#5 DePAK

  • Members
  • 4 posts

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.

#6 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

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.

#7 DePAK

  • Members
  • 4 posts

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?





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users