ROS2SpawnerComponentController.cpp 9.1 KB

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