Motor to Position

Here is some code to move a motor to specific positions (relative to where it is now).

#!/usr/bin/python3

from time import sleep
def main():

    position1 = 720 # forward two full rotations
    speed1 = 400

    position2 = -360 # backward one full rotation
    speed2 = 200

    # set up the motor
    ev3 = Device('this')
    m = ev3.LargeMotor('outB')

    # run to position 1
    print('Going to Position 1: ' + str(position1))
    m.run_to_rel_pos(position_sp=position1, speed_sp=speed1, stop_action="hold")
    # wait till done moving (keep checking)
    while m.is_running:
        sleep(0.1)

    # wait 1/2 second before moving to next position
    sleep(0.5)

    # run to position 2
    print('Going to Position 2: ' + str(position2))
    m.run_to_rel_pos(position_sp=position2, speed_sp=speed2, stop_action="hold")
    # wait till done moving (keep checking)
    while m.is_running:
        sleep(0.1)

    # "relax" the motor at end of the program
    m.stop(stop_action="coast")

if __name__ == '__main__':
    main()

This uses _run_to_relpos.  There is also a _run_to_abspos function.

This also uses a "while m.is_running: sleep(0.1)" trick to wait until motor stops running.  Note there is also a "m.wait_while('running')" command that might work instead.

More on the ev3dev Python motor functions: