ROS2SystemComponent.h 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #pragma once
  9. #include <AzCore/Component/Component.h>
  10. #include <AzCore/Component/TickBus.h>
  11. #include <AzCore/std/smart_ptr/unique_ptr.h>
  12. #include <Lidar/LidarSystem.h>
  13. #include <ROS2/Clock/ROS2Clock.h>
  14. #include <ROS2/ROS2Bus.h>
  15. #include <builtin_interfaces/msg/time.hpp>
  16. #include <memory>
  17. #include <rclcpp/rclcpp.hpp>
  18. #include <tf2_ros/static_transform_broadcaster.h>
  19. #include <tf2_ros/transform_broadcaster.h>
  20. /**
  21. * \mainpage
  22. *
  23. * Welcome to the **Open 3D Engine (O3DE)** API Reference for the **ROS 2 Gem**!
  24. *
  25. * The Components and Classes of this Gem support direct interaction with the ROS 2 ecosystem.
  26. * The Robot Operating System (ROS) middleware documentation links in this API Reference point to the most recent ROS distribution. If you
  27. * are using an older version, you might need to navigate to the corresponding documentation pages for the distribution which you have
  28. * installed.
  29. *
  30. * For the overview, features and usage of this Gem, please refer to [ROS 2 Gem
  31. * Documentation](https://development--o3deorg.netlify.app/docs/user-guide/gems/reference/robotics/ros2/).
  32. *
  33. * o3de-doxygen-insert-table
  34. *
  35. */
  36. namespace ROS2
  37. {
  38. //! Central singleton-like System Component for ROS2 Gem.
  39. class ROS2SystemComponent
  40. : public AZ::Component
  41. , public AZ::TickBus::Handler
  42. , protected ROS2RequestBus::Handler
  43. {
  44. public:
  45. AZ_COMPONENT(ROS2SystemComponent, "{cb28d486-afa4-4a9f-a237-ac5eb42e1c87}");
  46. static void Reflect(AZ::ReflectContext* context);
  47. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
  48. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible);
  49. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required);
  50. static void GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent);
  51. ROS2SystemComponent();
  52. ~ROS2SystemComponent() override;
  53. //////////////////////////////////////////////////////////////////////////
  54. // ROS2RequestBus::Handler overrides
  55. std::shared_ptr<rclcpp::Node> GetNode() const override;
  56. void ConnectOnNodeChanged(NodeChangedEvent::Handler& handler) override;
  57. builtin_interfaces::msg::Time GetROSTimestamp() const override;
  58. void BroadcastTransform(const geometry_msgs::msg::TransformStamped& t, bool isDynamic) override;
  59. const ROS2Clock& GetSimulationClock() const override;
  60. float GetExpectedSimulationLoopTime() const override;
  61. //////////////////////////////////////////////////////////////////////////
  62. protected:
  63. ////////////////////////////////////////////////////////////////////////
  64. // AZ::Component override
  65. void Init() override;
  66. void Activate() override;
  67. void Deactivate() override;
  68. ////////////////////////////////////////////////////////////////////////
  69. ////////////////////////////////////////////////////////////////////////
  70. // AZTickBus interface implementation
  71. void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
  72. ////////////////////////////////////////////////////////////////////////
  73. private:
  74. void InitClock();
  75. std::vector<geometry_msgs::msg::TransformStamped> m_frameTransforms;
  76. std::shared_ptr<rclcpp::Node> m_ros2Node;
  77. AZStd::shared_ptr<rclcpp::executors::SingleThreadedExecutor> m_executor;
  78. AZStd::unique_ptr<tf2_ros::TransformBroadcaster> m_dynamicTFBroadcaster;
  79. AZStd::unique_ptr<tf2_ros::StaticTransformBroadcaster> m_staticTFBroadcaster;
  80. AZStd::unique_ptr<ROS2Clock> m_simulationClock;
  81. NodeChangedEvent m_nodeChangedEvent;
  82. AZStd::deque<float> m_simulationLoopTimes;
  83. builtin_interfaces::msg::Time m_lastSimulationTime;
  84. float m_simulationLoopTimeMedian = 1.f / 60.0f;
  85. };
  86. } // namespace ROS2