# Acceleration and Deceleration Using Absolute Value

I’ve gotten a PID loop to work, but I realized that the loop only slows down the robot as it reaches its target. So in light of that discovery, I developed a movement algorithm that accelerates and decelerates the robot using absolute value equations. The algorithm also automatically straightens the robot if it were to drift off course. Is this technically better than PID?

``````  int absFwd(double value){
resetTrack();

while(1){

double averagePosition = (trackLeft + trackRight)/2;
double distance1 = ((averagePosition/360)*2.75*pi);
double a =1/value;
double equation = -fabs((21*a*distance1)-9)+13;

if (equation>10){

equation = 10;
}

if (trackLeft > trackRight){
FR.spin(forward, equation , voltageUnits::volt);
BR.spin(forward, equation , voltageUnits::volt);
FL.spin(forward, 0.95*equation , voltageUnits::volt);
BL.spin(forward, 0.95*equation , voltageUnits::volt);
}

else if (trackLeft < trackRight){
FR.spin(forward, 0.95*equation , voltageUnits::volt);
BR.spin(forward, 0.95*equation , voltageUnits::volt);
FL.spin(forward, equation , voltageUnits::volt);
BL.spin(forward, equation , voltageUnits::volt);
}

else if (trackLeft == trackRight) {
FR.spin(forward, equation , voltageUnits::volt);
BL.spin(forward, equation , voltageUnits::volt);
FL.spin(forward, equation , voltageUnits::volt);
BL.spin(forward, equation , voltageUnits::volt);
}

if(5>(value+2) - distance1>0){

FL.stop(vex::brakeType::brake);
BL.stop(vex::brakeType::brake);
FR.stop(vex::brakeType::brake);
BR.stop(vex::brakeType::brake);
break;
}

}

return 1;

}
``````

Picture of graphed equation:

4 Likes

Congratulations! you have discovered the world of motion profiling. Good work.

Here’s the next challenge for you - Accelerate at maximum acceleration, cruise at maximum velocity, and decelerate at maximum deceleration. This is called a trapezoidal motion profile

7 Likes

Better is subjective. If your code works better than PID for you, great!