SimulationInterfacesROS2SystemComponent.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 "SimulationInterfacesROS2SystemComponent.h"
  9. #include <Actions/SimulateStepsActionServerHandler.h>
  10. #include <AzCore/std/string/string.h>
  11. #include <Services/ROS2ServiceBase.h>
  12. #include <SimulationInterfacesROS2/SimulationInterfacesROS2RequestBus.h>
  13. #include <ROS2/ROS2Bus.h>
  14. #include <SimulationInterfacesROS2/SimulationInterfacesROS2TypeIds.h>
  15. #include <AzCore/Serialization/SerializeContext.h>
  16. #include <AzCore/std/smart_ptr/make_shared.h>
  17. namespace SimulationInterfacesROS2
  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. SimulationInterfacesROS2SystemComponent, "SimulationInterfacesROS2SystemComponent", SimulationInterfacesROS2SystemComponentTypeId);
  33. void SimulationInterfacesROS2SystemComponent::Reflect(AZ::ReflectContext* context)
  34. {
  35. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  36. {
  37. serializeContext->Class<SimulationInterfacesROS2SystemComponent, AZ::Component>()->Version(0);
  38. }
  39. }
  40. void SimulationInterfacesROS2SystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  41. {
  42. provided.push_back(AZ_CRC_CE("SimulationInterfacesROS2Service"));
  43. }
  44. void SimulationInterfacesROS2SystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  45. {
  46. incompatible.push_back(AZ_CRC_CE("SimulationInterfacesROS2Service"));
  47. }
  48. void SimulationInterfacesROS2SystemComponent::GetRequiredServices(
  49. [[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& required)
  50. {
  51. required.push_back(AZ_CRC_CE("ROS2Service"));
  52. }
  53. void SimulationInterfacesROS2SystemComponent::GetDependentServices(
  54. [[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& dependent)
  55. {
  56. }
  57. void SimulationInterfacesROS2SystemComponent::Init()
  58. {
  59. }
  60. void SimulationInterfacesROS2SystemComponent::Activate()
  61. {
  62. SimulationInterfacesROS2RequestBus::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. }
  76. void SimulationInterfacesROS2SystemComponent::Deactivate()
  77. {
  78. SimulationInterfacesROS2RequestBus::Handler::BusDisconnect();
  79. for (auto& [handlerType, handler] : m_availableRos2Interface)
  80. {
  81. handler.reset();
  82. }
  83. }
  84. AZStd::unordered_set<AZ::u8> SimulationInterfacesROS2SystemComponent::GetSimulationFeatures()
  85. {
  86. AZStd::unordered_set<AZ::u8> result;
  87. for (auto& [handlerType, handler] : m_availableRos2Interface)
  88. {
  89. auto features = handler->GetProvidedFeatures();
  90. result.insert(features.begin(), features.end());
  91. }
  92. return result;
  93. }
  94. } // namespace SimulationInterfacesROS2