当您选择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()