summaryrefslogtreecommitdiff
path: root/src/hal/components/timedelta.comp
blob: 44b0bf0dcfa5c4ae39422eb0a2837c46e2db0fa3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
component timedelta "LinuxCNC HAL component that measures thread scheduling timing behavior";
pin out s32 out;
pin out s32 err=0;
pin out s32 min_=0;
pin out s32 max_=0;
pin out s32 jitter=0;
pin out float avg_err=0;
pin in bit reset;
function _ nofp;
variable __s64 last=0;
variable int first=1;
license "GPL";
;;
#undef max
#define max(a,b) ((a)>(b)?(a):(b))

__s64 now = rtapi_get_time();
__s64 del = (now - last);
out = del;


if(last != 0) {
	err = err + del - period;
	if(first) {
		first = 0;
		min_ = max_ = del;
		jitter = 0;
	} else {
		if(del < min_) min_ = del;
		if(del > max_) max_ = del;
		jitter = max(max_ - period, period - min_);
	}
	count++;
	avg_err = err / (double)count;
}

if(reset) { first = 1; last = 0; out = 0; jitter = 0; max_ = 0; }
else last = now;