Is there a Bug in motor.run_target command?

296 views Asked by At

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()

This works if I use True, so it runs left then right. But if I set it false it does nothing. [should run both in parallel]

1

There are 1 answers

0
Laurens Valk On BEST ANSWER

When you choose wait=False, the run_target command is initiated and your program continues with the rest of your program right away. The motor completes its command in the background.

But if there is nothing else in your program, the program ends right away. And when the program ends, the motors are stopped so you don't see any movement in this case.

You will see the motors move if there is something else in your program like a wait:

#!/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)

If your goal is to wait until both motors have reached their target, you could instead wait until the angle values of each motor equal your given targets:

#!/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()