-
Notifications
You must be signed in to change notification settings - Fork 1
/
adapted.py
78 lines (61 loc) · 2.01 KB
/
adapted.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
from time import time, sleep
import RPi.GPIO as GPIO
class UltrasonicSensors:
"""
Example code which prints distance for 10 seconds for one sensor:
u_sensor = UltrasonicSensor([23], [24])
start_time = time()
while time() - start_time < 10:
print u_sensor.distance(0)
u_sensor.reset()
"""
def __init__(self, trigger_pins, echo_pins):
"""
Setting GPIO numbering; BCM sets it according to the Broadcom SOC Channel
and initialize Sensors
:param trigger_pins:
:param echo_pins:
"""
self.trigger_pins = trigger_pins
self.echo_pins = echo_pins
GPIO.setmode(GPIO.BCM)
for TRIG in trigger_pins:
GPIO.setup(TRIG, GPIO.OUT)
for ECHO in echo_pins:
GPIO.setup(ECHO, GPIO.IN)
for TRIG in trigger_pins:
GPIO.output(TRIG, False)
# print "Waiting for the sensor to settle"
sleep(1)
def reset(self):
"""
Init has to be called after this.
"""
GPIO.cleanup()
def distance(self, sensor_number):
"""
Returns distance on given sensor number. Returns None when out of range.
:param sensor_number:
:return:
"""
trig = self.trigger_pins[sensor_number]
echo = self.echo_pins[sensor_number]
GPIO.output(trig, True)
sleep(0.00001) # pulse for 10us to trigger the sensor
GPIO.output(trig, False)
while GPIO.input(echo) == 0:
pass
pulse_start = time()
while GPIO.input(echo) == 1:
# print GPIO.input(ECHO)
if time() - pulse_start > 0.03:
return None
else:
pulse_end = time()
pulse_duration = pulse_end - pulse_start
# Sound travels at ~343m/s
# 34300cm/s = 2*distance/time
# So, distance = time*17150
distance = pulse_duration * 17150
distance = round(distance, 2)
return distance