mi-task/test/ros2/rgb-camera/location-1.py

34 lines
1.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
import rospy
import tf2_ros
from geometry_msgs.msg import TransformStamped
def get_camera_height():
rospy.init_node('camera_height_monitor')
# 创建TF2监听器
tf_buffer = tf2_ros.Buffer()
tf_listener = tf2_ros.TransformListener(tf_buffer)
rate = rospy.Rate(10.0) # 10Hz
while not rospy.is_shutdown():
try:
# 查询从world到RGB_camera_link的变换
# 如果您的Gazebo使用其他世界坐标系名称请相应调整
trans = tf_buffer.lookup_transform('world', 'RGB_camera_link', rospy.Time())
# 提取z轴值高度
camera_height = trans.transform.translation.z
rospy.loginfo("RGB相机当前高度: %.3f", camera_height)
except (tf2_ros.LookupException, tf2_ros.ConnectivityException, tf2_ros.ExtrapolationException) as e:
rospy.logwarn("无法获取变换: %s", e)
rate.sleep()
if __name__ == '__main__':
try:
get_camera_height()
except rospy.ROSInterruptException:
pass