mirror of
https://github.com/node-red/node-red-nodes.git
synced 2023-10-10 13:36:58 +02:00
* Print error on exception and tweak GPIO setup * Restart script on timeout * Add GPIO.cleanup to restart() function * Use interrupts instead of polling input pin state
This commit is contained in:
parent
e376802ff4
commit
e61da83d5d
@ -19,32 +19,44 @@ SLEEP = 0.5
|
|||||||
|
|
||||||
def Measure():
|
def Measure():
|
||||||
start = 0
|
start = 0
|
||||||
realstart = 0
|
|
||||||
realstart = time.time()
|
|
||||||
GPIO.output(TRIGGER, True)
|
GPIO.output(TRIGGER, True)
|
||||||
time.sleep(0.00001)
|
time.sleep(0.00001)
|
||||||
GPIO.output(TRIGGER, False)
|
GPIO.output(TRIGGER, False)
|
||||||
start = time.time()
|
|
||||||
while GPIO.input(ECHO)==0:
|
|
||||||
start = time.time()
|
|
||||||
Dif = time.time() - realstart
|
|
||||||
if Dif > 0.2:
|
|
||||||
print("Ultrasonic Sensor Timed out, Restarting.")
|
|
||||||
time.sleep(0.4)
|
|
||||||
Main()
|
|
||||||
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)
|
|
||||||
Main()
|
|
||||||
|
|
||||||
|
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
|
elapsed = stop-start
|
||||||
distance = (elapsed * 36000)/2
|
distance = (elapsed * 34300)/2 # Using speed of sound at 20C (68F)
|
||||||
|
|
||||||
return distance
|
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
|
# Main program loop
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
@ -58,11 +70,7 @@ if len(sys.argv) > 1:
|
|||||||
ECHO = int(pins[1])
|
ECHO = int(pins[1])
|
||||||
SLEEP = float(pins[2])
|
SLEEP = float(pins[2])
|
||||||
|
|
||||||
GPIO.setmode(GPIO.BOARD) # Use GPIO BOARD numbers
|
restart()
|
||||||
GPIO.setup(TRIGGER, GPIO.OUT) # Trigger
|
|
||||||
GPIO.setup(ECHO, GPIO.OUT) # Echo
|
|
||||||
GPIO.output(ECHO, False)
|
|
||||||
GPIO.setup(ECHO,GPIO.IN)
|
|
||||||
|
|
||||||
# Flush stdin so we start clean
|
# Flush stdin so we start clean
|
||||||
while len(select.select([sys.stdin.fileno()], [], [], 0.0)[0])>0:
|
while len(select.select([sys.stdin.fileno()], [], [], 0.0)[0])>0:
|
||||||
@ -71,12 +79,13 @@ if len(sys.argv) > 1:
|
|||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
distance = int( Measure() + 0.5 )
|
distance = int( Measure() + 0.5 )
|
||||||
if distance != OLD:
|
if distance != OLD and distance > 2 and distance < 400:
|
||||||
print(distance)
|
print(distance)
|
||||||
OLD = distance
|
OLD = distance
|
||||||
time.sleep(SLEEP)
|
time.sleep(SLEEP)
|
||||||
except: # try to clean up on exit
|
except Exception as e: # try to clean up on exit
|
||||||
print("0.0");
|
print(e) # Print error message on exception
|
||||||
|
GPIO.remove_event_detect(ECHO)
|
||||||
GPIO.cleanup(TRIGGER)
|
GPIO.cleanup(TRIGGER)
|
||||||
GPIO.cleanup(ECHO)
|
GPIO.cleanup(ECHO)
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
Loading…
Reference in New Issue
Block a user