Inertial Sensor Drifting and Not Stopping

I’m using an inertial sensor to do turns in my autonomous program, but after the very first turn, the reading for the sensor at the yaw keeps going up by about two degrees per second, and it won’t stop. any ideas on how to tix this issue? it’s not near any motors or surrounded by metal, and my auton line is as follows:
TurnRt(115, 0.5);
MWait(100);

Program for TurnRt:
void TurnRt (int degTurn, float PctSpd) //robot turns right
{
int speed = 100 * PctSpd;
while((Inert.heading(DegUnits) < degTurn))
{
LTC.spin(DirFwd, speed, VelPct);
RTC.spin(DirRev, speed, VelPct);
LBC.spin(DirFwd, speed, VelPct);
RBC.spin(DirRev, speed, VelPct);
task::sleep(10);
}
LTC.stop(brake);
RTC.stop(brake);
LBC.stop(brake);
RBC.stop(brake);
}

MWait is just an easier way to write wait(Msec, msec);

did you calibrate the sensor?

4 Likes

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: https://api.vexcode.cloud/v5/html/classvex_1_1inertial.html

5 Likes

I calibrate at pre-auton, but not anywhere after that. it looks like this:
void pre_auton(void)
{
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
Inert.calibrate();

// waits for the Inertial Sensor to calibrate
while (Inert.isCalibrating())
{
wait(100, msec);
}
// All activities that occur before the competition starts
// Example: clearing encoders, setting servo positions, …
}

how did you start your program? I think if you started it with the competition or programming skills mode on your controller, your functions in pre auton won’t run

2 Likes

I fixed the problem. it turns out that because I placed the sensor at the edge of my robot, it got confused. I put it back more towards the center of the bot, and it stopped drifting.

That would make. A lot of sense.

Unfortunately, a few minutes after testing, it started drifting again. :frowning_face: so, the problem remains unsolved.

a few minutes is a very long time for this gyro. It tends to accumulate error over time and a few minutes(more than 2 I would think) it will become significantly off

1 Like

That alone shouldn’t be a problem. Angle of rotation is identical in every point - it is translation that differs.

I would suspect more if it wasn’t leveled perfectly horizontal or mounted very close to a wheel where it vibrates as robot moves.

People try to mount inertial sensor on the rubber links in the middle of the robot to minimize bumpy ride of the mecanum or omni wheels.

Finally, if all this fails, you can try to do factory re-calibration of the inertial sensor as described here:

2 Likes

This seems like a lot.

Either the IMU is moving while being calibrated, or it has some internal fault.

Check the IMU using the dashboard. Open the dashboard, there’s no need to run your program. Make sure the robot/imu is stationary and gently touch the calibrate button in the dashboard. After a couple of seconds, the imu will finish calibration and the roll, pitch and heading values will adjust to show the absolute orientation of the imu. Check the heading value remains stable with only a few tenths of a degree drift per minute. Check that the values for the gyro are close to 0. Rotate the robot/imu, check the heading changes accordingly, see if there is any significant drift after doing this. Let us know the results.

inertial

3 Likes