From 7e90504e8442db1574835218f4e9e476ad56dec1 Mon Sep 17 00:00:00 2001
From: Rainer Kartmann <rainer.kartmann@kit.edu>
Date: Thu, 26 Jan 2023 15:57:37 +0100
Subject: [PATCH] Change to running task to reduce IMPORTANT log by periodic
 task

---
 .../components/RobotToArViz/RobotToArViz.cpp  | 26 ++++++++++++++-----
 .../components/RobotToArViz/RobotToArViz.h    |  7 ++---
 2 files changed, 23 insertions(+), 10 deletions(-)

diff --git a/source/RobotAPI/components/RobotToArViz/RobotToArViz.cpp b/source/RobotAPI/components/RobotToArViz/RobotToArViz.cpp
index e59bbc111..1311e68e1 100644
--- a/source/RobotAPI/components/RobotToArViz/RobotToArViz.cpp
+++ b/source/RobotAPI/components/RobotToArViz/RobotToArViz.cpp
@@ -24,7 +24,7 @@
 
 #include <filesystem>
 
-#include <ArmarXCore/core/time/CycleUtil.h>
+#include <ArmarXCore/core/time/Metronome.h>
 
 
 namespace armarx
@@ -39,7 +39,7 @@ namespace armarx
         armarx::PropertyDefinitionsPtr defs(new RobotToArVizPropertyDefinitions(getConfigIdentifier()));
 
         // defs->optional(updateFrequency, "updateFrequency", "Target number of updates per second.");
-        defs->defineOptionalProperty("updateFrequency", updateFrequency, "Target number of updates per second.");
+        defs->defineOptionalProperty("updateFrequency", updateFrequencyHz, "Target number of updates per second.");
         defs->optional(gui.useCollisionModel, "UseCollisionModel", "Use the collision model for visualization");
 
         defs->optional(gui.showRobotNodeFrames, "ShowRobotNodeFrames",
@@ -57,7 +57,7 @@ namespace armarx
 
     void RobotToArViz::onInitComponent()
     {
-        getProperty(updateFrequency, "updateFrequency");
+        getProperty(updateFrequencyHz, "updateFrequency");
     }
 
 
@@ -83,10 +83,10 @@ namespace armarx
         createRemoteGuiTab();
         RemoteGui_startRunningTask();
 
-        task = new SimplePeriodicTask<>([this]()
+        task = new SimpleRunningTask<>([this]()
         {
-            this->updateRobot();
-        }, int(1000 / updateFrequency));
+            this->updateRobotRun();
+        });
         task->start();
     }
 
@@ -135,7 +135,19 @@ namespace armarx
     }
 
 
-    void RobotToArViz::updateRobot()
+    void RobotToArViz::updateRobotRun()
+    {
+        Metronome metronome(Frequency::Hertz(updateFrequencyHz));
+        while (task and not task->isStopped())
+        {
+            updateRobotOnce();
+
+            metronome.waitForNextTick();
+        }
+    }
+
+
+    void RobotToArViz::updateRobotOnce()
     {
         RobotState::synchronizeLocalClone(robotName);
 
diff --git a/source/RobotAPI/components/RobotToArViz/RobotToArViz.h b/source/RobotAPI/components/RobotToArViz/RobotToArViz.h
index e666ba1b1..5eea92090 100644
--- a/source/RobotAPI/components/RobotToArViz/RobotToArViz.h
+++ b/source/RobotAPI/components/RobotToArViz/RobotToArViz.h
@@ -96,14 +96,15 @@ namespace armarx
 
     private:
 
-        void updateRobot();
+        void updateRobotRun();
+        void updateRobotOnce();
         static bool trySetFilePathFromDataDir(viz::Robot& robotViz, const std::string& absolutePath);
 
 
     private:
 
-        float updateFrequency = 100;
-        SimplePeriodicTask<>::pointer_type task;
+        float updateFrequencyHz = 100;
+        SimpleRunningTask<>::pointer_type task;
 
         std::string robotName = "robot";
 
-- 
GitLab