Mercurial > hg > beaglert
annotate include/VirtualClock.h @ 152:8f98b32d0e23 ClockSync
Last commit on this branch for a while. Overall not very succesful
| author | Giulio Moro <giuliomoro@yahoo.it> | 
|---|---|
| date | Mon, 05 Oct 2015 13:06:14 +0100 | 
| parents | ebbfb154351a | 
| children | 
| 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@149 | 7 #include "IirFilter.h" | 
| giuliomoro@149 | 8 #include "Kalman.h" | 
| giuliomoro@141 | 9 #ifdef USE_JUCE | 
| giuliomoro@141 | 10 #else | 
| giuliomoro@141 | 11 #include <BeagleRT.h> | 
| giuliomoro@150 | 12 #include <I2c_Codec.h> | 
| giuliomoro@150 | 13 extern I2c_Codec* gAudioCodec; | 
| giuliomoro@141 | 14 #endif /* USE_JUCE */ | 
| giuliomoro@135 | 15 | 
| giuliomoro@135 | 16 class VirtualClock{ | 
| giuliomoro@135 | 17 private: | 
| giuliomoro@135 | 18 myClock_t startTime; | 
| giuliomoro@141 | 19 myClock_t startTimeOffset; | 
| giuliomoro@149 | 20 myClock_t lastSyncTime; | 
| giuliomoro@149 | 21 // myClock_t lastSyncEstimatedTime; | 
| giuliomoro@149 | 22 double lastSyncEstimatedTime; | 
| giuliomoro@135 | 23 bool firstRun; | 
| giuliomoro@141 | 24 double elapsedPeriods; | 
| giuliomoro@141 | 25 double elapsedPeriodsOffset; | 
| giuliomoro@149 | 26 double blockPeriod; | 
| giuliomoro@135 | 27 double period; | 
| giuliomoro@135 | 28 MovingAverage<double> movingAverage; | 
| giuliomoro@149 | 29 IirFilter iir; | 
| giuliomoro@149 | 30 KalmanOne kalman; | 
| giuliomoro@135 | 31 public: | 
| giuliomoro@149 | 32 void init(float initialValueUs); | 
| giuliomoro@135 | 33 VirtualClock(); | 
| giuliomoro@135 | 34 /** | 
| giuliomoro@135 | 35 Call this method at regular intervals to sync che virtual clock | 
| giuliomoro@135 | 36 */ | 
| giuliomoro@135 | 37 void sync(); | 
| giuliomoro@135 | 38 /** | 
| giuliomoro@135 | 39 Call this method asynchronously, passing a number of equally spaced events that have elapsed since the last call. | 
| giuliomoro@135 | 40 */ | 
| giuliomoro@141 | 41 void sync(double numPeriods); | 
| giuliomoro@135 | 42 /** | 
| giuliomoro@135 | 43 Get the current time according to the VirtualClock. | 
| giuliomoro@135 | 44 | 
| giuliomoro@135 | 45 @return Time elapsed since the first call to sync(), in period units. | 
| giuliomoro@135 | 46 */ | 
| giuliomoro@135 | 47 double getNow(); | 
| giuliomoro@135 | 48 /** | 
| giuliomoro@135 | 49 Get the length of the period. | 
| giuliomoro@135 | 50 | 
| giuliomoro@135 | 51 Get the length of the period (difference between calls to sync() after various filtering operations) | 
| giuliomoro@135 | 52 */ | 
| giuliomoro@135 | 53 double getPeriod(); | 
| giuliomoro@141 | 54 /** | 
| giuliomoro@141 | 55 * Add an offset to the number of elapsed periods. | 
| giuliomoro@141 | 56 * | 
| giuliomoro@141 | 57 * Add an offset to the number of elapsed periods. It also compensates for the corresponding time offset. | 
| giuliomoro@141 | 58 */ | 
| giuliomoro@141 | 59 void addOffset(double periodOffset); | 
| giuliomoro@135 | 60 }; | 
| giuliomoro@135 | 61 | 
| giuliomoro@135 | 62 #endif /* VIRTUAL_CLOCK_H_INCLUDED */ | 
