-2

I have some python code which reads the port of a motor and when the port switches from closed (9) to open (8) the while loop should exit. However i'm finding that the while loop seems to be stuck even though the above are seen to change.

Below is the code and output.

        self.send(param, iostatus,'move', 'e', '-1000')  # move mirror above home switch
        self.send(param, iostatus,'move', 'a',  '-1000')  # move mirror west of home switch

        sleep(4)

        print int(self.send(param, iostatus,'portread', 'e'))
        print int(self.send(param, iostatus,'portread', 'a'))

        while int(self.send(param, iostatus,'portread', 'e')) \
        or  int(self.send(param, iostatus,'portread', 'a'))  == 9:

            if int(self.send(param, iostatus,'portread', 'e')) == 8:
                self.send(param, iostatus,'stop', 'e') #stop motor moving
            if int(self.send(param, iostatus,'portread', 'a')) == 8:
                self.send(param, iostatus,'stop', 'a') #stop motor moving

            altt = int(self.send(param, iostatus,'portread', 'e'))
            azt= int(self.send(param, iostatus,'portread', 'a'))
            print 'altt', altt, 'of type', type(altt), altt==9
            print 'azt', azt, 'of type', type(azt), azt==9
            sleep(3)

Terminal output: (note i had to space out the output so it didn't bunch up in this post)

At 140815-13:03:03 instruction to send is aA

At 140815-13:03:03 instruction to send is eA

At 140815-13:03:03 instruction to send is eF1000 1

At 140815-13:03:04 instruction to send is aF1000 1

At 140815-13:03:06 instruction to send is e^

waiting for home

..........Text cut for clarity

At 140815-13:03:21 instruction to send is e^

At 140815-13:03:21 instruction to send is a^

At 140815-13:03:21 instruction to send is eM-1000

At 140815-13:03:21 instruction to send is aM-1000

At 140815-13:03:25 instruction to send is eA 9 At 140815-13:03:25 instruction to send is aA 9 At 140815-13:03:25 instruction to send is eA

At 140815-13:03:25 instruction to send is eA

At 140815-13:03:25 instruction to send is aA

At 140815-13:03:25 instruction to send is eA At 140815-13:03:25 instruction to send is aA

altt 9 of type True

azt 9 of type True

................Text cut for clarity

At 140815-13:03:57 instruction to send is eA

At 140815-13:03:57 instruction to send is eA

At 140815-13:03:57 instruction to send is aA

At 140815-13:03:58 instruction to send is eA

At 140815-13:03:58 instruction to send is aA

altt 9 of type True

azt 9 of type True

At 140815-13:04:01 instruction to send is eA

At 140815-13:04:01 instruction to send is eA

At 140815-13:04:01 instruction to send is e@

At 140815-13:04:01 instruction to send is aA

At 140815-13:04:01 instruction to send is a@

At 140815-13:04:01 instruction to send is eA

At 140815-13:04:01 instruction to send is aA

altt 8 of type False

azt 8 of type False

At 140815-13:04:04 instruction to send is eA

At 140815-13:04:04 instruction to send is eA

At 140815-13:04:04 instruction to send is e@

At 140815-13:04:04 instruction to send is aA

At 140815-13:04:04 instruction to send is a@

At 140815-13:04:04 instruction to send is eA

At 140815-13:04:04 instruction to send is aA

altt 8 of type False

azt 8 of type False

At 140815-13:04:07 instruction to send is eA

At 140815-13:04:07 instruction to send is eA

At 140815-13:04:07 instruction to send is e@

At 140815-13:04:07 instruction to send is aA

At 140815-13:04:07 instruction to send is a@

At 140815-13:04:07 instruction to send is eA

At 140815-13:04:07 instruction to send is aA

altt 8 of type False

azt 8 of type False

.........and the loop will continue like this even though both those portchecks == 8 and are false.

Basically what's happening is i'm moving some motors and waiting for their port (home switch) to read 8 (open). When they're open i stop the motors and the loop should exit. However the loop is stuck inside the port read check.

You can see down the bottom of the below terminal output

altt 8 of type False azt 8 of type False

is what i'm reading and they're both saying false, but the while loop continues to run as if one of them is true.

Any ideas? i've been staring at it and perhaps i'm not seeing straight.

cheers

Oman
  • 23
  • 4

1 Answers1

4

You need to write it as

while int(send...) == 9 or int(send...) == 9:

Instead of:

while int(send...) or int(send...) == 9:

The latter asks:

Is int(send...) true? Which will be for any non-falsy value (like any non-zero int)
OR
Is int(send...) == 9 true, which actually does the comparison.

Please see this thread for more discussion. This is a very common problem for people that are just learning programming.

Community
  • 1
  • 1
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218