After the season’s over, I’ve started working on programming concepts I’ve only looked at briefly. I’ve started only with a P loop. Currently the robot drives forward infinitely unless stopped. I’ve tried changing the kP and turnkP constants to no avail. Any ideas what’s wrong here?
Help is much appreciated.
double kP = 1;
double turnKP = 1;
int desiredvalue;
int desiredturnvalue;
int error;
int preverror = 0;
int derivative;
int totalError;
int turnerror;
int turnpreverror = 0;
int turnderivative;
int turntotalError;
bool reset = false;
bool enableDrivePID = true;
int drivePID(){
while(enableDrivePID){
if(reset){
reset = false;
LeftMotor1.setPosition(0, degrees);
RightMotor1.setPosition(0, degrees);
}
//drive
int LeftMotorPosition = LeftMotor1.position(degrees);
int RightMotorPosition = RightMotor1.position(degrees);
int avgPosition = (LeftMotorPosition + RightMotorPosition)/2;
error = avgPosition - desiredvalue;
double motorPower = error*kP;
//turn
int turnDiff = -(LeftMotorPosition - RightMotorPosition)/2;
turnerror = turnDiff - desiredturnvalue;
double turnPower = error*turnKP;
LeftMotor1.spin(forward,motorPower+turnPower, voltageUnits::volt);
RightMotor1.spin(forward,motorPower - turnPower, voltageUnits::volt);
preverror = error;
vex::task::sleep(20);
}
return 1;
}
/*---------------------------------------------------------------------------*/
/* */
/* Autonomous Task */
/* */
/* This task is used to control your robot during the autonomous phase of */
/* a VEX Competition. */
/* */
/* You must modify the code to add your own robot specific commands here. */
/*---------------------------------------------------------------------------*/
void autonomous(void) {
vex::task something(drivePID);
reset = true;
desiredvalue = 10;
vex::task::sleep(1000);