-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimeClock.cpp
166 lines (139 loc) · 5.15 KB
/
TimeClock.cpp
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
156
157
158
159
160
161
162
163
164
165
166
#include "TimeClock.h"
SINGLETON_CPP(TimeClock)
/************************ ServerOffsetCumulativeMean **************************/
#define OFFSET_MEAN_CALIBRATION_CONSECUTIVE_PACKETS (conf->ClockSync->offsetMeanCalibrationConsecutivePackets)
#define OFFSET_MEAN_CALIBRATION_DERIVATIVE_THRESHOLD (conf->ClockSync->offsetMeanCalibrationDerivativeThreshold)
ServerOffsetCumulativeMean::ServerOffsetCumulativeMean(){
reset();
}
double ServerOffsetCumulativeMean::updateMean(Sample<long> offsetSample){
lastCumulativeMean = cumulativeMean;
packets++;
if (packets == 1){
cumulativeMean = Sample<double>(offsetSample.sample, offsetSample.timestamp);
}else{
double i = packets;
double x = offsetSample.sample;
cumulativeMean.sample = ((i-1)*cumulativeMean.sample)/i + x/i;
cumulativeMean.timestamp = offsetSample.timestamp;
}
return cumulativeMean.sample;
}
bool ServerOffsetCumulativeMean::isCalibrated(){
if (successCount >= OFFSET_MEAN_CALIBRATION_CONSECUTIVE_PACKETS)
return true;
if (packets == 1)
return false;
double deltaX = cumulativeMean.sample - lastCumulativeMean.sample;
double deltaT = cumulativeMean.timestamp - lastCumulativeMean.timestamp;
bool success = abs(deltaX/deltaT) < OFFSET_MEAN_CALIBRATION_DERIVATIVE_THRESHOLD;
if (success){
successCount++;
}else{
successCount = 0;
}
return (successCount >= OFFSET_MEAN_CALIBRATION_CONSECUTIVE_PACKETS);
}
long ServerOffsetCumulativeMean::getMean(){
return cumulativeMean.sample;
}
void ServerOffsetCumulativeMean::expireCalculation(){
reset();
}
void ServerOffsetCumulativeMean::reset(){
lastCumulativeMean = Sample<double>();
cumulativeMean = Sample<double>();
packets = 0;
successCount = 0;
conf = singleton(Configuration);
}
/******************************** TimeClock ***********************************/
#define EXPIRATION_PERIOD (configuration->ClockSync->expirationPeriod)
#define FIRST_PACKETS_IGNORE_QTY (configuration->ClockSync->firstPacketsIgnoreQty)
TimeClock::TimeClock(){
setup();
}
void TimeClock::setup(){
minimumOffset = 0xFFFFFFFF;
expirationPeriodIndex = 0;
offsetExpirationExponent = 0;
correction = 0;
firstInvalidPacketsQty = 0;
serverOffsetCumulativeMean.reset();
configuration = singleton(Configuration);
synced = false;
}
bool TimeClock::isSynced(){
return synced;
}
bool TimeClock::updateServerOffset(Frame* frame){
/* ignore first packets due to errors in packet timestamping */
if (firstInvalidPacketsQty < FIRST_PACKETS_IGNORE_QTY){
firstInvalidPacketsQty++;
return false;
}
/* update offset cumulative mean statistic */
Sample<long> serverOffset = frame->getOffsetAgainstServerTime();
double offsetMean = serverOffsetCumulativeMean.updateMean(serverOffset);
if (!serverOffsetCumulativeMean.isCalibrated()){
if (expirationPeriodIndex != 0){
/* the clock was synced with server but it's calibrating due to expiration */
DEBUG(Serial.printf("%i\t%lu\t%s\t%i\n", serverOffset, minimumOffset, String(offsetMean).c_str(),frame->seq));
return true;
} else {
/* the clock hasn't ever been calibrated */
return false;
}
}
/* if current offset sample is too far from the cumulative mean,
* consider it as an outlier.
*/
if (abs(serverOffset.sample - offsetMean) > configuration->ClockSync->offsetSigma){
DEBUG(
if (expirationPeriodIndex != 0)
Serial.printf("%i\t%lu\t%s\t%i\n", serverOffset, minimumOffset, String(offsetMean).c_str(),frame->seq);
)
return (expirationPeriodIndex != 0);
}
/* if we arrived here, we have the cumulatveMean calibrated
* (i.e. the cumulativeMean has a representative value)
* and an offset sample that is not an outlier.
*/
if (expirationPeriodIndex == 0) {
Serial.printf("Calibrating...Finished!\n");
/* multiplier was never calculated */
correction = serverOffset.sample;
expirationPeriodIndex = ((double)time()/(double)EXPIRATION_PERIOD) + 1;
DEBUG(Serial.println("serverOffset\tminimumOffset\tmean\tseq"));
}else{
if (time() >= expirationPeriodIndex*EXPIRATION_PERIOD) {
expirationPeriodIndex++;
minimumOffset = ((unsigned long)(abs(correction) + pow(2,offsetExpirationExponent)));
DEBUG(Serial.printf("Expiration -> minOff %i\n", minimumOffset));
offsetExpirationExponent++;
serverOffsetCumulativeMean.expireCalculation();
}
}
if (((unsigned long)abs(serverOffset.sample)) <= minimumOffset){
DEBUG(Serial.printf("Setting offset %i\n", serverOffset));
minimumOffset = (unsigned long)abs(serverOffset.sample);
correction = serverOffset.sample;
offsetExpirationExponent = 0;
synced = true;
}
DEBUG(Serial.printf("%i\t%lu\t%s\t%i\n", serverOffset.sample, minimumOffset, String(offsetMean).c_str(),frame->seq));
return true;
}
unsigned long TimeClock::time(){
bool isPositive = correction >= 0;
unsigned long corr = (unsigned long) abs(correction);
unsigned long milis = rawTime();
return (unsigned long)(isPositive ? (milis + corr) : (milis - corr));
}
unsigned long TimeClock::rawTime(){
return millis();
}
void TimeClock::configurationChanged(){
Serial.println("TimeClock::configurationChanged()");
setup();
}