Why is my Drive PID not working

//settings
double kP = 0.0;
double kI = 0.0;
double kD = 0.0;

//autonomous settings
int DesiredValue = 0;

int Error; //SensorValue - DesiredValue =position
int prevError = 0; //Position 20 mseconds ago
int derivative; //Error - prevError = speed
int totalError = 0;

bool ResetDriveSensors = false;

//varibles modified for use
bool EnableDrivePID = true;

int DrivePID(){

while(EnableDrivePID) {

//////////////////////////////////////////////////////////////////////
//Reset Drive Encoders
//////////////////////////////////////////////////////////////////////

if (ResetDriveSensors) {
  ResetDriveSensors = false;

  LeftTrain.setPosition(0,degrees);
  RightTrain.setPosition(0,degrees);
}
//////////////////////////////////////////////////////////////////////

//gets position of both drive trains
int LeftTrainPosition = LeftTrain.position(degrees);
int RightTrainPosition = RightTrain.position(degrees);

//////////////////////////////////////////////////////////////////////
//Lateral Movement PID
//////////////////////////////////////////////////////////////////////

//gets average of both drive trains
int averagePosition = (LeftTrainPosition + RightTrainPosition)/2;

//potential
Error = averagePosition - DesiredValue;

//derivative
derivative = Error - prevError;

totalError += Error;

double LateralMotorPower = Error * kP + derivative * kD + totalError *kI;
//////////////////////////////////////////////////////////////////////

LeftTrain.spin(forward, LateralMotorPower, voltageUnits::volt);
RightTrain.spin(forward, LateralMotorPower, voltageUnits::volt);

//code
prevError = Error;
vex::task::sleep(20);

}
return 1;
}

void autonomous(void) {
//creates operatable task for a drive PID
vex::task Xanderishome(DrivePID);

DesiredValue = 1320;
}

When ever you post a topic you might also want to include what is not working about it (Is it not driving? Is it going too far? Is only one side going? Etc. )

Looking through your code, it looks like you need to calibrate the values of Kp, Ki, and Kd. When they are all 0, it just makes the motor power 0

4 Likes