ROS2SimulationInterfacesSystemComponent.cpp 4.7 KB

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