ROS多坐标变换出现错误:error:“son2“ passed to lookupTransform argument target_frame does not exist. 【解决方法】

文章讲述了作者在使用Python进行ROS(RobotOperatingSystem)编程时,由于target_frame名称错误导致无法正确查找变换。通过提供修改前后的launch文件和Python代码,展示了如何修正frame_id以解决TF2中的坐标转换问题。
摘要由CSDN通过智能技术生成

我是因为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文件里面写了吧?只不过我是跟着视频敲的代码,没有自己再写一遍,没能完全理解代码意思,导致为了这点小错误搞了好久哈哈哈)

希望能帮到您!也希望大家不要像我一样再犯低级错误哈哈哈。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值