Skip to content

Instantly share code, notes, and snippets.

@jhh
Last active January 16, 2019 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhh/98801440e6c924e38a91beb1911cca30 to your computer and use it in GitHub Desktop.
Save jhh/98801440e6c924e38a91beb1911cca30 to your computer and use it in GitHub Desktop.
Minimum Swerve Wheel Rotation
public class Main {
public static void main(String[] args) {
minRotation(359, 0);
minRotation(359, 1);
minRotation(90, -90);
minRotation(180, -180);
minRotationWithReverse(359, 1);
minRotationWithReverse(90, -90);
minRotationWithReverse(180, -180);
minRotationWithReverse(30, 270);
minRotationWithReverse(0, 90);
minRotationWithReverse(0, 91);
}
static void minRotation(double current, double desired) {
double rotation = Math.IEEEremainder(desired - current, 360);
System.out.printf("current = %7.2f, desired = %7.2f, rotation = %7.2f%n", current, desired, rotation);
}
static void minRotationWithReverse(double current, double desired) {
double rotation = Math.IEEEremainder(desired - current, 360);
boolean reverse = false;
if (Math.abs(rotation) > 90) {
rotation -= Math.copySign(180, rotation);
reverse = true;
}
System.out.printf("current = %7.2f, desired = %7.2f, rotation = %7.2f, reverse = %b %n", current, desired, rotation, reverse);
}
}
@jhh
Copy link
Author

jhh commented Jan 3, 2019

Output:

current =  359.00, desired =    0.00, rotation =    1.00
current =  359.00, desired =    1.00, rotation =    2.00
current =   90.00, desired =  -90.00, rotation = -180.00
current =  180.00, desired = -180.00, rotation =   -0.00
current =  359.00, desired =    1.00, rotation =    2.00, reverse = false 
current =   90.00, desired =  -90.00, rotation =    0.00, reverse = true 
current =  180.00, desired = -180.00, rotation =   -0.00, reverse = false 
current =   30.00, desired =  270.00, rotation =   60.00, reverse = true 
current =    0.00, desired =   90.00, rotation =   90.00, reverse = false 
current =    0.00, desired =   91.00, rotation =  -89.00, reverse = true 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment