-
Notifications
You must be signed in to change notification settings - Fork 1
/
SonarTest.py
261 lines (203 loc) · 6.28 KB
/
SonarTest.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#-*- coding:UTF-8 -*-
import RPi.GPIO as GPIO
import time
#Definition of motor pin
IN1 = 20
IN2 = 21
IN3 = 19
IN4 = 26
ENA = 16
ENB = 13
ServoSonar = 23
#key definition (button on PCB)
key = 8
#Definition of ultrasonic module pin
EchoPin = 0
TrigPin = 1
#Set the GPIO port to BCM encoding mode
GPIO.setmode(GPIO.BCM)
#Ignore warning information
GPIO.setwarnings(False)
#Motor pin initialization operation
def init():
global pwm_ENA
global pwm_ENB
global delaytime
global pwm_ServoSonar
GPIO.setup(ENA,GPIO.OUT,initial=GPIO.HIGH)
GPIO.setup(IN1,GPIO.OUT,initial=GPIO.LOW)
GPIO.setup(IN2,GPIO.OUT,initial=GPIO.LOW)
GPIO.setup(ENB,GPIO.OUT,initial=GPIO.HIGH)
GPIO.setup(IN3,GPIO.OUT,initial=GPIO.LOW)
GPIO.setup(IN4,GPIO.OUT,initial=GPIO.LOW)
#Set the PWM pin and frequency is 2000hz
GPIO.setup(key,GPIO.IN) # setting button as input
GPIO.setup(EchoPin,GPIO.IN)
GPIO.setup(ServoSonar, GPIO.OUT)#ServoSonar setup
GPIO.setup(TrigPin,GPIO.OUT)#pins for sensor emmitter and listenning
pwm_ServoSonar = GPIO.PWM(ServoSonar, 50)
pwm_ServoSonar.start(6)
pwm_ENA = GPIO.PWM(ENA, 2000)
pwm_ENB = GPIO.PWM(ENB, 2000)
pwm_ENA.start(0)
pwm_ENB.start(0)
#turn left in place after detecting obstacle
def avoid_left():
GPIO.output(IN1, GPIO.LOW)
GPIO.output(IN2, GPIO.HIGH)
GPIO.output(IN3, GPIO.HIGH)
GPIO.output(IN4, GPIO.LOW)
pwm_ENA.ChangeDutyCycle(50)
pwm_ENB.ChangeDutyCycle(50)
time.sleep(1.5)
#Button detection
def key_scan():
while GPIO.input(key):
pass
while not GPIO.input(key):
time.sleep(0.01)
if not GPIO.input(key):
time.sleep(0.01)
while not GPIO.input(key):
pass
#Ultrasonic function
def Distance_test():
GPIO.output(TrigPin,GPIO.HIGH)
time.sleep(0.000015)
GPIO.output(TrigPin,GPIO.LOW)
while not GPIO.input(EchoPin):
pass
t1 = time.time()
while GPIO.input(EchoPin):
pass
t2 = time.time()
print "distance is %d " % (((t2 - t1)* 340 / 2) * 100)
time.sleep(0.01)
return ((t2 - t1)* 340 / 2) * 100
#advance
def run(speed):
GPIO.output(IN1, GPIO.HIGH)
GPIO.output(IN2, GPIO.LOW)
GPIO.output(IN3, GPIO.HIGH)
GPIO.output(IN4, GPIO.LOW)
pwm_ENA.ChangeDutyCycle(speed)
pwm_ENB.ChangeDutyCycle(speed)
#back
def back():
GPIO.output(IN1, GPIO.LOW)
GPIO.output(IN2, GPIO.HIGH)
GPIO.output(IN3, GPIO.LOW)
GPIO.output(IN4, GPIO.HIGH)
pwm_ENA.ChangeDutyCycle(25)
pwm_ENB.ChangeDutyCycle(25)
#turn left
def left():
GPIO.output(IN1, GPIO.LOW)
GPIO.output(IN2, GPIO.LOW)
GPIO.output(IN3, GPIO.HIGH)
GPIO.output(IN4, GPIO.LOW)
pwm_ENA.ChangeDutyCycle(25)
pwm_ENB.ChangeDutyCycle(50)
#turn right
def right():
GPIO.output(IN1, GPIO.HIGH)
GPIO.output(IN2, GPIO.LOW)
GPIO.output(IN3, GPIO.LOW)
GPIO.output(IN4, GPIO.LOW)
pwm_ENA.ChangeDutyCycle(50)
pwm_ENB.ChangeDutyCycle(25)
#turn left in place
def spin_left():
GPIO.output(IN1, GPIO.LOW)
GPIO.output(IN2, GPIO.HIGH)
GPIO.output(IN3, GPIO.HIGH)
GPIO.output(IN4, GPIO.LOW)
pwm_ENA.ChangeDutyCycle(50)
pwm_ENB.ChangeDutyCycle(50)
#turn right in place
def spin_right():
GPIO.output(IN1, GPIO.HIGH)
GPIO.output(IN2, GPIO.LOW)
GPIO.output(IN3, GPIO.LOW)
GPIO.output(IN4, GPIO.HIGH)
pwm_ENA.ChangeDutyCycle(50)
pwm_ENB.ChangeDutyCycle(50)
#back to the right
def back_to_right():
GPIO.output(IN1, GPIO.LOW)
GPIO.output(IN2, GPIO.HIGH)
GPIO.output(IN3, GPIO.LOW)
GPIO.output(IN4, GPIO.LOW)
pwm_ENA.ChangeDutyCycle(50)
pwm_ENB.ChangeDutyCycle(50)
#brake
def brake():
GPIO.output(IN1, GPIO.LOW)
GPIO.output(IN2, GPIO.LOW)
GPIO.output(IN3, GPIO.LOW)
GPIO.output(IN4, GPIO.LOW)
pwm_ENA.ChangeDutyCycle(10)
pwm_ENB.ChangeDutyCycle(10)
def Distancetotheright():
distance = Distance_test()
print('searching right')
pwm_ServoSonar.ChangeDutyCycle(4)
time.sleep(1)
pwm_ServoSonar.ChangeDutyCycle(0)
distance = Distance_test()
def Distancetotheleft():
distance = Distance_test()
print('searching left')
pwm_ServoSonar.ChangeDutyCycle(8)
time.sleep(1)
pwm_ServoSonar.ChangeDutyCycle(0)
distance = Distance_test()
try:
init()
while True:
if Distance_test() > 50:
run(50)
elif 25 <= Distance_test() <= 50:
run(20)
elif Distance_test() < 25:
brake()#we are static and we search to the right
time.sleep(0.5)
print('Obstacle Ahead')
Distancetotheright()
if Distance_test() > 50:
print('Clear path to the right')
spin_right()
time.sleep(0.5)
pwm_ServoSonar.ChangeDutyCycle(6.5)
time.sleep(0.5)
pwm_ServoSonar.ChangeDutyCycle(0)
print("resetting Servo")
time.sleep(0.5)
elif Distance_test() < 50:#static we found an obstacle to the right
#Distance_test()
print('Obstacle to the right, searching left')
Distancetotheleft()
time.sleep(0.5)
if Distance_test() < 50:
print('found obstacle to the left too, going back')
spin_left()
time.sleep(1.5)
pwm_ServoSonar.ChangeDutyCycle(6.5)
time.sleep(0.5)
pwm_ServoSonar.ChangeDutyCycle(0)
print("resetting Servo")
time.sleep(0.5)
elif Distance_test() > 50: #static no obstacle to the left
print('Clear path to the left')
spin_left()
time.sleep(0.5)
pwm_ServoSonar.ChangeDutyCycle(6.5)
time.sleep(0.5)
pwm_ServoSonar.ChangeDutyCycle(0)
distance = Distance_test()
finally:
GPIO.cleanup()
pwm_ENA.stop()
pwm_ENB.stop()
pwm_ServoSonar.stop()
GPIO.cleanup()