Turning with inertial sensor

So I have my robot turning to a certain degree in rotation and stop at that degree and it won’t stop and idk if it’s the code or the inertial sensor.

if the input value for turn is negative, your robot will not turn because imu rotation value initializes to 0 by default (therefore the while loop condition is never satified because turn is already less than 0).

Edit: Ignore what I just said above lol. imu reading becomes negative if the robot turns countercloackwise, so imu.rotation() will never become greater than turn (which is a positive value)


So I got the clockwise(right) turn to work but the counterclockwise(left) turn is not reading the negative number in the code

I think a quick fix would be instead of having imu.rotation(degrees) < turn, do fabs(imu.rotation(degrees)) < turn. This way you don’t have to worry about imu reading being negative, but the cost would be having to have separate functions for left turn and right turn.


What does fabs mean?

taking the absolute value

1 Like

Didn’t work I tried heading too and it didn’t work either

heading wouldn’t work because it’s range is limited to 0-360, so there’s a jump going from 359 to 0
also you would need to reset your imu rotation at the beginning of your function


Thanks for the help I had a slow moment I had to flip the inequality sign