-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ultrasonic_Sensor.py
executable file
·114 lines (71 loc) · 2.96 KB
/
Ultrasonic_Sensor.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
#Sources:
# - https://pimylifeup.com/raspberry-pi-distance-sensor/
import RPi.GPIO as GPIO
import time
import os
class Ultrasonic_Sensor(object):
def __init__(self, channel, settle_time = 0.005):
self.channel = channel
self.settle_time = settle_time
GPIO.setmode(GPIO.BCM)
def get_distance(self):
pulse_start_time = time.time()
GPIO.setup(self.channel, GPIO.OUT)
GPIO.output(self.channel, GPIO.LOW)
time.sleep(self.settle_time)
GPIO.output(self.channel, GPIO.HIGH)
time.sleep(0.00001)
GPIO.output(self.channel, GPIO.LOW)
GPIO.setup(self.channel, GPIO.IN)
while GPIO.input(self.channel)==0:
pulse_start_time = time.time()
while GPIO.input(self.channel)==1:
pulse_end_time = time.time()
pulse_duration = pulse_end_time - pulse_start_time
distance = round(pulse_duration * 17150, 2)
#print "Distance:",distance,"cm"
GPIO.setup(self.channel, GPIO.OUT)
GPIO.output(self.channel, GPIO.LOW)
return distance
def test_optimal_settle_time():
settle_time = 0
real_distance = 0
f = open('Pruebas Picar/My robot/Doc/Ultrasonic_sensor_measurements_settle_time_0cm.txt', 'w')
f.write('Settle time (s),Measured Distance (cm), Error to real distance = ' + str(real_distance) +' cm\n')
while settle_time < 0.3:
for i in range(0,3):
# print(settle_time)
US = Ultrasonic_Sensor(20, settle_time = settle_time)
distance = US.get_distance()
error = real_distance - distance
write = str(settle_time) + ',' + str(distance) +',' + str(error) + "\n"
f.write(write)
print('settle time: ', round(settle_time,4), ' Distance: ', distance, ' Error:', error)
settle_time += 0.001
def test_distance_accuracy():
settle_time = 0.005
real_distance = 0
f = open('Pruebas Picar/My robot/Doc/Ultrasonic_sensor_measurements_0cm.txt', 'w')
f.write('Settle time (s),Measured Distance (cm), Error to real distance = ' + str(real_distance) +' cm\n')
US = Ultrasonic_Sensor(20, settle_time = settle_time)
for i in range (0, 100):
distance = US.get_distance()
error = real_distance - distance
write = str(settle_time) + ',' + str(distance) +',' + str(error) + "\n"
f.write(write)
print('settle time: ', round(settle_time,4), ' Distance: ', distance, ' Error:', error)
def test_angle_impact():
settle_time = 0.005
real_distance = 100
f = open('Pruebas Picar/My robot/Doc/Ultrasonic_sensor_measurements_angle_67.5aaaa.txt', 'w')
f.write('Settle time (s),Measured Distance (cm), Error to real distance = ' + str(real_distance) +' cm\n')
US = Ultrasonic_Sensor(20, settle_time = settle_time)
for i in range (0, 100):
print(i)
distance = US.get_distance()
error = real_distance - distance
write = str(settle_time) + ',' + str(distance) +',' + str(error) + "\n"
f.write(write)
print('settle time: ', round(settle_time,4), ' Distance: ', distance, ' Error:', error)
if __name__ == '__main__':
test_angle_impact()