ROS2SystemComponent.cpp 5.6 KB

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