ROS2SystemComponent.cpp 5.4 KB

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