ROS2SpawnerComponentController.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. #ifndef WITH_GAZEBO_MSGS
  9. static_assert(false, "This file should not be included in the build, without WITH_GAZEBO_MSGS defined.");
  10. #endif
  11. #include "ROS2SpawnerComponentController.h"
  12. #include "Spawner/ROS2SpawnPointComponent.h"
  13. #include "Spawner/ROS2SpawnerComponent.h"
  14. #include <AzCore/Component/ComponentBus.h>
  15. #include <AzCore/Component/TransformBus.h>
  16. #include <AzCore/RTTI/RTTIMacros.h>
  17. #include <AzCore/Serialization/EditContext.h>
  18. #include <AzCore/Serialization/EditContextConstants.inl>
  19. #include <AzCore/Serialization/SerializeContext.h>
  20. #include <ROS2/Spawner/SpawnerInfo.h>
  21. namespace ROS2
  22. {
  23. void ROS2SpawnerServiceNames::Reflect(AZ::ReflectContext* context)
  24. {
  25. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  26. {
  27. serializeContext->Class<ROS2SpawnerServiceNames>()
  28. ->Version(1)
  29. ->Field("Get available spawnable names service name", &ROS2SpawnerServiceNames::m_availableSpawnableNamesServiceName)
  30. ->Field("Spawn entity service name", &ROS2SpawnerServiceNames::m_spawnEntityServiceName)
  31. ->Field("Delete entity service name", &ROS2SpawnerServiceNames::m_deleteEntityServiceName)
  32. ->Field("Get spawn point info service name", &ROS2SpawnerServiceNames::m_spawnPointInfoServiceName)
  33. ->Field("Get spawn points names service name", &ROS2SpawnerServiceNames::m_spawnPointsNamesServiceName);
  34. if (auto editContext = serializeContext->GetEditContext())
  35. {
  36. editContext->Class<ROS2SpawnerServiceNames>("ROS2SpawnerServiceNames", "Service names for ROS2SpawnerComponent")
  37. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  38. ->DataElement(
  39. AZ::Edit::UIHandlers::Default,
  40. &ROS2SpawnerServiceNames::m_availableSpawnableNamesServiceName,
  41. "Get available spawnable names service name",
  42. "Service name for getting available spawnable names")
  43. ->DataElement(
  44. AZ::Edit::UIHandlers::Default,
  45. &ROS2SpawnerServiceNames::m_spawnEntityServiceName,
  46. "Spawn entity service name",
  47. "Service name for spawning entity")
  48. ->DataElement(
  49. AZ::Edit::UIHandlers::Default,
  50. &ROS2SpawnerServiceNames::m_deleteEntityServiceName,
  51. "Delete entity service name",
  52. "Service name for deleting entity")
  53. ->DataElement(
  54. AZ::Edit::UIHandlers::Default,
  55. &ROS2SpawnerServiceNames::m_spawnPointInfoServiceName,
  56. "Get spawn point info service name",
  57. "Service name for getting spawn point info")
  58. ->DataElement(
  59. AZ::Edit::UIHandlers::Default,
  60. &ROS2SpawnerServiceNames::m_spawnPointsNamesServiceName,
  61. "Get spawn points names service name",
  62. "Service name for getting spawn points names");
  63. }
  64. }
  65. }
  66. void ROS2SpawnerComponentConfig::Reflect(AZ::ReflectContext* context)
  67. {
  68. ROS2SpawnerServiceNames::Reflect(context);
  69. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  70. {
  71. serializeContext->Class<ROS2SpawnerComponentConfig, AZ::ComponentConfig>()
  72. ->Version(1)
  73. ->Field("Editor entity id", &ROS2SpawnerComponentConfig::m_editorEntityId)
  74. ->Field("Support WGS", &ROS2SpawnerComponentConfig::m_supportWGS)
  75. ->Field("Spawnables", &ROS2SpawnerComponentConfig::m_spawnables)
  76. ->Field("Default spawn pose", &ROS2SpawnerComponentConfig::m_defaultSpawnPose)
  77. ->Field("Service names", &ROS2SpawnerComponentConfig::m_serviceNames)
  78. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  79. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly);
  80. if (auto editContext = serializeContext->GetEditContext())
  81. {
  82. editContext->Class<ROS2SpawnerComponentConfig>("ROS2SpawnerComponentConfig", "Config for ROS2SpawnerComponent")
  83. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  84. ->DataElement(AZ::Edit::UIHandlers::Default, &ROS2SpawnerComponentConfig::m_supportWGS, "Support WGS", "Support for spawning entities using WGS84 coordinate system")
  85. ->DataElement(AZ::Edit::UIHandlers::Default, &ROS2SpawnerComponentConfig::m_spawnables, "Spawnables", "Spawnables")
  86. ->DataElement(
  87. AZ::Edit::UIHandlers::Default,
  88. &ROS2SpawnerComponentConfig::m_defaultSpawnPose,
  89. "Default spawn pose",
  90. "Default spawn pose")
  91. ->DataElement(
  92. AZ::Edit::UIHandlers::Default, &ROS2SpawnerComponentConfig::m_serviceNames, "Service names", "Service names");
  93. }
  94. }
  95. }
  96. AZ::EntityId ROS2SpawnerComponentController::GetEditorEntityId() const
  97. {
  98. return m_config.m_editorEntityId;
  99. }
  100. AZStd::unordered_map<AZStd::string, AZ::Data::Asset<AzFramework::Spawnable>> ROS2SpawnerComponentController::GetSpawnables() const
  101. {
  102. return m_config.m_spawnables;
  103. }
  104. const AZ::Transform& ROS2SpawnerComponentController::GetDefaultSpawnPose() const
  105. {
  106. return m_config.m_defaultSpawnPose;
  107. }
  108. AZStd::unordered_map<AZStd::string, SpawnPointInfo> ROS2SpawnerComponentController::GetAllSpawnPointInfos() const
  109. {
  110. return GetSpawnPoints();
  111. }
  112. bool ROS2SpawnerComponentController::GetSupportWGS() const
  113. {
  114. return m_config.m_supportWGS;
  115. }
  116. void ROS2SpawnerComponentController::Reflect(AZ::ReflectContext* context)
  117. {
  118. ROS2SpawnerComponentConfig::Reflect(context);
  119. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  120. {
  121. serializeContext->Class<ROS2SpawnerComponentController>()->Version(1)->Field(
  122. "Configuration", &ROS2SpawnerComponentController::m_config);
  123. AZ::EditContext* editContext = serializeContext->GetEditContext();
  124. if (editContext)
  125. {
  126. editContext->Class<ROS2SpawnerComponentController>("ROS2SpawnerComponentController", "Controller for ROS2SpawnerComponent")
  127. ->ClassElement(AZ::Edit::ClassElements::EditorData, "Manages spawning of robots in configurable locations")
  128. ->DataElement(AZ::Edit::UIHandlers::Default, &ROS2SpawnerComponentController::m_config)
  129. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly);
  130. }
  131. }
  132. }
  133. AZStd::unordered_map<AZStd::string, SpawnPointInfo> ROS2SpawnerComponentController::GetSpawnPoints() const
  134. {
  135. AZStd::vector<AZ::EntityId> children;
  136. AZ::TransformBus::EventResult(children, m_config.m_editorEntityId, &AZ::TransformBus::Events::GetChildren);
  137. AZStd::unordered_map<AZStd::string, SpawnPointInfo> result;
  138. for (const AZ::EntityId& child : children)
  139. {
  140. AZ::Entity* childEntity = nullptr;
  141. AZ::ComponentApplicationBus::BroadcastResult(childEntity, &AZ::ComponentApplicationRequests::FindEntity, child);
  142. AZ_Assert(childEntity, "No child entity found for entity %s", child.ToString().c_str());
  143. if (const auto* spawnPoint = childEntity->FindComponent<ROS2SpawnPointComponent>(); spawnPoint != nullptr)
  144. {
  145. result.insert(spawnPoint->GetInfo());
  146. }
  147. }
  148. // setting name of spawn point component "default" in a child entity will have no effect since it is overwritten here with the
  149. // default spawn pose of spawner
  150. result["default"] = SpawnPointInfo{ "Default spawn pose defined in the Editor", m_config.m_defaultSpawnPose };
  151. return result;
  152. }
  153. void ROS2SpawnerComponentController::Init()
  154. {
  155. }
  156. void ROS2SpawnerComponentController::Activate(AZ::EntityId entityId)
  157. {
  158. m_config.m_editorEntityId = entityId;
  159. SpawnerRequestsBus::Handler::BusConnect(entityId);
  160. }
  161. void ROS2SpawnerComponentController::Deactivate()
  162. {
  163. SpawnerRequestsBus::Handler::BusDisconnect();
  164. }
  165. ROS2SpawnerComponentController::ROS2SpawnerComponentController(const ROS2SpawnerComponentConfig& config)
  166. {
  167. SetConfiguration(config);
  168. }
  169. void ROS2SpawnerComponentController::SetConfiguration(const ROS2SpawnerComponentConfig& config)
  170. {
  171. m_config = config;
  172. }
  173. const ROS2SpawnerComponentConfig& ROS2SpawnerComponentController::GetConfiguration() const
  174. {
  175. return m_config;
  176. }
  177. const ROS2SpawnerServiceNames& ROS2SpawnerComponentController::GetServiceNames() const
  178. {
  179. return m_config.m_serviceNames;
  180. }
  181. } // namespace ROS2