BP.set_motor_position() problems

Hello there,
I have run into a problem: The command BP.set_motor_position() does not work. In this code:

import brickpi3
import time
import sys

BP = brickpi3.BrickPi3()

BP.set_sensor_type(BP.PORT_1, BP.SENSOR_TYPE.NXT_ULTRASONIC)
BP.reset_motor_encoder(BP.PORT_A)

time.sleep(10)

def locate_ball():
    distance = BP.get_sensor(BP.PORT_1)
    BP.set_motor_power(BP.PORT_C, -35)
    BP.set_motor_power(BP.PORT_B, 35)
    if distance <= 20:
        BP.set_motor_power(BP.PORT_B + BP.PORT_C, 0)
        time.sleep(1)
        find_ball()
        
def find_ball():
    distance2 = BP.get_sensor(BP.PORT_1)
    BP.set_motor_power(BP.PORT_C, -35)
    BP.set_motor_power(BP.PORT_B, 35)
    if distance2 > 20:
        BP.set_motor_power(BP.PORT_B + BP.PORT_C, 0)
        time.sleep(1)
        grab_ball()

def grab_ball():
    BP.set_motor_power(BP.PORT_B + BP.PORT_C, 30)
    time.sleep(2.5)
    BP.set_motor_power(BP.PORT_B + BP.PORT_C, 0)
    BP.set_motor_position(BP.PORT_A, -3100)
    BP.reset_all()
    sys.exit()
    
try:
    while True:
        try:
            locate_ball()
        except brickpi3.SensorError as error:
            print(error)
except KeyboardInterrupt:
    BP.reset_all()

The last command in grab_ball() does not work. There, I specify BP.set_motor_position(BP.PORT_A, -3100) This is the instruction for moving the arm of my robot to an upwards position. But nothing happens at the end of the program, it is as if that line of code never were there.

But when I run this test program:

import brickpi3

BP = brickpi3.BrickPi3()

BP.reset_motor_encoder(BP.PORT_A)

BP.set_motor_position(BP.PORT_A, -3100)

Everything works as expected.

I really do not know what has happened here.

Does anyone else encounter this issue?
Thanks!

1 Like

Try putting

time.sleep(1) 

in between the set motor position and the reset_all()

1 Like