Inertial turn not working

my inertial proportional turn works fine when turning right but it spins forever when turning left.
pls help

void inertialTurn(double targetHeading, bool turnDirection ){

  IS.resetRotation();
  double error = targetHeading;
  double Kp = 0.5;
  double motorSpeed;

  while (fabs(error) > 3){

    motorSpeed = Kp*error;
    if (turnDirection == true){
      LDrive.spin(forward, motorSpeed, pct);
      RDrive.spin(reverse, motorSpeed, pct);
    }
    else if (turnDirection == false){
      LDrive.spin(reverse, motorSpeed, pct);
      RDrive.spin(forward, motorSpeed, pct);
    }
    error = targetHeading - IS.rotation(degrees);
    
  }
  Chassis.stop();

}

theres the code
any help would be greatly appreciated!!!

Remove the if and else with the turn direction and check if it works. It should automatically find the closet way and because you are turning robot relative it shouldn’t matter.

Change IS.rotation(degrees) to IS.heading(degrees).

1 Like

The inertial sensor goes forever in the positive direction when you turn right, and forever in the negative direction when it goes left. Your code for turning right is working because I assume you pass in a positive value for targetHeading and that is reached as it turns in the positive direction.
image
When you turn left, the heading never reaches the positive value because it is going negative. Your code only sees that it never reached your desired value.
image
A possible solution could be doing abs(IS.rotation(degrees)) so that the value it is checking is always positive.
image

thanks for your solutions everyone!