cps_rmp220_support/nodes/odom_publisher_linus.py

55 lines
1.5 KiB
Python
Raw Normal View History

2025-01-14 11:40:08 +00:00
#!/usr/bin/env python
import rospy
2025-01-14 11:41:33 +00:00
import tf2_ros
2025-01-14 11:40:08 +00:00
from nav_msgs.msg import Odometry
from geometry_msgs.msg import TransformStamped
def odom_callback(msg):
2025-01-14 11:41:33 +00:00
# Create a TransformStamped message
2025-01-14 11:40:08 +00:00
odom_trans = TransformStamped()
odom_trans.header.stamp = msg.header.stamp
odom_trans.header.frame_id = msg.header.frame_id
2025-01-14 11:46:06 +00:00
# Set child_frame_id explicitly (e.g., base_link)
odom_trans.child_frame_id = "base_link"
2025-01-14 11:40:08 +00:00
2025-01-14 11:41:33 +00:00
# Set the translation
2025-01-14 11:49:44 +00:00
odom_trans.transform.translation.x = msg.pose.pose.position.x
odom_trans.transform.translation.y = msg.pose.pose.position.y
2025-01-14 11:40:08 +00:00
odom_trans.transform.translation.z = msg.pose.pose.position.z
2025-01-14 11:41:33 +00:00
# Set the rotation
2025-01-14 11:40:08 +00:00
odom_trans.transform.rotation = msg.pose.pose.orientation
2025-01-14 11:41:33 +00:00
# Publish the transform
tf_broadcaster.sendTransform(odom_trans)
2025-01-14 11:40:08 +00:00
2025-01-15 08:22:19 +00:00
# print debug info
rospy.loginfo("Position: x={}, y={}, z={}".format(
msg.pose.pose.position.x,
msg.pose.pose.position.y,
msg.pose.pose.position.z
))
rospy.loginfo("Orientation: x={}, y={}, z={}, w={}".format(
msg.pose.pose.orientation.x,
msg.pose.pose.orientation.y,
msg.pose.pose.orientation.z,
msg.pose.pose.orientation.w
))
2025-01-14 11:40:08 +00:00
if __name__ == "__main__":
2025-01-14 11:41:33 +00:00
rospy.init_node("tf2_broadcaster")
2025-01-14 11:40:08 +00:00
# Create a TransformBroadcaster
2025-01-14 11:41:33 +00:00
tf_broadcaster = tf2_ros.TransformBroadcaster()
2025-01-14 11:40:08 +00:00
# Subscribe to the /odom topic
rospy.Subscriber("/odom", Odometry, odom_callback)
2025-01-14 11:41:33 +00:00
rospy.loginfo("TF2 broadcaster node is running...")
2025-01-14 11:40:08 +00:00
# Keep the node running
rospy.spin()