motor.run_target命令中是否存在错误?

时间:2019-07-03 11:52:33

标签: micropython pybricks-micropython

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

<TargetFrameworkVersion>v9.0</TargetFrameworkVersion>
<AndroidEnableMultiDex>true</AndroidEnableMultiDex>

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

1 个答案:

答案 0 :(得分: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()