giuliomoro@135: #include "VirtualClock.h" giuliomoro@135: void VirtualClock::init(){ giuliomoro@135: firstRun=true; giuliomoro@141: movingAverage.setLength(101); //TODO: a better filtering algorithm ( Did you say Kalman?) giuliomoro@135: period=-1; giuliomoro@141: elapsedPeriods=0; giuliomoro@141: startTime=0; giuliomoro@141: startTimeOffset=0; giuliomoro@141: elapsedPeriodsOffset=0; giuliomoro@135: } giuliomoro@135: giuliomoro@135: VirtualClock::VirtualClock(){ giuliomoro@135: init(); giuliomoro@135: } giuliomoro@135: void VirtualClock::sync(){ giuliomoro@135: sync(1); giuliomoro@135: } giuliomoro@141: void VirtualClock::sync(double numPeriods){ giuliomoro@135: myClock_t currentTime=Clock::getTimeUs(); giuliomoro@141: static int calls=0; giuliomoro@141: elapsedPeriods+=numPeriods; giuliomoro@141: if(calls==50){ //TODO: this is dangerous as the clock might jump suddenly if currentTime is not precise giuliomoro@141: calls=0; giuliomoro@141: startTimeOffset=startTime; giuliomoro@141: startTime=currentTime; giuliomoro@141: elapsedPeriodsOffset=elapsedPeriods; giuliomoro@141: } giuliomoro@141: calls++; giuliomoro@135: if(firstRun==true){ giuliomoro@135: firstRun=false; giuliomoro@135: startTime=currentTime; giuliomoro@135: } else { giuliomoro@141: double newPeriod=(currentTime-lastSync)/numPeriods; giuliomoro@141: double expectedPeriod=22.67; giuliomoro@141: double maxPeriodDeviation=10; giuliomoro@141: if(fabs(newPeriod-expectedPeriod)