PID Calibration and Testing

Tags: code and control
Personhours: 3
PID Calibration and Testing By Tycho

Task: Allow user to change PID coefficients from the controller

To allow each user to create their own settings, we're designing a way to allow the user to tune PID to their own liking from the controller. This also enables debugging for our robot.

public void PIDTune(PIDController pid, boolean pidIncrease, boolean pidDecrease, boolean magnitudeIncrease, boolean magnitudeDecrease, boolean shouldStateIncrement) {
 if (shouldStateIncrement) {
  pidTunerState = stateIncrement(pidTunerState, 0, 2, true);
 }
 if (magnitudeIncrease) {
  pidTunerMagnitude *= 10;
 }
 if (magnitudeDecrease) {
  pidTunerMagnitude /= 10;
 }
 double dir;
 if (pidIncrease) dir = 1;
 else if (pidDecrease) dir = -1;
 else if (pidDecrease) dir = -1;
 else dir = 0;
 switch (pidTunerState) {
  case 0:
   pid.setPID(pid.getP() pidTunerMagnitude * dir, pid.getI(), pid.getD());
   break;
  case 1:
   pid.setPID(pid.getP(), pid.getI() pidTunerMagnitude * dir, pid.getD());
   break;
  case 2:
   pid.setPID(pid.getP(), pid.getI(), pid.getD() pidTunerMagnitude * dir);
   break;
 }
}
public double getPidTunerMagnitude() {
 return pidTunerMagnitude;
}
public int getPidTunerState() {
 return pidTunerState;
}
public int stateIncrement(int val, int minVal, int maxVal, boolean increase) {
 if (increase) {
  if (val == maxVal) {
   return minVal;
  }
  val++;
  return val;
 } else {
  if (val == minVal) {
   return maxVal;
  }
  val--;
  return val;
 }
}

Written on August 27, 2017