Advertisement
Golden_Dragoon

PID Control X Drive

Feb 18th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.67 KB | None | 0 0
  1. /**
  2.      * @param speed    can be any number UNDER 1, the lower the better
  3.      * @param maxThres must be equal to 1 - speed, no more or less
  4.      * @param distance is how far you want to move, in ticks.
  5.      * @param heading  is the wanted heading in degrees
  6.      */
  7.  
  8.     public void driveByPID(double speed, double maxThres, int distance, double heading) {
  9.         //This grabs the error, and passes it the heading.
  10.         double error = getError(heading);
  11.         //This creates the proportional multiplier.
  12.         double multiplier = (error / 180) * maxThres;
  13.  
  14.         //I forget if it works exactly like this or not. Guess we'll find you.
  15.  
  16.         ForwardRight.setTargetPosition(distance);
  17.         BackRight.setTargetPosition(distance);
  18.         ForwardLeft.setTargetPosition(distance);
  19.         BackLeft.setTargetPosition(distance);
  20.  
  21.         //The theory behind this is that it will turn like a PID would.
  22.         while (ForwardRight.isBusy() && BackRight.isBusy() && ForwardLeft.isBusy() && BackLeft.isBusy()) {
  23.             //This grabs the error, and passes it the heading.
  24.             error = getError(heading);
  25.             //This creates the proportional multiplier.
  26.             multiplier = (error / 180) * maxThres;
  27.             ForwardRight.setPower(speed - multiplier);
  28.             BackRight.setPower(speed - multiplier);
  29.             ForwardLeft.setPower(speed + multiplier);
  30.             BackLeft.setPower(speed + multiplier);
  31.         }
  32.  
  33.     }
  34.  
  35.     public double getError(double heading) {
  36.         double error;
  37.         error = heading - imu.getAngularOrientation(AxesReference.INTRINSIC, AxesOrder.XYZ, AngleUnit.DEGREES).thirdAngle;
  38.         return error;
  39.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement