ROS2FrameComponent.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 <AzCore/Component/Entity.h>
  9. #include <AzCore/Component/EntityUtils.h>
  10. #include <AzCore/Serialization/EditContext.h>
  11. #include <AzCore/Serialization/EditContextConstants.inl>
  12. #include <AzCore/Serialization/SerializeContext.h>
  13. #include <ROS2/Frame/ROS2FrameComponent.h>
  14. #include <ROS2/ROS2Bus.h>
  15. #include <ROS2/ROS2GemUtilities.h>
  16. #include <ROS2/Utilities/ROS2Names.h>
  17. namespace ROS2
  18. {
  19. namespace Internal
  20. {
  21. AZ::TransformInterface* GetEntityTransformInterface(const AZ::Entity* entity)
  22. {
  23. if (!entity)
  24. {
  25. AZ_Error("GetEntityTransformInterface", false, "Invalid entity!");
  26. return nullptr;
  27. }
  28. auto* interface = Utils::GetGameOrEditorComponent<AzFramework::TransformComponent>(entity);
  29. return interface;
  30. }
  31. const ROS2FrameComponent* GetFirstROS2FrameAncestor(const AZ::Entity* entity)
  32. {
  33. auto* entityTransformInterface = GetEntityTransformInterface(entity);
  34. if (!entityTransformInterface)
  35. {
  36. AZ_Error("GetFirstROS2FrameAncestor", false, "Invalid transform interface!");
  37. return nullptr;
  38. }
  39. AZ::EntityId parentEntityId = entityTransformInterface->GetParentId();
  40. if (!parentEntityId.IsValid())
  41. { // We have reached the top level, there is no parent entity so there can be no parent ROS2Frame
  42. return nullptr;
  43. }
  44. AZ::Entity* parentEntity = nullptr;
  45. AZ::ComponentApplicationBus::BroadcastResult(parentEntity, &AZ::ComponentApplicationRequests::FindEntity, parentEntityId);
  46. AZ_Assert(parentEntity, "No parent entity id : %s", parentEntityId.ToString().c_str());
  47. auto* component = Utils::GetGameOrEditorComponent<ROS2FrameComponent>(parentEntity);
  48. if (component == nullptr)
  49. { // Parent entity has no ROS2Frame, but there can still be a ROS2Frame in its ancestors
  50. return GetFirstROS2FrameAncestor(parentEntity);
  51. }
  52. // Found the component!
  53. return component;
  54. }
  55. //! Checks whether the entity has a component of the given type
  56. //! @param entity pointer to entity
  57. //! @param typeId type of the component
  58. //! @returns true if entity has component with given type
  59. static bool CheckIfEntityHasComponentOfType(const AZ::Entity* entity, const AZ::Uuid typeId)
  60. {
  61. auto components = AZ::EntityUtils::FindDerivedComponents(entity, typeId);
  62. return !components.empty();
  63. }
  64. } // namespace Internal
  65. void ROS2FrameComponent::Activate()
  66. {
  67. m_namespaceConfiguration.PopulateNamespace(IsTopLevel(), GetEntity()->GetName());
  68. if (m_publishTransform)
  69. {
  70. AZ_TracePrintf("ROS2FrameComponent", "Setting up %s", GetFrameID().data());
  71. // The frame will always be dynamic if it's a top entity.
  72. if (IsTopLevel())
  73. {
  74. m_isDynamic = true;
  75. }
  76. // Otherwise it'll be dynamic when it has joints and it's not a fixed joint.
  77. else
  78. {
  79. const bool hasJoints = Internal::CheckIfEntityHasComponentOfType(
  80. m_entity, AZ::Uuid("{B01FD1D2-1D91-438D-874A-BF5EB7E919A8}")); // Physx::JointComponent;
  81. const bool hasFixedJoints = Internal::CheckIfEntityHasComponentOfType(
  82. m_entity, AZ::Uuid("{02E6C633-8F44-4CEE-AE94-DCB06DE36422}")); // Physx::FixedJointComponent
  83. m_isDynamic = hasJoints && !hasFixedJoints;
  84. }
  85. AZ_TracePrintf(
  86. "ROS2FrameComponent",
  87. "Setting up %s transform between parent %s and child %s to be published %s\n",
  88. IsDynamic() ? "dynamic" : "static",
  89. GetParentFrameID().data(),
  90. GetFrameID().data(),
  91. IsDynamic() ? "continuously to /tf" : "once to /tf_static");
  92. m_ros2Transform = AZStd::make_unique<ROS2Transform>(GetParentFrameID(), GetFrameID(), IsDynamic());
  93. if (IsDynamic())
  94. {
  95. AZ::TickBus::Handler::BusConnect();
  96. }
  97. else
  98. {
  99. m_ros2Transform->Publish(GetFrameTransform());
  100. }
  101. }
  102. }
  103. void ROS2FrameComponent::Deactivate()
  104. {
  105. if (m_publishTransform)
  106. {
  107. if (IsDynamic())
  108. {
  109. AZ::TickBus::Handler::BusDisconnect();
  110. }
  111. m_ros2Transform.reset();
  112. }
  113. }
  114. void ROS2FrameComponent::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time)
  115. {
  116. m_ros2Transform->Publish(GetFrameTransform());
  117. }
  118. AZStd::string ROS2FrameComponent::GetGlobalFrameName() const
  119. {
  120. return ROS2Names::GetNamespacedName(GetNamespace(), AZStd::string("odom"));
  121. }
  122. bool ROS2FrameComponent::IsTopLevel() const
  123. {
  124. return GetGlobalFrameName() == GetParentFrameID();
  125. }
  126. bool ROS2FrameComponent::IsDynamic() const
  127. {
  128. return m_isDynamic;
  129. }
  130. const ROS2FrameComponent* ROS2FrameComponent::GetParentROS2FrameComponent() const
  131. {
  132. return Internal::GetFirstROS2FrameAncestor(GetEntity());
  133. }
  134. AZ::Transform ROS2FrameComponent::GetFrameTransform() const
  135. {
  136. auto* transformInterface = Internal::GetEntityTransformInterface(GetEntity());
  137. if (const auto* parentFrame = GetParentROS2FrameComponent(); parentFrame != nullptr)
  138. {
  139. auto* ancestorTransformInterface = Internal::GetEntityTransformInterface(parentFrame->GetEntity());
  140. AZ_Assert(ancestorTransformInterface, "No transform interface for an entity with a ROS2Frame component, which requires it!");
  141. const auto worldFromAncestor = ancestorTransformInterface->GetWorldTM();
  142. const auto worldFromThis = transformInterface->GetWorldTM();
  143. const auto ancestorFromWorld = worldFromAncestor.GetInverse();
  144. return ancestorFromWorld * worldFromThis;
  145. }
  146. return transformInterface->GetWorldTM();
  147. }
  148. AZStd::string ROS2FrameComponent::GetParentFrameID() const
  149. {
  150. if (auto parentFrame = GetParentROS2FrameComponent(); parentFrame != nullptr)
  151. {
  152. return parentFrame->GetFrameID();
  153. }
  154. // If parent entity does not exist or does not have a ROS2FrameComponent, return ROS2 default global frame.
  155. return GetGlobalFrameName();
  156. }
  157. AZStd::string ROS2FrameComponent::GetFrameID() const
  158. {
  159. return ROS2Names::GetNamespacedName(GetNamespace(), m_frameName);
  160. }
  161. void ROS2FrameComponent::SetFrameID(const AZStd::string& frameId)
  162. {
  163. m_frameName = frameId;
  164. }
  165. AZStd::string ROS2FrameComponent::GetNamespace() const
  166. {
  167. auto parentFrame = GetParentROS2FrameComponent();
  168. AZStd::string parentNamespace;
  169. if (parentFrame != nullptr)
  170. {
  171. parentNamespace = parentFrame->GetNamespace();
  172. }
  173. return m_namespaceConfiguration.GetNamespace(parentNamespace);
  174. }
  175. void ROS2FrameComponent::Reflect(AZ::ReflectContext* context)
  176. {
  177. NamespaceConfiguration::Reflect(context);
  178. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  179. {
  180. serialize->Class<ROS2FrameComponent, AZ::Component>()
  181. ->Version(1)
  182. ->Field("Namespace Configuration", &ROS2FrameComponent::m_namespaceConfiguration)
  183. ->Field("Frame Name", &ROS2FrameComponent::m_frameName)
  184. ->Field("Publish Transform", &ROS2FrameComponent::m_publishTransform);
  185. if (AZ::EditContext* ec = serialize->GetEditContext())
  186. {
  187. ec->Class<ROS2FrameComponent>("ROS2 Frame", "[ROS2 Frame component]")
  188. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  189. ->Attribute(AZ::Edit::Attributes::Category, "ROS2")
  190. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Game"))
  191. ->DataElement(
  192. AZ::Edit::UIHandlers::Default,
  193. &ROS2FrameComponent::m_namespaceConfiguration,
  194. "Namespace Configuration",
  195. "Namespace Configuration")
  196. ->DataElement(AZ::Edit::UIHandlers::Default, &ROS2FrameComponent::m_frameName, "Frame Name", "Frame Name")
  197. ->DataElement(
  198. AZ::Edit::UIHandlers::Default, &ROS2FrameComponent::m_publishTransform, "Publish Transform", "Publish Transform");
  199. }
  200. }
  201. }
  202. void ROS2FrameComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  203. {
  204. provided.push_back(AZ_CRC_CE("ROS2Frame"));
  205. }
  206. void ROS2FrameComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  207. {
  208. incompatible.push_back(AZ_CRC_CE("ROS2Frame"));
  209. }
  210. void ROS2FrameComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  211. {
  212. required.push_back(AZ_CRC_CE("TransformService"));
  213. }
  214. ROS2FrameComponent::ROS2FrameComponent() = default;
  215. ROS2FrameComponent::ROS2FrameComponent(const AZStd::string& frameId)
  216. : m_frameName(frameId)
  217. {
  218. }
  219. } // namespace ROS2