-
Notifications
You must be signed in to change notification settings - Fork 0
/
ultrasonic.py
92 lines (73 loc) · 2.82 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
import RPi.GPIO as GPIO
import time
DEBUG = False
class UltraSonicSensor(object):
def __init__(self, id, trigger, echo):
self.id = id
self.trigger = trigger
self.echo = echo
self.continousMeasure = False
self.initGPIO()
self.value = 0
def initGPIO(self):
GPIO.setmode(GPIO.BCM)
GPIO.setup(self.trigger,GPIO.OUT)
GPIO.setup(self.echo,GPIO.IN)
GPIO.output(self.trigger, False)
print("Waiting For Sensor To Settle")
time.sleep(2)
def doMeasure(self):
# In case while loops fail (because of timing property)
pulse_start = 0
pulse_end = 0
GPIO.output(self.trigger, True)
time.sleep(0.00001)
GPIO.output(self.trigger, False)
while GPIO.input(self.echo)==0:
pulse_start = time.time()
while GPIO.input(self.echo)==1:
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
distance = pulse_duration * 17150
distance = round(distance, 2)
return max(0, distance)
def getValue(self):
return self.value
def startContinousMeasure(self, minDistance, maxDistance, callback, minSleepDistance=-1, maxSleepDistance=-1, allowedSleepTimes=3):
# Init arguments
if minSleepDistance == -1:
minSleepDistance = minDistance
if maxSleepDistance == -1:
maxSleepDistance = maxDistance
# Setup variables
self.continousMeasure = True
intervalSize = maxDistance-minDistance
sleepCounter = 0
# Loop for values until explicitly stopped
while(self.continousMeasure):
measure = self.doMeasure()
if measure > maxSleepDistance or measure < minSleepDistance:
sleepCounter += 1
if sleepCounter > allowedSleepTimes:
if DEBUG:
print('ContinousMeasure: Measurement not in boundaries (',measure,'cm). Waiting 2s')
time.sleep(2)
else:
if DEBUG:
print('Increasing sleep counter to ',sleepCounter)
time.sleep(0.01)
else:
if self.continousMeasure:
sleepCounter = 0
ratio = (measure-minDistance)/intervalSize
ratio = min(1, max(0, ratio)) # Make sure it is between 0 and 1
self.value = ratio
callback(self, ratio)
time.sleep(0.01)
def stopContinousMeasure(self):
self.continousMeasure = False
def terminate(self):
self.continousMeasure = False
GPIO.cleanup()
def cleanup():
GPIO.cleanup()