aboutsummaryrefslogtreecommitdiffstats
path: root/klippy/pyhelper.c
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2017-02-06 13:31:34 -0500
committerKevin O'Connor <kevin@koconnor.net>2017-02-06 13:31:34 -0500
commit20d0936fa256e0caa41a18a79556c8ade3f8347f (patch)
tree230424417d2f1b0c148d677ecf3d1003dfd943ac /klippy/pyhelper.c
parentc24b7a7ef96b6c9770fdf4c821f5719892732033 (diff)
downloadkutter-20d0936fa256e0caa41a18a79556c8ade3f8347f.tar.gz
kutter-20d0936fa256e0caa41a18a79556c8ade3f8347f.tar.xz
kutter-20d0936fa256e0caa41a18a79556c8ade3f8347f.zip
reactor: Use the system monotonic clock instead of the normal system clock
The normal system clock can have sudden jumps if the system clock is changed. Use the system monotonic clock to avoid these sudden changes in time. It appears the Raspbian OS (which is used by OctoPi) is setup to update the system clock upon network connectivity. This could cause sudden system clock changes which could lead to Klippy processing errors. Using the monotonic clock eliminates these issues. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Diffstat (limited to 'klippy/pyhelper.c')
-rw-r--r--klippy/pyhelper.c17
1 files changed, 10 insertions, 7 deletions
diff --git a/klippy/pyhelper.c b/klippy/pyhelper.c
index 2716b9b7..410fd764 100644
--- a/klippy/pyhelper.c
+++ b/klippy/pyhelper.c
@@ -9,17 +9,20 @@
#include <stdint.h> // uint8_t
#include <stdio.h> // fprintf
#include <string.h> // strerror
-#include <sys/time.h> // gettimeofday
#include <time.h> // struct timespec
-#include "pyhelper.h" // get_time
+#include "pyhelper.h" // get_monotonic
-// Return the current system time as a double
+// Return the monotonic system time as a double
double
-get_time(void)
+get_monotonic(void)
{
- struct timeval tv;
- gettimeofday(&tv, NULL);
- return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.;
+ struct timespec ts;
+ int ret = clock_gettime(CLOCK_MONOTONIC, &ts);
+ if (ret) {
+ report_errno("clock_gettime", ret);
+ return 0.;
+ }
+ return (double)ts.tv_sec + (double)ts.tv_nsec * .000000001;
}
// Fill a 'struct timespec' with a system time stored in a double