summaryrefslogtreecommitdiff
path: root/host/calibrator_client.c
blob: 26d5002c9a6fd759ba9ad75277babfb5ebcb7e06 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* Thinger to read 60hz cycle times and display diagnostics in a kind
 * of stripcharty way.
 */

#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <time.h>

// window for reporting, in cycles
#define REPORT_FREQ 60
// width of the bargraph
#define WIDTH 40
// timer cycles per second
#define SCALE 1000000
// length between nominal timer overflows, in microseconds
// (1 overflow / 65'536 microseconds) * (1'000'000 microseconds)
#define TICKSIZE (SCALE / 65536)

#define NANOSECOND 1000000000
#define MILLISECOND 1000

#define NOMINAL 60
#define MOTION 0.2

int main(int *argc, char **argv)
{
	char line[WIDTH + 10];
	char blankline[] = "                    |                    ";
	double bottom = SCALE/(NOMINAL-MOTION);
	double top = SCALE/(NOMINAL+MOTION);
	double center = SCALE/(NOMINAL);
	int counter;
	int qpos = 0;
	int fd = open("/dev/ttyACM0", O_RDONLY);

	long int count_total = 0;	// total overflows so far ever
	struct timeval initial;	// initial timestamp

	signed long long int initial_nsec;
	signed long int initial_msec;

	strcpy(&line, &blankline);

	gettimeofday(&initial, NULL);

	initial_msec = (initial.tv_sec * 1000) + (initial.tv_usec / 1000);

	while (1) {
		double current, average;
		char inlin[20];
		int position, pos_avg, i;
		int len;

		struct timeval current_time;
		signed long long int cur_nsec;
		signed long int cur_msec;

		long double nominal, measured;

		len = read(fd, &inlin, 1);
		if (len == 0) {
			puts("Fucked\n");
			continue;
		}
		gettimeofday(&current_time, NULL);
		cur_msec = (current_time.tv_usec / 1000) + (current_time.tv_sec * 1000);

		count_total++;

		nominal = (long double) count_total / TICKSIZE;
		//measured = (cur_nsec - initial_nsec) / NANOSECOND;
		measured = (cur_msec - initial_msec) / MILLISECOND;

		printf("% '05.8Lf - % '05.8Lf, % '05.8Lf\n", nominal, measured, measured/nominal);

	}
}