ROS2FrameComponent.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #pragma once
  9. #include <AzCore/Component/Component.h>
  10. #include <AzCore/Serialization/Json/BaseJsonSerializer.h>
  11. #include <AzCore/std/smart_ptr/unique_ptr.h>
  12. #include <AzFramework/Components/TransformComponent.h>
  13. #include <ROS2/Frame/NamespaceConfiguration.h>
  14. #include <ROS2/Frame/ROS2FrameConfiguration.h>
  15. #include <ROS2/Frame/ROS2Transform.h>
  16. #include <ROS2/ROS2TypeIds.h>
  17. namespace ROS2
  18. {
  19. // Custom JSON serializer for ROS2FrameComponent configuration to handle version conversion
  20. class JsonFrameComponentConfigSerializer : public AZ::BaseJsonSerializer
  21. {
  22. public:
  23. AZ_RTTI(ROS2::JsonFrameComponentConfigSerializer, ROS2FrameComponentTypeId, AZ::BaseJsonSerializer);
  24. AZ_CLASS_ALLOCATOR_DECL;
  25. AZ::JsonSerializationResult::Result Load(
  26. void* outputValue,
  27. const AZ::Uuid& outputValueTypeId,
  28. const rapidjson::Value& inputValue,
  29. AZ::JsonDeserializerContext& context) override;
  30. };
  31. //! This component marks an interesting reference frame for ROS2 ecosystem.
  32. //! It serves as sensor data frame of reference and is responsible, through ROS2Transform, for publishing
  33. //! ros2 static and dynamic transforms (/tf_static, /tf). It also facilitates namespace handling.
  34. //! An entity can only have a single ROS2Frame on each level. Many ROS2 Components require this component.
  35. //! @note A robot should have this component on every level of entity hierarchy (for each joint, fixed or dynamic)
  36. class ROS2FrameComponent
  37. : public AZ::Component
  38. , public AZ::TickBus::Handler
  39. {
  40. friend class JsonFrameComponentConfigSerializer;
  41. public:
  42. AZ_COMPONENT(ROS2FrameComponent, "{EE743472-3E25-41EA-961B-14096AC1D66F}");
  43. ROS2FrameComponent();
  44. ROS2FrameComponent(const ROS2FrameConfiguration& configuration);
  45. //////////////////////////////////////////////////////////////////////////
  46. // Component overrides
  47. void Init() override;
  48. void Activate() override;
  49. void Deactivate() override;
  50. //////////////////////////////////////////////////////////////////////////
  51. static void Reflect(AZ::ReflectContext* context);
  52. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
  53. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible);
  54. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required);
  55. //! Get a frame id, which is needed for any ROS2 message with a Header
  56. //! @return Frame id which includes the namespace, ready to send in a ROS2 message
  57. AZStd::string GetNamespacedFrameID() const;
  58. //! Set a above-mentioned frame id
  59. void SetFrameID(const AZStd::string& frameId);
  60. //! Get the joint name including the namespace
  61. //! @note Supplementary metadata for Joint components, necessary in some cases for joints addressed by name in ROS 2
  62. //! @return The namespaced joint name, ready to send in a ROS2 message
  63. AZ::Name GetNamespacedJointName() const;
  64. //! Set the joint name
  65. //! @note May be populated during URDF import or set by the user in the Editor view
  66. //! @param jointName does not include the namespace. The namespace prefix is added automatically.
  67. void SetJointName(const AZStd::string& jointName);
  68. //! Get a namespace, which should be used for any publisher or subscriber in the same entity.
  69. //! @return A complete namespace (including parent namespaces)
  70. AZStd::string GetNamespace() const;
  71. //! Get a transform between this frame and the next frame up in hierarchy.
  72. //! @return If the parent frame is found, return a Transform between this frame and the parent.
  73. //! Otherwise, return a global Transform.
  74. //! @note Parent frame is not the same as parent Transform: there could be many Transforms in between without ROS2Frame components.
  75. AZ::Transform GetFrameTransform() const;
  76. //! Global frame name in ros2 ecosystem.
  77. //! @return The name of the global frame with namespace attached. It is typically "odom", "map", "world".
  78. AZStd::string GetGlobalFrameName() const;
  79. //! Updates the namespace and namespace strategy of the underlying namespace configuration
  80. //! @param ros2Namespace Namespace to set.
  81. //! @param strategy Namespace strategy to use.
  82. void UpdateNamespaceConfiguration(const AZStd::string& ros2Namespace, NamespaceConfiguration::NamespaceStrategy strategy);
  83. //! Get the configuration of this component.
  84. ROS2FrameConfiguration GetConfiguration() const;
  85. private:
  86. //////////////////////////////////////////////////////////////////////////
  87. // AZ::TickBus::Handler overrides
  88. void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
  89. //////////////////////////////////////////////////////////////////////////
  90. bool IsTopLevel() const; //!< True if this entity does not have a parent entity with ROS2.
  91. //! Whether transformation to parent frame can change during the simulation, or is fixed.
  92. bool IsDynamic() const;
  93. const ROS2FrameComponent* GetParentROS2FrameComponent() const;
  94. //! Return the frame id of this frame's parent. It can be useful to determine ROS 2 transformations.
  95. //! @return Parent frame ID.
  96. //! @note This also works with top-level frames, returning a global frame name.
  97. //! @see GetGlobalFrameName().
  98. AZStd::string GetParentFrameID() const;
  99. // Deprecated values used for backwards compatibility
  100. NamespaceConfiguration m_namespaceConfiguration;
  101. AZStd::string m_frameName;
  102. AZStd::string m_jointName;
  103. bool m_publishTransform;
  104. bool m_isDynamic;
  105. AZStd::unique_ptr<ROS2Transform> m_ros2Transform;
  106. };
  107. } // namespace ROS2