ROS2SystemComponent.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 "QoS/QoS.h"
  10. #include "Sensor/PublisherConfiguration.h"
  11. #include <AzCore/Serialization/EditContext.h>
  12. #include <AzCore/Serialization/EditContextConstants.inl>
  13. #include <AzCore/Serialization/SerializeContext.h>
  14. #include <AzCore/Time/ITime.h>
  15. #include <AzCore/std/smart_ptr/make_shared.h>
  16. namespace ROS2
  17. {
  18. void ROS2SystemComponent::Reflect(AZ::ReflectContext* context)
  19. {
  20. // Reflect structs not strictly owned by any single component
  21. QoS::Reflect(context);
  22. PublisherConfiguration::Reflect(context);
  23. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  24. {
  25. serialize->Class<ROS2SystemComponent, AZ::Component>()->Version(0);
  26. if (AZ::EditContext* ec = serialize->GetEditContext())
  27. {
  28. ec->Class<ROS2SystemComponent>("ROS2 System Component", "[Description of functionality provided by this System Component]")
  29. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  30. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("System"))
  31. ->Attribute(AZ::Edit::Attributes::Category, "ROS2")
  32. ->Attribute(AZ::Edit::Attributes::AutoExpand, true);
  33. }
  34. }
  35. }
  36. void ROS2SystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  37. {
  38. provided.push_back(AZ_CRC_CE("ROS2Service"));
  39. }
  40. void ROS2SystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  41. {
  42. incompatible.push_back(AZ_CRC_CE("ROS2Service"));
  43. }
  44. void ROS2SystemComponent::GetRequiredServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& required)
  45. {
  46. }
  47. void ROS2SystemComponent::GetDependentServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& dependent)
  48. {
  49. }
  50. ROS2SystemComponent::ROS2SystemComponent()
  51. {
  52. if (ROS2Interface::Get() == nullptr)
  53. {
  54. ROS2Interface::Register(this);
  55. }
  56. }
  57. ROS2SystemComponent::~ROS2SystemComponent()
  58. {
  59. if (ROS2Interface::Get() == this)
  60. {
  61. ROS2Interface::Unregister(this);
  62. }
  63. rclcpp::shutdown();
  64. }
  65. void ROS2SystemComponent::Init()
  66. {
  67. rclcpp::init(0, 0);
  68. m_ros2Node = std::make_shared<rclcpp::Node>("o3de_ros2_node");
  69. m_executor = AZStd::make_shared<rclcpp::executors::SingleThreadedExecutor>();
  70. m_executor->add_node(m_ros2Node);
  71. }
  72. void ROS2SystemComponent::Activate()
  73. {
  74. m_staticTFBroadcaster = AZStd::make_unique<tf2_ros::StaticTransformBroadcaster>(m_ros2Node);
  75. m_dynamicTFBroadcaster = AZStd::make_unique<tf2_ros::TransformBroadcaster>(m_ros2Node);
  76. ROS2RequestBus::Handler::BusConnect();
  77. AZ::TickBus::Handler::BusConnect();
  78. }
  79. void ROS2SystemComponent::Deactivate()
  80. {
  81. AZ::TickBus::Handler::BusDisconnect();
  82. ROS2RequestBus::Handler::BusDisconnect();
  83. m_dynamicTFBroadcaster.reset();
  84. m_staticTFBroadcaster.reset();
  85. }
  86. builtin_interfaces::msg::Time ROS2SystemComponent::GetROSTimestamp() const
  87. {
  88. return m_simulationClock.GetROSTimestamp();
  89. }
  90. std::shared_ptr<rclcpp::Node> ROS2SystemComponent::GetNode() const
  91. {
  92. return m_ros2Node;
  93. }
  94. void ROS2SystemComponent::BroadcastTransform(const geometry_msgs::msg::TransformStamped& t, bool isDynamic) const
  95. {
  96. if (isDynamic)
  97. {
  98. m_dynamicTFBroadcaster->sendTransform(t);
  99. }
  100. else
  101. {
  102. m_staticTFBroadcaster->sendTransform(t);
  103. }
  104. }
  105. void ROS2SystemComponent::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time)
  106. {
  107. if (rclcpp::ok())
  108. {
  109. m_simulationClock.Tick();
  110. // TODO - this can be in another thread and done with a higher resolution for less latency.
  111. // TODO - callbacks will be called in the spinning thread (here, the main thread).
  112. m_executor->spin_some();
  113. }
  114. }
  115. } // namespace ROS2