Inertial Sensor Drifting and Not Stopping

Please, consider the following example:

// optional code to re-calibrate inertial sensor (see quote above)
void ReCalibrateGyro()
{
  Inert.calibrate();
  while( Inert.isCalibrating() )
  { wait(10,msec); }
}

void TurnRt(int degTurn, double PctSpd) //robot turns right
{
  double Kp = 0.3; // tune for least error and best performance

  while( Inert.isCalibrating() ) // make sure robot doesn't move
  { wait(10,msec); }

  Inert.resetRotation(); // tare rotation counter

  while(true)
  {
    double gError = degTurn - Inertial.rotation(deg);
    if( abs(gError) < 3)  // we will stop within 3 deg from target
    {
       break;
    }
    double speed = gError * Kp;

    // clamp max turning speed to -PctSpeed...+PctSpeed
    if( speed > +PctSpeed ) speed = +PctSpeed;
    if( speed < -PctSpeed ) speed = -PctSpeed;

    LTC.spin(DirFwd, speed, VelPct);
    RTC.spin(DirRev, speed, VelPct);
    LBC.spin(DirFwd, speed, VelPct);
    RBC.spin(DirRev, speed, VelPct);

    wait(10,msec);
   }

  LTC.stop();
  RTC.stop();
  LBC.stop();
  RBC.stop();
}

If you still have problems running this code, please, take a look at this topic and examples I linked earlier and, if it still doesn’t help, post your updated code back in this topic enclosed in the [code]...[/code] tags. Please, don’t create a new topic again.

Inertial sensor API: VEX Help

5 Likes