btcg2807
Posts: 1
Joined: Wed Sep 18, 2019 3:09 am

Change in logic in sensor not breaking 'while' loop?

Wed Sep 18, 2019 4:20 am

Hello, I am currently a beginner at Python coding thus I apologise if there is a basic/obvious solution to this. I want to code a program that runs 2 motors forwards simultaneously for 3 seconds, then backwards for 3 seconds. In addition, I've added a photoelectric sensor (WT160-F172) that will act as a collision detector for the motors. I can only get the sensor to stop the motors and continue running forwards (how i want it to work) when the loop is infinite (no time.sleep) . When i make it the finite loop i want (3 seconds front and 3 second back), i do not know how to get the sensor to function during the loop, the sensor only affects the motors between two said finite loops. I'm on python 3 and raspberry pi 3.

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #photoelectric sensor
GPIO.setup(11,GPIO.OUT)
GPIO.setup(12,GPIO.OUT)
GPIO.setup(13,GPIO.OUT)
GPIO.setup(15,GPIO.OUT)

try:
	while True:
		print("Sensor Value:")
		value = GPIO.input(7)
		print(value)
		if (value == 0):
			print("Move forward")
			GPIO.output(11,False)
			GPIO.output(12,True)
			GPIO.output(13,True)
			GPIO.output(15,False)
			time.sleep(3)
			print("Move backward")
			GPIO.output(11,True)
			GPIO.output(12,False)
			GPIO.output(13,False)
			GPIO.output(15,True)
			time.sleep(3)
		elif (value == 1):
			print("Obstacle detected")
			GPIO.output(11,False)
			GPIO.output(12,False)
			GPIO.output(13,False)
			GPIO.output(15,False)
except KeyboardInterrupt:
	pass
finally:
	GPIO.cleanup()
There is evidently a wrong usage of the loop functions in this code thus any help would be appreciated.

RattusRattus
Posts: 70
Joined: Sat Sep 29, 2018 11:27 am

Re: Change in logic in sensor not breaking 'while' loop?

Wed Sep 18, 2019 8:06 am

Have you confirmed that you can read from the sensor at all?
For now I would ignore your motor control and just write a bit of code to show you that the sensor is working as you would expect.


I would also use a multimeter to check if the line does go to a logic 1 (in this case 3.3V) and then go to logic 0 (0V) when the sensor is active....
I would do this *before* trying to write code...


I have had a quick look at the data sheet for the sensor that you mentioned it shows its output, Q, as PNP: open collector.
Now I do not know how you have connected this to your RPi - but if this is directly to GPIO pin 7 then this will be your problem.

An open collector output from your sensor will 'pull down' the output when active but will be high Impedance when not active.
You have your input on the RPi set as

Code: Select all

GPIO.setup(7,GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #photoelectric sensor
This means that *IF* you have no other hardware between the sensor and your RPi that this sensor line will never show anything other than 0 (because the line is always at 0V)

You could try changing your GPIO configuration to pull up not pull down.

I hope this helps

/Andy

pcmanbob
Posts: 7937
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Change in logic in sensor not breaking 'while' loop?

Wed Sep 18, 2019 8:59 am

My first question is what voltage is your sensor working at ?

if it has a PNP open collector output then the 2 output states would be,

output off = 0V to pi gpio,
output on= supply voltage to pi gpio.

I suggest you do some testing with you sensor and a digital voltmeter to determine what the output is doing when the sensor in in both the triggered and un-triggered states and then you might have a better Idea how to connect it to your pi.

As the output has both high and low output values you should not need any pull-up /down on the gpio pin.
Last edited by pcmanbob on Wed Sep 18, 2019 9:41 am, edited 1 time in total.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
rpiMike
Posts: 1077
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: Change in logic in sensor not breaking 'while' loop?

Wed Sep 18, 2019 9:23 am

I would have one main loop continually checking the collision detector and activating forward or backward according to time:

Code: Select all

import RPi.GPIO as GPIO
import time
import datetime
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #photoelectric sensor
GPIO.setup(11,GPIO.OUT)
GPIO.setup(12,GPIO.OUT)
GPIO.setup(13,GPIO.OUT)
GPIO.setup(15,GPIO.OUT)
#12 and 13 drive motors forwards
#11 and 15 drive motors backwards

moveForwardTime = datetime.datetime.now()
moveBackwardTime = None
try:

    while True:
        collision = GPIO.input(7)
        if collision == 1:
            print('Obstacle detected')
            GPIO.output(11,False)
            GPIO.output(12,False)
            GPIO.output(13,False)
            GPIO.output(15,False)

        if collision == 0 and moveForwardTime != None and datetime.datetime.now() > moveForwardTime:
            print('Move Forward')
            GPIO.output(11,False)
            GPIO.output(12,True)
            GPIO.output(13,True)
            GPIO.output(15,False)
            moveForwardTime = None
            moveBackwardTime = datetime.datetime.now() + datetime.timedelta(seconds=3)

        if collision == 0 and moveBackwardTime != None and datetime.datetime.now() > moveBackwardTime:
            print('Move Backward')
            GPIO.output(11,True)
            GPIO.output(12,False)
            GPIO.output(13,False)
            GPIO.output(15,True)
            moveBackwardTime = None
            moveForwardTime = datetime.datetime.now() + datetime.timedelta(seconds=3)
            
        time.sleep(0.1)
        
except KeyboardInterrupt:
    pass
finally:
    GPIO.cleanup()
    


Return to “Python”