Advertisement
jarod_reynolds

Untitled

May 11th, 2021
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.11 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3.  
  4. # MTRX5700 Assignment 3 question 2 transform listener and path publisher
  5. # Author: Tim Martin
  6. # Edited: 07/05/21
  7.  
  8. import rospy
  9. import math
  10. import numpy as np
  11. import tf2_ros
  12. from tf.transformations import *
  13. import geometry_msgs.msg
  14. from nav_msgs.msg import Odometry, Path
  15.  
  16. class Turtle_Odometry:
  17.  
  18. def __init__(self):
  19. self.odom_sub = rospy.Subscriber("/odom", Odometry, self.odom_callback)
  20. self.odom_quat = quaternion_from_euler(0,0,0)
  21. self.Opos_x = 0
  22. self.Opos_y = 0
  23. self.Opos_z = 0
  24.  
  25. def odom_callback(self, msg):
  26. self.Opos_x = msg.pose.pose.position.x
  27. self.Opos_y = msg.pose.pose.position.y
  28. self.Opos_z = msg.pose.pose.position.z
  29.  
  30. self.odom_quat[0] = msg.pose.pose.orientation.x
  31. self.odom_quat[1] = msg.pose.pose.orientation.y
  32. self.odom_quat[2] = msg.pose.pose.orientation.z
  33. self.odom_quat[3] = msg.pose.pose.orientation.w
  34.  
  35.  
  36. if __name__ == '__main__':
  37. rospy.init_node('tf2_listener')
  38. Odom_stuff = Turtle_Odometry()
  39. tfBuffer = tf2_ros.Buffer()
  40. listener = tf2_ros.TransformListener(tfBuffer)
  41.  
  42. map_odom_pub = rospy.Publisher('map_odom', Odometry, queue_size=1)
  43. map_path_pub = rospy.Publisher('Trajectory_path', Path, queue_size=10)
  44. map_odompath_pub = rospy.Publisher('Trajectory_odompath', Path, queue_size=10)
  45.  
  46. # odom_path_p = np.array([])
  47. # odom_path_o = np.array([])
  48. odom_path_p = np.zeros(shape=(200000, 3))
  49. odom_path_o = np.zeros(shape=(200000, 4))
  50. map_path = Path()
  51. map_path.header.frame_id = "odom"
  52. prev_trans = geometry_msgs.msg.Transform()
  53. curr_trans = geometry_msgs.msg.Transform()
  54. first_flag = 0
  55. i = 0
  56. pos_num = 0
  57.  
  58. rate = rospy.Rate(10)
  59. while not rospy.is_shutdown():
  60. try:
  61. trans = tfBuffer.lookup_transform('map', 'odom', rospy.Time())
  62. print('trans is: ', trans)
  63. except (tf2_ros.LookupException, tf2_ros.ConnectivityException, tf2_ros.ExtrapolationException):
  64. rate.sleep()
  65. continue
  66.  
  67. # retrieve components of map to odom transformation
  68. #print(trans)
  69. odom_pos = np.array([Odom_stuff.Opos_x, Odom_stuff.Opos_y, Odom_stuff.Opos_z])
  70. print('odom_pos', odom_pos)
  71. odom_quat = np.array([Odom_stuff.odom_quat[0], Odom_stuff.odom_quat[1], Odom_stuff.odom_quat[2], Odom_stuff.odom_quat[3]])
  72. print('odom_quat', odom_quat)
  73.  
  74. tf_x = trans.transform.translation.x
  75. tf_y = trans.transform.translation.y
  76. tf_z = trans.transform.translation.z
  77. tf_trans = np.array([tf_x, tf_y, tf_z])
  78. print ('tf_trans',tf_trans)
  79. #print(odom_pos)
  80.  
  81. tf_quat = quaternion_from_euler(0,0,0)
  82. tf_quat[0] = trans.transform.rotation.x
  83. tf_quat[1] = trans.transform.rotation.y
  84. tf_quat[2] = trans.transform.rotation.z
  85. tf_quat[3] = -trans.transform.rotation.w
  86. print ('tf_quat',tf_quat)
  87. #print(type(tf_quat))
  88.  
  89. # save current odom pose to a path array
  90. odom_path_p[pos_num] = odom_pos
  91. odom_path_o[pos_num] = odom_quat
  92. pos_num += 1
  93. # if first_flag == 0:
  94. # odom_path_o = np.append(odom_path_o, odom_pos, axis=0)
  95. # odom_path_p = np.append(odom_path_p, odom_quat, axis=0)
  96. # first_flag = 1
  97. # elif first_flag == 1:
  98. # odom_path_o = np.append([odom_path_o], [odom_pos], axis=0)
  99. # odom_path_p = np.append([odom_path_p], [odom_quat], axis=0)
  100. # first_flag = 2
  101. # else:
  102. # odom_path_o = np.append(odom_path_o, [odom_pos], axis=0)
  103. # odom_path_p = np.append(odom_path_p, [odom_quat], axis=0)
  104.  
  105. #odom_path.poses.append(Odom_stuff.odom_pose)
  106. #print(odom_path_o[0])
  107. #print(odom_path_o[-1])
  108. #print("\n\n")
  109.  
  110.  
  111.  
  112. # apply current transform to all path poses
  113. updated_path = Path()
  114. nontransformed_path=Path()
  115. updated_path.header.frame_id = "odom"
  116. nontransformed_path.header.frame_id = "odom"
  117.  
  118. #L = len(odom_path_o)/3
  119.  
  120. if prev_trans == trans:
  121. L = range(int(len(odom_path_o) / 3))
  122. #print("old")
  123. else:
  124. L = [-1]
  125. #print("new")
  126.  
  127. for c in L:
  128. i += 1
  129. current_position = odom_path_p[c]
  130. current_orientation = odom_path_o[c]
  131. # print("current")
  132. # print(current_orientation)
  133. # perform transform
  134. tmp_transform = tf_quat
  135. #tmp_transform[3] = tmp_transform[3]
  136. transformed_orientation = quaternion_multiply(tmp_transform, current_orientation)
  137. # print("tf_trans")
  138. # print(tf_trans)
  139. # print("current_position")
  140. # print(current_position)
  141. transformed_position = np.add(tf_trans, current_position)
  142. # print("Transformed")
  143. # print(transformed_orientation)
  144. # print("\n\n")
  145. # prepare transformed waypoint
  146. transformed_pose = geometry_msgs.msg.PoseStamped()
  147. transformed_pose.header.frame_id = "waypoint"
  148. transformed_pose.pose.orientation.x = transformed_orientation[0]
  149. transformed_pose.pose.orientation.y = transformed_orientation[1]
  150. transformed_pose.pose.orientation.z = transformed_orientation[2]
  151. transformed_pose.pose.orientation.w = transformed_orientation[3]
  152. transformed_pose.pose.position.x = transformed_position[0]
  153. transformed_pose.pose.position.y = transformed_position[1]
  154. transformed_pose.pose.position.z = transformed_position[2]
  155.  
  156. # prepare transformed waypoint CV add
  157. nontransformed_pose = geometry_msgs.msg.PoseStamped()
  158. nontransformed_pose.header.frame_id = "waypoint"
  159. nontransformed_pose.pose.orientation.x = current_orientation[0]
  160. nontransformed_pose.pose.orientation.y = current_orientation[1]
  161. nontransformed_pose.pose.orientation.z = current_orientation[2]
  162. nontransformed_pose.pose.orientation.w = current_orientation[3]
  163. nontransformed_pose.pose.position.x = current_position[0]
  164. nontransformed_pose.pose.position.y = current_position[1]
  165. nontransformed_pose.pose.position.z = current_position[2]
  166.  
  167. # append waypoint to path
  168. updated_path.poses.append(transformed_pose)
  169.  
  170. # append waypoint to path
  171. nontransformed_path.poses.append(nontransformed_pose)
  172.  
  173.  
  174. #print(updated_path)
  175.  
  176. #print(updated_path.poses[0])
  177. #print(updated_path.poses[1])
  178. # send path as message
  179.  
  180. # apply transformation to current pose
  181. new_quat = quaternion_multiply(tf_quat,Odom_stuff.odom_quat)
  182. new_pos = np.add(tf_trans, odom_pos)
  183.  
  184.  
  185. # create the New Map framed odometry message
  186. map_odom = Odometry()
  187. map_odom.header.stamp = rospy.Time.now()
  188. map_odom.header.frame_id = "odom"
  189. map_odom.pose.pose.position.x = new_pos[0]
  190. map_odom.pose.pose.position.y = new_pos[1]
  191. map_odom.pose.pose.position.z = new_pos[2]
  192. new_quat_msg = geometry_msgs.msg.Quaternion(new_quat[0], new_quat[1], new_quat[2], new_quat[3])
  193. map_odom.pose.pose.orientation = new_quat_msg
  194. #print(map_odom.pose.pose)
  195.  
  196. # append most recent pose to create the uncorrected path message
  197. # map_wp = geometry_msgs.msg.PoseStamped()
  198. # map_wp.header.frame_id = "waypoint"
  199. # map_wp.pose = map_odom.pose.pose
  200. #map_wp.pose.position = map_odom.pose.pose.position
  201. #map_wp.pose.orientation = map_odom.pose.pose.orientation
  202. #map_path.poses.append(map_wp)
  203. #print(updated_path.poses[1])
  204. #print(map_path.poses)
  205. #print('\n')
  206.  
  207. # publish this new odometry and path message
  208. #map_odom_pub.publish(map_odom)
  209. #map_path_pub.publish(map_path)
  210. map_path_pub.publish(updated_path)
  211. #CV add
  212. map_odompath_pub.publish(nontransformed_path)
  213. prev_trans = trans
  214. rate.sleep()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement