Mauzen

Untitled

May 17th, 2014
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.56 KB | None | 0 0
  1. /**
  2.      * Inverse kinematics to get the servo PWMs to reach a certain point, with a given angle
  3.      * of the last segment.
  4.      * @see http://www.aber.ac.uk/~dcswww/Dept/Teaching/CourseNotes/current/CS36510/Inverse_kinematics.pdf
  5.      * @param x X target.
  6.      * @param y Y target.
  7.      * @param z Z target.
  8.      * @param pp4 Global angle of the last segment.
  9.      * @return Result set
  10.      */
  11.     public IKResult getIKForPosition(double x, double y, double z, double pp4) {
  12.         double[] p = new double[4];
  13.        
  14.         // len1, ... are the lengths of the single segments of the arm
  15.         p[0] = Math.toDegrees(Math.atan(y / x));
  16.         double r = Math.sqrt(x * x + y * y);
  17.         p[3] = Math.toRadians(pp4);
  18.         double rw = r - (len3 * Math.cos(p[3]));
  19.         double z0 = (z - (len3 * Math.sin(p[3]))) - len0;
  20.         double be = Math.atan(z0 / rw);
  21.         double r0 = Math.sqrt(z0 * z0 + rw * rw);
  22.         double al = Math.acos((r0 * r0 + len1 * len1 - len2 * len2) / (2 * r0 * len1));
  23.         p[1] = al + be;
  24.         double ga = Math.acos((len2 * len2 + len1 * len1 - r0 * r0) / (2 * len2 * len1));
  25.         p[2] = Math.toDegrees((p[1] + ga) - Math.PI);
  26.        
  27.         p[0] = (p[0] > 0) ? (p[0] - 90) : (p[0] + 90);
  28.         p[3] = pp4;
  29.         p[1] = 90 - Math.toDegrees(p[1]);
  30.         p[2] = 90 - p[1] - p[2];
  31.         p[3] = 180.0 - p[1] - p[2] - (90 + p[3]);
  32.        
  33.        
  34.         // Angles are transformed to PWM lengths for the servo positions
  35.         // off1, ... are the PWM lengths of a centered position (straight arm)
  36.         // 0.09/0.108 simply is the degrees-to-PWM factor. I shouldnt have
  37.         // put them in here as constant number, but for some reason I did.
  38.         double[] pwms = new double[4];
  39.         if (Math.abs(p[0]) < 90.0) pwms[0] = (p[0] / 0.09 + off1);
  40.         else pwms[0] = ((p[0] / 0.09) - 2500);
  41.         pwms[1] = (off2 - p[1] / 0.09);
  42.         pwms[2] = (p[2] / 0.09 + off3);
  43.         pwms[3] = off4 + p[3] / 0.108;
  44.        
  45.         int type = IKResult.RESULT_SUCCESS;
  46.         //System.out.println(p[0] + ":" + pwms[0] + ", " + pwms[1] + ", " + pwms[2] + "," + pwms[3]);
  47.        
  48.         // Check for unreachability
  49.         if (Double.isNaN(pwms[0]) || Double.isNaN(pwms[1]) || Double.isNaN(pwms[2]) || Double.isNaN(pwms[3])) {
  50.             type = IKResult.RESULT_UNREACHABLE;
  51.         } else
  52.            
  53.         // Check servo ranges
  54.         // Hand servo is excluded from check for better ranges
  55.         if (pwms[0] > rotate.getMaxPWM() || pwms[0] < rotate.getMinPWM()) {
  56.             type = IKResult.RESULT_OUT_OF_RANGE;
  57.             //System.out.println("rotate " + pwms[0]);
  58.         } else
  59.         if (pwms[1] > shoulder.getMaxPWM() || pwms[1] < shoulder.getMinPWM()) {
  60.             type = IKResult.RESULT_OUT_OF_RANGE;
  61.             //System.out.println("shoulder " + pwms[1]);
  62.         } else
  63.         if (pwms[2] > elbow.getMaxPWM() || pwms[2] < elbow.getMinPWM()) {
  64.             type = IKResult.RESULT_OUT_OF_RANGE;
  65.             //System.out.println("elbow " + pwms[2]);
  66.         }
  67.         /*if (pwms[0] > rotate.getMaxPWM() || pwms[0] < rotate.getMinPWM()
  68.                 || pwms[1] > shoulder.getMaxPWM() || pwms[1] < shoulder.getMinPWM()
  69.                 || pwms[2] > elbow.getMaxPWM() || pwms[2] < elbow.getMinPWM()
  70.                 /*|| pwms[3] > hand.getMaxPWM() || pwms[3] < hand.getMinPWM()*///) {
  71.            
  72.            
  73.         //    type = IKResult.RESULT_OUT_OF_RANGE;
  74.         //}
  75.        
  76.        
  77.         IKResult res = new IKResult(this, p, pwms, type);
  78.        
  79.         return res;        
  80.     }
Advertisement
Add Comment
Please, Sign In to add comment