-
Notifications
You must be signed in to change notification settings - Fork 1
/
hostdrift.c
156 lines (138 loc) · 3.51 KB
/
hostdrift.c
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// hostdrift -f tickrate -i /dev/tty -b baud
// default -f 1000 -i /dev/ttyACM0 -b 9600
// -d secs option for one byte uptick (TODO flawed)
// hostdrift.c talk to drift.c
// send 4 bytes, get back 4 bytes millis() LSB first
// uno default, ds3234 ds3231 -f 1024 ds1307 -f 4096
// jazz -i /dev/ttyUSB0 -b 19200
// robot spin -i /dev/ttyUSB0 -b 115200
// maple -b 115200
#include <ctype.h>
#include <stdlib.h>
#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <string.h>
#include <sys/types.h>
#define DATABITS CS8
#define STOPBITS 0
#define PARITY 0
#define PARITYON 0
static double tickrate = 1000.;
static int baud = B9600;
static char *devtty = "/dev/ttyACM0", *baudstr = "9600";;
static int uptick = 0;
/* rclock.c return elapsed time */
double
rclock()
{
#include <sys/time.h>
struct timeval t;
(void) gettimeofday(&t, (struct timezone *)0);
return(t.tv_sec+ t.tv_usec/1.e6);
}
void doopts(int argc, char *argv[]) {
int c;
while ((c = getopt (argc, argv, "f:i:b:d:")) != -1)
switch (c)
{
case 'i':
devtty = optarg;
break;
case 'b':
baudstr = optarg;
if (strcmp(baudstr,"9600") == 0) baud = B9600;
else if (strcmp(baudstr,"19200") == 0) baud = B19200;
else if (strcmp(baudstr,"115200") == 0) baud = B115200;
break;
case 'd':
uptick = atoi(optarg);
break;
case 'f':
tickrate = atof(optarg);
break;
default:
abort ();
}
}
int dotty() {
int fd;
struct termios oldtio, newtio;
fd = open(devtty, O_RDWR | O_NOCTTY );
if (fd < 0)
{
perror(devtty);
exit(-1);
}
tcgetattr(fd,&oldtio); // save current port settings
newtio.c_cflag = baud | DATABITS | STOPBITS | PARITYON | PARITY | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR;
newtio.c_oflag = 0;
newtio.c_lflag = 0; //ICANON;
newtio.c_cc[VMIN]=1;
newtio.c_cc[VTIME]=0;
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
return fd;
}
void douptick(int fd) {
// remote sends a byte every uptick seconds
// hostdrift -d 10
double t,t0=0.0,t1;
int n,i,secs=0,ppm;
read(fd,&i,1); // discard first
while(1) {
n = read(fd,&i,1);
if (n !=1) {
printf("read %d\n",n);
perror("read");
}
if (t0 == 0.0) {
t0 = rclock();
secs = 0;
} else {
t = rclock();
secs += uptick;
ppm = 1.e6 * (secs - (t-t0))/(t-t0);
printf("%f %d %f %d \n",t,secs,t-t0, ppm);
}
}
}
main(int argc, char *argv[]) {
int fd,i,n,cnt=0,reps,ms,ms0=0,ppm;
unsigned char in, byte = 'A',*v;
double t,t0,t1;
doopts(argc,argv);
fprintf(stderr,"%s at %s baud with tick rate %.0f uptick %d\n",
devtty,baudstr,tickrate,uptick);
fd = dotty();
sleep(2); // let bootloader get out of the way
if (uptick) douptick(fd); // pps method
v = (unsigned char *) &ms;
t0=rclock();
while(1) {
time_t utime;
t=rclock();
utime = time(NULL);
write(fd,&utime,4); // send unixtime
n = read(fd,&v[0],1);
if (n !=1) {
printf("read %d\n",n);
perror("read");
}
n = read(fd,&v[1],1);
n = read(fd,&v[2],1);
n = read(fd,&v[3],1);
t1=(rclock() -t)/2; // one way time don't need this for drift
if (ms0 == 0) {
ms0 =ms ;
t0 = t ;
}
ppm = 1.e6 * (((ms-ms0)/tickrate) - (t-t0))/(t-t0);
printf("%f %d %f %f %d \n",t,ms,t-t0, (ms-ms0)/tickrate,ppm);
cnt++;
sleep(10);
}
}