Browse Source

Added registry setting to SteadyClock that allows to disable resettin… (#819)

This is useful when you want to keep the clock running between level reloads,
for example to keep some ROS 2 nodes happy that are running in the background, and
you are reload / reset the level.

Signed-off-by: Michał Pełka <[email protected]>
Signed-off-by: Jan Hanca <[email protected]>
Co-authored-by: Jan Hanca <[email protected]>
Michał Pełka 6 months ago
parent
commit
fb4009da31

+ 1 - 0
Gems/ROS2/Code/Include/ROS2/Clock/SimulationTimeSource.h

@@ -36,6 +36,7 @@ namespace ROS2
 
     private:
         double m_elapsed = 0;
+        bool m_resetTimeOnRestart = true;
         AzPhysics::SceneEvents::OnSceneSimulationFinishHandler m_onSceneSimulationEvent;
         AzPhysics::SystemEvents::OnSceneAddedEvent::Handler m_onSceneAdded;
         AzPhysics::SystemEvents::OnSceneRemovedEvent::Handler m_onSceneRemoved;

+ 21 - 5
Gems/ROS2/Code/Source/Clock/SimulationTimeSource.cpp

@@ -6,19 +6,31 @@
  *
  */
 
+#include <AzCore/Settings/SettingsRegistry.h>
 #include <AzCore/Time/ITime.h>
 #include <ROS2/Clock/SimulationTimeSource.h>
 #include <ROS2/ROS2Bus.h>
 
 namespace ROS2
 {
+    namespace
+    {
+        constexpr AZStd::string_view ResetTimestampOnLevelReload = "/O3DE/ROS2/SteadyClock/ResetTimestampOnLevelReload";
+    }
 
     void SimulationTimeSource::Activate()
     {
+        auto* registry = AZ::SettingsRegistry::Get();
+        AZ_Assert(registry, "No Registry available");
+        if (registry)
+        {
+            registry->Get(m_resetTimeOnRestart, ResetTimestampOnLevelReload);
+        }
+
         auto* systemInterface = AZ::Interface<AzPhysics::SystemInterface>::Get();
         if (!systemInterface)
         {
-            AZ_Warning("SimulationPhysicalClock", false, "Failed to get AzPhysics::SystemInterface");
+            AZ_Warning("SimulationTimeSource", false, "Failed to get AzPhysics::SystemInterface");
             return;
         }
         m_onSceneSimulationEvent = AzPhysics::SceneEvents::OnSceneSimulationFinishHandler(
@@ -34,8 +46,12 @@ namespace ROS2
                 AzPhysics::SceneHandle defaultSceneHandle = sceneInterface->GetSceneHandle(AzPhysics::DefaultPhysicsSceneName);
                 if (sceneHandle == defaultSceneHandle)
                 {
-                    AZ_Printf("SimulationPhysicalClock", "Registering clock to default scene");
-                    m_elapsed = 0.0;
+                    AZ_Printf("SimulationTimeSource", "Registering clock to default scene");
+                    if (m_resetTimeOnRestart)
+                    {
+                        AZ_Printf("SimulationTimeSource", "Reseting clock");
+                        m_elapsed = 0.0;
+                    }
                     sceneInterface->RegisterSceneSimulationFinishHandler(sceneHandle, m_onSceneSimulationEvent);
                 }
             });
@@ -48,7 +64,7 @@ namespace ROS2
                 AzPhysics::SceneHandle defaultSceneHandle = sceneInterface->GetSceneHandle(AzPhysics::DefaultPhysicsSceneName);
                 if (sceneHandle == defaultSceneHandle)
                 {
-                    AZ_Printf("SimulationPhysicalClock", "Removing clock to default scene");
+                    AZ_Printf("SimulationTimeSource", "Removing clock to default scene");
                     m_onSceneSimulationEvent.Disconnect();
                 }
             });
@@ -67,4 +83,4 @@ namespace ROS2
         timeStamp.nanosec = static_cast<uint32_t>((m_elapsed - timeStamp.sec) * 1e9);
         return timeStamp;
     }
-} // namespace ROS2
+} // namespace ROS2