小白第一次上手 python ,用树莓派,红外和超声波传感器做智障小车。 编译错误:
RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.
GPIO.setup(trip,GPIO.OUT)
Traceback (most recent call last):
File "xiaochetest.py", line 82, in <module>
fwd()
TypeError: fwd() takes exactly 1 argument (0 given)
下面是小车的代码:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
m1_fwd = 12
m1_rev = 11
m2_fwd = 13
m2_rev = 15
red_left = 07
red_right = 16
trip = 38
echo = 37
def init():
GPIO.setup(m1_fwd,GPIO.OUT)
GPIO.setup(m1_rev,GPIO.OUT)
GPIO.setup(m2_fwd,GPIO.OUT)
GPIO.setup(m2_rev,GPIO.OUT)
def stop(sleep_time):
GPIO.output(m1_fwd,False)
GPIO.output(m1_rev,False)
GPIO.output(m2_fwd,False)
GPIO.output(m2_rev,False)
time.sleep(sleep_time)
GPIO.cleanup()
def fwd(sleep_time):
GPIO.output(m1_fwd,GPIO.HIGH)
GPIO.output(m1_rev,GPIO.LOW)
GPIO.output(m2_fwd,GPIO.HIGH)
GPIO.output(m2_rev,GPIO.LOW)
time.sleep(sleep_time)
GPIO.cleanup()
def rev(sleep_time):
GPIO.output(m1_fwd,GPIO.LOW)
GPIO.output(m1_rev,GPIO.HIGH)
GPIO.output(m2_fwd,GPIO.LOW)
GPIO.output(m2_rev,GPIO.HIGH)
time.sleep(sleep_time)
GPIO.cleanup()
def right(sleep_time):
GPIO.output(m1_fwd,GPIO.HIGH)
GPIO.output(m1_rev,GPIO.LOW)
GPIO.output(m2_fwd,False)
GPIO.output(m2_rev,False)
time.sleep(sleep_time)
GPIO.cleanup()
def left(sleep_time):
GPIO.output(m1_fwd,False)
GPIO.output(m1_rev,False)
GPIO.output(m2_fwd,GPIO.HIGH)
GPIO.output(m2_rev,GPIO.LOW)
time.sleep(sleep_time)
GPIO.cleanup()
def get_distance():
GPIO.setup(trip,GPIO.OUT)
GPIO.setup(echo,GPIO.IN)
GPIO.output(trip,GPIO.HIGH)
time.sleep(0.000015)
GPIO.output(trip,GPIO.LOW)
while not GPIO.input(echo):
pass
t1 = time.time()
while GPIO.input(echo):
pass
t2 = time.time()
return (t2-t1)*34300/2
def turnaround():
GPIO.setup(red_left,GPIO.IN)
GPIO.setup(red_right,GPIO.IN)
while GPIO.input(red_left) and GPIO.input(red_right)==0:
rev()
if GPIO.input(red_left)==1:
left(1)
else:
right(1)
GPIO.cleanup()
while True:
distance = get_distance()
time.sleep(0.5)
if distance > 20:
fwd()
elif distance == 20:
stop()
else:
stop()
turnaround()
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.