这是个用遥控器控制一个小车的程序,用红外传感器来接收遥控器,左上按键左转(左轮不动,右轮动),右上按键右转(左轮动,右轮不动),左下和右下按键直退。
from pybricks import ev3brick as brick
from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor,
InfraredSensor, UltrasonicSensor, GyroSensor)
from pybricks.parameters import (Port, Stop, Direction, Button, Color,
SoundFile, ImageFile, Align)
from pybricks.tools import print, wait, StopWatch
from pybricks.robotics import DriveBase
# Write your program here
brick.sound.beep()
infra_sensor = InfraredSensor(Port.S3)
white_motor = Motor(Port.D)
red_motor = Motor(Port.A)
print(Button.LEFT_UP)
while True:
buttons = infra_sensor.buttons(1)
print(buttons)
if Button.LEFT_UP in buttons and Button.RIGHT_UP in buttons:
white_motor.run(1000)
red_motor.run(1000)
elif Button.LEFT_UP in buttons:
white_motor.run(1000)
red_motor.run(0)
elif Button.RIGHT_UP in buttons:
white_motor.run(0)
red_motor.run(1000)
elif Button.LEFT_DOWN in buttons or Button.RIGHT_DOWN in buttons:
white_motor.run(-1000)
red_motor.run(-1000)
wait(100)