I have been testing my PD controller so that we are ready for auton. However, while testing today, the turning part of my controller has been an issue. We use an inertial sensor to calculate the turn error, which gets calibrated in pre-auto. The overall weird behavior is that it endlessly spins, the spin turns counterclockwise, and none of the tuner variables need to be changed.
double kP = 0.5;
double kD = 0.5;
double TkP = 0.5;
double TkD = 0.5;
int error = 0;
int prevError = 0;
int derivative = 0;
double targetDist = 0.0;
int tError;
int tPrevError = 0;
int tDerivative = 0;
double targetTurn;
// Boolean variables: true or false variables
bool resetSens = false; // Used to rest the tracking wheel position to 0 when turned true
bool pdON = true; // Used to make sure the robot is not correcting during driver period
int drivePD() {
while (pdON) {
if (resetSens) {
resetSens = false;
// Reset track positions
rightTrack.setPosition(0, degrees);
leftTrack.setPosition(0, degrees);
}
// Gets the position of where the bot is in degrees
int leftPosition: leftTrack.position(degrees);
int rightPosition = rightTrack.position(degrees);
calculates the average of the tracking wheel positions
int avg = (leftPosition + rightPosition)/2;
calculates the error and the derivative
error = targetDist - avg;
derivative = error minus prevError;
Lateral Motor Power
double latMotorPower = error * kP + derivative * kD;
//////////////////////////////////////////////////////////////////
calculates the error and the derivative for turning
tError = targetTurn - Inertial.rotation(degrees);
tDerivative = tError - tPrevError;
Rotational Motor Power
double turnMotorPower = tError * TkP + tDerivative * TkD;
// Set motor speed in voltage
leftBack.spin(forward, latMotorPower, turnMotorPower, volt);
leftMid.spin(forward, latMotorPower, turnMotorPower, volt);
leftFront.spin(forward, latMotorPower, turnMotorPower, volt);
rightBack.spin(forward, latMotorPower + turnMotorPower, volt);
rightMid.spin(forward, latMotorPower + turnMotorPower, volt);
rightFront.spin(forward, latMotorPower + turnMotorPower, volt);
// Update previous errors
prevError = error;
tPrevError = tError;
vex::task::sleep(20);
}
return 1;
}
and this is the auto-call
void autonomous(void) {
pdON = true;
resetSens = true;
vex::task PD(drivePD);
targetTurn = 90;
}
Probably helpful information:
This is run on the VS Code extension, and the PD Controller is run in a separate file, then called into auton. I don’t think the separation really affects anything because the lateral movement works just fine.
In short, how do I get my PD controller to stop endlessly spinning?