ROS2ClockSystemComponent.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 "ITimeSource.h"
  10. #include <AzCore/Component/Component.h>
  11. #include <AzCore/Component/TickBus.h>
  12. #include <AzCore/Outcome/Outcome.h>
  13. #include <AzCore/std/smart_ptr/unique_ptr.h>
  14. #include <AzCore/std/string/string.h>
  15. #include <ROS2/Clock/ROS2ClockRequestBus.h>
  16. #include <rclcpp/publisher.hpp>
  17. #include <rosgraph_msgs/msg/clock.hpp>
  18. namespace ROS2
  19. {
  20. //! The ROS2Clock provides ROS timestamps as builtin_interface::msg::Time messages.
  21. //! The clock can use different types of the time sources and publish the current
  22. //! time to the ROS 2 `/clock/` topic. The published time can be used with
  23. //! the /use_sim_time parameter set to true.
  24. class ROS2ClockSystemComponent
  25. : public AZ::Component
  26. , public AZ::TickBus::Handler
  27. , public ROS2ClockRequestBus::Handler
  28. {
  29. public:
  30. AZ_COMPONENT_DECL(ROS2ClockSystemComponent);
  31. static void Reflect(AZ::ReflectContext* context);
  32. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
  33. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible);
  34. ROS2ClockSystemComponent();
  35. ~ROS2ClockSystemComponent() override;
  36. // AZ::Component overrides
  37. void Activate();
  38. void Deactivate();
  39. // AZ::TickBus overrides
  40. void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
  41. // ROS2ClockRequestBus overrides
  42. builtin_interfaces::msg::Time GetROSTimestamp() const override;
  43. double GetROSTimestampSec() const override;
  44. AZ::Outcome<void, AZStd::string> AdjustTime(const builtin_interfaces::msg::Time& time) override;
  45. bool AdjustTimeSec(double time) override;
  46. void PublishTime() override;
  47. float GetExpectedLoopTime() const override;
  48. private:
  49. rclcpp::Publisher<rosgraph_msgs::msg::Clock>::SharedPtr m_clockPublisher;
  50. AZStd::unique_ptr<ITimeSource> m_timeSource; //! Source Clock implementation
  51. AZStd::deque<float> m_simulationLoopTimes;
  52. builtin_interfaces::msg::Time m_lastSimulationTime;
  53. float m_simulationLoopTimeMedian = 1.0f / 60.0f;
  54. bool m_publishClock;
  55. };
  56. } // namespace ROS2