Autonomous loop not working

I’ve been recently experimenting with accelerating and decelerating a robot based on its distance travelled. The code uses an absolute value equation that calculates the voltage the drive motors are supposed to be at based on the distance travelled. The issue I ran into is that the code only calculates the starting distance and applies the voltage to the motor. What should I do to make sure the values are updating and increasing the voltage?

image

Code:

int LeftDrive = FL.position(degrees);
int RightDrive = FR.position(degrees);
int averagePosition = (LeftDrive + RightDrive)/2;
bool resetDriveSensors = false;
bool enableDrivePID = true;

double distance1 = (averagePosition/360*4);
double move = 12;
double a =1/move;
double equation = -fabs(( 20*a*distance1)-10)+12;

int drivePID(){
while(enableDrivePID){

if (resetDriveSensors) {
  resetDriveSensors = false;

  FL.setPosition(0,degrees);
  FR.setPosition(0,degrees);

}


FR.spin(forward, equation , voltageUnits::volt);
BL.spin(forward, equation , voltageUnits::volt);
FL.spin(forward, equation , voltageUnits::volt);
BL.spin(forward, equation , voltageUnits::volt);
vex::task::sleep(20);

}
return 1;
}

void autonomous (void){
vex::task test1(drivePID);
resetDriveSensors = true;
Brain.Screen.print(equation);
Brain.Screen.print(move);
vex::task::sleep(5000);
Brain.Screen.print(equation);
vex::task::sleep(1000);
}

Your current issue is that when you declare:

double equation = -fabs(( 20*a*distance1)-10)+12;

You are setting the variable “equation” at the start of your code without ever updating it throughout the loop.

Your solution to updating each loop would be to move your var setting into your while loop (specifically, both distance 1 and equation).

5 Likes

Alright, I’ll try it in a sec.

It worked. Thanks! 20char