-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathultrasonicReader.py
More file actions
61 lines (48 loc) · 1.91 KB
/
ultrasonicReader.py
File metadata and controls
61 lines (48 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#Libraries
import RPi.GPIO as GPIO
import time
from func_timeout import FunctionTimedOut, func_timeout
class ultrasonicReader:
def __init__(self, exit_event, GPIO_TRIGGER, GPIO_ECHO, ultrasonicDistance):
self.GPIO_TRIGGER = GPIO_TRIGGER
self.GPIO_ECHO = GPIO_ECHO
self.ultrasonicDistance = ultrasonicDistance
self.exit_event = exit_event
GPIO.setmode(GPIO.BCM)
GPIO.setup(self.GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(self.GPIO_ECHO, GPIO.IN)
def readSensor(self):
# set Trigger to HIGH
GPIO.output(self.GPIO_TRIGGER, True)
# set Trigger after 0.01ms to LOW
time.sleep(0.00001)
GPIO.output(self.GPIO_TRIGGER, False)
StartTime = time.time()
StopTime = time.time()
# save StartTime
while GPIO.input(self.GPIO_ECHO) == 0:
StartTime = time.time()
# save time of arrival
while GPIO.input(self.GPIO_ECHO) == 1:
StopTime = time.time()
# time difference between start and arrival
TimeElapsed = StopTime - StartTime
# multiply with the sonic speed (34300 cm/s)
# and divide by 2, because there and back
distance = (TimeElapsed * 34300) / 2
return distance
def iterateSensor(self):
while not self.exit_event.is_set():
try:
self.ultrasonicDistance.value = func_timeout(0.1, self.readSensor)
# time.sleep(0.01)
# possibly delay here to allow for reasonable mutex acquisition
except FunctionTimedOut:
print("", end="")
# print('timeout error')
except Exception as e:
print(f"Function raised an exception: {e}")
except KeyboardInterrupt:
self.exit_event.set()
GPIO.cleanup()
print('closing ultrasonic reader gracefully')