Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. import rospy
  2. from geometry_msgs.msg import Twist
  3. from std_msgs.msg import Float32
  4.  
  5.  
  6. wheel_radius = 0.1
  7. robot_radius = 1.0
  8.  
  9.  
  10. # computing the forward kinematics for a differential drive
  11. def forward_kinematics(w_l, w_r):
  12. c_l = wheel_radius * w_l
  13. c_r = wheel_radius * w_r
  14. v = (c_l + c_r) / 2
  15. a = (c_r - c_l) / (2 * robot_radius)
  16. return (v, a)
  17.  
  18.  
  19. # computing the inverse kinematics for a differential drive
  20. def inverse_kinematics(v, a):
  21. c_l = v - (robot_radius * a)
  22. c_r = v + (robot_radius * a)
  23. w_l = c_l / wheel_radius
  24. w_r = c_r / wheel_radius
  25. return (w_l, w_r)
  26.  
  27.  
  28. # inverse kinematics from a Twist message (This is what a ROS robot has to do)
  29. def inverse_kinematics_from_twist(t):
  30. return inverse_kinematics(t.linear.x, t.angular.z)
  31.  
  32.  
  33.  
  34. class wheel_test:
  35.  
  36. def __init__(self):
  37. rospy.init_node('wheel_test', anonymous=True)
  38. #self.
  39. self.sub = rospy.Subscriber('/wheel_vel_left', Float32, self.callback)
  40. #print("test")
  41. def callback(self,data):
  42. w_l = data.data
  43. w_r = 0.0
  44. (v, a) = forward_kinematics(w_l, w_r)
  45. #print "v = %f,\ta = %f" % (v, a)
  46. pub = rospy.Publisher('/mobile_base/commands/velocity', Twist, queue_size = 1)
  47. t = Twist()
  48. t.linear.x = v
  49. t.angular.z = a
  50.  
  51. pub.publish(t)
  52.  
  53. if __name__ == "__main__":
  54.  
  55. wheel_test()
  56.  
  57. #pub = rospy.Publisher('/mobile_base/commands/velocity', Twist, queue_size = 1)
  58. #sub = rospy.Subscriber('/wheel_vel_left', Float32, callback)
  59. rospy.spin()
  60.  
  61. (w_l, w_r) = inverse_kinematics(0.0, 1.0)
  62. print "w_l = %f,\tw_r = %f" % (w_l, w_r)
  63.  
  64. (v, a) = forward_kinematics(w_l, w_r)
  65. print "v = %f,\ta = %f" % (v, a)
  66.  
  67. (w_l, w_r) = inverse_kinematics_from_twist(t)
  68. print "w_l = %f,\tw_r = %f" % (w_l, w_r)
  69.  
  70. #this is the kinematics.py file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement