-
Notifications
You must be signed in to change notification settings - Fork 5
/
ultrasonic.py
126 lines (97 loc) · 3.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import RPi.GPIO as gpio
import time
print "Distance Measurement In Progress"
def ultra():
try:
gpio.setmode(gpio.BCM)
trig = 20
echo = 21
gpio.setup(trig,gpio.OUT)
gpio.setup(echo,gpio.IN)
gpio.output(trig, False)
time.sleep(1)
gpio.output(trig, True)
time.sleep(0.00001)
gpio.output(trig, False)
while gpio.input(echo)==0:
pulse_start = time.time()
while gpio.input(echo)==1:
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
distance = pulse_duration*17150
distance = round(distance, 2)
return distance
except:
print "reading was interrupted"
finally:
gpio.cleanup(trig)
gpio.cleanup(echo)
def callibration_ultra():
try:
gpio.setmode(gpio.BCM)
trig_forward = 20
echo_forward = 21
trig_back = 22
echo_back = 23
trig_left = 19
echo_left = 26
gpio.setup(trig_forward,gpio.OUT)
gpio.setup(trig_left,gpio.OUT)
gpio.setup(trig_back,gpio.OUT)
gpio.setup(echo_forward,gpio.IN)
gpio.setup(echo_left,gpio.IN)
gpio.setup(echo_back,gpio.IN)
gpio.output(trig_forward, False)
gpio.output(trig_left, False)
gpio.output(trig_back, False)
time.sleep(1)
#taking forward distance
gpio.output(trig_forward, True)
time.sleep(0.00001)
gpio.output(trig_forward, False)
while gpio.input(echo_forward) == 0:
pulse_start = time.time()
while gpio.input(echo_forward) == 1:
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
d_forward = pulse_duration*17150
d_forward = round(d_forward, 2)
print "forward done"
#taking left distance
gpio.output(trig_left, False)
time.sleep(1)
gpio.output(trig_left, True)
time.sleep(0.00001)
gpio.output(trig_left, False)
while gpio.input(echo_left) == 0:
pulse_start = time.time()
while gpio.input(echo_left) == 1:
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
d_left = pulse_duration*17150
d_left = round(d_left, 2)
print "left done"
#taking back distance
gpio.output(trig_back, False)
time.sleep(1)
gpio.output(trig_back, True)
time.sleep(0.00001)
gpio.output(trig_back, False)
while gpio.input(echo_back) == 0:
pulse_start = time.time()
while gpio.input(echo_back) == 1:
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
d_back = pulse_duration*17150
d_back = round(d_back, 2)
print "back taken"
return [d_forward, d_left, d_back]
except KeyboardInterrupt:
print "reading was interrupted"
finally:
gpio.cleanup(trig_forward)
gpio.cleanup(echo_forward)
gpio.cleanup(trig_left)
gpio.cleanup(echo_left)
gpio.cleanup(trig_back)
gpio.cleanup(echo_back)