0

I wrote a node called translater that basically doesn't do anything except for translating one message type to another.

It subscribes to a the topic pose_before and publishes to another topic pose_after.

The topic pose_before is of message type "PoseWithCovarianceStamped" and pose_after is "TransformStamped".

The issue is that all values in pose_after remain 0 when they're being published, but when I try to print the values inside the callback they do respond correctly.

#!/usr/bin/env python
import rospy
from geometry_msgs.msg import PoseWithCovarianceStamped
from geometry_msgs.msg import TransformStamped

def callback(data):
    TransformStamped().transform.translation.x = data.pose.pose.position.x
    TransformStamped().transform.translation.y = data.pose.pose.position.y
    TransformStamped().transform.translation.z = data.pose.pose.position.z
    TransformStamped().transform.rotation.x = data.pose.pose.orientation.x
    TransformStamped().transform.rotation.y = data.pose.pose.orientation.y
    TransformStamped().transform.rotation.z = data.pose.pose.orientation.z
    TransformStamped().transform.rotation.w = data.pose.pose.orientation.w
def listener():
    pub = rospy.Publisher('pose_after', TransformStamped, queue_size=40)
    pose_after = TransformStamped()
    rospy.init_node('translater', anonymous=True)
    rospy.loginfo("Publishing pose_after")
    rospy.Subscriber('pose_before', PoseWithCovarianceStamped, callback)
    rate = rospy.Rate(40)
    pose_after.header.stamp = rospy.Time.now()
    pose_after.child_frame_id = 'base_footprint'
    print(pose_after.transform.translation.x) # this prints (0,0)
    while not rospy.is_shutdown():
        pub.publish(pose_after)
        rate.sleep()

if __name__ == '__main__':
    try:
        listener()
    except rospy.ROSInterruptException:
        pass

When I put the print line in callback then it actually is able to print the correct values, so I'm wondering why the values are lost outside of the callback. I've also tried assigning a global variable inside the callback but still didn't work.

Patricia Sung
  • 55
  • 1
  • 5
  • I think you should replace ```TransformStamped()``` by ```pose_after``` in your callback? Or why are you creating a new instance every line? – Fruchtzwerg Dec 31 '20 at 08:42
  • that was my initial version but still didn't work. However I tried declaring the publisher outside of talker() and publish inside my callback, which seemed to do the trick – Patricia Sung Dec 31 '20 at 16:48

0 Answers0