annotate core/VirtualClock.cpp @ 138:53e3c0a3295d ClockSync

Added xenomai version of Clock, now includes header
author Giulio Moro <giuliomoro@yahoo.it>
date Sun, 13 Sep 2015 21:33:01 +0100
parents e77e2e712fbc
children 4e2dd3eb1d28
rev   line source
giuliomoro@135 1 #include "VirtualClock.h"
giuliomoro@135 2 void VirtualClock::init(){
giuliomoro@135 3 firstRun=true;
giuliomoro@135 4 movingAverage.setLength(31); //TODO: a better filtering algorithm ( Did you say Kalman?)
giuliomoro@135 5 period=-1;
giuliomoro@135 6 }
giuliomoro@135 7
giuliomoro@135 8 VirtualClock::VirtualClock(){
giuliomoro@135 9 init();
giuliomoro@135 10 }
giuliomoro@135 11 void VirtualClock::sync(){
giuliomoro@135 12 sync(1);
giuliomoro@135 13 }
giuliomoro@135 14 void VirtualClock::sync(double count){
giuliomoro@135 15 myClock_t currentTime=Clock::getTimeUs();
giuliomoro@135 16 if(firstRun==true){
giuliomoro@135 17 firstRun=false;
giuliomoro@135 18 startTime=currentTime;
giuliomoro@135 19 } else {
giuliomoro@135 20 period=movingAverage.add((currentTime-lastSync)/count); //TODO: replace with Kalman filter
giuliomoro@135 21 }
giuliomoro@135 22 lastSync=currentTime;
giuliomoro@135 23 }
giuliomoro@135 24
giuliomoro@135 25 double VirtualClock::getNow(){
giuliomoro@135 26 myClock_t now=Clock::getTimeUs();
giuliomoro@135 27 if(period<=0){
giuliomoro@135 28 return now;
giuliomoro@135 29 }
giuliomoro@135 30 // double beginningOfPeriod=lastSync; // TODO: if sync() does not get called every time (but e.g. only every so often),
giuliomoro@135 31 // then this line (and the class) needs editing
giuliomoro@135 32 myClock_t elapsed=(now-startTime);
giuliomoro@135 33 double frac=elapsed/(double)period;
giuliomoro@135 34 // printf("now=%lld; beginningOfPeriod=%f; lastSync=%lld; period=%lld; frac=%f\n", now, beginningOfPeriod, lastSync, period, frac);
giuliomoro@135 35 return frac;
giuliomoro@135 36 }
giuliomoro@135 37
giuliomoro@135 38 double VirtualClock::getPeriod(){
giuliomoro@135 39 return period;
giuliomoro@135 40 }