Help with the Inertial sensor

i = 0
turned = 0
LeftMotor.set_velocity(20, PERCENT)
RightMotor.set_velocity(20, PERCENT)
LeftMotor.spin(REVERSE)
RightMotor.spin(FORWARD)
wait(5.2,SECONDS)
LeftMotor.spin(REVERSE)
RightMotor.spin(FORWARD)

while turned == 0:
    if GyroInertial.heading(DEGREES) > 269:
        if GyroInertial.heading(DEGREES) <= 270:
            brain.screen.print("in range")
            turned = 0 
            LeftMotor.stop()
            RightMotor.stop()

The code is supposed to detect if the heading is in the specific range. It usually works as expected, however occasionally it will print “in range” despite not physically nor the heading value being in range. I assume when heading overflows from 0 to 360 it incorrectly detects it as being in range. Is there a possible fix or workaround?

More likely is that you never clear the screen. So you get the robot in range, turn it elsewhere, look at the screen, and it still says “in range” from before, but that’s just because it hasn’t been cleared.

Thank you for replying, however I’m aware that it does not clean. The concern is when it prints “in range” initially when it’s nowhere near the range heading. Also it sets turning to 0 which acts as a debounce in order for it to stop detecting when it already detects it once.

Do you give your gyro time to calibrate on every run? (about 3 seconds)

I tested with and without calibration and the result seems the same

You should not get the same results with and without calibration. If you do not allow the gyro to calibrate at the beginning of your run, it should drift at a constant rate in one direction. This should not happen if the gyro is calibrated. I suggest printing the gyro’s heading to the screen and analyzing.

It seems like you want to use rotation, not heading. Rotation won’t wrap around it will go negative.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.