0

MicroPython 1.0.0,ev3dev Linux echo 4.14.96-ev3dev-2.3.2-ev3 #1 PREEMPT Sun Jan 27 21:27:35 CST 2019 armv5tejl GNU/Linux

#!/usr/bin/env pybricks-micropython
from pybricks import ev3brick as brick
from pybricks.ev3devices import Motor
from pybricks.parameters import Port, Stop
from pybricks.tools import print, wait

leftMotor = Motor(Port.B)
rightMotor = Motor(Port.C)

# speed range -100 to 100

def leftWheel():
    leftMotor.run_target(50, 360, Stop.COAST, True)

def rightWheel():
    rightMotor.run_target(50, -360, Stop.COAST, True)

leftWheel()
rightWheel()

如果我使用 True,则此方法有效,因此它向左然后向右运行。但是,如果我将其设置为 false,则它什么也不做。[应该同时运行]

4

1 回答 1

1

当您选择wait=False时,将启动该run_target命令,并且您的程序会立即继续执行程序的其余部分。电机在后台完成其命令。

但是,如果您的程序中没有其他内容,程序将立即结束。当程序结束时,电机停止,因此在这种情况下您看不到任何运动。

如果您的程序中有其他东西(例如等待),您将看到电机移动:

#!/usr/bin/env pybricks-micropython
from pybricks import ev3brick as brick
from pybricks.ev3devices import Motor
from pybricks.parameters import Port, Stop
from pybricks.tools import print, wait

left_motor = Motor(Port.B)
right_motor = Motor(Port.C)

# Initiate the run target commands, without waiting for completion.
left_motor.run_target(50, 360, Stop.COAST, False)
right_motor.run_target(50, -360, Stop.COAST, False)

# Do something else while the motors are moving, like waiting.
wait(10000)

如果您的目标是等到两个电机都达到目标,您可以改为等到angle每个电机的值等于您给定的目标:

#!/usr/bin/env pybricks-micropython
from pybricks import ev3brick as brick
from pybricks.ev3devices import Motor
from pybricks.parameters import Port, Stop
from pybricks.tools import print, wait

left_motor = Motor(Port.B)
right_motor = Motor(Port.C)

target_left = 360
target_right = -360
tolerance = 2

left_motor.run_target(50, target_left, Stop.COAST, False)
right_motor.run_target(50, target_right, Stop.COAST, False)

# Wait until both motors have reached their targets up to a desired tolerance.
while abs(left_motor.angle()-target_left) > tolerance or abs(right_motor.angle()-target_right) > tolerance:
   wait(10)

# Make a sound when we're done.
brick.sound.beep()
于 2019-07-05T15:02:51.587 回答