Advertisement
Guest User

Mecanum Code

a guest
May 20th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. package mecanum2;
  2.  
  3.  
  4. public class Mecanum2 {
  5.  
  6. static double x = -1;
  7. static double y = -1;
  8.  
  9. static double rawTheta = 0;
  10. static double theta = 0;
  11. static double magnitude = 0;
  12. static double d = 0;
  13.  
  14. static double FLPower;
  15. static double FRPower;
  16. static double BLPower;
  17. static double BRPower;
  18.  
  19. public static void main(String[] args) {
  20. magnitude = Math.hypot(x, y);
  21. //Quadrant 1
  22. if (getTheta() >= 0 && getTheta() <= 90) {
  23. d = getTheta() / 90;
  24.  
  25. FLPower = 1;
  26. FRPower = -1 + (2 * d);
  27. BLPower = -1 + (2 * d);
  28. BRPower = 1;
  29. }
  30. //Quadrant II
  31. else if (getTheta() > 90 && getTheta() <= 180) {
  32. d = (getTheta() - 90) / 90;
  33.  
  34. FLPower = 1 - (2 * d);
  35. FRPower = 1;
  36. BLPower = 1;
  37. BRPower = 1 - (2 * d);
  38. }
  39. //Quadrant III
  40. else if (getTheta() > 180 && getTheta() <= 270) {
  41. d = (getTheta() - 180) / 90;
  42.  
  43. FLPower = -1;
  44. FRPower = 1 - (2 * d);
  45. BLPower = 1 - (2 * d);
  46. BRPower = -1;
  47. }
  48. //Quadrant IV
  49. else if (getTheta() > 270 && getTheta() < 360) {
  50. d = (360 - getTheta()) / 90;
  51.  
  52. FLPower = -1 + (2 * d);
  53. FRPower = -1;
  54. BLPower = -1;
  55. BRPower = -1 + (2 * d);
  56. }
  57.  
  58. System.out.println(FLPower);
  59. System.out.println(FRPower);
  60. System.out.println(BLPower);
  61. System.out.println(BRPower);
  62.  
  63. if(FLPower > FRPower && BRPower > BLPower && FLPower > 0){ //Motors should behave like this in QI
  64. System.out.println("Northeast");
  65. }
  66. else if(FLPower < FRPower && BRPower < BLPower && FRPower > 0){ //Motors should behave like this in QII
  67. System.out.println("Northwest");
  68. }
  69. else if(FLPower < FRPower && BRPower < BLPower && FLPower < 0){ //Motors should behave like this in QIII
  70. System.out.println("Southwest");
  71. }
  72. else if(FLPower > FRPower && BRPower > BLPower && FRPower < 0){ //Motors should behave like this in QIV
  73. System.out.println("Southeast");
  74. }
  75.  
  76.  
  77.  
  78. }
  79.  
  80.  
  81.  
  82. public static double getTheta(){
  83. if(x != 0){
  84.  
  85. rawTheta = Math.atan2(y, x) * 180 / Math.PI;
  86.  
  87. if(rawTheta > 0){
  88. theta = rawTheta;
  89. }
  90.  
  91. else if(rawTheta < 0){
  92. theta = 360 + rawTheta; //Negative angle measures in quadrants 3 and 4 are converted to angles from 180-360
  93. }
  94. }
  95.  
  96. else if(x == 0){ //Required because the atan function divides y by x and you can't divide by 0
  97. if(y > 0){
  98. theta = 90;
  99. }
  100. else if(y < 0){
  101. theta = 270;
  102. }
  103. }
  104.  
  105. else if(y == 0){ //Not sure if I need this but I'll keep it in here for consistency.
  106. if(x > 0){
  107. theta = 0;
  108. }
  109. else if(x < 0){
  110. theta = 180;
  111. }
  112. }
  113. else{
  114.  
  115. }
  116. return theta;
  117. }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement