Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.91 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import rospy
  3. import tf_conversions
  4. import tf2_ros
  5. import copy
  6. from math import fmod, pi, sin, cos, atan2, sqrt, pow, acos, floor
  7. from sensor_msgs.msg import JointState
  8. from geometry_msgs.msg import Pose, Point, Quaternion, TransformStamped
  9.  
  10. l1 = 3.0
  11. l2 = 3.0
  12. l3 = 2.0
  13.  
  14. # Returns a 2D list:
  15. # [[solA], [solB]]
  16. def solveTheta2And3(r, z):
  17.     print('In solveTheta23')
  18.  
  19.     #******************************************************
  20.     # theta2 + theta3
  21.     #******************************************************
  22.     A = -2.0*l3*r
  23.     B = -2.0*l3*(z - l1)
  24.     C = pow(r,2) + pow(z-l1,2) + pow(l3,2) - pow(l2,2)
  25.     print('A: %s B: %s C: %s' % (A,B,C))
  26.     print('-C/(sqrt(pow(A,2) + pow(B,2))): %s' % (-C/(sqrt(pow(A,2) + pow(B,2)))))
  27.  
  28.     psi = atan2(B,A)
  29.     print('psi: %s' % psi)
  30.  
  31.     # Two pairs of values of theta2 and theta3 based on +-acos
  32.     # Do fmod because we can end up with values outside of [-pi,pi]
  33.     # because acos returns [-pi,pi] and psi can be [-pi,pi]
  34.     theta23_a = fmod((acos( -C/(sqrt(pow(A,2) + pow(B,2))) ) + psi), pi)
  35.     theta23_b = fmod((-acos( -C/(sqrt(pow(A,2) + pow(B,2))) ) + psi), pi)
  36.     print('theta23_a: %s theta23_b: %s' % (theta23_a, theta23_b))
  37.  
  38.  
  39.     #******************************************************
  40.     # theta2
  41.     #******************************************************
  42.     theta2_a = atan2( z-l1 - (l3*sin(theta23_a)), r - (l3*cos(theta23_a)) )
  43.     theta2_b = atan2( z-l1 - (l3*sin(theta23_b)), r - (l3*cos(theta23_b)) )
  44.     print('theta2_b: "%s' % theta2_b)
  45.  
  46.     #******************************************************
  47.     # theta3
  48.     #******************************************************
  49.     theta3_a = theta23_a - theta2_a
  50.     theta3_b = theta23_b - theta2_b
  51.  
  52.     print('Exiting solveTheta23')
  53.     return [[theta2_a, theta3_a], [theta2_b, theta3_b]]
  54.  
  55.  
  56.  
  57. def IK(eePose):
  58.     print('In IK')
  59.  
  60.     x = eePose.position.x
  61.     y = eePose.position.y
  62.     z = eePose.position.z
  63.  
  64.     #******************************************************
  65.     # theta1
  66.     # theta 1 is based on an overhead view. z is a dot.
  67.     # Two values for theta1 based on +r and -r
  68.     #******************************************************
  69.     r_a = sqrt( pow(x,2) + pow(y,2) )
  70.     r_b = -r_a
  71.     theta1_a = atan2( y/r_a, x/r_a )
  72.     theta1_b = atan2( y/r_b, x/r_b )
  73.     print('r_a: %s r_b: %s theta1_a: %s theta1_b: %s' % (r_a, r_b, theta1_a, theta1_b))
  74.  
  75.  
  76.     # Use a function for the other theta values
  77.  
  78.     print('*************2nd SOL*****************')
  79.     solsRA = solveTheta2And3(r_a, z)
  80.     print('*************END 2nd SOL*****************')
  81.     solsRB = solveTheta2And3(r_b, z)
  82.  
  83.     sol1 = [theta1_a, solsRA[0][0], solsRA[0][1]]
  84.     sol2 = [theta1_a, solsRA[1][0], solsRA[1][1]]
  85.     sol3 = [theta1_b, solsRB[0][0], solsRB[0][1]]
  86.     sol4 = [theta1_b, solsRB[1][0], solsRB[1][1]]
  87.  
  88.     print('Exiting IK')
  89.     return [sol1, sol2, sol3, sol4]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement