ROS2SystemComponent.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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/ROS2Bus.h>
  13. #include <ROS2/ROS2NamesBus.h>
  14. #include <ROS2/TF/TransformInterface.h>
  15. #include <builtin_interfaces/msg/time.hpp>
  16. #include <memory>
  17. #include <rclcpp/rclcpp.hpp>
  18. #include <tf2_ros/buffer.h>
  19. #include <tf2_ros/static_transform_broadcaster.h>
  20. #include <tf2_ros/transform_broadcaster.h>
  21. #include <tf2_ros/transform_listener.h>
  22. /**
  23. * \mainpage
  24. *
  25. * Welcome to the **Open 3D Engine (O3DE)** API Reference for the **ROS 2 Gem**!
  26. *
  27. * The Components and Classes of this Gem support direct interaction with the ROS 2 ecosystem.
  28. * The Robot Operating System (ROS) middleware documentation links in this API Reference point to the most recent ROS distribution. If you
  29. * are using an older version, you might need to navigate to the corresponding documentation pages for the distribution which you have
  30. * installed.
  31. *
  32. * For the overview, features and usage of this Gem, please refer to [ROS 2 Gem
  33. * Documentation](https://development--o3deorg.netlify.app/docs/user-guide/gems/reference/robotics/ros2/).
  34. *
  35. * o3de-doxygen-insert-table
  36. *
  37. */
  38. namespace ROS2
  39. {
  40. //! Central singleton-like System Component for ROS2 Gem.
  41. class ROS2SystemComponent
  42. : public AZ::Component
  43. , public AZ::TickBus::Handler
  44. , protected ROS2Requests
  45. , protected ROS2NamesRequestBus::Handler
  46. , protected TFInterfaceBus::Handler
  47. {
  48. public:
  49. AZ_COMPONENT_DECL(ROS2SystemComponent);
  50. static void Reflect(AZ::ReflectContext* context);
  51. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
  52. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible);
  53. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required);
  54. static void GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent);
  55. ROS2SystemComponent();
  56. ~ROS2SystemComponent();
  57. protected:
  58. ////////////////////////////////////////////////////////////////////////
  59. // ROS2RequestBus interface implementation
  60. std::shared_ptr<rclcpp::Node> GetNode() const override;
  61. void ConnectOnNodeChanged(NodeChangedEvent::Handler& handler) override;
  62. void BroadcastTransform(const geometry_msgs::msg::TransformStamped& t, bool isDynamic) override;
  63. ////////////////////////////////////////////////////////////////////////
  64. ////////////////////////////////////////////////////////////////////////
  65. // ROS2NamesRequestBus handler implementation
  66. AZStd::string GetNamespacedName(const AZStd::string& ns, const AZStd::string& name) override;
  67. AZStd::string RosifyName(const AZStd::string& input) override;
  68. AZ::Outcome<void, AZStd::string> ValidateNamespace(const AZStd::string& ros2Namespace) override;
  69. AZ::Outcome<void, AZStd::string> ValidateNamespaceField(void* newValue, const AZ::Uuid& valueType) override;
  70. AZ::Outcome<void, AZStd::string> ValidateTopic(const AZStd::string& topic) override;
  71. AZ::Outcome<void, AZStd::string> ValidateTopicField(void* newValue, const AZ::Uuid& valueType) override;
  72. ////////////////////////////////////////////////////////////////////////
  73. ////////////////////////////////////////////////////////////////////////
  74. // AZ::Component interface implementation
  75. void Init() override;
  76. void Activate() override;
  77. void Deactivate() override;
  78. ////////////////////////////////////////////////////////////////////////
  79. ////////////////////////////////////////////////////////////////////////
  80. // AZ::TickBus handler implementation
  81. void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
  82. ////////////////////////////////////////////////////////////////////////
  83. ////////////////////////////////////////////////////////////////////////
  84. // TFInterface implementation
  85. AZ::Outcome<AZ::Transform, AZStd::string> GetTransform(
  86. const AZStd::string& source, const AZStd::string& target, const builtin_interfaces::msg::Time& time) override;
  87. AZ::Transform GetLatestTransform(const AZStd::string& source, const AZStd::string& target) override;
  88. void PublishTransform(const AZStd::string& source, const AZStd::string& target, const AZ::Transform& transform, bool isDynamic) override;
  89. ////////////////////////////////////////////////////////////////////////
  90. private:
  91. std::vector<geometry_msgs::msg::TransformStamped> m_frameTransforms;
  92. std::shared_ptr<rclcpp::Node> m_ros2Node;
  93. AZStd::shared_ptr<rclcpp::executors::SingleThreadedExecutor> m_executor;
  94. AZStd::unique_ptr<tf2_ros::TransformBroadcaster> m_dynamicTFBroadcaster;
  95. AZStd::unique_ptr<tf2_ros::StaticTransformBroadcaster> m_staticTFBroadcaster;
  96. AZStd::shared_ptr<tf2_ros::Buffer> m_tfBuffer;
  97. AZStd::shared_ptr<tf2_ros::TransformListener> m_tfListener;
  98. NodeChangedEvent m_nodeChangedEvent;
  99. };
  100. } // namespace ROS2