Advertisement
Guest User

broadcast_map_odom_base_

a guest
May 14th, 2024
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import rclpy
  3. from rclpy.node import Node
  4. import tf2_ros
  5. from geometry_msgs.msg import TransformStamped, Quaternion
  6. from math import cos, sin, radians
  7.  
  8. class DynamicBroadcaster(Node):
  9. def __init__(self):
  10. super().__init__('dynamic_tf2_broadcaster')
  11. self.br = tf2_ros.TransformBroadcaster(self)
  12. self.timer = self.create_timer(0.1, self.timer_callback) # Timer to control the update rate
  13.  
  14. def timer_callback(self):
  15. # Broadcast transform from odom to base_footprint
  16. odom_to_base = self.create_transform(
  17. 'odom', 'base_footprint', 0.0, 0.0, 0.0, 0.0)
  18. self.br.sendTransform(odom_to_base)
  19.  
  20. # Broadcast transform from map to odom
  21. map_to_odom = self.create_transform(
  22. 'map', 'odom', 0.0, 0.0, 0.0, 0.0) # Modify these values as needed
  23. self.br.sendTransform(map_to_odom)
  24.  
  25. def create_transform(self, parent_frame, child_frame, x, y, z, yaw):
  26. """ Helper function to create and return a TransformStamped. """
  27. t = TransformStamped()
  28. t.header.stamp = self.get_clock().now().to_msg()
  29. t.header.frame_id = parent_frame
  30. t.child_frame_id = child_frame
  31. t.transform.translation.x = x
  32. t.transform.translation.y = y
  33. t.transform.translation.z = z
  34. t.transform.rotation = self.create_quaternion_from_yaw(yaw)
  35. return t
  36.  
  37. def create_quaternion_from_yaw(self, yaw):
  38. """ Helper function to create quaternion from yaw angle in radians. """
  39. q = Quaternion()
  40. q.w = cos(radians(yaw) / 2)
  41. q.x = 0.0
  42. q.y = 0.0
  43. q.z = sin(radians(yaw) / 2)
  44. return q
  45.  
  46. def main():
  47. rclpy.init()
  48. node = DynamicBroadcaster()
  49. try:
  50. rclpy.spin(node)
  51. except KeyboardInterrupt:
  52. pass
  53.  
  54. node.destroy_node()
  55. rclpy.shutdown()
  56.  
  57. if __name__ == '__main__':
  58. main()
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement