ROS2SimulationInterfacesSystemComponent.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 <Actions/SimulateStepsActionServerHandler.h>
  10. #include <AzCore/Serialization/SerializeContext.h>
  11. #include <AzCore/std/smart_ptr/make_shared.h>
  12. #include <AzCore/std/string/string.h>
  13. #include <AzFramework/API/ApplicationAPI.h>
  14. #include <ROS2/ROS2Bus.h>
  15. #include <SimulationInterfaces/ROS2SimulationInterfacesRequestBus.h>
  16. #include <SimulationInterfaces/ROS2SimulationInterfacesTypeIds.h>
  17. #include <Services/DeleteEntityServiceHandler.h>
  18. #include <Services/GetEntitiesServiceHandler.h>
  19. #include <Services/GetEntitiesStatesServiceHandler.h>
  20. #include <Services/GetEntityStateServiceHandler.h>
  21. #include <Services/GetSimulationFeaturesServiceHandler.h>
  22. #include <Services/GetSimulationStateServiceHandler.h>
  23. #include <Services/GetSpawnablesServiceHandler.h>
  24. #include <Services/ROS2ServiceBase.h>
  25. #include <Services/ResetSimulationServiceHandler.h>
  26. #include <Services/SetEntityStateServiceHandler.h>
  27. #include <Services/SetSimulationStateServiceHandler.h>
  28. #include <Services/SpawnEntityServiceHandler.h>
  29. #include <Services/StepSimulationServiceHandler.h>
  30. namespace ROS2SimulationInterfaces
  31. {
  32. namespace
  33. {
  34. template<typename T>
  35. void RegisterInterface(
  36. AZStd::unordered_map<AZStd::string, AZStd::shared_ptr<IROS2HandlerBase>>& interfacesMap, rclcpp::Node::SharedPtr ros2Node)
  37. {
  38. AZStd::shared_ptr handler = AZStd::make_shared<T>();
  39. handler->Initialize(ros2Node);
  40. interfacesMap[handler->GetTypeName()] = AZStd::move(handler);
  41. handler.reset();
  42. };
  43. } // namespace
  44. AZ_COMPONENT_IMPL(
  45. ROS2SimulationInterfacesSystemComponent, "ROS2SimulationInterfacesSystemComponent", ROS2SimulationInterfacesSystemComponentTypeId);
  46. void ROS2SimulationInterfacesSystemComponent::Reflect(AZ::ReflectContext* context)
  47. {
  48. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  49. {
  50. serializeContext->Class<ROS2SimulationInterfacesSystemComponent, AZ::Component>()->Version(0);
  51. }
  52. }
  53. void ROS2SimulationInterfacesSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  54. {
  55. provided.push_back(AZ_CRC_CE("ROS2SimulationInterfacesService"));
  56. }
  57. void ROS2SimulationInterfacesSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  58. {
  59. incompatible.push_back(AZ_CRC_CE("ROS2SimulationInterfacesService"));
  60. }
  61. void ROS2SimulationInterfacesSystemComponent::GetRequiredServices(
  62. [[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& required)
  63. {
  64. required.push_back(AZ_CRC_CE("ROS2Service"));
  65. }
  66. void ROS2SimulationInterfacesSystemComponent::GetDependentServices(
  67. [[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& dependent)
  68. {
  69. }
  70. void ROS2SimulationInterfacesSystemComponent::Activate()
  71. {
  72. ROS2SimulationInterfacesRequestBus::Handler::BusConnect();
  73. rclcpp::Node::SharedPtr ros2Node = rclcpp::Node::SharedPtr(ROS2::ROS2Interface::Get()->GetNode());
  74. AZ_Assert(ros2Node, "ROS2 node is not available.");
  75. RegisterInterface<DeleteEntityServiceHandler>(m_availableRos2Interface, ros2Node);
  76. RegisterInterface<GetEntitiesServiceHandler>(m_availableRos2Interface, ros2Node);
  77. RegisterInterface<GetEntitiesStatesServiceHandler>(m_availableRos2Interface, ros2Node);
  78. RegisterInterface<GetEntityStateServiceHandler>(m_availableRos2Interface, ros2Node);
  79. RegisterInterface<GetSpawnablesServiceHandler>(m_availableRos2Interface, ros2Node);
  80. RegisterInterface<SetEntityStateServiceHandler>(m_availableRos2Interface, ros2Node);
  81. RegisterInterface<SpawnEntityServiceHandler>(m_availableRos2Interface, ros2Node);
  82. RegisterInterface<GetSimulationFeaturesServiceHandler>(m_availableRos2Interface, ros2Node);
  83. RegisterInterface<ResetSimulationServiceHandler>(m_availableRos2Interface, ros2Node);
  84. RegisterInterface<SimulateStepsActionServerHandler>(m_availableRos2Interface, ros2Node);
  85. RegisterInterface<SetSimulationStateServiceHandler>(m_availableRos2Interface, ros2Node);
  86. RegisterInterface<GetSimulationStateServiceHandler>(m_availableRos2Interface, ros2Node);
  87. RegisterInterface<StepSimulationServiceHandler>(m_availableRos2Interface, ros2Node);
  88. }
  89. void ROS2SimulationInterfacesSystemComponent::Deactivate()
  90. {
  91. ROS2SimulationInterfacesRequestBus::Handler::BusDisconnect();
  92. for (auto& [handlerType, handler] : m_availableRos2Interface)
  93. {
  94. handler.reset();
  95. }
  96. }
  97. AZStd::unordered_set<SimulationFeatureType> ROS2SimulationInterfacesSystemComponent::GetSimulationFeatures()
  98. {
  99. AZStd::unordered_set<SimulationFeatureType> result;
  100. for (auto& [handlerType, handler] : m_availableRos2Interface)
  101. {
  102. auto features = handler->GetProvidedFeatures();
  103. result.insert(features.begin(), features.end());
  104. }
  105. return result;
  106. }
  107. } // namespace ROS2SimulationInterfaces