RealTimeSource.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. #include <AzCore/Time/ITime.h>
  9. #include <ROS2/Clock/RealTimeSource.h>
  10. namespace ROS2
  11. {
  12. builtin_interfaces::msg::Time RealTimeSource::GetROSTimestamp() const
  13. {
  14. const auto elapsedTime = GetElapsedTimeMicroseconds();
  15. builtin_interfaces::msg::Time timeStamp;
  16. timeStamp.sec = static_cast<int32_t>(elapsedTime / 1000000);
  17. timeStamp.nanosec = static_cast<uint32_t>((elapsedTime % 1000000) * 1000);
  18. return timeStamp;
  19. }
  20. int64_t RealTimeSource::GetElapsedTimeMicroseconds() const
  21. {
  22. if (auto* timeSystem = AZ::Interface<AZ::ITime>::Get())
  23. {
  24. return static_cast<int64_t>(timeSystem->GetElapsedTimeUs());
  25. }
  26. else
  27. {
  28. AZ_Error("RealTimeSource", false, "No ITime interface available for ROS2 Gem simulation clock");
  29. return 0;
  30. }
  31. }
  32. AZ::Outcome<void, AZStd::string> RealTimeSource::AdjustTime(const builtin_interfaces::msg::Time& time)
  33. {
  34. return AZ::Failure(AZStd::string("RealTimeSource does not support setting a specific time."));
  35. }
  36. } // namespace ROS2