So I’m encountering an issue with a PID loop in my code where the loop is not being entered, or the task not being started at all. Not sure what the issue could be?
Related code: All in main.cpp.
double kp = 1.0;
double kd = 1.0;
int dist = 0;
int turnDeg = 0;
int error;
int prevError = 0;
int deriv;
int turnError;
int turnPrevError = 0;
int turnDeriv;
bool resetDrive = false;
bool enabledrivePD = true;
int drivePD() {
Controller1.rumble("--");
while (true) {
if(enabledrivePD) {
if (resetDrive) {
resetDrive = false;
LeftFrontDrive.setRotation(0, degrees);
LeftMidDrive.setRotation(0, degrees);
LeftRearDrive.setRotation(0, degrees);
RightFrontDrive.setRotation(0, degrees);
RightMidDrive.setRotation(0, degrees);
RightRearDrive.setRotation(0, degrees);
}
int LeftRearPosition = LeftRearDrive.position(degrees);
int LeftMidPosition = LeftMidDrive.position(degrees);
int LeftFrontPosition = LeftFrontDrive.position(degrees);
int RightRearPosition = RightRearDrive.position(degrees);
int RightMidPosition = RightMidDrive.position(degrees);
int RightFrontPosition = RightFrontDrive.position(degrees);
double averagePosition =
(RightFrontPosition + RightMidPosition + RightRearPosition +
LeftRearPosition + LeftFrontPosition + LeftMidPosition) /
6;
Brain.Screen.print("Error: %d\n", error);
Brain.Screen.print("Target: %d\n", dist);
error = averagePosition - dist;
deriv = prevError - error;
double motorSpeed = kp * error + deriv * kd;
turnError = averagePosition - turnDeg;
turnDeriv = turnError - turnPrevError;
//double motorturnSpeed = kp * turnError + turnDeriv *kd;
LeftDrive.spin(forward, motorSpeed, voltageUnits::volt);
RightDrive.spin(forward, motorSpeed,voltageUnits::volt);
}
wait(20, msec);
}
return 1;
}
void autonomous(void) {
task driveTask(drivePD);
dist = 2000;
vex::task::sleep(1000);
//Right Auton WP
/*Arms.spin(reverse);
wait(20,msec);
turnRight(20, 15);
driveForDist(10, 300);
FrontClaw.set(true);
driveForDist(20, -400);
turnLeft(40,20);*/
//Right Neutral Goal
// Arms.spin(reverse);
// driveForDist(80, 800);
// driveForDist(50, 50);
// driveForDist(20, 30);
// FrontClaw.set(true);
// driveForDist(80, -800);
//Left Auton WP
/*Arms.spin(reverse);
driveForDist(30, 100);
FrontClaw.set(true);
driveForDist(30, -200);
turnLeft(20,50);*/
}