motor.run_target 命令中有错误吗?

Is there a Bug in motor.run_target command?

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,它什么也不做。 [应该运行两者并行]

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