From b3e2a55844793a84a684c668539876830f6bc0f0 Mon Sep 17 00:00:00 2001
From: Raphael Grimm <raphael.grimm@kit.edu>
Date: Wed, 25 Jul 2018 16:40:50 +0200
Subject: [PATCH] Make warnings about NJointController runtime dependent on the
 number of ControlDevices

---
 .../NJointControllers/NJointController.h      |  5 ++++
 .../NJointControllers/NJointController.ipp    |  6 +++++
 .../components/units/RobotUnit/RobotUnit.h    |  2 ++
 .../RobotUnitModuleControlThread.cpp          |  4 ++--
 .../RobotUnitModuleControlThread.h            | 23 +++++++++++++++++++
 5 files changed, 38 insertions(+), 2 deletions(-)

diff --git a/source/RobotAPI/components/units/RobotUnit/NJointControllers/NJointController.h b/source/RobotAPI/components/units/RobotUnit/NJointControllers/NJointController.h
index fec9d8df7..6085b8a6d 100644
--- a/source/RobotAPI/components/units/RobotUnit/NJointControllers/NJointController.h
+++ b/source/RobotAPI/components/units/RobotUnit/NJointControllers/NJointController.h
@@ -714,6 +714,11 @@ namespace armarx
          */
         bool rtGetErrorState() const;
 
+        /**
+         * @brief Returns the number of used \ref ControlDevice "ControlDevices"
+         * @return The number of used \ref ControlDevice "ControlDevices"
+         */
+        std::size_t rtGetNumberOfUsedControlDevices() const;
     protected:
         /**
          * @brief This function is called before the controller is activated.
diff --git a/source/RobotAPI/components/units/RobotUnit/NJointControllers/NJointController.ipp b/source/RobotAPI/components/units/RobotUnit/NJointControllers/NJointController.ipp
index 0cd310b21..a4b8ee87b 100644
--- a/source/RobotAPI/components/units/RobotUnit/NJointControllers/NJointController.ipp
+++ b/source/RobotAPI/components/units/RobotUnit/NJointControllers/NJointController.ipp
@@ -204,6 +204,12 @@ namespace armarx
     {
         return errorState;
     }
+
+    inline std::size_t NJointController::rtGetNumberOfUsedControlDevices() const
+    {
+        return controlDeviceUsedIndices.size();
+    }
+
     //NJointControllerWithTripleBuffer<ControlDataStruct>
     template <typename ControlDataStruct>
     inline void NJointControllerWithTripleBuffer<ControlDataStruct>::rtSwapBufferAndRun(
diff --git a/source/RobotAPI/components/units/RobotUnit/RobotUnit.h b/source/RobotAPI/components/units/RobotUnit/RobotUnit.h
index 30541f320..eac584c03 100644
--- a/source/RobotAPI/components/units/RobotUnit/RobotUnit.h
+++ b/source/RobotAPI/components/units/RobotUnit/RobotUnit.h
@@ -40,6 +40,7 @@ namespace armarx
         public RobotUnitModule::RobotDataPropertyDefinitions,
         public RobotUnitModule::PublisherPropertyDefinitions,
         public RobotUnitModule::ManagementPropertyDefinitions,
+        public RobotUnitModule::ControlThreadPropertyDefinitions,
         public RobotUnitModule::SelfCollisionCheckerPropertyDefinitions
     {
     public:
@@ -50,6 +51,7 @@ namespace armarx
             RobotDataPropertyDefinitions(prefix),
             PublisherPropertyDefinitions(prefix),
             ManagementPropertyDefinitions(prefix),
+            ControlThreadPropertyDefinitions(prefix),
             SelfCollisionCheckerPropertyDefinitions(prefix)
         {}
     };
diff --git a/source/RobotAPI/components/units/RobotUnit/RobotUnitModules/RobotUnitModuleControlThread.cpp b/source/RobotAPI/components/units/RobotUnit/RobotUnitModules/RobotUnitModuleControlThread.cpp
index 811a2bf87..3d5b227b8 100644
--- a/source/RobotAPI/components/units/RobotUnit/RobotUnitModules/RobotUnitModuleControlThread.cpp
+++ b/source/RobotAPI/components/units/RobotUnit/RobotUnitModules/RobotUnitModuleControlThread.cpp
@@ -323,11 +323,11 @@ namespace armarx
                         rtSyncNJointControllerRobot(nJointCtrl);
                         nJointCtrl->rtSwapBufferAndRun(sensorValuesTimestamp, timeSinceLastIteration);
                         auto duration = TimeUtil::GetTime(true) - start;
-                        if (duration.toMicroSeconds() > 500)
+                        if (static_cast<std::size_t>(duration.toMicroSeconds()) > nJointCtrl->rtGetNumberOfUsedControlDevices() * usPerDevUntilError)
                         {
                             ARMARX_ERROR << deactivateSpam(5) << "The NJointController " << nJointCtrl->getClassName() << " took " << duration.toMicroSeconds() << " µs to run!";
                         }
-                        else if (duration.toMicroSeconds() > 50)
+                        else if (static_cast<std::size_t>(duration.toMicroSeconds()) > nJointCtrl->rtGetNumberOfUsedControlDevices() * usPerDevUntilWarn)
                         {
                             ARMARX_WARNING << deactivateSpam(5) << "The NJointController " << nJointCtrl->getClassName() << " took " << duration.toMicroSeconds() << " µs to run!";
                         }
diff --git a/source/RobotAPI/components/units/RobotUnit/RobotUnitModules/RobotUnitModuleControlThread.h b/source/RobotAPI/components/units/RobotUnit/RobotUnitModules/RobotUnitModuleControlThread.h
index 5d75c17c0..37f339650 100644
--- a/source/RobotAPI/components/units/RobotUnit/RobotUnitModules/RobotUnitModuleControlThread.h
+++ b/source/RobotAPI/components/units/RobotUnit/RobotUnitModules/RobotUnitModuleControlThread.h
@@ -36,6 +36,24 @@ namespace armarx
     class RTThreadTimingsSensorDevice;
     namespace RobotUnitModule
     {
+        class ControlThreadPropertyDefinitions: public ModuleBasePropertyDefinitions
+        {
+        public:
+            ControlThreadPropertyDefinitions(std::string prefix): ModuleBasePropertyDefinitions(prefix)
+            {
+                defineOptionalProperty<std::size_t>(
+                    "NjointController_AllowedExecutionTimePerControlDeviceUntilWarning", 2,
+                    "A Warning will be printed, If the execution time in micro seconds of a NJointController "
+                    "exceeds this parameter times the number of ControlDevices."
+                );
+                defineOptionalProperty<std::size_t>(
+                    "NjointController_AllowedExecutionTimePerControlDeviceUntilError", 20,
+                    "An Error will be printed, If the execution time in micro seconds of a NJointController "
+                    "exceeds this parameter times the number of ControlDevices."
+                );
+            }
+        };
+
         /**
          * @brief This \ref ModuleBase "Module" manages the \ref ControlThread.
          *
@@ -261,6 +279,11 @@ namespace armarx
 
             ///@brief The \ref ControlThread's thread id
             std::atomic<std::thread::id> controlThreadId;
+
+            /// @brief A Warning will be printed, if the execution time per ControlDev of a NJointController exceeds this parameter
+            std::size_t usPerDevUntilWarn;
+            /// @brief An Error will be printed, if the execution time per ControlDev of a NJointController exceeds this parameter
+            std::size_t usPerDevUntilError;
         };
     }
 }
-- 
GitLab