aboutsummaryrefslogtreecommitdiffstats
path: root/src/hal/components/timedelta.comp
blob: 3922e8f71ad3f79de4d75ede4b22e88323fe79c6 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
component timedelta "LinuxCNC HAL component that measures thread scheduling timing behavior";

pin out s32 jitter=0 "Worst-case scheduling error (in ns).  This is the largest discrepancy between ideal thread period, and actual time between sequential runs of this component.  This uses the absolute value of the error, so 'got run too early' and 'got run too late' both show up as positive jitter.";

pin out s32 current_jitter=0 "Scheduling error (in ns) of the current invocation.  This is the discrepancy between ideal thread period, and actual time since the previous run of this component.  This uses the absolute value of the error, so 'got run too early' and 'got run too late' both show up as positive jitter.";

pin out s32 current_error=0 "Scheduling error (in ns) of the current invocation.  This is the discrepancy between ideal thread period, and actual time since the previous run of this component.  This does not use the absolute value of the error, so 'got run too early' shows up as negative error and 'got run too late' shows up as positive error.";

pin out s32 min_=0 "Minimum time (in ns) between sequential runs of this component.";

pin out s32 max_=0 "Maximum time (in ns) between sequential runs of this component.";

pin in bit reset "Set this pin to True, then back to False, to reset some of the statistics.";

pin out s32 out "Time (in ns) since the previous run of this component.  This should ideally be equal to the thread period.";

pin out s32 err=0 "Cumulative time error (in ns).  Probably not useful.";

pin out float avg_err=0 "The average scheduling error (in ns).";


function _ nofp;
variable rtapi_s64 last=0;
variable int first=1;
license "GPL";
author "Jeff Epler";
;;
#undef max
#define max(a,b) ((a)>(b)?(a):(b))

rtapi_s64 now = rtapi_get_time();

if(last != 0) {
        rtapi_s64 del = (now - last);
        out = del;

	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_);
		current_jitter = max(del - period, period - del);
		current_error = del - period;
	}
	count++;
	avg_err = err / (double)count;
}

if(reset) { first = 1; last = 0; out = 0; jitter = 0; max_ = 0; }
else last = now;
bues.ch cgit interface