ROS2SystemComponent.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 <ROS2SystemComponent.h>
  9. #include <ROS2/Communication/QoS.h>
  10. #include <ROS2/Communication/TopicConfiguration.h>
  11. #include <Atom/RPI.Public/Pass/PassSystemInterface.h>
  12. #include <AzCore/Serialization/EditContext.h>
  13. #include <AzCore/Serialization/EditContextConstants.inl>
  14. #include <AzCore/Serialization/SerializeContext.h>
  15. #include <AzCore/Time/ITime.h>
  16. #include <AzCore/std/smart_ptr/make_shared.h>
  17. namespace ROS2
  18. {
  19. void ROS2SystemComponent::Reflect(AZ::ReflectContext* context)
  20. {
  21. // Reflect structs not strictly owned by any single component
  22. QoS::Reflect(context);
  23. TopicConfiguration::Reflect(context);
  24. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  25. {
  26. serialize->Class<ROS2SystemComponent, AZ::Component>()->Version(0);
  27. if (AZ::EditContext* ec = serialize->GetEditContext())
  28. {
  29. ec->Class<ROS2SystemComponent>("ROS2 System Component", "[Description of functionality provided by this System Component]")
  30. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  31. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("System"))
  32. ->Attribute(AZ::Edit::Attributes::Category, "ROS2")
  33. ->Attribute(AZ::Edit::Attributes::AutoExpand, true);
  34. }
  35. }
  36. }
  37. void ROS2SystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  38. {
  39. provided.push_back(AZ_CRC_CE("ROS2Service"));
  40. }
  41. void ROS2SystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  42. {
  43. incompatible.push_back(AZ_CRC_CE("ROS2Service"));
  44. }
  45. void ROS2SystemComponent::GetRequiredServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& required)
  46. {
  47. required.push_back(AZ_CRC("AssetDatabaseService", 0x3abf5601));
  48. required.push_back(AZ_CRC("RPISystem", 0xf2add773));
  49. }
  50. void ROS2SystemComponent::GetDependentServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& dependent)
  51. {
  52. }
  53. ROS2SystemComponent::ROS2SystemComponent()
  54. {
  55. if (ROS2Interface::Get() == nullptr)
  56. {
  57. ROS2Interface::Register(this);
  58. }
  59. }
  60. ROS2SystemComponent::~ROS2SystemComponent()
  61. {
  62. if (ROS2Interface::Get() == this)
  63. {
  64. ROS2Interface::Unregister(this);
  65. }
  66. rclcpp::shutdown();
  67. }
  68. void ROS2SystemComponent::Init()
  69. {
  70. rclcpp::init(0, 0);
  71. m_ros2Node = std::make_shared<rclcpp::Node>("o3de_ros2_node");
  72. m_executor = AZStd::make_shared<rclcpp::executors::SingleThreadedExecutor>();
  73. m_executor->add_node(m_ros2Node);
  74. }
  75. void ROS2SystemComponent::Activate()
  76. {
  77. m_staticTFBroadcaster = AZStd::make_unique<tf2_ros::StaticTransformBroadcaster>(m_ros2Node);
  78. m_dynamicTFBroadcaster = AZStd::make_unique<tf2_ros::TransformBroadcaster>(m_ros2Node);
  79. auto* passSystem = AZ::RPI::PassSystemInterface::Get();
  80. AZ_Assert(passSystem, "Cannot get the pass system.");
  81. m_loadTemplatesHandler = AZ::RPI::PassSystemInterface::OnReadyLoadTemplatesEvent::Handler(
  82. [this]()
  83. {
  84. this->LoadPassTemplateMappings();
  85. });
  86. passSystem->ConnectEvent(m_loadTemplatesHandler);
  87. ROS2RequestBus::Handler::BusConnect();
  88. AZ::TickBus::Handler::BusConnect();
  89. }
  90. void ROS2SystemComponent::Deactivate()
  91. {
  92. AZ::TickBus::Handler::BusDisconnect();
  93. ROS2RequestBus::Handler::BusDisconnect();
  94. m_loadTemplatesHandler.Disconnect();
  95. m_dynamicTFBroadcaster.reset();
  96. m_staticTFBroadcaster.reset();
  97. }
  98. builtin_interfaces::msg::Time ROS2SystemComponent::GetROSTimestamp() const
  99. {
  100. return m_simulationClock.GetROSTimestamp();
  101. }
  102. std::shared_ptr<rclcpp::Node> ROS2SystemComponent::GetNode() const
  103. {
  104. return m_ros2Node;
  105. }
  106. void ROS2SystemComponent::BroadcastTransform(const geometry_msgs::msg::TransformStamped& t, bool isDynamic) const
  107. {
  108. if (isDynamic)
  109. {
  110. m_dynamicTFBroadcaster->sendTransform(t);
  111. }
  112. else
  113. {
  114. m_staticTFBroadcaster->sendTransform(t);
  115. }
  116. }
  117. void ROS2SystemComponent::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time)
  118. {
  119. if (rclcpp::ok())
  120. {
  121. m_simulationClock.Tick();
  122. m_executor->spin_some();
  123. }
  124. }
  125. void ROS2SystemComponent::LoadPassTemplateMappings()
  126. {
  127. AZ_Printf("ROS2CameraSensorComponent", "LoadPassTemplateMappings\n");
  128. auto* passSystem = AZ::RPI::PassSystemInterface::Get();
  129. AZ_Assert(passSystem, "PassSystemInterface is null");
  130. const char* passTemplatesFile = "Passes/ROSPassTemplates.azasset";
  131. [[maybe_unused]] bool isOk = passSystem->LoadPassTemplateMappings(passTemplatesFile);
  132. AZ_Assert(isOk, "LoadPassTemplateMappings return false ");
  133. }
  134. } // namespace ROS2