Mercurial > hg > beaglert
annotate include/VirtualClock.h @ 141:44d07fa9bd03 ClockSync
Ultra-basic feedback for clock sync works^CIssues: response time of the IIR filter is too slow, requires PID and better filtering algorithm.
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Mon, 14 Sep 2015 15:42:11 +0100 |
parents | e77e2e712fbc |
children | 134bff10e561 |
rev | line source |
---|---|
giuliomoro@135 | 1 #ifndef VIRTUAL_CLOCK_H_INCLUDED |
giuliomoro@135 | 2 #define VIRTUAL_CLOCK_H_INCLUDED |
giuliomoro@135 | 3 |
giuliomoro@141 | 4 #include "math.h" |
giuliomoro@135 | 5 #include "stats.hpp" |
giuliomoro@135 | 6 #include "Clock.h" |
giuliomoro@141 | 7 #ifdef USE_JUCE |
giuliomoro@141 | 8 #else |
giuliomoro@141 | 9 #include <BeagleRT.h> |
giuliomoro@141 | 10 #endif /* USE_JUCE */ |
giuliomoro@135 | 11 |
giuliomoro@135 | 12 class VirtualClock{ |
giuliomoro@135 | 13 private: |
giuliomoro@135 | 14 myClock_t startTime; |
giuliomoro@141 | 15 myClock_t startTimeOffset; |
giuliomoro@135 | 16 myClock_t lastSync; |
giuliomoro@135 | 17 bool firstRun; |
giuliomoro@141 | 18 double elapsedPeriods; |
giuliomoro@141 | 19 double elapsedPeriodsOffset; |
giuliomoro@135 | 20 double period; |
giuliomoro@135 | 21 MovingAverage<double> movingAverage; |
giuliomoro@135 | 22 public: |
giuliomoro@135 | 23 void init(); |
giuliomoro@135 | 24 VirtualClock(); |
giuliomoro@135 | 25 /** |
giuliomoro@135 | 26 Call this method at regular intervals to sync che virtual clock |
giuliomoro@135 | 27 */ |
giuliomoro@135 | 28 void sync(); |
giuliomoro@135 | 29 /** |
giuliomoro@135 | 30 Call this method asynchronously, passing a number of equally spaced events that have elapsed since the last call. |
giuliomoro@135 | 31 */ |
giuliomoro@141 | 32 void sync(double numPeriods); |
giuliomoro@135 | 33 /** |
giuliomoro@135 | 34 Get the current time according to the VirtualClock. |
giuliomoro@135 | 35 |
giuliomoro@135 | 36 @return Time elapsed since the first call to sync(), in period units. |
giuliomoro@135 | 37 */ |
giuliomoro@135 | 38 double getNow(); |
giuliomoro@135 | 39 /** |
giuliomoro@135 | 40 Get the length of the period. |
giuliomoro@135 | 41 |
giuliomoro@135 | 42 Get the length of the period (difference between calls to sync() after various filtering operations) |
giuliomoro@135 | 43 */ |
giuliomoro@135 | 44 double getPeriod(); |
giuliomoro@141 | 45 /** |
giuliomoro@141 | 46 * Add an offset to the number of elapsed periods. |
giuliomoro@141 | 47 * |
giuliomoro@141 | 48 * Add an offset to the number of elapsed periods. It also compensates for the corresponding time offset. |
giuliomoro@141 | 49 */ |
giuliomoro@141 | 50 void addOffset(double periodOffset); |
giuliomoro@135 | 51 }; |
giuliomoro@135 | 52 |
giuliomoro@135 | 53 #endif /* VIRTUAL_CLOCK_H_INCLUDED */ |