revert srf node to not use edges

seems more reliable to me... :-)
This commit is contained in:
Dave Conway-Jones 2018-11-11 22:36:42 +00:00
parent 5f22603beb
commit e1e5432ac6
No known key found for this signature in database
GPG Key ID: 9E7F9C73F5168CD4
2 changed files with 29 additions and 42 deletions

View File

@ -16,48 +16,32 @@ ECHO = 0
TRIGGER = 0
OLD = 0
SLEEP = 0.5
MAX_DIST = 400
def Measure():
start = 0
realstart = time.time()
GPIO.output(TRIGGER, True)
time.sleep(0.00001)
GPIO.output(TRIGGER, False)
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
channel = GPIO.wait_for_edge(ECHO, GPIO.BOTH, timeout=200)
if channel is None:
print("Ultrasonic sensor timed out (pre-echo).")
GPIO.remove_event_detect(ECHO)
restart()
# else:
# print("Echo start detected")
start = time.time()
GPIO.wait_for_edge(ECHO, GPIO.BOTH, timeout=400)
if channel is None:
print("Ultrasonic sensor timed out (post-echo).")
GPIO.remove_event_detect(ECHO)
restart()
# else:
# print("Echo finish detected")
stop = time.time()
elapsed = stop-start
distance = (elapsed * 34300)/2 # Using speed of sound at 20C (68F)
elapsed = stop - start
distance = (elapsed * 36000)/2
return distance
def restart():
# print("Restarting...")
GPIO.setmode(GPIO.BOARD) # Use GPIO BOARD numbers
GPIO.setup(TRIGGER, GPIO.OUT) # Trigger
GPIO.output(TRIGGER, False) # Set low
GPIO.setup(ECHO, GPIO.OUT) # Echo
GPIO.output(ECHO, False)
time.sleep(0.1)
GPIO.setup(ECHO,GPIO.IN)
GPIO.add_event_detect(ECHO, GPIO.BOTH)
time.sleep(2.0)
# Main program loop
if len(sys.argv) > 1:
pins = sys.argv[1].lower().split(',')
@ -70,7 +54,12 @@ if len(sys.argv) > 1:
ECHO = int(pins[1])
SLEEP = float(pins[2])
restart()
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)
# Flush stdin so we start clean
while len(select.select([sys.stdin.fileno()], [], [], 0.0)[0])>0:
@ -83,14 +72,12 @@ if len(sys.argv) > 1:
print(distance)
OLD = distance
time.sleep(SLEEP)
except Exception as e: # try to clean up on exit
print(e) # Print error message on exception
GPIO.remove_event_detect(ECHO)
GPIO.cleanup(TRIGGER)
GPIO.cleanup(ECHO)
except: # try to clean up on exit
print("0.0")
GPIO.cleanup()
sys.exit(0)
else:
print("Bad params")
print(" sudo nrsrf.py trigger_pin,echo_pin,rate_in_seconds")
print(" nrsrf.py trigger_pin, echo_pin, rate_in_seconds")
sys.exit(0)

View File

@ -1,6 +1,6 @@
{
"name" : "node-red-node-pisrf",
"version" : "0.1.2",
"version" : "0.1.3",
"description" : "A Node-RED node for a Raspberry Pi to use a SRF04 or SRF05 range finder",
"dependencies" : {
},