My team is switching from block code to Cpp and we are having difficulty with PID control. Here is our PID loop:
int targetLeft = 100;
int targetRight = 100; //global variables
void drivePID(){
int actualLeft = 0;
int errorLeft = 0;
int leftDriveVelo = 0;
int actualRight = 0;
int errorRight = 0;
int rightDriveVelo = 0;
float kp = 0.1;
while(true){
actualLeft = leftDrive.position(degrees);
errorLeft = targetLeft - actualLeft;
leftDriveVelo = errorLeft * kp;
actualRight = rightDrive.position(degrees);
errorRight = targetRight - actualRight;
rightDriveVelo = errorRight * kp;
leftDrive.setVelocity(leftDriveVelo, percent);
rightDrive.setVelocity(rightDriveVelo, percent);
wait(30, msec);
}
}
And here is our program.
int main() {
vexcodeInit();
//configure motors
leftDrive.spin(forward);
rightDrive.spin(forward);
drivePID();
targetLeft = 100;
targetRight = 100;
//more stuff
}
Once the while loop in the PID task begins, the rest of task main stops running. I can define targetLeft and targetRight to be 100 and the robot will drive forward 100 turns, but I cannot set new targets after the robot has already started driving. How can I run PID in Cpp?