我是因为python代码中target_frame的名字写错了,导致找不到这个target_frame。
这是我的launch文件(发布者)
<launch>
<!--publish coordinates of "son1" with respect to "world" and "son2"-->
<node pkg="tf2_ros" type="static_transform_publisher" name="son1" args="5 0 0 0 0 0 1 /world/son1" output="screen" />
<node pkg="tf2_ros" type="static_transform_publisher" name="son2" args="3 0 0 0 0 0 1 /world/son2" output="screen" />
</launch>
这是我修改前的python文件(订阅者)
#! /usr/bin/env python3
import rospy
import tf2_ros
from tf2_geometry_msgs import tf2_geometry_msgs
from geometry_msgs.msg import TransformStamped
def stc_sub():
rospy.init_node("static_sub")
# create the cache object first, and then create the subscriber
buffer = tf2_ros.Buffer()
sub = tf2_ros.TransformListener(buffer)
ps = tf2_geometry_msgs.PointStamped()
ps.header.stamp = rospy.Time.now()
ps.header.frame_id = "son1"
ps.point.x = 1.0
ps.point.y = 2.0
ps.point.z = 3.0
rate = rospy.Rate(1)
while not rospy.is_shutdown():
try:
ts = buffer.lookup_transform("son2", "son1", rospy.Time(0))
rospy.loginfo("Parent coordinate: [%s]; Child coordinate: [%s]; Offset: [%.2f, %.2f, %.2f]"
% (ts.header.frame_id,
ts.child_frame_id,
ts.transform.translation.x,
ts.transform.translation.y,
ts.transform.translation.z))
ps_out = buffer.transform(ps, "son2", rospy.Duration(1))
rospy.loginfo("The converted coordinates: [%.2f, %.2f, %.2f]; Current coordinate system: [%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()
if __name__ == "__main__":
stc_sub()
这是我修改后的python文件(订阅者)
#! /usr/bin/env python3
import rospy
import tf2_ros
from tf2_geometry_msgs import tf2_geometry_msgs
from geometry_msgs.msg import TransformStamped
def stc_sub():
rospy.init_node("static_sub")
# create the cache object first, and then create the subscriber
buffer = tf2_ros.Buffer()
sub = tf2_ros.TransformListener(buffer)
ps = tf2_geometry_msgs.PointStamped()
ps.header.stamp = rospy.Time.now()
ps.header.frame_id = "world/son1"
ps.point.x = 1.0
ps.point.y = 2.0
ps.point.z = 3.0
rate = rospy.Rate(1)
while not rospy.is_shutdown():
try:
ts = buffer.lookup_transform("world/son2", "world/son1", rospy.Time(0))
rospy.loginfo("Parent coordinate: [%s]; Child coordinate: [%s]; Offset: [%.2f, %.2f, %.2f]"
% (ts.header.frame_id,
ts.child_frame_id,
ts.transform.translation.x,
ts.transform.translation.y,
ts.transform.translation.z))
ps_out = buffer.transform(ps, "world/son2", rospy.Duration(1))
rospy.loginfo("The converted coordinates: [%.2f, %.2f, %.2f]; Current coordinate system: [%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()
if __name__ == "__main__":
stc_sub()
不知道frame_id的也可以打开rviz看看(其实应该在launch文件里面写了吧?只不过我是跟着视频敲的代码,没有自己再写一遍,没能完全理解代码意思,导致为了这点小错误搞了好久哈哈哈)
希望能帮到您!也希望大家不要像我一样再犯低级错误哈哈哈。