comparison core/VirtualClock.cpp @ 135:e77e2e712fbc ClockSync

To work with the ClockSync plugin
author Giulio Moro <giuliomoro@yahoo.it>
date Sat, 12 Sep 2015 20:05:55 +0100
parents
children 4e2dd3eb1d28
comparison
equal deleted inserted replaced
133:04b1678614c9 135:e77e2e712fbc
1 #include "VirtualClock.h"
2 void VirtualClock::init(){
3 firstRun=true;
4 movingAverage.setLength(31); //TODO: a better filtering algorithm ( Did you say Kalman?)
5 period=-1;
6 }
7
8 VirtualClock::VirtualClock(){
9 init();
10 }
11 void VirtualClock::sync(){
12 sync(1);
13 }
14 void VirtualClock::sync(double count){
15 myClock_t currentTime=Clock::getTimeUs();
16 if(firstRun==true){
17 firstRun=false;
18 startTime=currentTime;
19 } else {
20 period=movingAverage.add((currentTime-lastSync)/count); //TODO: replace with Kalman filter
21 }
22 lastSync=currentTime;
23 }
24
25 double VirtualClock::getNow(){
26 myClock_t now=Clock::getTimeUs();
27 if(period<=0){
28 return now;
29 }
30 // 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 myClock_t elapsed=(now-startTime);
33 double frac=elapsed/(double)period;
34 // printf("now=%lld; beginningOfPeriod=%f; lastSync=%lld; period=%lld; frac=%f\n", now, beginningOfPeriod, lastSync, period, frac);
35 return frac;
36 }
37
38 double VirtualClock::getPeriod(){
39 return period;
40 }