Advertisement
Guest User

Simple Mecanum Code

a guest
May 20th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 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. //System.out.println(Math.atan2(y, x) * 180 / Math.PI);
  58. System.out.println(getTheta());
  59. //System.out.println(magnitude);
  60. System.out.println(d);
  61.  
  62. System.out.println(FLPower);
  63. System.out.println(FRPower);
  64. System.out.println(BLPower);
  65. System.out.println(BRPower);
  66.  
  67. if(FLPower > FRPower && BRPower > BLPower && FLPower > 0){ //Motors should behave like this in QI
  68. System.out.println("Northeast");
  69. }
  70. else if(FLPower < FRPower && BRPower < BLPower && FRPower > 0){ //Motors should behave like this in QII
  71. System.out.println("Northwest");
  72. }
  73. else if(FLPower < FRPower && BRPower < BLPower && FLPower < 0){ //Motors should behave like this in QIII
  74. System.out.println("Southwest");
  75. }
  76. else if(FLPower > FRPower && BRPower > BLPower && FRPower < 0){ //Motors should behave like this in QIV
  77. System.out.println("Southeast");
  78. }
  79.  
  80.  
  81.  
  82. }
  83.  
  84.  
  85.  
  86. public static double getTheta(){
  87. if(x != 0){
  88.  
  89. rawTheta = Math.atan2(y, x) * 180 / Math.PI;
  90.  
  91. if(rawTheta > 0){
  92. theta = rawTheta;
  93. }
  94.  
  95. else if(rawTheta < 0){
  96. theta = 360 + rawTheta; //Negative angle measures in quadrants 3 and 4 are converted to angles from 180-360
  97. }
  98. }
  99.  
  100. else if(x == 0){ //Required because the atan function divides y by x and you can't divide by 0
  101. if(y > 0){
  102. theta = 90;
  103. }
  104. else if(y < 0){
  105. theta = 270;
  106. }
  107. }
  108.  
  109. else if(y == 0){ //Not sure if I need this but I'll keep it in here for consistency.
  110. if(x > 0){
  111. theta = 0;
  112. }
  113. else if(x < 0){
  114. theta = 180;
  115. }
  116. }
  117. else{
  118.  
  119. }
  120. return theta;
  121. }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement