-
Notifications
You must be signed in to change notification settings - Fork 0
/
MAC Old Version.py
206 lines (154 loc) · 5.32 KB
/
MAC Old Version.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#Libraries
import RPi.GPIO as GPIO
import time
import random
from gpiozero import Motor
#GPIO Mode (BOARD / BCM)
GPIO.setmode(GPIO.BCM)
#set GPIO Pins
GPIO_TRIGGERright = 18
GPIO_ECHOright = 24
GPIO_TRIGGERmiddle = 18
GPIO_ECHOmiddle = 24
GPIO_TRIGGERleft = 18
GPIO_ECHOleft = 24
#Setting Motors
motorleft = Motor(4, 14)
motorright = Motor(17, 27)
#set GPIO direction (IN / OUT)
GPIO.setup(GPIO_TRIGGERmiddle, GPIO.OUT)
GPIO.setup(GPIO_ECHOmiddle, GPIO.IN)
GPIO.setup(GPIO_TRIGGERright, GPIO.OUT)
GPIO.setup(GPIO_ECHOright, GPIO.IN)
GPIO.setup(GPIO_TRIGGERleft, GPIO.OUT)
GPIO.setup(GPIO_ECHOleft, GPIO.IN)
def distanceMid():
# set Trigger to HIGH
GPIO.output(GPIO_TRIGGERmiddle, True)
# set Trigger after 0.01ms to LOW
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGERmiddle, False)
StartTime = time.time()
StopTime = time.time()
# save StartTime
while GPIO.input(GPIO_ECHOmiddle) == 0:
StartTime = time.time()
# save time of arrival
while GPIO.input(GPIO_ECHOmiddle) == 1:
StopTime = time.time()
# time difference between start and arrival
TimeElapsed = StopTime - StartTime
# multiply with the sonic speed (34300 cm/s)
# and divide by 2, because there and back
distance = (TimeElapsed * 34300) / 2
return distance
def distanceLeft():
# set Trigger to HIGH
GPIO.output(GPIO_TRIGGERleft, True)
# set Trigger after 0.01ms to LOW
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGERleft, False)
StartTime = time.time()
StopTime = time.time()
# save StartTime
while GPIO.input(GPIO_ECHOleft) == 0:
StartTime = time.time()
# save time of arrival
while GPIO.input(GPIO_ECHOleft) == 1:
StopTime = time.time()
# time difference between start and arrival
TimeElapsed = StopTime - StartTime
# multiply with the sonic speed (34300 cm/s)
# and divide by 2, because there and back
distance = (TimeElapsed * 34300) / 2
return distance
def distanceRight():
# set Trigger to HIGH
GPIO.output(GPIO_TRIGGERright, True)
# set Trigger after 0.01ms to LOW
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGERright, False)
StartTime = time.time()
StopTime = time.time()
# save StartTime
while GPIO.input(GPIO_ECHOright) == 0:
StartTime = time.time()
# save time of arrival
while GPIO.input(GPIO_ECHOmiddle) == 1:
StopTime = time.time()
# time difference between start and arrival
TimeElapsed = StopTime - StartTime
# multiply with the sonic speed (34300 cm/s)
# and divide by 2, because there and back
distance = (TimeElapsed * 34300) / 2
return distance
if __name__ == '__main__':
try:
while True:
motorleft.forward(0.4)
motorright.forward(0.4)
distMid = distanceMid()
print ("MIddle Distance = %.1f cm" % distMid)
time.sleep(0.01)
distRight = distanceRight()
print ("Right Distance = %.1f cm" % distRight)
time.sleep(0.01)
distLeft = distanceLeft()
print ("Left Distance = %.1f cm" % distLeft)
time.sleep(0.01)
if distLeft < 15.0:
motorleft.stop()
motorright.stop()
time.sleep(1)
motorleft.forward(0.35)
motorright.backward(0.35)
time.sleep(0.4)
motorleft.stop()
motorright.stop()
time.sleep(1)
elif distRight < 15.0:
motorleft.stop()
motorright.stop()
time.sleep(1)
motorleft.backward(0.35)
motorright.forward(0.35)
time.sleep(0.4)
motorleft.stop()
motorright.stop()
time.sleep(1)
elif distMid < 25.0:
X = random.randint(0,1)
motorleft.stop()
motorright.stop()
time.sleep(1)
motorleft.backward(0.35)
motorright.backward(0.35)
time.sleep(0.4)
#THIS SHIT GO LEFT
if X == 1:
motorleft.stop()
motorright.stop()
time.sleep(1)
motorleft.backward(0.35)
motorright.forward(0.35)
time.sleep(0.4)
motorleft.stop()
motorright.stop()
time.sleep(1)
#THIS SHIT GO RIGHT
else:
motorleft.stop()
motorright.stop()
time.sleep(1)
motorleft.forward(0.35)
motorright.backward(0.35)
time.sleep(0.4)
motorleft.stop()
motorright.stop()
time.sleep(1)
# Reset by pressing CTRL + C
except KeyboardInterrupt:
print("Measurement stopped by User")
motorleft.stop()
motorright.stop()
GPIO.cleanup()