SimulationTimeSource.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 <AzFramework/Physics/Common/PhysicsEvents.h>
  11. #include <AzFramework/Physics/PhysicsSystem.h>
  12. namespace ROS2
  13. {
  14. //! The SimulationTimeSource provides timestamps calculated using updates from the physics engine.
  15. //! Simulation paces the clock with stable steps, and as a result, this time source eliminates data jitter.
  16. //! The simulated time can be faster or slower than the real-time, according to the configuration of the physics engine.
  17. //! SimulationTimeSource is used as the default time source for the projects.
  18. //! The simulated clock is incremented with delta times that were simulated by physics.
  19. //! The time source registers and observes AZ::PhysicsScene and when the
  20. //! simulation starts, it attaches and starts counting the updates.
  21. //! It is recommended to use it with high-frequency sensors such as odometry and IMUs.
  22. class SimulationTimeSource : public ITimeSource
  23. {
  24. public:
  25. virtual ~SimulationTimeSource() = default;
  26. // ITimeSource overrides ...
  27. virtual void Activate() override;
  28. virtual void Deactivate() override;
  29. //! Sets the time source to the given time.
  30. //! @param time The time to set the time source to.
  31. //! @return An outcome indicating success or failure.
  32. virtual AZ::Outcome<void, AZStd::string> AdjustTime(const builtin_interfaces::msg::Time& time) override;
  33. //! Get ROS 2 time as ROS2 message.
  34. //! @see ROS2Requests::GetROSTimestamp() for more details.
  35. virtual builtin_interfaces::msg::Time GetROSTimestamp() const override;
  36. private:
  37. double m_elapsed = 0;
  38. bool m_resetTimeOnRestart = true;
  39. AzPhysics::SceneEvents::OnSceneSimulationFinishHandler m_onSceneSimulationEvent;
  40. AzPhysics::SystemEvents::OnSceneAddedEvent::Handler m_onSceneAdded;
  41. AzPhysics::SystemEvents::OnSceneRemovedEvent::Handler m_onSceneRemoved;
  42. };
  43. } // namespace ROS2