-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavoid_ultrasonic.py
224 lines (197 loc) · 5.94 KB
/
avoid_ultrasonic.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
#-*- coding:UTF-8 -*-
import RPi.GPIO as GPIO
import time
#小车电机引脚定义
IN1 = 20
IN2 = 21
IN3 = 19
IN4 = 26
ENA = 16
ENB = 13
#小车按键定义
key = 8
#超声波引脚定义
EchoPin = 0
TrigPin = 1
#设置GPIO口为BCM编码方式
GPIO.setmode(GPIO.BCM)
#忽略警告信息
GPIO.setwarnings(False)
#电机引脚初始化为输出模式
#按键引脚初始化为输入模式
#超声波引脚初始化
def init():
global pwm_ENA
global pwm_ENB
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)
GPIO.setup(key,GPIO.IN)
GPIO.setup(EchoPin,GPIO.IN)
GPIO.setup(TrigPin,GPIO.OUT)
#设置pwm引脚和频率为2000hz
pwm_ENA = GPIO.PWM(ENA, 2000)
pwm_ENB = GPIO.PWM(ENB, 2000)
pwm_ENA.start(0)
pwm_ENB.start(0)
#小车前进
def run(leftspeed, rightspeed):
GPIO.output(IN1, GPIO.HIGH)
GPIO.output(IN2, GPIO.LOW)
GPIO.output(IN3, GPIO.HIGH)
GPIO.output(IN4, GPIO.LOW)
pwm_ENA.ChangeDutyCycle(leftspeed)
pwm_ENB.ChangeDutyCycle(rightspeed)
#小车后退
def back(leftspeed, rightspeed):
GPIO.output(IN1, GPIO.LOW)
GPIO.output(IN2, GPIO.HIGH)
GPIO.output(IN3, GPIO.LOW)
GPIO.output(IN4, GPIO.HIGH)
pwm_ENA.ChangeDutyCycle(leftspeed)
pwm_ENB.ChangeDutyCycle(rightspeed)
#小车左转
def left(leftspeed, rightspeed):
GPIO.output(IN1, GPIO.LOW)
GPIO.output(IN2, GPIO.LOW)
GPIO.output(IN3, GPIO.HIGH)
GPIO.output(IN4, GPIO.LOW)
pwm_ENA.ChangeDutyCycle(leftspeed)
pwm_ENB.ChangeDutyCycle(rightspeed)
#小车右转
def right(leftspeed, rightspeed):
GPIO.output(IN1, GPIO.HIGH)
GPIO.output(IN2, GPIO.LOW)
GPIO.output(IN3, GPIO.LOW)
GPIO.output(IN4, GPIO.LOW)
pwm_ENA.ChangeDutyCycle(leftspeed)
pwm_ENB.ChangeDutyCycle(rightspeed)
#小车原地左转
def spin_left(leftspeed, rightspeed):
GPIO.output(IN1, GPIO.LOW)
GPIO.output(IN2, GPIO.HIGH)
GPIO.output(IN3, GPIO.HIGH)
GPIO.output(IN4, GPIO.LOW)
pwm_ENA.ChangeDutyCycle(leftspeed)
pwm_ENB.ChangeDutyCycle(rightspeed)
#小车原地右转
def spin_right(leftspeed, rightspeed):
GPIO.output(IN1, GPIO.HIGH)
GPIO.output(IN2, GPIO.LOW)
GPIO.output(IN3, GPIO.LOW)
GPIO.output(IN4, GPIO.HIGH)
pwm_ENA.ChangeDutyCycle(leftspeed)
pwm_ENB.ChangeDutyCycle(rightspeed)
#小车停止
def brake():
GPIO.output(IN1, GPIO.LOW)
GPIO.output(IN2, GPIO.LOW)
GPIO.output(IN3, GPIO.LOW)
GPIO.output(IN4, GPIO.LOW)
#按键检测
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
#超声波函数
'''
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
'''
#超声波函数
def Distance():
GPIO.output(TrigPin,GPIO.LOW)
time.sleep(0.000002)
GPIO.output(TrigPin,GPIO.HIGH)
time.sleep(0.000015)
GPIO.output(TrigPin,GPIO.LOW)
t3 = time.time()
while not GPIO.input(EchoPin):
t4 = time.time()
if (t4 - t3) > 0.03 :
return -1
t1 = time.time()
while GPIO.input(EchoPin):
t5 = time.time()
if(t5 - t1) > 0.03 :
return -1
t2 = time.time()
time.sleep(0.01)
# print "distance is %d " % (((t2 - t1)* 340 / 2) * 100)
return ((t2 - t1)* 340 / 2) * 100
def Distance_test():
num = 0
ultrasonic = []
while num < 5:
distance = Distance()
while int(distance) == -1 :
distance = Distance()
print("Tdistance is %f"%(distance) )
while (int(distance) >= 500 or int(distance) == 0) :
distance = Distance()
print("Edistance is %f"%(distance) )
ultrasonic.append(distance)
num = num + 1
time.sleep(0.01)
print ultrasonic
distance = (ultrasonic[1] + ultrasonic[2] + ultrasonic[3])/3
print("distance is %f"%(distance) )
return distance
#延时2s
time.sleep(2)
#try/except语句用来检测try语句块中的错误,
#从而让except语句捕获异常信息并处理。
try:
init()
key_scan()
while True:
distance = Distance_test()
if distance > 50:
run(50, 50) #当距离障碍物较远时全速前进
elif 30 <= distance <= 50:
run(35, 35) #当快靠近障碍物时慢速前进
elif distance < 30:
spin_right(55, 55)
time.sleep(0.35) #当靠近障碍物时原地右转大约90度
brake()
time.sleep(0.001)
distance = Distance_test() #再次测试判断前方距离
if distance >= 30:
run(35, 35) #转弯后当前方距离大于25cm时前进
elif distance < 30:
spin_left(55, 55)
time.sleep(0.6) #转弯后前方距离小于25cm时向左原地转弯180度
brake()
time.sleep(0.001)
distance = Distance_test() #再次测试判断前方距离
if distance >= 30:
run(35, 35) #转弯后当前方距离大于25cm时前进
elif distance < 30:
spin_left(55, 55) #转弯后前方距离小于25cm时向左原地转弯90度
time.sleep(0.3)
brake()
time.sleep(0.001)
except KeyboardInterrupt:
pass
pwm_ENA.stop()
pwm_ENB.stop()
GPIO.cleanup()