ConstantHeightFramePublisher.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 # license removed for brevity
00003 import rospy
00004 from nav_msgs.msg import Odometry
00005 from std_msgs.msg import Float64
00006 from dynamic_reconfigure.server import Server
00007 from jsk_robot_startup.cfg import ConstantHeightFramePublisherReconfigureConfig
00008 
00009 import tf
00010 import numpy
00011 
00012 class ConstantHeightFramePublisher:
00013     def __init__(self):
00014         rospy.init_node("ConstantHeightFramePublisher", anonymous=True)
00015         self.sub = rospy.Subscriber("~height", Float64, self.height_callback)
00016         self.broadcast = tf.TransformBroadcaster()
00017         self.listener = tf.TransformListener()
00018         self.rate = rospy.Rate(rospy.get_param("~rate", 10.0)) # [Hz]
00019         self.height = rospy.get_param("~height", 1.0) # [m]
00020         self.parent = rospy.get_param("~parent_frame", "BODY")
00021         self.odom = rospy.get_param("~odom_frame", "odom")
00022         self.frame_name = rospy.get_param("~frame_name", "pointcloud_to_scan_base")
00023         self.reconfigure_server = Server(ConstantHeightFramePublisherReconfigureConfig, self.reconfigure_callback)
00024 
00025     def execute(self):
00026         while not rospy.is_shutdown():
00027             self.make_constant_tf()
00028             self.rate.sleep()
00029 
00030     def height_callback(self, msg):
00031         self.height = msg.data
00032 
00033     def reconfigure_callback(self, config, level):
00034         self.height = config["height"]
00035         rospy.loginfo("[%s]" + " Modified simulated scan height to %f", rospy.get_name(), self.height)
00036         return config
00037 
00038     def make_constant_tf(self):
00039         try:
00040             (trans,rot) = self.listener.lookupTransform(self.parent, self.odom, rospy.Time(0))
00041             # transformation: (x, y): same as parent, z: equal to height
00042             T = tf.transformations.quaternion_matrix(rot)
00043             T[:3, 3] = trans
00044             T_inv = tf.transformations.inverse_matrix(T)
00045             target_trans = T.dot(numpy.array([T_inv[:3, 3][0], T_inv[:3, 3][1], self.height, 1]))[:3]
00046             # rotation: (x, y): same as /odom, z: same as parent 
00047             rot_euler = tf.transformations.euler_from_quaternion(rot)
00048             target_rot = tf.transformations.quaternion_from_euler(rot_euler[0], rot_euler[1], 0.0)
00049             # publish tf
00050             self.broadcast.sendTransform(target_trans, target_rot, rospy.Time.now(), self.frame_name, self.parent)
00051         except (tf.LookupException, tf.ConnectivityException, tf.ExtrapolationException):
00052             rospy.logwarn("[%s] Failed to solve tf of %s to %s.", rospy.get_name(), self.parent, self.odom)
00053             return
00054 
00055 if __name__ == '__main__':
00056     try:
00057         node = ConstantHeightFramePublisher()
00058         node.execute()
00059     except rospy.ROSInterruptException: pass


jsk_robot_startup
Author(s):
autogenerated on Sat Jul 1 2017 02:42:18