ROS2Transform.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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/Clock/ROS2ClockRequestBus.h>
  9. #include <ROS2/Frame/ROS2Transform.h>
  10. #include <ROS2/ROS2Bus.h>
  11. #include <ROS2/Utilities/ROS2Conversions.h>
  12. #include <tf2_ros/qos.hpp>
  13. #include <tf2_ros/transform_broadcaster.h>
  14. namespace ROS2
  15. {
  16. ROS2Transform::ROS2Transform(AZStd::string parentFrame, AZStd::string childFrame, bool isDynamic)
  17. : m_parentFrame(AZStd::move(parentFrame))
  18. , m_childFrame(AZStd::move(childFrame))
  19. , m_isDynamic(isDynamic)
  20. {
  21. }
  22. geometry_msgs::msg::TransformStamped ROS2Transform::CreateTransformMessage(const AZ::Transform& o3deTransform)
  23. {
  24. geometry_msgs::msg::TransformStamped t;
  25. ROS2::ROS2ClockRequestBus::BroadcastResult(t.header.stamp, &ROS2::ROS2ClockRequestBus::Events::GetROSTimestamp);
  26. t.header.frame_id = m_parentFrame.data();
  27. t.child_frame_id = m_childFrame.data();
  28. t.transform.translation = ROS2Conversions::ToROS2Vector3(o3deTransform.GetTranslation());
  29. t.transform.rotation = ROS2Conversions::ToROS2Quaternion(o3deTransform.GetRotation());
  30. return t;
  31. }
  32. void ROS2Transform::Publish(const AZ::Transform& transform)
  33. {
  34. if (m_isPublished && !m_isDynamic)
  35. { // Only publish static transforms once
  36. return;
  37. }
  38. ROS2Interface::Get()->BroadcastTransform(CreateTransformMessage(transform), m_isDynamic);
  39. m_isPublished = true;
  40. }
  41. } // namespace ROS2