My team is trying to use an Imu for our robot to make it more stable during autonomous but for the life of me I can’t figure out what is bugging the code. I think it might be something to do with the distance function? It turns the wheels around 1 degree before stopping. Here’s the code:
We’re using both PROS and Okapi
double howFarMoved(double start) {
double currentPosition = driveMotor.get_position();
double distanceMovedInRotations = currentPosition - start;
double distanceMoved = distanceMovedInRotations * (4*3.14159265359);
return distanceMoved;
}
void moveGyro(double distance, double voltage = 200) {
//reset gyro
gyro.tare();
//turn on left and right sides
drive->getModel()->left(voltage);
drive->getModel()->right(voltage);
double start = driveMotor.get_position();
const double k1 = 0.1,k2=0.01;
bool driving = true;
double lastRotation = 0.0;
while (driving) {
//z axis points into ground
//positive drift rate is clockwise drift
double rotation = gyro.get_rotation();
double driftRate = rotation - lastRotation;
double correction = k1*driftRate + k2*rotation;
drive->getModel()->left(voltage-correction);
drive->getModel()->right(voltage+correction);
double distanceTraveled = howFarMoved(start);
if (distanceTraveled >= distance) {
driving = false;
}
lastRotation = rotation;
}
drive->stop();
}