Mercurial > hg > beaglert
annotate include/ClockSync.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 | 134bff10e561 |
children |
rev | line source |
---|---|
giuliomoro@135 | 1 #ifndef CLOCK_SYNC_H_INCLUDED |
giuliomoro@135 | 2 #define CLOCK_SYNC_H_INCLUDED |
giuliomoro@135 | 3 #include "stats.hpp" |
giuliomoro@135 | 4 #include "UdpServer.h" |
giuliomoro@135 | 5 #include "UdpClient.h" |
giuliomoro@135 | 6 #include "Clock.h" |
giuliomoro@135 | 7 #include "VirtualClock.h" |
giuliomoro@149 | 8 #include "Pid.h" |
giuliomoro@144 | 9 #ifdef USE_JUCE |
giuliomoro@144 | 10 #else |
giuliomoro@144 | 11 #include <I2c_Codec.h> |
giuliomoro@144 | 12 extern I2c_Codec* gAudioCodec; |
giuliomoro@144 | 13 #endif /* USE_JUCE */ |
giuliomoro@135 | 14 |
giuliomoro@135 | 15 enum ptpMessageConsts{ |
giuliomoro@135 | 16 kSyncMessageLength=sizeof(myClock_t)+sizeof(int) |
giuliomoro@135 | 17 }; |
giuliomoro@135 | 18 enum ptpMessageType{ |
giuliomoro@135 | 19 kSync=0, |
giuliomoro@135 | 20 kFollowUp=1, |
giuliomoro@135 | 21 kDelayReq=2, |
giuliomoro@135 | 22 kDelayResp=3, |
giuliomoro@135 | 23 kNone=4 |
giuliomoro@135 | 24 }; |
giuliomoro@135 | 25 |
giuliomoro@135 | 26 enum ptpStatus{ |
giuliomoro@135 | 27 kSyncSent, |
giuliomoro@135 | 28 kFollowUpSent, |
giuliomoro@135 | 29 kDelayReqSent, |
giuliomoro@135 | 30 kDelayRespSent |
giuliomoro@135 | 31 }; |
giuliomoro@135 | 32 |
giuliomoro@135 | 33 class ClockSync{ |
giuliomoro@135 | 34 private: |
giuliomoro@135 | 35 MovingAverage<double> movingAverage; |
giuliomoro@135 | 36 UdpServer server; |
giuliomoro@135 | 37 UdpClient client; |
giuliomoro@135 | 38 bool slave; |
giuliomoro@135 | 39 int bufferLength; |
giuliomoro@135 | 40 int clockSyncType; |
giuliomoro@135 | 41 int expectedClockSyncType; |
giuliomoro@135 | 42 myClock_t clockSyncTimestamp; |
giuliomoro@135 | 43 myClock_t localTimestamp; |
giuliomoro@135 | 44 myClock_t T1; |
giuliomoro@135 | 45 myClock_t T1p; |
giuliomoro@135 | 46 myClock_t T2; |
giuliomoro@135 | 47 myClock_t T2p; |
giuliomoro@135 | 48 int receiveLoopSleepUs; |
giuliomoro@135 | 49 int receiveLoopTimeout; |
giuliomoro@135 | 50 char buffer[kSyncMessageLength]; |
giuliomoro@135 | 51 VirtualClock *virtualClock; |
giuliomoro@141 | 52 void resetTs(); |
giuliomoro@141 | 53 bool areTsValid(); |
giuliomoro@152 | 54 void processOffset(); |
giuliomoro@152 | 55 float offset; |
giuliomoro@152 | 56 float nlOffset; |
giuliomoro@152 | 57 float iirOffset; |
giuliomoro@149 | 58 Pid pid; |
giuliomoro@135 | 59 public: |
giuliomoro@135 | 60 ClockSync(){}; |
giuliomoro@135 | 61 ClockSync(bool thisIsSlave, int aPort, VirtualClock &aVirtualClock); |
giuliomoro@135 | 62 void init(bool thisIsSlave, int aPort, VirtualClock &aVirtualClock); |
giuliomoro@135 | 63 void* getBuffer(); |
giuliomoro@135 | 64 bool isSlave(); |
giuliomoro@135 | 65 bool isMaster(); |
giuliomoro@135 | 66 int getType(); |
giuliomoro@135 | 67 myClock_t getTimestamp(); |
giuliomoro@135 | 68 void setVirtualClock(VirtualClock &aVirtualClock); |
giuliomoro@135 | 69 void setPort(int aPort); |
giuliomoro@135 | 70 void setType(int clockSyncType); |
giuliomoro@135 | 71 void setTimestamp(myClock_t timestamp); |
giuliomoro@135 | 72 void print(); |
giuliomoro@152 | 73 float getOffset(); |
giuliomoro@152 | 74 float getIirOffset(); |
giuliomoro@152 | 75 |
giuliomoro@135 | 76 /** |
giuliomoro@135 | 77 * sends a clockSync without blocking, checks results and returns the timestamp |
giuliomoro@135 | 78 * immediately after the clockSync has been sent or -1 if there was an error or timeout expired. |
giuliomoro@135 | 79 */ |
giuliomoro@135 | 80 myClock_t send(); |
giuliomoro@135 | 81 /** |
giuliomoro@135 | 82 * receives a clockSync without blocking, checks results and returns the timestamp |
giuliomoro@135 | 83 * immediately after the clockSync has been received, or -1 if there was an error |
giuliomoro@135 | 84 * or 0 if timeout expired. |
giuliomoro@135 | 85 */ |
giuliomoro@135 | 86 myClock_t receive(); |
giuliomoro@135 | 87 int masterSendSync(); |
giuliomoro@135 | 88 int receiveLoop(); |
giuliomoro@135 | 89 int slaveHandleMessage(); |
giuliomoro@135 | 90 int masterHandleMessage(); |
giuliomoro@135 | 91 int sendReceiveLoop(); |
giuliomoro@135 | 92 operator void*(){ |
giuliomoro@135 | 93 return getBuffer(); |
giuliomoro@135 | 94 } |
giuliomoro@135 | 95 }; |
giuliomoro@135 | 96 |
giuliomoro@141 | 97 #endif /* CLOCK_SYNC_H_INCLUDED */ |