Mercurial > hg > beaglert
annotate include/VirtualClock.h @ 149:134bff10e561 ClockSync
Added simple one-variable one-measurement Kalman filter, Pid controller(which output is not used). Virtual clock is now much more precise and reactive for period. Still it is lagging behind a bit on the overall offset.
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Mon, 21 Sep 2015 03:12:21 +0100 |
parents | 44d07fa9bd03 |
children | ebbfb154351a |
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@141 | 12 #endif /* USE_JUCE */ |
giuliomoro@135 | 13 |
giuliomoro@135 | 14 class VirtualClock{ |
giuliomoro@135 | 15 private: |
giuliomoro@135 | 16 myClock_t startTime; |
giuliomoro@141 | 17 myClock_t startTimeOffset; |
giuliomoro@149 | 18 myClock_t lastSyncTime; |
giuliomoro@149 | 19 // myClock_t lastSyncEstimatedTime; |
giuliomoro@149 | 20 double lastSyncEstimatedTime; |
giuliomoro@135 | 21 bool firstRun; |
giuliomoro@141 | 22 double elapsedPeriods; |
giuliomoro@141 | 23 double elapsedPeriodsOffset; |
giuliomoro@149 | 24 double blockPeriod; |
giuliomoro@135 | 25 double period; |
giuliomoro@135 | 26 MovingAverage<double> movingAverage; |
giuliomoro@149 | 27 IirFilter iir; |
giuliomoro@149 | 28 KalmanOne kalman; |
giuliomoro@135 | 29 public: |
giuliomoro@149 | 30 void init(float initialValueUs); |
giuliomoro@135 | 31 VirtualClock(); |
giuliomoro@135 | 32 /** |
giuliomoro@135 | 33 Call this method at regular intervals to sync che virtual clock |
giuliomoro@135 | 34 */ |
giuliomoro@135 | 35 void sync(); |
giuliomoro@135 | 36 /** |
giuliomoro@135 | 37 Call this method asynchronously, passing a number of equally spaced events that have elapsed since the last call. |
giuliomoro@135 | 38 */ |
giuliomoro@141 | 39 void sync(double numPeriods); |
giuliomoro@135 | 40 /** |
giuliomoro@135 | 41 Get the current time according to the VirtualClock. |
giuliomoro@135 | 42 |
giuliomoro@135 | 43 @return Time elapsed since the first call to sync(), in period units. |
giuliomoro@135 | 44 */ |
giuliomoro@135 | 45 double getNow(); |
giuliomoro@135 | 46 /** |
giuliomoro@135 | 47 Get the length of the period. |
giuliomoro@135 | 48 |
giuliomoro@135 | 49 Get the length of the period (difference between calls to sync() after various filtering operations) |
giuliomoro@135 | 50 */ |
giuliomoro@135 | 51 double getPeriod(); |
giuliomoro@141 | 52 /** |
giuliomoro@141 | 53 * Add an offset to the number of elapsed periods. |
giuliomoro@141 | 54 * |
giuliomoro@141 | 55 * Add an offset to the number of elapsed periods. It also compensates for the corresponding time offset. |
giuliomoro@141 | 56 */ |
giuliomoro@141 | 57 void addOffset(double periodOffset); |
giuliomoro@135 | 58 }; |
giuliomoro@135 | 59 |
giuliomoro@135 | 60 #endif /* VIRTUAL_CLOCK_H_INCLUDED */ |