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
|
TRIGGER = 0
|
||||||
OLD = 0
|
OLD = 0
|
||||||
SLEEP = 0.5
|
SLEEP = 0.5
|
||||||
|
MAX_DIST = 400
|
||||||
|
|
||||||
def Measure():
|
def Measure():
|
||||||
start = 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)
|
||||||
|
while GPIO.input(ECHO)==0:
|
||||||
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()
|
start = time.time()
|
||||||
|
Dif = time.time() - realstart
|
||||||
GPIO.wait_for_edge(ECHO, GPIO.BOTH, timeout=400)
|
if Dif > 0.2:
|
||||||
if channel is None:
|
print("Ultrasonic Sensor Timed out, Restart.")
|
||||||
print("Ultrasonic sensor timed out (post-echo).")
|
time.sleep(0.4)
|
||||||
GPIO.remove_event_detect(ECHO)
|
return 400
|
||||||
restart()
|
while GPIO.input(ECHO)==1:
|
||||||
# else:
|
|
||||||
# print("Echo finish detected")
|
|
||||||
stop = time.time()
|
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
|
elapsed = stop - start
|
||||||
distance = (elapsed * 34300)/2 # Using speed of sound at 20C (68F)
|
distance = (elapsed * 36000)/2
|
||||||
|
|
||||||
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:
|
||||||
pins = sys.argv[1].lower().split(',')
|
pins = sys.argv[1].lower().split(',')
|
||||||
@ -70,7 +54,12 @@ if len(sys.argv) > 1:
|
|||||||
ECHO = int(pins[1])
|
ECHO = int(pins[1])
|
||||||
SLEEP = float(pins[2])
|
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
|
# 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:
|
||||||
@ -83,14 +72,12 @@ if len(sys.argv) > 1:
|
|||||||
print(distance)
|
print(distance)
|
||||||
OLD = distance
|
OLD = distance
|
||||||
time.sleep(SLEEP)
|
time.sleep(SLEEP)
|
||||||
except Exception as e: # try to clean up on exit
|
except: # try to clean up on exit
|
||||||
print(e) # Print error message on exception
|
print("0.0")
|
||||||
GPIO.remove_event_detect(ECHO)
|
GPIO.cleanup()
|
||||||
GPIO.cleanup(TRIGGER)
|
|
||||||
GPIO.cleanup(ECHO)
|
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print("Bad params")
|
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)
|
sys.exit(0)
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name" : "node-red-node-pisrf",
|
"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",
|
"description" : "A Node-RED node for a Raspberry Pi to use a SRF04 or SRF05 range finder",
|
||||||
"dependencies" : {
|
"dependencies" : {
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user