Turning Troubles

I am programing my vex brain using Python. I am trying to use the inertial sensor for accurate turning but when I attach the turning to a for loop it will not make multiple turns. It makes the first turn, second turn, and then skips over the third turn. brain_inertial.set_rotation is suppose to be indented

def accurate_turn(degrees):
brain_inertial.set_rotation(0, DEGREES)

wait(.5, SECONDS)
driveMotors_motor_a.set_velocity(5, PERCENT)
driveMotors_motor_b.set_velocity(5, PERCENT)
wait(1, SECONDS)
if degrees > 0:
    while brain_inertial.rotation(DEGREES) < degrees:
        driveMotors_motor_a.spin(FORWARD)
        driveMotors_motor_b.spin(REVERSE)
    driveMotors.stop()
    
else:
    while brain_inertial.rotation(DEGREES) > degrees:
        driveMotors_motor_a.spin(REVERSE)
        driveMotors_motor_b.spin(FORWARD)
    driveMotors.stop()

#Main Program
brain_inertial.calibrate()
while brain_inertial.is_calibrating():
wait(50, MSEC)

for sides in range(4):
linear_movement(24)
wait(1, SECONDS)
accurate_turn(-90)
wait(1, SECONDS)

Make a turning pd controller.

while true:
err=target-inertial.heading
speed=err-lasterr
lasterr=err
pdout=errkp+speedkd
motorLeft.spin(forward, pdout*.12, volt)
motorRight.spin(forward, -pdout*.12, volt)

wait(.02, SECONDS)
if abs(err)<threshold and abs(speed)<threshold  //this exits the loop and moves on.
    break 

then just set up a threshold (threshold=4 or whatever) as well as tuning your kp and kd. I would start with .1, and you will need to learn how to tune pid controllers. the I term will make it more accurate and better, but I am not doing all the work for you. Start here and improve it.

Also, this does not have a time-based exit condition in case you get stuck. For that you can use timers.

Common trouble shooting problems involve not calibrating your sensor, turning the wrong way, turning alllll the way around clockwise instead of turning counterclockwise, having a bad threshold, having the motors turn the wrong way, having the wrong desired heading, forgetting to set lasterr=0 before you start the loop, not scaling down to volts, using rotations instead of degrees for heading, having a bad wait time at the end, or forgetting to change the desired value. However after a while you know how to diagnose it.

The problem with your code is that these robots have inertia. You are basically going full send through the destination, and its inertia will carry it past and you will wind up facing a direction you did not intend.

2 Likes