Robotics StackExchange | Archived questions

Print tf data using python

I'm using Ubuntu 18.04 (LTS), Melodic. I'm currently trying to print tf data in the terminal using python code for my 4-DoF robot. I tried customizing python code, but I couldn't print tf data in the terminal through python code. I have seen this YouTube video, but no luck. Below is my code:

#!/usr/bin/env python
import rospy

# Because of transformations
import tf_conversions

import tf2_ros
from geometry_msgs.msg import TransformStamped

t = TransformStamped()


def handle_turtle_pose(msg):

    global t

    br = tf2_ros.TransformBroadcaster()

    t.header.stamp = rospy.Time.now()
    t.header.frame_id = "link_3"
    t.child_frame_id = "link_4"
    t.transform.translation.x = msg.x
    t.transform.translation.y = msg.y
    t.transform.translation.z = msg.z
    q = tf2_conversions.transformations.quaternion_from_euler(0, 0, msg.theta)
    t.transform.rotation.x = q[0]
    t.transform.rotation.y = q[1]
    t.transform.rotation.z = q[2]
    t.transform.rotation.w = q[3]

    br.sendTransform(t)


if __name__ == "__main__":
    rospy.init_node("tf2_turtle_broadcaster")
    turtlename = rospy.get_param("robot_description")
    rospy.Subscriber("/%s/pose" % turtlename, TransformStamped, handle_turtle_pose, turtlename)
    rospy.spin()

I changed the above code ample of times. Below is my progress so far:

  1. Some times nothing shows in terminal
  2. Sometimes callback function is bad and etc.

I'm new to this tf concept, so please try to provide an answer to my problem. If you come up with suitable python code for my situation, then it is appreciable, or else tell me where I should change in my python code to print tf data in the terminal

Please help me

Asked by ramkumar on 2022-10-12 14:21:32 UTC

Comments

All you need is a tf2 listener. Please read Writing a tf2 listener (Python).

Asked by ravijoshi on 2022-10-14 05:42:26 UTC

Answers

Just 3 Alternatives to display tf:

  1. Use rostopic: rostopic echo /tf

  2. Use rqt to show the output of the tf topic (http://wiki.ros.org/rqt)

  3. Use a logger (http://wiki.ros.org/roscpp/Overview/Logging)

Asked by nils_iseke on 2022-10-15 07:33:59 UTC

Comments