ROS2FrameComponent.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. const bool hasArticulations = Internal::CheckIfEntityHasComponentOfType(
  84. m_entity, AZ::Uuid("{48751E98-B35F-4A2F-A908-D9CDD5230264}")); // Physx::ArticulationComponent
  85. m_isDynamic = (hasJoints && !hasFixedJoints) || hasArticulations;
  86. }
  87. AZ_TracePrintf(
  88. "ROS2FrameComponent",
  89. "Setting up %s transform between parent %s and child %s to be published %s\n",
  90. IsDynamic() ? "dynamic" : "static",
  91. GetParentFrameID().data(),
  92. GetFrameID().data(),
  93. IsDynamic() ? "continuously to /tf" : "once to /tf_static");
  94. m_ros2Transform = AZStd::make_unique<ROS2Transform>(GetParentFrameID(), GetFrameID(), IsDynamic());
  95. if (IsDynamic())
  96. {
  97. AZ::TickBus::Handler::BusConnect();
  98. }
  99. else
  100. {
  101. m_ros2Transform->Publish(GetFrameTransform());
  102. }
  103. }
  104. }
  105. void ROS2FrameComponent::Deactivate()
  106. {
  107. if (m_publishTransform)
  108. {
  109. if (IsDynamic())
  110. {
  111. AZ::TickBus::Handler::BusDisconnect();
  112. }
  113. m_ros2Transform.reset();
  114. }
  115. }
  116. void ROS2FrameComponent::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time)
  117. {
  118. m_ros2Transform->Publish(GetFrameTransform());
  119. }
  120. AZStd::string ROS2FrameComponent::GetGlobalFrameName() const
  121. {
  122. return ROS2Names::GetNamespacedName(GetNamespace(), AZStd::string("odom"));
  123. }
  124. bool ROS2FrameComponent::IsTopLevel() const
  125. {
  126. return GetGlobalFrameName() == GetParentFrameID();
  127. }
  128. bool ROS2FrameComponent::IsDynamic() const
  129. {
  130. return m_isDynamic;
  131. }
  132. const ROS2FrameComponent* ROS2FrameComponent::GetParentROS2FrameComponent() const
  133. {
  134. return Internal::GetFirstROS2FrameAncestor(GetEntity());
  135. }
  136. AZ::Transform ROS2FrameComponent::GetFrameTransform() const
  137. {
  138. auto* transformInterface = Internal::GetEntityTransformInterface(GetEntity());
  139. if (const auto* parentFrame = GetParentROS2FrameComponent(); parentFrame != nullptr)
  140. {
  141. auto* ancestorTransformInterface = Internal::GetEntityTransformInterface(parentFrame->GetEntity());
  142. AZ_Assert(ancestorTransformInterface, "No transform interface for an entity with a ROS2Frame component, which requires it!");
  143. const auto worldFromAncestor = ancestorTransformInterface->GetWorldTM();
  144. const auto worldFromThis = transformInterface->GetWorldTM();
  145. const auto ancestorFromWorld = worldFromAncestor.GetInverse();
  146. return ancestorFromWorld * worldFromThis;
  147. }
  148. return transformInterface->GetWorldTM();
  149. }
  150. AZStd::string ROS2FrameComponent::GetParentFrameID() const
  151. {
  152. if (auto parentFrame = GetParentROS2FrameComponent(); parentFrame != nullptr)
  153. {
  154. return parentFrame->GetFrameID();
  155. }
  156. // If parent entity does not exist or does not have a ROS2FrameComponent, return ROS2 default global frame.
  157. return GetGlobalFrameName();
  158. }
  159. AZStd::string ROS2FrameComponent::GetFrameID() const
  160. {
  161. return ROS2Names::GetNamespacedName(GetNamespace(), m_frameName);
  162. }
  163. void ROS2FrameComponent::SetFrameID(const AZStd::string& frameId)
  164. {
  165. m_frameName = frameId;
  166. }
  167. AZStd::string ROS2FrameComponent::GetNamespace() const
  168. {
  169. auto parentFrame = GetParentROS2FrameComponent();
  170. AZStd::string parentNamespace;
  171. if (parentFrame != nullptr)
  172. {
  173. parentNamespace = parentFrame->GetNamespace();
  174. }
  175. return m_namespaceConfiguration.GetNamespace(parentNamespace);
  176. }
  177. AZ::Name ROS2FrameComponent::GetJointName() const
  178. {
  179. return AZ::Name(ROS2Names::GetNamespacedName(GetNamespace(), m_jointNameString).c_str());
  180. }
  181. void ROS2FrameComponent::SetJointName(const AZStd::string& jointNameString)
  182. {
  183. m_jointNameString = jointNameString;
  184. }
  185. void ROS2FrameComponent::Reflect(AZ::ReflectContext* context)
  186. {
  187. NamespaceConfiguration::Reflect(context);
  188. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  189. {
  190. serialize->Class<ROS2FrameComponent, AZ::Component>()
  191. ->Version(1)
  192. ->Field("Namespace Configuration", &ROS2FrameComponent::m_namespaceConfiguration)
  193. ->Field("Frame Name", &ROS2FrameComponent::m_frameName)
  194. ->Field("Joint Name", &ROS2FrameComponent::m_jointNameString)
  195. ->Field("Publish Transform", &ROS2FrameComponent::m_publishTransform);
  196. if (AZ::EditContext* ec = serialize->GetEditContext())
  197. {
  198. ec->Class<ROS2FrameComponent>("ROS2 Frame", "[ROS2 Frame component]")
  199. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  200. ->Attribute(AZ::Edit::Attributes::Category, "ROS2")
  201. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Game"))
  202. ->DataElement(
  203. AZ::Edit::UIHandlers::Default,
  204. &ROS2FrameComponent::m_namespaceConfiguration,
  205. "Namespace Configuration",
  206. "Namespace Configuration")
  207. ->DataElement(AZ::Edit::UIHandlers::Default, &ROS2FrameComponent::m_frameName, "Frame Name", "Frame Name")
  208. ->DataElement(AZ::Edit::UIHandlers::Default, &ROS2FrameComponent::m_jointNameString, "Joint Name", "Joint Name")
  209. ->DataElement(
  210. AZ::Edit::UIHandlers::Default, &ROS2FrameComponent::m_publishTransform, "Publish Transform", "Publish Transform");
  211. }
  212. }
  213. }
  214. void ROS2FrameComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  215. {
  216. provided.push_back(AZ_CRC_CE("ROS2Frame"));
  217. }
  218. void ROS2FrameComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  219. {
  220. incompatible.push_back(AZ_CRC_CE("ROS2Frame"));
  221. }
  222. void ROS2FrameComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  223. {
  224. required.push_back(AZ_CRC_CE("TransformService"));
  225. }
  226. ROS2FrameComponent::ROS2FrameComponent() = default;
  227. ROS2FrameComponent::ROS2FrameComponent(const AZStd::string& frameId)
  228. : m_frameName(frameId)
  229. {
  230. }
  231. } // namespace ROS2