Inertial Sensor doesn't Zero after 3rd run

Currently I am working on a P loop for turning, but encountering some issues using inertial.set_rotation. The first two times I call it, it works properly. Whenever I call it for the third time, the inertial sensor fails to reset to 0 degrees.
My turning function:

def accurateTurn(expected, coefficient):
    print("")
    test = inertial.rotation(DEGREES)
    inertial.set_rotation(0, DEGREES)
    test2 = inertial.rotation(DEGREES)
    print("test")
    print(test)
    print("test2")
    print(test2)
    wait (0.5, SECONDS)
    error = expected
    while (abs(error) > 1):
        actual = inertial.rotation(DEGREES)
        print("actual")
        print(actual)
        error = (expected - actual)
        print("error")
        print(error)
        speed = (error * coefficient)
        print("speed")
        print(speed)
        LD1.set_velocity(speed, RPM)
        LD2.set_velocity(speed, RPM)
        LD3.set_velocity(speed, RPM)

        RD1.set_velocity(-speed, RPM)
        RD2.set_velocity(-speed, RPM)
        RD3.set_velocity(-speed, RPM)

        LD1.spin(FORWARD)
        LD2.spin(FORWARD)
        LD3.spin(FORWARD)
        RD1.spin(FORWARD)
        RD2.spin(FORWARD)
        RD3.spin(FORWARD)

        wait(0.15, SECONDS)

    LD1.stop()
    LD2.stop()
    LD3.stop()
    RD1.stop()
    RD2.stop()
    RD3.stop()

Me calling it in my function:

    accurateTurn(-45, 1)
    print(inertial.rotation(DEGREES))
    wait(0.5, SECONDS)
    accurateTurn (-90, 1)
    print(inertial.rotation(DEGREES))
    wait(0.5, SECONDS)
    accurateTurn (-45, 1)
    print(inertial.rotation(DEGREES))

The printout:

test
-0.01119822
test2
0.0
actual
-0.001398116
error
-44.9986
speed
-44.9986
actual
-0.1942517
error
-44.80575
speed
-44.80575
actual
-17.25658
error
-27.74342
speed
-27.74342
actual
-28.03722
error
-16.96278
speed
-16.96278
actual
-35.95252
error
-9.047478
speed
-9.047478
actual
-39.13806
error
-5.861942
speed
-5.861942
actual
-40.0613
error
-4.938705
speed
-4.938705
actual
-41.13585
error
-3.864155
speed
-3.864155
actual
-42.3746
error
-2.625404
speed
-2.625404
actual
-42.9912
error
-2.008801
speed
-2.008801
actual
-43.85899
error
-1.141014
speed
-1.141014
actual
-44.09687
error
-0.9031296
speed
-0.9031296
-43.96519

test
-43.97866
test2
-0.01119822
actual
-0.01761886
error
-89.98239
speed
-89.98239
actual
-1.789509
error
-88.21049
speed
-88.21049
actual
-18.63723
error
-71.36276
speed
-71.36276
actual
-38.81096
error
-51.18905
speed
-51.18905
actual
-56.9284
error
-33.07161
speed
-33.07161
actual
-71.45178
error
-18.54822
speed
-18.54822
actual
-82.24475
error
-7.755257
speed
-7.755257
actual
-84.34506
error
-5.654938
speed
-5.654938
actual
-86.53947
error
-3.460533
speed
-3.460533
actual
-87.35354
error
-2.646454
speed
-2.646454
actual
-87.94542
error
-2.054581
speed
-2.054581
actual
-88.48877
error
-1.51123
speed
-1.51123
actual
-89.16098
error
-0.8390274
speed
-0.8390274
-89.10068

test
-89.10609
test2
-43.97866
actual
-43.98113
error
-1.018875
speed
-1.018875
actual
-43.98352
error
-1.016479
speed
-1.016479
actual
-43.99193
error
-1.008068
speed
-1.008068
actual
-44.02208
error
-0.9779205
speed
-0.9779205
-44.00014

As you can see, on the third turn it fails to reset the rotation of the inertial sensor to zero (marked by test one, or the angle before the sensor is rest, and test 2, the angle after the sensor is reset).

I have tried different ports, different inertial sensors, what is the issue here?

1 Like