From 20d0936fa256e0caa41a18a79556c8ade3f8347f Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Mon, 6 Feb 2017 13:31:34 -0500 Subject: 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 --- klippy/pyhelper.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'klippy/pyhelper.c') 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 // uint8_t #include // fprintf #include // strerror -#include // gettimeofday #include // 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 -- cgit v1.2.3-70-g09d2