Transforming pose with tf listener is not working in Rviz
I have set some fixed position and orientation (pose) values to PR2's headplateframe in Rviz, trying to transform pose from headplateframe to base_link frame and save the values in position and quaternion arrays. But instead of going to try block, the except block is being executed, anyone can help? What is the bug here?
I have edited my code, so here is the new code:
pose1 = PoseStamped()
pose1.header.stamp = rospy.Time.now()
pose1.header.frame_id = "head_plate_frame"
pose1.pose.position.x = 0.7
pose1.pose.position.y = -0.05
pose1.pose.position.z = 1.1
pose1.pose.orientation.x = 0.3
pose1.pose.orientation.y = 0.3
pose1.pose.orientation.z = 0.2
pose1.pose.orientation.w = 0.2
listener = tf.TransformListener()
while not rospy.is_shutdown():
try:
now = rospy.Time.now()
listener.waitForTransform('/base_link','/head_plate_frame',now, rospy.Duration(10.0))
(position, quaternion) = listener.lookupTransform('/base_link', '/head_plate_frame', now)
except (tf.LookupException, tf.ConnectivityException, tf.ExtrapolationException):
traceback.print_exc()
pose_source = geometry_msgs.msg.Pose()
pose_source.orientation.x = quaternion[0]
pose_source.orientation.y = quaternion[1]
pose_source.orientation.z = quaternion[2]
pose_source.orientation.w = quaternion[3]
pose_source.position.x = position[0]
pose_source.position.y = position[1]
pose_source.position.z = position[2]
Here is the exception message;
Traceback (most recent call last):
File "/home/tawfiq/catkin_ws/src/pr2_moveit_tutorials/src/move_group_python_interface_tutorial.py", line 128, in <module>
move_group_python_interface_tutorial()
File "/home/tawfiq/catkin_ws/src/pr2_moveit_tutorials/src/move_group_python_interface_tutorial.py", line 71, in move_group_python_interface_tutorial
listener.waitForTransform('/base_link','/head_plate_frame',now, rospy.Duration(10.0))
File "/opt/ros/indigo/lib/python2.7/dist-packages/tf/listener.py", line 76, in waitForTransform
raise tf2_ros.TransformException(error_msg or "no such transformation: \"%s\" -> \"%s\"" % (source_frame, target_frame))
tf2.TransformException: Lookup would require extrapolation into the past. Requested time 1548633924.249253988 but the earliest data is at time 1548633924.770589113, when looking up transform from frame [head_plate_frame] to frame [base_link]
Asked by Tawfiq Chowdhury on 2019-01-27 00:31:39 UTC
Answers
It is true that a more specific error message would help, although I can see the problem from looking at your source code.
You're creating the transform listener object immediately before querying it for a transform, this will never work. The transform listener object needs time to collect a buffer full of transform messages before it will be able to respond to requests. It is recommended that a single tf.TransformListener is created for each node when they start up, and that object is used for all transform requests. A structure like this can be seen in the python tutorial.
Hope this helps.
Asked by PeteBlackerThe3rd on 2019-01-27 06:24:34 UTC
Comments
My point was more general than "we cannot help you". Printing "it doesn't work" in an exception handler isn't exactly helpful or good practice.
re: problem: that is indeed most likely the issue.
Asked by gvdhoorn on 2019-01-27 11:35:49 UTC
There seems to be no buffer in tf, it is tf2, are you referring to rate = rospy.Rate(10.0) ?
Asked by Tawfiq Chowdhury on 2019-01-27 18:12:38 UTC
Comments
It would certainly help if, instead of
print("Not working")
, the except block either printed the full stack trace, or just re-raised the exception so that you can examine the real problem.Right now ..
Asked by gvdhoorn on 2019-01-27 04:35:51 UTC
.. we can do nothing but guess.
Asked by gvdhoorn on 2019-01-27 04:37:29 UTC
@Tawfiq Chowdhury: why did you delete the question and post #q313913? The answer by @PeteBlackerThe3rd is a good one, so please either accept it, or tell us why it still didn't work for you.
Asked by gvdhoorn on 2019-01-28 04:04:30 UTC