-
Notifications
You must be signed in to change notification settings - Fork 1
/
ultrasonic.py
104 lines (77 loc) · 2.92 KB
/
ultrasonic.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/python
import time
from threading import Thread
import RPi.GPIO as GPIO
# Ultrasonic time settings [s]
FULL_TIME_THRESH = 3.0 # time until a section is detected as full
REFRESH_TIME = 0.5 # frequency of polling the ultrasonic sensor
GPIO.setmode(GPIO.BCM)
class Ultrasonic:
def __init__(self, trigPin, echoPin):
self.trigPin = trigPin
self.echoPin = echoPin
GPIO.setup(self.trigPin, GPIO.OUT)
GPIO.setup(self.echoPin, GPIO.IN)
self.topTimeElapsed = 0.0
self.bottomTimeElapsed = 0.0
self.threads = []
self.terminate = {'topTimer': False, 'bottomTimer': False}
def timeTop(self):
while True:
if self.minTopDist <= self.distance() <= self.maxTopDist:
self.topTimeElapsed += REFRESH_TIME
else:
self.topTimeElapsed = 0.0
time.sleep(REFRESH_TIME)
if self.terminate['topTimer']:
return
def timeBottom(self):
while True:
if self.minBottomDist <= self.distance() <= self.maxBottomDist or 1000 <= self.distance() <= 5000:
self.bottomTimeElapsed += REFRESH_TIME
else:
self.bottomTimeElapsed = 0.0
time.sleep(REFRESH_TIME)
if self.terminate['bottomTimer']:
return
def killThread(self, threadName):
for thread in self.threads:
if thread.getName() == threadName:
self.terminate[threadName] = True
thread.join()
self.terminate[threadName] = False
self.threads.remove(thread)
def setupTopTimer(self, minDist, maxDist):
self.topTimeElapsed = 0.0
self.minTopDist = minDist
self.maxTopDist = maxDist
self.killThread('topTimer')
timer = Thread(target=self.timeTop, name='topTimer')
self.threads.append(timer)
timer.start()
def setupBottomTimer(self, minDist, maxDist):
self.bottomTimeElapsed = 0.0
self.minBottomDist = minDist
self.maxBottomDist = maxDist
self.killThread('bottomTimer')
timer = Thread(target=self.timeBottom, name='bottomTimer')
self.threads.append(timer)
timer.start()
def getTopTimerReached(self):
return self.topTimeElapsed >= FULL_TIME_THRESH
def getBottomTimerReached(self):
return self.bottomTimeElapsed >= FULL_TIME_THRESH
def distance(self):
GPIO.output(self.trigPin, True)
time.sleep(0.00001)
GPIO.output(self.trigPin, False)
startTime = time.time()
stopTime = time.time()
while GPIO.input(self.echoPin) == 0:
startTime = time.time()
while GPIO.input(self.echoPin) == 1:
stopTime = time.time()
timeElapsed = stopTime - startTime
# function of ultrasonic wave speed
distance = timeElapsed * 34300 / 2
return distance