From 21ee714abe4da7e762ab331ecf31e3b736db7a3b Mon Sep 17 00:00:00 2001
From: Rainer Kartmann <rainer.kartmann@kit.edu>
Date: Thu, 30 Nov 2023 10:48:58 +0100
Subject: [PATCH] Add nonRtNow() and mapRtTimestampToNonRtTimestamp()

---
 .../components/units/RobotUnit/CMakeLists.txt |  1 +
 .../units/RobotUnit/util/NonRtTiming.h        | 66 +++++++++++++++++++
 2 files changed, 67 insertions(+)
 create mode 100644 source/RobotAPI/components/units/RobotUnit/util/NonRtTiming.h

diff --git a/source/RobotAPI/components/units/RobotUnit/CMakeLists.txt b/source/RobotAPI/components/units/RobotUnit/CMakeLists.txt
index afb8f6a0c..dbaaf4873 100755
--- a/source/RobotAPI/components/units/RobotUnit/CMakeLists.txt
+++ b/source/RobotAPI/components/units/RobotUnit/CMakeLists.txt
@@ -78,6 +78,7 @@ set(LIB_HEADERS
     util/introspection/ClassMemberInfo.h
     util/RtLogging.h
     util/RtTiming.h
+    util/NonRtTiming.h
     util/CtrlUtil.h
 
     #robot unit modules need to be added to the list below (but not here)
diff --git a/source/RobotAPI/components/units/RobotUnit/util/NonRtTiming.h b/source/RobotAPI/components/units/RobotUnit/util/NonRtTiming.h
new file mode 100644
index 000000000..132d1d43b
--- /dev/null
+++ b/source/RobotAPI/components/units/RobotUnit/util/NonRtTiming.h
@@ -0,0 +1,66 @@
+/*
+ * This file is part of ArmarX.
+ *
+ * Copyright (C) 2011-2017, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
+ *
+ * ArmarX is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * ArmarX is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @package    ArmarX
+ * @author     Mirko Waechter( mirko.waechter at kit dot edu)
+ * @date       2018
+ * @copyright  http://www.gnu.org/licenses/gpl-2.0.txt
+ *             GNU General Public License
+ */
+#pragma once
+
+#include <time.h>
+
+#include <IceUtil/Time.h>
+
+#include "RtTiming.h"
+
+namespace armarx
+{
+    inline IceUtil::Time
+    nonRtNow()
+    {
+        using namespace rt_timing::constants;
+
+        struct timespec ts;
+        clock_gettime(CLOCK_REALTIME, &ts);
+        return IceUtil::Time::microSeconds(ts.tv_sec * seconds2MicroSeconds +
+                                           ts.tv_nsec / nanoSeconds2MicroSeconds);
+    }
+
+    inline IceUtil::Time
+    mapRtTimestampToNonRtTimestamp(const IceUtil::Time& time_monotic_raw)
+    {
+        // This is the "real time" clock, i.e. NTP-synchronized and relative to epoch.
+        IceUtil::Time now_real_time = armarx::nonRtNow();
+        // This is not relative to epoch and not NTP-synchronized.
+        IceUtil::Time now_monotonic_raw = armarx::rtNow();
+
+        /*
+             * Assumption for small very small time deltas (i.e. "time" is close to "now"):
+             *
+             * time_real_time - now_real_time == time_monotic_raw - now_monotonic_raw
+             * =>
+             * time_real_time = time_monotic_raw - now_monotonic_raw + now_real_time
+             */
+        //
+        IceUtil::Time time_real_time = time_monotic_raw - now_monotonic_raw + now_real_time;
+
+        return time_real_time;
+    }
+
+} // namespace armarx
-- 
GitLab