Mercurial > hg > beaglert
comparison core/VirtualClock.cpp @ 139:4e2dd3eb1d28 ClockSync
The reported offset is now meaningful. The whole thing is waaay too jittery.
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Sun, 13 Sep 2015 21:34:47 +0100 |
parents | e77e2e712fbc |
children | 44d07fa9bd03 |
comparison
equal
deleted
inserted
replaced
138:53e3c0a3295d | 139:4e2dd3eb1d28 |
---|---|
18 startTime=currentTime; | 18 startTime=currentTime; |
19 } else { | 19 } else { |
20 period=movingAverage.add((currentTime-lastSync)/count); //TODO: replace with Kalman filter | 20 period=movingAverage.add((currentTime-lastSync)/count); //TODO: replace with Kalman filter |
21 } | 21 } |
22 lastSync=currentTime; | 22 lastSync=currentTime; |
23 // printf("lastSync: %lld\n",lastSync-startTime); | |
23 } | 24 } |
24 | 25 |
25 double VirtualClock::getNow(){ | 26 double VirtualClock::getNow(){ |
26 myClock_t now=Clock::getTimeUs(); | 27 myClock_t currentSystemTime=Clock::getTimeUs(); |
27 if(period<=0){ | 28 if(period<=0){ |
28 return now; | 29 return currentSystemTime; |
29 } | 30 } |
30 // double beginningOfPeriod=lastSync; // TODO: if sync() does not get called every time (but e.g. only every so often), | 31 // double beginningOfPeriod=lastSync; // TODO: if sync() does not get called every time (but e.g. only every so often), |
31 // then this line (and the class) needs editing | 32 // then this line (and the class) needs editing |
32 myClock_t elapsed=(now-startTime); | 33 myClock_t elapsed=(currentSystemTime-startTime); |
33 double frac=elapsed/(double)period; | 34 double now=elapsed/(double)period; |
34 // printf("now=%lld; beginningOfPeriod=%f; lastSync=%lld; period=%lld; frac=%f\n", now, beginningOfPeriod, lastSync, period, frac); | 35 // printf("elapsed=%lld; sincelastSync=%lld; period=%f; now=%f\n", elapsed, currentSystemTime-lastSync, period, now); |
35 return frac; | 36 return now; |
36 } | 37 } |
37 | 38 |
38 double VirtualClock::getPeriod(){ | 39 double VirtualClock::getPeriod(){ |
39 return period; | 40 return period; |
40 } | 41 } |