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?

Link to code running:https: //youtu.be/ByDsDm8nkPI

  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;
}

vex::task::sleep(20);

}

return 1;

}

Picture of graphed equation:
image

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
image

7 Likes

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

You might find this thread fun to read if you haven’t already:

6 Likes

Thanks! I was also thinking of that. I’m probably going to use a bunch of if statements and distances travelled to make it cruise after a set distance.

Highly recommend the FRC presentation on Motion Profiling here: 2015 FIRST World Championships Conference - Motion Planning & Control in FRC - YouTube

3 Likes