Epoch time realtime component help

More
06 Nov 2023 03:22 - 06 Nov 2023 03:27 #284682 by dberndt
Hello all,

I'm looking to get epoch time (seconds since linux epoch jan1, 1970)  onto a float pin so that I can compare that with the epoch time a userspace component will generate in post-processing of some halsample'd data. Basically so I can understand the lag and re-stitch the data offline.

I've never written a realtime component before and my C hasn't been used much in a couple of decades. Perhaps I could get some assistance?
pin out float totalmseconds "totalmseconds";
variable int curtime;
function _;

;;

#include <stdio.h>
#include <stdint.h>
#include <sys/time.h>


FUNCTION(_) {
    struct timeval tv;
    struct timezone tz;

    gettimeofday(&tv, &tz);
    curtime = (long int)tv.tv_sec;
    //subtract 1699.... just to make the float smaller so it'll print more nicely in halmeter/halcmd/etc
    totalmseconds = (float)curtime - 1699226972;

}


This does get a number into the the output pin, but it does not seem to update every time the function is called. Though it does seem to work on occasion for a few seconds, or that is just a C induced hallucination.

I eventually need to add milliseconds to this, but that seems trivial to pull out of tv_usec.

Can anyone point me in the right direction?


During this coding journey I found it pretty annoying that larger floats just print in exponent notation in all the linuxcnc tools. How do I go about viewing the full value of a float in halmeter, halcmd, anything? Is there a flag/option for this somewhere?

Thanks,
-Dave

Edit I'm compiling with halcompile mycomp.comp followed by halcompile --install mycomp.comp
Running 2.9pre1 RIP on Mint 21.
 
Last edit: 06 Nov 2023 03:27 by dberndt.

Please Log in or Create an account to join the conversation.

More
06 Nov 2023 07:40 #284686 by rodw
I don't think its necessary to use any conversion to get epocch time
#include <stdio.h>
#include <time.h>

int main () {
   time_t seconds;

   seconds = time(NULL);
   printf("Hours since January 1, 1970 = %ld\n", seconds/3600);
  
   return(0);
}

Ref: www.tutorialspoint.com/c_standard_library/c_function_time.htm

Please Log in or Create an account to join the conversation.

More
08 Nov 2023 04:43 #284838 by dberndt
Thanks. I got it working. I'll leave the ugly code below, it's got a bunch of extra logic in there that didn't end up being used but I don't want to clean it up and potentially break it and post it as code I'm actually using when I'm not.

I'd still love to see a way to display floats in a more useful way. Displaying epoch seconds as a float is kind of disasterous. 1.699373e+09 is just not that helpful...


// Output Pins
pin out float totalmseconds "totalmsecondsaaa";

variable int curtime;
variable int curms;
variable int firstrun = 0;
variable int counter = 0;
variable float elapsedtime = 0;
function _;

;;

#include <stdio.h>
#include <stdint.h>
#include <sys/time.h>


struct timeval tv;
struct timezone tz;

FUNCTION(_) {

    //totalmseconds = (float)curtime - 1699226972;
    if (firstrun == 0) {
    	firstrun = 1;
    }
    elapsedtime += period * 0.000000001;
    if (counter % 1 == 0) {
    	counter = 1;
    	gettimeofday(&tv, &tz);
    	curtime = (long int)tv.tv_sec;
    	curms = (long int)tv.tv_usec;
    	//printf("***** curtime: %d %d\n", curtime, curms);
    	//printf("%lf\n", elapsedtime);
    	totalmseconds = curtime + ((float)curms * 0.000001);
    }
    counter += 1;
}

Please Log in or Create an account to join the conversation.

Time to create page: 0.133 seconds
Powered by Kunena Forum