Advertisement
Guest User

jjj

a guest
May 6th, 2015
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import roslib
  4. import rospy
  5. import serial
  6. from math import pi
  7. import threading
  8. import tf
  9.  
  10. from geometry_msgs.msg import Twist
  11.  
  12. #joint_states_pub = None
  13. stop_time = 0;
  14. tf_listener = tf.TransformListener()
  15. tf_sender = tf.TransformBroadcaster()
  16.  
  17. wide = 0.153 + 0.030 #wide of car
  18. step = 2*pi*0.30 / (1000.0/3.0) # meters in 1 tic
  19.  
  20. old_tickL = 0
  21. old_tickR = 0
  22.  
  23. def publish_joint_states(tickL, tickR):
  24. global old_tickL
  25. global old_tickL
  26.  
  27. odom_frame = '/odom'
  28. base_frame = '/base_link'
  29.  
  30. ## get current pos
  31. trans,rot = tf_listener.lookupTransform(odom_frame, base_frame, rospy.Time(0))
  32.  
  33. ## calc radius, angle, from angR, angL
  34. l = (tickL - old_tickL) * step
  35. r = (tickR - oldtickR) * step
  36. theta = rot.getAngle()
  37.  
  38. if r != l: # moving along the arc
  39. if tickL > tickR: # turn to the right
  40. R = r/alpha
  41. alpha = (l-r) / wide
  42. elif tickR > tickL: # turn to the left
  43. R = l/alpha
  44. alpha = (r-l) / wide
  45. else:
  46. R = 0
  47. alpha = 0
  48.  
  49. newX = trans[0] + ( (R+wide/2) * (math.sin(theta + alpha) - math.sin(theta)) )
  50. newY = trans[1] + ( (R+wide/2) * (-math.cos(theta + alpha) + math.cos(theta)) )
  51. newTheta = theta + alpha
  52.  
  53.  
  54.  
  55. else: # facing
  56. newX = trans[0] + l*math.cos(theta)
  57. newY = trans[1] + l*math.sin(theta)
  58. newTheta = 0.0
  59.  
  60.  
  61. ## send trans, rot
  62. tf_sender. sendTransform( (newX, newY, .0), tf.transformations.quaternion_about_axis(newTheta, (0,0,1) ), \
  63. rospy.Time.now(), base_frame, odom_frame )
  64.  
  65. old_tickL = tickL
  66. old_tickR = tickR
  67.  
  68.  
  69.  
  70. ser = serial.Serial("/dev/ttyACM0",115200)
  71.  
  72. def read_from_port():
  73. rospy.loginfo("got from serial: "+ ser.readline() )
  74. try:
  75. while True:
  76. l = ser.readline()
  77. arr = l.split(",")
  78. if len(arr) < 4: continue
  79. publish_joint_states( int(arr[1]), int(arr[3]) )
  80. except Exception as e:
  81. rospy.signal_shutdown( str(e) )
  82.  
  83. def recv_cmd(data):
  84. if data.linear.x > 0 :
  85. ser.write("w\n")
  86. elif data.linear.x < 0:
  87. ser.write("s\n")
  88. elif data.angular.z > 0:
  89. ser.write("a\n")
  90. elif data.angular.z < 0:
  91. ser.write("d\n")
  92. stop_time = rospy.Time.now() + rospy.Duration.from_sec(1)
  93.  
  94. #rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.data)
  95.  
  96. if __name__ == '__main__':
  97.  
  98. thread = threading.Thread(target=read_from_port)
  99. thread.daemon = True
  100. thread.start()
  101.  
  102. try:
  103. rospy.init_node('arduino_node')
  104.  
  105. #joint_states_pub = rospy.Publisher('/joint_states', JointState, queue_size=10)
  106.  
  107. rospy.Subscriber("/turtle1/cmd_vel", Twist, recv_cmd)
  108.  
  109. rospy.loginfo("Starting arduino node")
  110.  
  111. rate = rospy.get_param('~rate', 20)
  112. r = rospy.Rate(rate)
  113. while not rospy.is_shutdown():
  114. if stop_time < rospy.Time.now():
  115. ser.write("s\n")
  116. stop_time = rospy.Time.now() + rospy.Duration.from_sec(10)
  117. r.sleep()
  118.  
  119. except rospy.ROSInterruptException as e:
  120. print e
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement