// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
}
//settings
double kP = 0.000001;
double kI = 0.000001;
double kD = 0.000001;
double turnkP = 0.0;
double turnkI = 0.0;
double turnkD = 0.0;
// autonomous settings
int desiredvalue = 200;
int desiredturnvalue = 0;
int error;//sensor value - desired value = positional value
int preverror = 0;//position 20 millisecounds ago
int derivitive;// error - preverror = spped
int totalerror = 0;//totalerror = totalerror + error
int turnerror;//sensor value - desired value = positional value
int turnpreverror = 0;//position 20 millisecounds ago
int turnderivitive;// error - preverror = spped
int turntotalerror = 0;//totalerror = totalerror + error
bool resetdrivesensors = false;
//varibles modified for use
bool enabledrivePID=true;
int drivePID(){
while(enabledrivePID){
if (resetdrivesensors){
resetdrivesensors = false;
frontleft.setPosition(0,degrees);
backleft.setPosition(0,degrees);
frontright.setPosition(0,degrees);
backright.setPosition(0,degrees);
}
//get the positions of all four motors
int backleftmotorposition = backleft.position(degrees);
int backrightmotorposition = backright.position(degrees);
int frontleftmotorposition = frontleft.position(degrees);
int frontrightmotorposition = frontright.position(degrees);
///// lateral movement pid
// get the average of the motors
int averageposition = (backleftmotorposition + backrightmotorposition + frontleftmotorposition + frontrightmotorposition)/2;
//potential
error = averageposition - desiredvalue;
//derivitive
derivitive = error - preverror;
//integral
totalerror += error;
int lateralmotorpower = error * kP + derivitive * kD + totalerror * kI;
//turning movement pid
// get the average of the motors
int turndifference = backleftmotorposition + frontleftmotorposition - backrightmotorposition + frontrightmotorposition;
//potential
turnerror = turndifference - desiredturnvalue;
//derivitive
turnderivitive = turnerror - turnpreverror;
//integral
turntotalerror += turnerror;
int turnmotorpower = turnerror * turnkP + turnderivitive * turnkD + turntotalerror * turnkI;
frontleft.spin(forward, turnmotorpower, velocityUnits::pct);
backleft.spin(forward, turnmotorpower, velocityUnits::pct);
frontright.spin(forward, turnmotorpower, velocityUnits::pct);
backright.spin(forward, turnmotorpower, velocityUnits::pct);
frontleft.spin(forward, lateralmotorpower, velocityUnits::pct);
backleft.spin(forward, lateralmotorpower, velocityUnits::pct);
frontright.spin(forward, lateralmotorpower, velocityUnits::pct);
backright.spin(forward, lateralmotorpower, velocityUnits::pct);
preverror = error;
turnpreverror = turnerror;
vex::task::sleep(20);
}
return 1;
}
ok so i am really new to vex coding and i was trying to get pids for my suton and i found a guide but it doesnt seem to work im just looking for some help or some explanation so i can understand this code more and make it work when it did work the robot would just drive forward and would not stop even tho it was givin a value to stop at