2015-11-01 15:59:21 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
# Python driver for SRF04 and SRO05 Ultrasonic sensors
|
|
|
|
# Modified to use PINS not BCM : Dave Conway-Jones
|
|
|
|
|
|
|
|
#import
|
|
|
|
import RPi.GPIO as GPIO
|
|
|
|
import time
|
|
|
|
import sys
|
|
|
|
import os, select
|
|
|
|
|
|
|
|
# Turn off warnings if you run it a second time...
|
|
|
|
GPIO.setwarnings(False)
|
|
|
|
|
|
|
|
ECHO = 0
|
|
|
|
TRIGGER = 0
|
|
|
|
OLD = 0
|
2017-08-14 10:20:18 +02:00
|
|
|
SLEEP = 0.5
|
2018-11-11 23:36:42 +01:00
|
|
|
MAX_DIST = 400
|
2015-11-01 15:59:21 +01:00
|
|
|
|
|
|
|
def Measure():
|
2018-11-11 23:36:42 +01:00
|
|
|
realstart = time.time()
|
2015-11-01 15:59:21 +01:00
|
|
|
GPIO.output(TRIGGER, True)
|
|
|
|
time.sleep(0.00001)
|
|
|
|
GPIO.output(TRIGGER, False)
|
2018-11-11 23:36:42 +01:00
|
|
|
while GPIO.input(ECHO)==0:
|
|
|
|
start = time.time()
|
|
|
|
Dif = time.time() - realstart
|
|
|
|
if Dif > 0.2:
|
|
|
|
print("Ultrasonic Sensor Timed out, Restart.")
|
|
|
|
time.sleep(0.4)
|
|
|
|
return 400
|
|
|
|
while GPIO.input(ECHO)==1:
|
|
|
|
stop = time.time()
|
|
|
|
Dif = time.time() - realstart
|
|
|
|
if Dif > 0.4:
|
|
|
|
print("Ultrasonic Sensor Timed out, Restarting.")
|
|
|
|
time.sleep(0.2)
|
|
|
|
return 400
|
|
|
|
|
|
|
|
elapsed = stop - start
|
|
|
|
distance = (elapsed * 36000)/2
|
2015-11-01 15:59:21 +01:00
|
|
|
return distance
|
|
|
|
|
|
|
|
# Main program loop
|
|
|
|
if len(sys.argv) > 1:
|
|
|
|
pins = sys.argv[1].lower().split(',')
|
2017-08-14 10:20:18 +02:00
|
|
|
if len(pins) != 3:
|
2018-07-07 20:24:35 +02:00
|
|
|
print("Bad parameters supplied")
|
|
|
|
print(pins)
|
2015-11-01 15:59:21 +01:00
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
TRIGGER = int(pins[0])
|
|
|
|
ECHO = int(pins[1])
|
2017-08-14 10:20:18 +02:00
|
|
|
SLEEP = float(pins[2])
|
2015-11-01 15:59:21 +01:00
|
|
|
|
2018-11-11 23:36:42 +01:00
|
|
|
GPIO.setmode(GPIO.BOARD) # Use GPIO BOARD numbers
|
|
|
|
GPIO.setup(TRIGGER, GPIO.OUT) # Trigger
|
|
|
|
GPIO.output(TRIGGER, False)
|
|
|
|
GPIO.setup(ECHO, GPIO.OUT) # Echo
|
|
|
|
GPIO.output(ECHO, False)
|
|
|
|
GPIO.setup(ECHO,GPIO.IN)
|
2015-11-01 15:59:21 +01:00
|
|
|
|
|
|
|
# Flush stdin so we start clean
|
|
|
|
while len(select.select([sys.stdin.fileno()], [], [], 0.0)[0])>0:
|
|
|
|
os.read(sys.stdin.fileno(), 4096)
|
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
distance = int( Measure() + 0.5 )
|
2017-09-07 22:12:33 +02:00
|
|
|
if distance != OLD and distance > 2 and distance < 400:
|
2015-11-01 15:59:21 +01:00
|
|
|
print(distance)
|
2017-05-08 10:01:45 +02:00
|
|
|
OLD = distance
|
2017-08-14 10:20:18 +02:00
|
|
|
time.sleep(SLEEP)
|
2018-11-11 23:36:42 +01:00
|
|
|
except: # try to clean up on exit
|
|
|
|
print("0.0")
|
|
|
|
GPIO.cleanup()
|
2015-11-01 15:59:21 +01:00
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
else:
|
2018-07-07 20:24:35 +02:00
|
|
|
print("Bad params")
|
2018-11-11 23:36:42 +01:00
|
|
|
print(" nrsrf.py trigger_pin, echo_pin, rate_in_seconds")
|
2015-11-01 15:59:21 +01:00
|
|
|
sys.exit(0)
|