ROS2Transform.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <ROS2/Frame/ROS2Transform.h>
  9. #include <ROS2/ROS2Bus.h>
  10. #include <ROS2/Utilities/ROS2Conversions.h>
  11. #include <tf2_ros/qos.hpp>
  12. #include <tf2_ros/transform_broadcaster.h>
  13. namespace ROS2
  14. {
  15. ROS2Transform::ROS2Transform(AZStd::string parentFrame, AZStd::string childFrame, bool isDynamic)
  16. : m_parentFrame(AZStd::move(parentFrame))
  17. , m_childFrame(AZStd::move(childFrame))
  18. , m_isDynamic(isDynamic)
  19. {
  20. }
  21. geometry_msgs::msg::TransformStamped ROS2Transform::CreateTransformMessage(const AZ::Transform& o3deTransform)
  22. {
  23. geometry_msgs::msg::TransformStamped t;
  24. t.header.stamp = ROS2Interface::Get()->GetROSTimestamp();
  25. t.header.frame_id = m_parentFrame.data();
  26. t.child_frame_id = m_childFrame.data();
  27. t.transform.translation = ROS2Conversions::ToROS2Vector3(o3deTransform.GetTranslation());
  28. t.transform.rotation = ROS2Conversions::ToROS2Quaternion(o3deTransform.GetRotation());
  29. return t;
  30. }
  31. void ROS2Transform::Publish(const AZ::Transform& transform)
  32. {
  33. if (m_isPublished && !m_isDynamic)
  34. { // Only publish static transforms once
  35. return;
  36. }
  37. ROS2Interface::Get()->BroadcastTransform(CreateTransformMessage(transform), m_isDynamic);
  38. m_isPublished = true;
  39. }
  40. } // namespace ROS2