1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
import rospy import tf2_ros from tf2_geometry_msgs import tf2_geometry_msgs from geometry_msgs.msg import TransformStamped,Twist import math
if __name__ == "__main__":
rospy.init_node("dsub")
buffer = tf2_ros.Buffer() sub = tf2_ros.TransformListener(buffer)
pub = rospy.Publisher("/turtle2/cmd_vel",Twist,queue_size=100)
rate = rospy.Rate(10) while not rospy.is_shutdown(): try: ''' 参数1:目标坐标系 参数2:源坐标系 参数3:rospy.Time(0)---取时间间隔最近的两个坐标帧(son1 相对 world 与 son2 相对 world)计算结果 返回值:son1 与 son2 的坐标关系 ''' ts = buffer.lookup_transform("turtle2","turtle1",rospy.Time(0)) rospy.loginfo("父级坐标系:%s,子级坐标系:%s,偏移量(%.2f,%.2f,%.2f)", ts.header.frame_id, ts.child_frame_id, ts.transform.translation.x, ts.transform.translation.y, ts.transform.translation.z ) twist = Twist() twist.linear.x = 0.5 * math.sqrt(math.pow(ts.transform.translation.x,2) + math.pow(ts.transform.translation.y,2)) twist.angular.z = 4 * math.atan2(ts.transform.translation.y,ts.transform.translation.x)
pub.publish(twist)
except Exception as e: rospy.logwarn("异常:%s",e) rate.sleep()
|