2
0

ROS2SimulationInterfacesSystemComponent.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 "ROS2SimulationInterfacesSystemComponent.h"
  9. #include <AzCore/Component/ComponentApplicationBus.h>
  10. #include <Actions/SimulateStepsActionServerHandler.h>
  11. #include <AzCore/Serialization/SerializeContext.h>
  12. #include <AzCore/std/smart_ptr/make_shared.h>
  13. #include <AzCore/std/string/string.h>
  14. #include <AzFramework/API/ApplicationAPI.h>
  15. #include <ROS2/ROS2Bus.h>
  16. #include <SimulationInterfaces/ROS2SimulationInterfacesRequestBus.h>
  17. #include <SimulationInterfaces/ROS2SimulationInterfacesTypeIds.h>
  18. #include <Services/DeleteEntityServiceHandler.h>
  19. #include <Services/GetAvailableWorldsServiceHandler.h>
  20. #include <Services/GetCurrentWorldServiceHandler.h>
  21. #include <Services/GetEntitiesServiceHandler.h>
  22. #include <Services/GetEntitiesStatesServiceHandler.h>
  23. #include <Services/GetEntityBoundsServiceHandler.h>
  24. #include <Services/GetEntityInfoServiceHandler.h>
  25. #include <Services/GetEntityStateServiceHandler.h>
  26. #include <Services/GetNamedPoseBoundsServiceHandler.h>
  27. #include <Services/GetNamedPosesServiceHandler.h>
  28. #include <Services/GetSimulationStateServiceHandler.h>
  29. #include <Services/GetSimulatorFeaturesServiceHandler.h>
  30. #include <Services/GetSpawnablesServiceHandler.h>
  31. #include <Services/LoadWorldServiceHandler.h>
  32. #include <Services/ResetSimulationServiceHandler.h>
  33. #include <Services/SetEntityInfoServiceHandler.h>
  34. #include <Services/SetEntityStateServiceHandler.h>
  35. #include <Services/SetSimulationStateServiceHandler.h>
  36. #include <Services/SpawnEntityServiceHandler.h>
  37. #include <Services/StepSimulationServiceHandler.h>
  38. #include <Services/UnloadWorldServiceHandler.h>
  39. namespace ROS2SimulationInterfaces
  40. {
  41. AZ_COMPONENT_IMPL(
  42. ROS2SimulationInterfacesSystemComponent, "ROS2SimulationInterfacesSystemComponent", ROS2SimulationInterfacesSystemComponentTypeId);
  43. void ROS2SimulationInterfacesSystemComponent::Reflect(AZ::ReflectContext* context)
  44. {
  45. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  46. {
  47. serializeContext->Class<ROS2SimulationInterfacesSystemComponent, AZ::Component>()->Version(0);
  48. }
  49. }
  50. void ROS2SimulationInterfacesSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  51. {
  52. provided.push_back(AZ_CRC_CE("ROS2SimulationInterfacesService"));
  53. }
  54. void ROS2SimulationInterfacesSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  55. {
  56. incompatible.push_back(AZ_CRC_CE("ROS2SimulationInterfacesService"));
  57. }
  58. void ROS2SimulationInterfacesSystemComponent::GetRequiredServices(
  59. [[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& required)
  60. {
  61. required.push_back(AZ_CRC_CE("ROS2Service"));
  62. }
  63. void ROS2SimulationInterfacesSystemComponent::GetDependentServices(
  64. [[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& dependent)
  65. {
  66. }
  67. void ROS2SimulationInterfacesSystemComponent::Activate()
  68. {
  69. ROS2SimulationInterfacesRequestBus::Handler::BusConnect();
  70. AZ::ApplicationTypeQuery appType;
  71. bool isAppEditor;
  72. AZ::ComponentApplicationBus::Broadcast(&AZ::ComponentApplicationBus::Events::QueryApplicationType, appType);
  73. isAppEditor = (appType.IsValid() && appType.IsEditor());
  74. rclcpp::Node::SharedPtr ros2Node = rclcpp::Node::SharedPtr(ROS2::ROS2Interface::Get()->GetNode());
  75. AZ_Assert(ros2Node, "ROS2 node is not available.");
  76. RegisterInterface<DeleteEntityServiceHandler>(ros2Node);
  77. RegisterInterface<GetEntitiesServiceHandler>(ros2Node);
  78. RegisterInterface<GetEntitiesStatesServiceHandler>(ros2Node);
  79. RegisterInterface<GetEntityStateServiceHandler>(ros2Node);
  80. RegisterInterface<GetSpawnablesServiceHandler>(ros2Node);
  81. RegisterInterface<SetEntityStateServiceHandler>(ros2Node);
  82. RegisterInterface<SpawnEntityServiceHandler>(ros2Node);
  83. RegisterInterface<GetSimulatorFeaturesServiceHandler>(ros2Node);
  84. RegisterInterface<ResetSimulationServiceHandler>(ros2Node);
  85. RegisterInterface<SimulateStepsActionServerHandler>(ros2Node);
  86. RegisterInterface<SetSimulationStateServiceHandler>(ros2Node);
  87. RegisterInterface<GetSimulationStateServiceHandler>(ros2Node);
  88. RegisterInterface<StepSimulationServiceHandler>(ros2Node);
  89. RegisterInterface<GetNamedPosesServiceHandler>(ros2Node);
  90. RegisterInterface<GetNamedPoseBoundsServiceHandler>(ros2Node);
  91. RegisterInterface<GetEntityInfoServiceHandler>(ros2Node);
  92. RegisterInterface<SetEntityInfoServiceHandler>(ros2Node);
  93. RegisterInterface<GetEntityBoundsServiceHandler>(ros2Node);
  94. RegisterInterface<GetAvailableWorldsServiceHandler>(ros2Node);
  95. if (!isAppEditor)
  96. {
  97. // services related to world loading and unloading are available only in GameLauncher
  98. // prevent creation of those service handlers
  99. RegisterInterface<GetCurrentWorldServiceHandler>(ros2Node);
  100. RegisterInterface<LoadWorldServiceHandler>(ros2Node);
  101. RegisterInterface<UnloadWorldServiceHandler>(ros2Node);
  102. }
  103. }
  104. void ROS2SimulationInterfacesSystemComponent::Deactivate()
  105. {
  106. ROS2SimulationInterfacesRequestBus::Handler::BusDisconnect();
  107. for (auto& [handlerType, handler] : m_availableRos2Interface)
  108. {
  109. handler.reset();
  110. }
  111. m_availableRos2Interface.clear();
  112. m_externallyRegisteredFeatures.clear();
  113. }
  114. void ROS2SimulationInterfacesSystemComponent::AddSimulationFeatures(const AZStd::unordered_set<SimulationFeatureType>& features)
  115. {
  116. m_externallyRegisteredFeatures.insert(features.begin(), features.end());
  117. }
  118. AZStd::unordered_set<SimulationFeatureType> ROS2SimulationInterfacesSystemComponent::GetSimulationFeatures()
  119. {
  120. AZStd::unordered_set<SimulationFeatureType> result;
  121. // add externally registered features
  122. result.insert(m_externallyRegisteredFeatures.begin(), m_externallyRegisteredFeatures.end());
  123. // add features from existing handlers
  124. for (auto& [handlerType, handler] : m_availableRos2Interface)
  125. {
  126. auto features = handler->GetProvidedFeatures();
  127. result.insert(features.begin(), features.end());
  128. }
  129. return result;
  130. }
  131. } // namespace ROS2SimulationInterfaces