Basically, its not stopping because your environment is not perfect you are saying that their needs to be 0 units of error which is almost impossible for a P loop or anything to perform so it just keeps going back and forth (oscillating) correct? So what I recommend is changing this
while(fabs(error)> 0){
to something like this
while(fabs(error)> 1){ // If this still happens increase
Anyway what you need is a wait, for like 15 milliseconds or something.
Vex Api functions like .position(deg) don’t have a wait in them and therefore don’t work correctly without a wait in the loop.
Your code should look something like this :
void drivePID(double target){
double current = 0;
double error = target-current;
double kp = .8; //change this//
double speed = 0;
rightB.resetPosition();
while(fabs(error)> 0){
current = rightB.position(deg);
speed = kp*error;
rightB.spin(reverse,speed, volt);
rightF.spin(reverse,speed, volt);
rightM.spin(reverse,speed, volt);
leftB.spin(reverse,speed, volt);
leftF.spin(reverse,speed, volt);
leftM.spin(reverse,speed, volt);
error = (target - current);
task::sleep(15); // Change this to what ever you want just a wait
}
rightB.stop(brake);
rightM.stop(brake);
rightF.stop(brake);
leftB.stop(brake);
leftM.stop(brake);
leftF.stop(brake);
}
void autonomous(void) {
drivePID(1000);
}