mirror of
https://github.com/node-red/node-red-nodes.git
synced 2023-10-10 13:36:58 +02:00
revert srf node to not use edges
seems more reliable to me... :-)
This commit is contained in:
parent
5f22603beb
commit
e1e5432ac6
@ -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)
|
||||
|
||||
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")
|
||||
while GPIO.input(ECHO)==0:
|
||||
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")
|
||||
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 * 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)
|
||||
|
@ -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" : {
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user