visual_odometry_transform.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import rospy
3 import numpy as np
4 import math
5 import tf
6 from geometry_msgs.msg import Point, Pose, Quaternion, PoseStamped
7 from tf.transformations import euler_from_quaternion, quaternion_from_euler, quaternion_matrix, quaternion_from_matrix, quaternion_multiply
8 from nav_msgs.msg import Odometry
9 
10 # Topic given by rtabmap in odom frame
11 rtabmap_original_odom_topic = "/rtabmap/ugv_odom"
12 # Odom in base link topic
13 rtabmap_odom_topic = "/rtabmap/odom"
14 
15 target_frame_id = "/ugv_base_link"
16 source_frame_id = "/ugv_odom" # /camera_link
17 
18 # Create the odometry message
19 odom = Odometry()
20 odom_original = Odometry()
21 
22 def odomCallback(odom_msg):
23  global odom_original
24  odom_original = odom_msg
25  # odom.header.stamp = odom_msg.header.stamp
26 
27 
28 if __name__ == '__main__':
29  rospy.init_node('visual_odom_frame_transformer')
30 
31  listener = tf.TransformListener()
32 
33  # Create the odom publisher
34  rtabmap_odom_pub = rospy.Publisher(rtabmap_odom_topic, Odometry, queue_size=1)
35 
36  # Create the odometry subscriber
37  rospy.Subscriber(rtabmap_original_odom_topic, Odometry, odomCallback)
38 
39  listener.waitForTransform(target_frame_id, source_frame_id, rospy.Time(), rospy.Duration(100.0))
40 
41  rate = rospy.Rate(10.0)
42  while not rospy.is_shutdown():
43  try:
44  (trans,rot) = listener.lookupTransform(target_frame_id, source_frame_id, rospy.Time(0))
46  continue
47 
48  # Apply the transformation from odom frame to base link frame
49  odom_x = odom_original.pose.pose.position.x
50  odom_y = odom_original.pose.pose.position.y
51  odom_z = odom_original.pose.pose.position.z
52  odom_rx = odom_original.pose.pose.orientation.x
53  odom_ry = odom_original.pose.pose.orientation.y
54  odom_rz = odom_original.pose.pose.orientation.z
55  odom_rw = odom_original.pose.pose.orientation.w
56 
57  # Tranform the rot and trans to a matrix
58  R_L = quaternion_matrix([rot[0],rot[1],rot[2],rot[3]])
59  R_L[0][3] = trans[0]
60  R_L[1][3] = trans[1]
61  R_L[2][3] = trans[2]
62  R_L = np.linalg.inv(R_L)
63 
64  # Matrix with the original message rotation and translation
65  R_I = quaternion_matrix([odom_rx, odom_ry, odom_rz, odom_rw])
66  R_I[0][3] = odom_x
67  R_I[1][3] = odom_y
68  R_I[2][3] = odom_z
69 
70  # Matrix with the final rotation and translation
71  R_F = R_I.dot(R_L)
72  # Final quaternion matrix
73  quat = quaternion_from_matrix(R_F)
74 
75  # Fill the odometry message
76  odom.header.stamp = odom_original.header.stamp
77  odom.header.frame_id = source_frame_id
78  odom.child_frame_id = target_frame_id
79  odom.pose.pose = Pose(Point(R_F[0][3],R_F[1][3],R_F[2][3]),Quaternion(quat[0],quat[1],quat[2],quat[3]))
80 
81  # Publish the odometry message
82  rtabmap_odom_pub.publish(odom)
83 
84  rate.sleep()


earth_rover_localization
Author(s):
autogenerated on Wed Apr 28 2021 02:15:34