Python Wait until Function Terminates Before Continuing

We have created a function that handles PID turning for autonomous skills, however when it is called, the script continues to execute the next lines of code within the autonomous function without waiting for the PID turning to complete.

please post the code, that’s the only way we can help.

3 Likes

only thing I see is that “cprint” is not an available function for VEX Python, is that defined somewhere else ?

3 Likes
def turn1(expected):

    expected = (expected + drivetrain_inertial.rotation(DEGREES))

    wait(0.25, SECONDS)
    
    error = expected

    while True:
        actual = drivetrain_inertial.rotation(DEGREES)
        error = (expected - actual)
        speed = (error * 0.5)
        left_motor_a.set_velocity(speed,PERCENT)
        left_motor_b.set_velocity(speed,PERCENT)
        right_motor_a.set_velocity(-speed,PERCENT)
        right_motor_b.set_velocity(-speed,PERCENT)
        
        left_motor_a.spin(FORWARD)
        left_motor_b.spin(FORWARD)
        right_motor_a.spin(FORWARD)
        right_motor_b.spin(FORWARD)
        
        cprint(3,str(error))
        
        wait(0.15,SECONDS)
        
        if abs(error) < 2:
            break

        
    
    drivetrain.stop()
    cprint(2,'PID Terminated')```

Called in Context of: 
while rear_distance.object_distance(MM) < 1600:
    drivetrain.drive(FORWARD)
drivetrain.stop()
turn1(13)```

Hi,

“cprint” is defined elsewhere; it is used to print text on the controller screen.