-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetectObjectV1.py
410 lines (277 loc) · 11.2 KB
/
detectObjectV1.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import time
import RPi.GPIO as GPIO
import json
import RGB
import Adafruit_PCA9685
pwm = Adafruit_PCA9685.PCA9685()
pwm.set_pwm_freq(50)
Tr = 11 #Pin No. of Ultrasonic Module Input
Ec = 8 # Pin number of the output end of the ultrasonic module
servoPort = 1 #The number of the servo that controls the horizontal rotation of the ultrasonic module
servoMiddle = 330 #The middle position of the servo
servoLeft = 180 #Left position of the servo
servoRight = 480 #The right position of the servo
servoPort2 = 2 #The number of the servo that controls the horizontal rotation of the ultrasonic module
rangeKeep = 0.3 #Avoidance distance
scanDir = 1 #Scan direction, 1 is from left to right, -1 is from right to left
scanPos = 1 #Store the current scan position (1 is the left, 2 is the middle, and 3 is the right)
scanNum = 3 #The number of scan positions (left, middle, and right, these are three positions)
scanList = [0,0,0] #Save scan results
GPIO.setmode(GPIO.BCM)
GPIO.setup(Tr, GPIO.OUT,initial=GPIO.LOW)
GPIO.setup(Ec, GPIO.IN)
Motor_A_EN = 4
Motor_B_EN = 17
Motor_A_Pin1 = 14
Motor_A_Pin2 = 15
Motor_B_Pin1 = 27
Motor_B_Pin2 = 18
Dir_forward = 0
Dir_backward = 1
left_forward = 0
left_backward = 1
right_forward = 0
right_backward= 1
pwn_A = 0
pwm_B = 0
pwm2_direction = 1
pwm2_init = 300
pwm2_range = 150
pwm2_max = 450
pwm2_min = 150
pwm2_pos = pwm2_init
RGB.setup()
RGB.cyan()
def ctrl_range(raw, max_genout, min_genout):
if raw > max_genout:
raw_output = max_genout
elif raw < min_genout:
raw_output = min_genout
else:
raw_output = raw
return int(raw_output)
def turnLeft(coe=1):
global pwm2_pos
pwm2_pos = pwm2_init + int(coe*pwm2_range*pwm2_direction)
pwm2_pos = ctrl_range(pwm2_pos, pwm2_max, pwm2_min)
RGB.turn_left(3)
#RGB.yellow()
pwm.set_pwm(2, 0, 225)
def turnRight(coe=1):
global pwm2_pos
pwm2_pos = pwm2_init - int(coe*pwm2_range*pwm2_direction)
pwm2_pos = ctrl_range(pwm2_pos, pwm2_max, pwm2_min)
RGB.turn_right(3)
#RGB.yellow()
pwm.set_pwm(2, 0, 375)
def turnMiddle():
global pwm2_pos
pwm2_pos = pwm2_init
RGB.both_on()
pwm.set_pwm(2, 0, pwm2_pos)
def motorStop():#Motor stops
GPIO.output(Motor_A_Pin1, GPIO.LOW)
GPIO.output(Motor_A_Pin2, GPIO.LOW)
GPIO.output(Motor_B_Pin1, GPIO.LOW)
GPIO.output(Motor_B_Pin2, GPIO.LOW)
GPIO.output(Motor_A_EN, GPIO.LOW)
GPIO.output(Motor_B_EN, GPIO.LOW)
def setup():#Motor initialization
global pwm_A, pwm_B
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(Motor_A_EN, GPIO.OUT)
GPIO.setup(Motor_B_EN, GPIO.OUT)
GPIO.setup(Motor_A_Pin1, GPIO.OUT)
GPIO.setup(Motor_A_Pin2, GPIO.OUT)
GPIO.setup(Motor_B_Pin1, GPIO.OUT)
GPIO.setup(Motor_B_Pin2, GPIO.OUT)
motorStop()
try:
pwm_A = GPIO.PWM(Motor_A_EN, 1000)
pwm_B = GPIO.PWM(Motor_B_EN, 1000)
except:
pass
def motor(status, direction, speed):#Motor 2 positive and negative rotation
if status == 0: # stop
GPIO.output(Motor_B_Pin1, GPIO.LOW)
GPIO.output(Motor_B_Pin2, GPIO.LOW)
GPIO.output(Motor_B_EN, GPIO.LOW)
else:
if direction == Dir_backward:
turnMiddle()
#pwm.set_pwm(servoPort2, 0, servoMiddle2)
GPIO.output(Motor_B_Pin1, GPIO.HIGH)
GPIO.output(Motor_B_Pin2, GPIO.LOW)
pwm_B.start(100)
pwm_B.ChangeDutyCycle(speed)
elif direction == Dir_forward:
turnMiddle()
#pwm.set_pwm(servoPort2, 0, servoMiddle2)
GPIO.output(Motor_B_Pin1, GPIO.LOW)
GPIO.output(Motor_B_Pin2, GPIO.HIGH)
pwm_B.start(0)
pwm_B.ChangeDutyCycle(speed)
def motor_left(status, direction, speed):#Motor 2 positive and negative rotation
if status == 0: # stop
GPIO.output(Motor_B_Pin1, GPIO.LOW)
GPIO.output(Motor_B_Pin2, GPIO.LOW)
GPIO.output(Motor_B_EN, GPIO.LOW)
else:
if direction == Dir_backward:
turnLeft()
#pwm.set_pwm(servoPort2, 0, servoLeft2)
GPIO.output(Motor_B_Pin1, GPIO.HIGH)
GPIO.output(Motor_B_Pin2, GPIO.LOW)
pwm_B.start(100)
pwm_B.ChangeDutyCycle(speed)
elif direction == Dir_forward:
turnLeft()
#pwm.set_pwm(servoPort2, 0, servoLeft2)
GPIO.output(Motor_B_Pin1, GPIO.LOW)
GPIO.output(Motor_B_Pin2, GPIO.HIGH)
pwm_B.start(0)
pwm_B.ChangeDutyCycle(speed)
def motor_right(status, direction, speed):#Motor 1 positive and negative rotation
if status == 0: # stop
GPIO.output(Motor_B_Pin1, GPIO.LOW)
GPIO.output(Motor_B_Pin2, GPIO.LOW)
GPIO.output(Motor_B_EN, GPIO.LOW)
else:
if direction == Dir_forward:#
turnRight()
#pwm.set_pwm(servoPort2, 0, servoRight2)
GPIO.output(Motor_B_Pin1, GPIO.HIGH)
GPIO.output(Motor_B_Pin2, GPIO.LOW)
pwm_B.start(100)
pwm_B.ChangeDutyCycle(speed)
elif direction == Dir_backward:
turnRight()
#pwm.set_pwm(servoPort2, 0, servoRight2)
GPIO.output(Motor_B_Pin1, GPIO.LOW)
GPIO.output(Motor_B_Pin2, GPIO.HIGH)
pwm_B.start(0)
pwm_B.ChangeDutyCycle(speed)
return direction
def move(speed, direction, turn, radius=0.6): # 0 < radius <= 1
#speed = 100
if direction == 'forward':
if turn == 'right':
motor_right(1, right_forward, speed)
elif turn == 'left':
motor_left(1, left_forward, speed)
else:
motor(1, left_forward, speed)
elif direction == 'backward':
if turn == 'right':
motor_right(1, right_backward, speed)
elif turn == 'left':
motor_left(1, left_backward, speed)
else:
motor(1, left_backward, speed)
elif direction == 'no':
if turn == 'right':
motor_right(1, right_forward, speed)
elif turn == 'left':
motor_left(1, left_forward, speed)
else:
motorStop()
else:
pass
def destroy():
motorStop()
GPIO.cleanup() # Release resource
def checkdist():
''' Refer to the realization of basic functions-ultrasonic module '''
GPIO.output(Tr, GPIO.HIGH) # Set the input terminal of the module to high level and send out an initial sound wave.
time.sleep(0.000015)
GPIO.output(Tr, GPIO.LOW)
while not GPIO.input(Ec): # When the module no longer receives the initial sound wave ①
pass
t1 = time.time() # Write down the time when the initial sound wave was emitted.
while GPIO.input(Ec): # When the module receives the return sound wave.
pass
t2 = time.time() # Write down the time when the return sound wave was captured.
return (t2-t1)*340/2 # Calculate the distance.
while 1:
print('Automatic obstacle avoidance mode')
if scanPos == 1:
pwm.set_pwm(servoPort, 0, servoLeft)
time.sleep(0.3)
scanList[0] = checkdist()
elif scanPos == 2:
pwm.set_pwm(servoPort, 0, servoMiddle)
time.sleep(0.3)
scanList[1] = checkdist()
elif scanPos == 3:
pwm.set_pwm(servoPort, 0, servoRight)
time.sleep(0.3)
scanList[2] = checkdist()
scanPos = scanPos + scanDir
if scanPos > scanNum or scanPos < 1:
if scanDir == 1:scanDir = -1
elif scanDir == -1:scanDir = 1
scanPos = scanPos + scanDir*2
print(scanList)
if min(scanList) < rangeKeep:
if scanList.index(min(scanList)) == 0: #The shortest distance detected on the left
'''
Turn right
'''
speed_set = 80
setup()
move(speed_set, 'forward', 'right', 0.8)
print('Turn right')
time.sleep(1.3)
motorStop()
elif scanList.index(min(scanList)) == 1: #The shortest distance detected in the middle
if scanList[0] < scanList[2]:
'''
If the detected distance on the left is shorter than the right, turn to the right
'''
speed_set = 80
setup()
move(speed_set, 'forward', 'right', 0.8)
print('Turn right')
time.sleep(1.3)
motorStop()
else:
'''''
Otherwise, turn left
'''
speed_set = 80
setup()
move(speed_set, 'forward', 'left', 0.8)
time.sleep(1.3)
motorStop()
print('Turn left')
elif scanList.index(min(scanList)) == 2: #The shortest distance detected on the right
'''
Turn Left
'''
speed_set = 80
setup()
move(speed_set, 'forward', 'left', 0.8)
time.sleep(1.3)
motorStop()
print('Turn Left')
elif max(scanList) < rangeKeep:
'''
If the distances in the left, center, and right directions are all closer than rangeKeep, reverse
'''
speed_set = 80
setup()
move(speed_set, 'backward', 'no', 0.8)
time.sleep(1.3)
motorStop()
print('reverse')
else:
'''
All three directions are farther than rangeKeep
'''
speed_set = 80
setup()
move(speed_set, 'forward', 'no', 0.8)
time.sleep(1.3)
motorStop()
print('Go forward')