用Python画小车车

Python017

用Python画小车车,第1张

你好,下面是一个对应的代码

import turtle

import time

t = turtle.Pen()

def fun1(t, x, y):

t.forward(x)

t.left(y)

def fun2(t, x, y):

t.forward(x)

t.right(y)

'''

color函数有三个参数

第一个参数指定有多少红色

第二个参数指定有多少绿色

第三个参数指定有多少蓝色

都为0的时候此时为黑色

都为1的时候此时为白色

这种红色,绿色,蓝色的混搭叫做RGB

蓝色和红色混合产生紫色

黄色和红色混合产生橙色

'''

t.color(1, 0, 0)

t.begin_fill()

fun1(t, 100, 90)

fun1(t, 20, 90)

fun2(t, 20, 90)

fun1(t, 20, 90)

fun1(t, 60, 90)

fun2(t, 20, 90)

fun1(t, 20, 90)

t.forward(20)

t.end_fill()

t.color(0, 0, 0)

t.up()

t.forward(10)

t.down()

# 开始位置

#t.begin_fill()

# 画圆

t.circle(10)

# 结束位置

#t.end_fill()

# 设置当前的指定角度为0度

t.setheading(0)

t.up()

t.forward(90)

t.right(90)

t.forward(10)

t.setheading(0)

t.down()

#t.begin_fill()

t.circle(10)

#t.end_fill()

t.up()

time.sleep(20)

小白第一次上手 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()

def fwd(sleep_time)

if distance >20:

fwd()

调用 fwd 的时候要传参数啊,错误提示说的比较清楚了。

: test_start_makes_vehicle_run ▲点赞 7 # 需要导入模块: from vehicle import Vehicle [as 别名]# 或者: from vehicle.Vehicle i...

纯净天空