2
0

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