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 60 61 62 63 64 65 66 67
|
import rospy import tf2_ros from tf2_geometry_msgs import tf2_geometry_msgs
''' 订阅方:订阅坐标变换消息,传入被转换的坐标点,调用转换算法 流程: 1.导包 2.初始化ros节点 3.创建订阅对象 4.组织被转换的坐标点 5.转换逻辑实现,调用tf封装的算法 6.输出结果 '''
if __name__ == "__main__":
rospy.init_node("static_sub")
buffer = tf2_ros.Buffer() sub = tf2_ros.TransformListener(buffer)
ps = tf2_geometry_msgs.PointStamped() ps.header.stamp = rospy.Time() ps.header.frame_id = "turtle1" ps.point.x = 2.0 ps.point.y = 5.0 ps.point.z = 3.0
rate = rospy.Rate(10) while not rospy.is_shutdown(): try: ''' param 1:被转换的座标点 param 2:目标坐标系 返回值:输出的目标点
ps: 问题:抛出异常 base_link 不存在 原因:转换函数调用时,可能还没有订阅到坐标系的相对关系 解决:try 捕获异常,并处理 ''' ps_out = buffer.transform(ps,"world")
rospy.loginfo("转换后的点(%.2f,%.2f,%.2f),参考的坐标:%s", ps_out.point.x, ps_out.point.y, ps_out.point.z, ps_out.header.frame_id ) except Exception as e: rospy.logwarn("异常:%s",e) rate.sleep()
|