My PID code has just started spinning forever and I have no idea why… I was tuning it at practice earlier today and it was working just fine and I brought the robot home to finish tuning it when all of a sudden it just started driving into walls. I believe it is an issue with the while loop and/or the error calculation, because I had the brain print the error out after 1sec of running and it was the same as the value I put into the function. My PID code is:
(the 1 as kP was just a test value - I set everything else to 0 after the issue started to occur and it still happened)
double kP = 1;
double kI = 0.0;
double kD = 0.0;
double TurnkP = 0.0;
double TurnkI = 0.0;
double TurnkD = 0.0;
float bettererror;
float previousError = 0;
float derivative;
float totalError = 0;
float TurnBettererror;
float TurnPreviousError = 0;
float TurnDerivative;
float TurnTotalError = 0;
bool resetDriveSensors = false;
bool enableDrivePID = false;
bool enableTurnPID = false;
double desValue;
double desTurnValue;
double latmotorPower = 0;
double averagePos = 0;
int fwdPID(){
while (enableDrivePID) {
if (resetDriveSensors) {
resetDriveSensors = false;
BL.setPosition(0, deg);
RM.setPosition(0, deg);
BR.setPosition(0, deg);
FR.setPosition(0, deg);
RM.setPosition(0, deg);
FL.setPosition(0, deg);
}
float BLpos = BL.position(deg);
float RMpos = RM.position(deg);
float BRpos = BR.position(deg);
float FRpos = FR.position(deg);
float LMpos = LM.position(deg);
float FLpos = FL.position(deg);
// START LAT PID
// _-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-___
averagePos = (BLpos + BRpos + FLpos + FRpos + LMpos + RMpos) / 6;
//bettererror = desValue - (((BLpos*(36/60))/360)*(M_1_PI*3.25));
bettererror = desValue - averagePos*(36/60);
totalError += bettererror;
if(bettererror == 0 || averagePos > desValue){
totalError = 0;
}
if(totalError > 1000){
totalError = 1000;
}
derivative = bettererror - previousError;
latmotorPower = (bettererror * kP + derivative * kD + totalError * kI)/12;
BR.spin(forward,latmotorPower, voltageUnits::volt);
RM.spin(forward,latmotorPower, voltageUnits::volt);
BL.spin(forward,latmotorPower, voltageUnits::volt);
FR.spin(forward,latmotorPower, voltageUnits::volt);
LM.spin(forward,latmotorPower, voltageUnits::volt);
FL.spin(forward,latmotorPower, voltageUnits::volt);
previousError = bettererror;
if(std::abs(bettererror) < 0.05){
enableDrivePID = false;
}
vex::task::sleep(10);
// END LAT PID
// _-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-___
}
FL.stop(brakeType::brake);
FR.stop(brakeType::brake);
LM.stop(brakeType::brake);
BR.stop(brakeType::brake);
BL.stop(brakeType::brake);
RM.stop(brakeType::brake);
return 1;
}
int turnPID(){
while(enableTurnPID){
if (resetDriveSensors) {
resetDriveSensors = false;
Inertia.calibrate();
Inertia.setRotation(0, deg);
while(Inertia.isCalibrating()){
wait(10,msec);
}
/*Inertia2.calibrate();
Inertia2.setRotation(0,deg);
while(Inertia2.isCalibrating()){
wait(10, msec);
}*/
}
// START TURN PID
// _-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-___
double TurncurPos = Inertia.rotation(deg);
TurnBettererror = desTurnValue - TurncurPos;
if(fabs(TurnBettererror) >= 180){
TurnBettererror = -fabs(TurnBettererror)/TurnBettererror * 360 - fabs(TurnBettererror);
}
TurnDerivative = TurnBettererror - TurnPreviousError;
TurnTotalError += TurnBettererror;
if(TurnBettererror == 0 || TurncurPos > desTurnValue){
TurnTotalError = 0;
}
if(TurnTotalError > 1000){
TurnTotalError = 1000;
}
double turnMotorPower = (TurnBettererror * TurnkP + TurnDerivative * TurnkD + TurnTotalError * TurnkI) /12.0;
// END TURN PID
// _-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-___
BR.spin(forward,- turnMotorPower, voltageUnits::volt);
RM.spin(forward,- turnMotorPower, voltageUnits::volt);
BL.spin(forward, + turnMotorPower, voltageUnits::volt);
FR.spin(forward, - turnMotorPower, voltageUnits::volt);
LM.spin(forward, + turnMotorPower, voltageUnits::volt);
FL.spin(forward, + turnMotorPower, voltageUnits::volt);
TurnPreviousError = TurnBettererror;
if(std::abs(TurnBettererror) > 0.1){
enableTurnPID = false;
}
vex::task::sleep(10);
}
FL.stop(brakeType::brake);
FR.stop(brakeType::brake);
LM.stop(brakeType::brake);
BR.stop(brakeType::brake);
BL.stop(brakeType::brake);
RM.stop(brakeType::brake);
return 1;
}
and I call the PID in auton like this:
void autonomous(void) {
enableDrivePID = true;
vex::task drivePID(fwdPID);
resetDriveSensors = true;
desValue = 300;
wait(1000,msec);
Brain.Screen.print(bettererror);
}
I’m sure it’s just a really small error - it just seemed to happen out of nowhere while I was tuning it - but I can’t seem to figure it out, so I appreciate any help. Thanks!