ROS2Clock.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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/Outcome/Outcome.h>
  11. #include <AzCore/std/chrono/chrono.h>
  12. #include <AzCore/std/smart_ptr/unique_ptr.h>
  13. #include <AzCore/std/string/string.h>
  14. #include <rclcpp/publisher.hpp>
  15. #include <rosgraph_msgs/msg/clock.hpp>
  16. namespace ROS2
  17. {
  18. //! The ROS2Clock provides ROS timestamps as builtin_interface::msg::Time messages.
  19. //! The clock can use different types of the time sources and publish the current
  20. //! time to the ROS 2 `/clock/` topic. The published time can be used with
  21. //! the /use_sim_time parameter set to true.
  22. class ROS2Clock
  23. {
  24. public:
  25. ROS2Clock();
  26. ROS2Clock(AZStd::unique_ptr<ITimeSource> timeSource, bool publishClock);
  27. ~ROS2Clock() = default;
  28. void Activate();
  29. void Deactivate();
  30. builtin_interfaces::msg::Time GetROSTimestamp() const;
  31. //! Sets the time source to the given time.
  32. //! @param time The time to set the time source to.
  33. //! @return An outcome indicating success or failure.
  34. AZ::Outcome<void, AZStd::string> AdjustTime(const builtin_interfaces::msg::Time& time) const;
  35. //! Update time in the ROS 2 ecosystem.
  36. //! This will publish current time to the ROS 2 `/clock` topic, if Clock is configured to do it.
  37. void Tick();
  38. private:
  39. rclcpp::Publisher<rosgraph_msgs::msg::Clock>::SharedPtr m_clockPublisher;
  40. AZStd::unique_ptr<ITimeSource> m_timeSource;
  41. bool m_publishClock;
  42. };
  43. } // namespace ROS2