rjhazeld
Posts: 38
Joined: Mon Feb 16, 2015 9:19 am

Getting result out of while loop

Sat Jul 25, 2015 2:57 pm

I have built a cambot with a python control script and html front end that works fine. I have added a pair of infrared distance sensors with the aim of overriding the forward speed when an obstacle is in the way of cambot.. Seemed sensible to put the code in a separate module and import it into the main control script.
The sensors are connected to GPIO pins. I have set up a 'while var ==1:" loop which collects each pin value (leftahead and rightahead) and then via "if...else" determines the value of either leftahead or rightahead the last element of the loop is another "if ...else" that sets the value of 'ahead' to blocked or clear. if I put a print statement after this loop to print ahead and run the script the correct value for ahead scrolls through the python shell window. However, if I move the print statement out of the loop nothing is printed nor can I pass the value of ahead to another variable. The code is attached and largely created by reference to code on the web. As far as I can tell the loop finishes each iteration at the print ahead statement and starts again.
In a nutshell my problem is that I can't get the result available so that I can use it in a condition in the main control script my last attempt was to put the while loop in a function but that hasn't helped. I am sure the solution must be simple but I haven't been able to find it. As is doubtless clear I am a noob so need all the help I can get!!
Thanks.
Attachments
sensor.py.JPG
sensor.py.JPG (57.77 KiB) Viewed 1214 times

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: Getting result out of while loop

Sat Jul 25, 2015 3:25 pm

Can you post your code?
Make sure it's between the code tags in the editor so it looks like...

Code: Select all

my_code()
Dave.
Apple say... Monkey do !!

rjhazeld
Posts: 38
Joined: Mon Feb 16, 2015 9:19 am

Re: Getting result out of while loop

Sat Jul 25, 2015 4:42 pm

Sorry thought the attachment would suffice here is the code:

Code: Select all

#!/usr/bin/python2.7

import RPi.GPIO as GPIO, time, os

GPIO.setmode(GPIO.BCM)
GPIO.setup(20, GPIO.IN)
GPIO.setup(21, GPIO.IN)

IR_L = 21 #GPIO pin number left sensor
IR_R = 20 #GPIO pin number right sensor

var = 1
global ahead
ahead = ""
#print  "ahead = " + ahead
def aheadsen():
    while var ==1:
        lvalue = GPIO.input(IR_L)
        if lvalue == 1:
          leftahead = "Clear Ahead"
        else:
          leftahead = "Blocked Ahead"

        rvalue = GPIO.input(IR_R)
        if rvalue == 1:
          rightahead = "Clear Ahead"
        else:
           rightahead = "Blocked Ahead"

        if leftahead == "Blocked Ahead" or rightahead == "Blocked Ahead":
           ahead = "Blocked"
        else:
            ahead = "Clear"        
        print ahead
    return ahead

aheadsen()

forward = ahead
print  forward
print ahead


gordon77
Posts: 4172
Joined: Sun Aug 05, 2012 3:12 pm

Re: Getting result out of while loop

Sat Jul 25, 2015 5:20 pm

What happens if you get rid of 'global ahead'?

User avatar
sommersoft
Posts: 35
Joined: Sat Jul 11, 2015 1:43 pm
Location: Delaware, US

Re: Getting result out of while loop

Sat Jul 25, 2015 5:33 pm

Try this:

Code: Select all

if leftahead == "Blocked Ahead" or rightahead == "Blocked Ahead":
    ahead = "Blocked"
    break
else:
    ahead = "Clear"
    break
You have no break point in your loop, so the loop is endless as long as 'var == 1'. This is why the 'print ahead' works inside the loop, but not outside. At least, that's how I see it. Still learning Python myself (loops in general I understand from other languages...just nailing down Python rules still).
"We mock what we don't understand." - Dr. Trowbridge

rjhazeld
Posts: 38
Joined: Mon Feb 16, 2015 9:19 am

Re: Getting result out of while loop

Sat Jul 25, 2015 9:02 pm

Thanks.
Having taken the 'def...' out and added the 'breaks' in it does now pass the value back. The problem now is that I need some way of restarting the loop so that an obstacle is detected as it comes into range. The script doesn't give a continuous read of the GPIO pins as it did within the loop.
One step forward.........

User avatar
sommersoft
Posts: 35
Joined: Sat Jul 11, 2015 1:43 pm
Location: Delaware, US

Re: Getting result out of while loop

Sat Jul 25, 2015 9:35 pm

Well, now that the obstacle variable is readable outside of the loop, you could put it back into your main script. I'm assuming you have a control variable that determines when you need to "see"? I.e. "I'm moving: set varMoving to true".

Beyond that, I'm still trying to figure out Python's schema on importing modules, multi-threading, and if the two are even different. :?
"We mock what we don't understand." - Dr. Trowbridge

rjhazeld
Posts: 38
Joined: Mon Feb 16, 2015 9:19 am

Re: Getting result out of while loop

Sat Jul 25, 2015 10:36 pm

Thanks, I had been hoping to have the 'current' view ahead available in 'real time' as it were so the forward/reverse slider output variable which is continuously refreshed would be revised to zero if the view ahead was 'blocked' and the speed variable is greater than zero. There will, if I can crack the forward version, be a similar routine using an ultrasonic sensor at the rear in that case the override would be if the if the forward/reverse value was less than zero. Maybe I can set up a timed refresh of the variable in the main script. Not tonight though!!
I have found the only reliable way of getting modules importable is to place them in the same folder as the script calling (in once case it is just a sym link) them there doesn't seem to be an easy way of setting a path that will find them. I have only encountered threads in the context of the NGINX, UWSGI, and PHP web server setup were number of process are specified in the configurations which appear to align them with the cores on the processor (4 on my v2 pi). In my case it is serving the web front end to the control script, the RPI-Cam-Web-Interface, and the control script a good deal of which is happening in parallel so I guess the web server setup is sharing the resources effectively. Good figuring!!

Return to “Python”