Go to the documentation of this file.00001
00002
00003 import rospy
00004 import numpy
00005 from geometry_msgs.msg import Quaternion, Vector3
00006 from sensor_msgs.msg import Imu
00007 import tf
00008
00009 class ImuRootlinkCalculator(object):
00010 def __init__(self):
00011 rospy.init_node("ImuRootlinkCalculator", anonymous=True)
00012
00013 self.base_link_frame = rospy.get_param("~base_link_frame", "BODY")
00014 self.listener = tf.TransformListener(True, rospy.Duration(10))
00015 self.imu_sub = rospy.Subscriber("~input", Imu, self.imu_callback, queue_size = 1)
00016 self.pub = rospy.Publisher("~output", Imu, queue_size = 1)
00017
00018 def execute(self):
00019 while not rospy.is_shutdown():
00020 rospy.spin()
00021
00022 def init_signal_callback(self, msg):
00023 time.sleep(1)
00024 with self.lock:
00025 self.initial_matrix = None
00026
00027 def imu_callback(self, msg):
00028 raw_imu_quat = [msg.orientation.x, msg.orientation.y, msg.orientation.z, msg.orientation.w]
00029 raw_imu_av = [msg.angular_velocity.x, msg.angular_velocity.y, msg.angular_velocity.z]
00030 raw_imu_acc = [msg.linear_acceleration.x, msg.linear_acceleration.y, msg.linear_acceleration.z]
00031
00032 try:
00033 self.listener.waitForTransform(self.base_link_frame, msg.header.frame_id, msg.header.stamp, rospy.Duration(1.0))
00034
00035 trans = self.listener.asMatrix(self.base_link_frame, msg.header)
00036 rootlink_quat = tf.transformations.quaternion_from_matrix(trans * tf.transformations.quaternion_matrix(raw_imu_quat))
00037 rootlink_av = numpy.dot(trans[:3, :3], numpy.array(raw_imu_av))
00038 rootlink_acc = numpy.dot(trans[:3, :3], numpy.array(raw_imu_acc))
00039
00040 except:
00041 rospy.logwarn("[%s] failed to solve imu_to_base tf: %s to %s", rospy.get_name(), msg.header.frame_id, self.base_link_frame)
00042 return
00043
00044 msg.header.frame_id = self.base_link_frame
00045 msg.orientation = Quaternion(*rootlink_quat)
00046 msg.angular_velocity = Vector3(*rootlink_av)
00047 msg.linear_acceleration = Vector3(*rootlink_acc)
00048
00049
00050 self.pub.publish(msg)
00051
00052 if __name__ == '__main__':
00053 try:
00054 node = ImuRootlinkCalculator()
00055 node.execute()
00056 except rospy.ROSInterruptException: pass
00057