-
Notifications
You must be signed in to change notification settings - Fork 19
/
sensor.py
115 lines (93 loc) · 3.07 KB
/
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
115
import RPi.GPIO as GPIO
from time import time, sleep
from piarm import PiArm
class ultrasonic:
#GPIO Mode (BOARD / BCM)
GPIO.setmode(GPIO.BOARD)
# set GPIO Pin
Trigger_Pin = 31
Echo_Pin = 29
def __init__(self):
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
# Pin Configuration
GPIO.setup(self.Trigger_Pin, GPIO.OUT)
GPIO.setup(self.Echo_Pin, GPIO.IN)
def distance_check(self):
'''
Check distance from the pulse duration
'''
self.trigger()
StartTime = time()
StopTime = time()
# save StartTime
while GPIO.input(self.Echo_Pin) == 0:
StartTime = time()
# save time of arrival
while GPIO.input(self.Echo_Pin) == 1:
StopTime = time()
# time difference between start and arrival
TimeElapsed = StopTime - StartTime
# multiply with the sonic speed (34300 cm/s) and divide by 2
distance = (TimeElapsed * 34300) / 2
return distance
def trigger(self):
'''
Send high on Trigger pin
'''
# set Trigger_Pin to HIGH
GPIO.output(self.Trigger_Pin, True)
# set Trigger_Pin after 0.01ms to LOW
sleep(0.00001)
GPIO.output(self.Trigger_Pin, False)
class example_ultrasonic:
robot = PiArm()
# write your serial comm
robot.connect("/dev/ttyS0")
def __init__(self, distance):
self.usonic = ultrasonic()
self.min_dist = distance
self.commands = [[503, 455, 681, 325, 541, 563],
[263, 457, 848, 229, 552, 565],
[600, 456, 847, 229, 559, 565],
[600, 456, 681, 325, 542, 566],
[600, 456, 679, 327, 555, 924],
[600, 456, 910, 325, 643, 924],
[400, 456, 909, 323, 647, 924],
[600, 456, 679, 327, 555, 924],
[503, 455, 681, 325, 541, 563],
]
def distance_compare(self):
'''
compare distance and return object detection status
'''
distance = self.usonic.distance_check()
print(distance)
if distance <= self.min_dist:
return True
else:
return False
def move_piarm(self):
'''
move PiArm if ditance is in given minimum distance
'''
if self.distance_compare():
print('changing PiArm position')
for command in self.commands:
for ID in range(6):
self.robot.servoWrite(ID + 1, command[ID], 500)
sleep(1)
else:
pass
if __name__ == '__main__':
ex = example_ultrasonic(distance = 20)
try:
while True:
ex.move_piarm()
sleep(.5)
except KeyboardInterrupt:
robot = PiArm()
# write your serial comm
robot.connect("/dev/ttyS0")
for ID in range(1, 7):
robot.servoWrite(ID, 500, 500)