Mercurial > hg > beaglert
comparison core/ClockSync.cpp @ 139:4e2dd3eb1d28 ClockSync
The reported offset is now meaningful. The whole thing is waaay too jittery.
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Sun, 13 Sep 2015 21:34:47 +0100 |
parents | e77e2e712fbc |
children | 44d07fa9bd03 |
comparison
equal
deleted
inserted
replaced
138:53e3c0a3295d | 139:4e2dd3eb1d28 |
---|---|
12 bufferLength=kSyncMessageLength; | 12 bufferLength=kSyncMessageLength; |
13 T1=-1; | 13 T1=-1; |
14 T1p=-1; | 14 T1p=-1; |
15 T2=-1; | 15 T2=-1; |
16 T2p=-1; | 16 T2p=-1; |
17 receiveLoopSleepUs=10; | 17 receiveLoopSleepUs=100; |
18 receiveLoopTimeout=1e5; | 18 receiveLoopTimeout=1e5; |
19 movingAverage.setLength(201); | 19 movingAverage.setLength(31); |
20 expectedClockSyncType=isSlave() ? kSync : kNone; | 20 expectedClockSyncType=isSlave() ? kSync : kNone; |
21 } | 21 } |
22 ClockSync::ClockSync(bool thisIsSlave, int aPort, VirtualClock &aVirtualClock){ | 22 ClockSync::ClockSync(bool thisIsSlave, int aPort, VirtualClock &aVirtualClock){ |
23 init(thisIsSlave, aPort, aVirtualClock); | 23 init(thisIsSlave, aPort, aVirtualClock); |
24 } | 24 } |
39 } | 39 } |
40 void ClockSync::setType(int clockSyncType){ | 40 void ClockSync::setType(int clockSyncType){ |
41 ((int*)buffer)[0]=clockSyncType; | 41 ((int*)buffer)[0]=clockSyncType; |
42 } | 42 } |
43 void ClockSync::setTimestamp(myClock_t timestamp){ | 43 void ClockSync::setTimestamp(myClock_t timestamp){ |
44 // printf("setting timestamp: %lld\n", timestamp); | |
44 ((myClock_t*)((char*)buffer+sizeof(int)))[0]=timestamp; | 45 ((myClock_t*)((char*)buffer+sizeof(int)))[0]=timestamp; |
45 } | 46 } |
46 void ClockSync::print(){ | 47 void ClockSync::print(){ |
47 //printf("type: %d, timestamp: %lld\n",*((int*)buffer),*((myClock_t*)(((int*)buffer)+1))); | 48 //printf("type: %d, timestamp: %lld\n",*((int*)buffer),*((myClock_t*)(((int*)buffer)+1))); |
48 } | 49 } |
144 usleep(receiveLoopSleepUs); //how often to check for new clockSyncs; | 145 usleep(receiveLoopSleepUs); //how often to check for new clockSyncs; |
145 // //printf("waiting for clockSyncs\n"); | 146 // //printf("waiting for clockSyncs\n"); |
146 localTimestamp=receive(); | 147 localTimestamp=receive(); |
147 if(localTimestamp<=0){ | 148 if(localTimestamp<=0){ |
148 if(localTimestamp==0){ | 149 if(localTimestamp==0){ |
149 // //printf("Socket not ready to be read: %lld\n", localTimestamp); | 150 // printf("Socket not ready to be read: %lld\n", localTimestamp); |
150 } | 151 } |
151 else if(localTimestamp==-1){ | 152 else if(localTimestamp==-1){ |
152 //printf("Error while receiving: %lld\n", localTimestamp); | 153 printf("Error while receiving: %lld\n", localTimestamp); |
153 } | 154 } |
154 else if(localTimestamp==-2){ | 155 else if(localTimestamp==-2){ |
155 // //printf("Wrong size of the received clockSync: %lld\n", localTimestamp); | 156 printf("Wrong size of the received clockSync: %lld\n", localTimestamp); |
156 } | 157 } |
157 continue ; //keep waiting | 158 continue ; //keep waiting |
158 } | 159 } |
159 clockSyncType=getType(); | 160 clockSyncType=getType(); |
160 clockSyncTimestamp=getTimestamp(); | 161 clockSyncTimestamp=getTimestamp(); |
161 if(clockSyncType!=expectedClockSyncType){ | 162 if(clockSyncType!=expectedClockSyncType){ |
162 //printf("Wrong clockSync type: %d, expected: %d\n",clockSyncType, expectedClockSyncType); | 163 //printf("Wrong clockSync type: %d, expected: %d\n",clockSyncType, expectedClockSyncType); |
163 return -2; //start over | 164 return -2; //start over |
164 } | 165 } |
165 //printf("Received clockSync type: %d, clockSyncTimestamp: %lld\n", clockSyncType, clockSyncTimestamp); | 166 // printf("Received clockSync type: %d, clockSyncTimestamp: %lld\n", clockSyncType, clockSyncTimestamp); |
166 if(isSlave()==true){ | 167 if(isSlave()==true){ |
167 int ret=slaveHandleMessage(); | 168 int ret=slaveHandleMessage(); |
168 if(ret==1 && clockSyncType==kDelayResp){ //we are done, end of a cycle! | 169 if(ret==1 && clockSyncType==kDelayResp){ //we are done, end of a cycle! |
169 return 1; | 170 return 1; |
170 } else if (ret!=1) { | 171 } else if (ret!=1) { |
206 break; | 207 break; |
207 case kDelayResp: {//the clockSyncTimestamp is the instant when the master received the kDelayResp clockSync, the localTimestamp is meaningless | 208 case kDelayResp: {//the clockSyncTimestamp is the instant when the master received the kDelayResp clockSync, the localTimestamp is meaningless |
208 T2p=clockSyncTimestamp; | 209 T2p=clockSyncTimestamp; |
209 //TODO: evaluate things | 210 //TODO: evaluate things |
210 double offset=(T1p-T1-T2p+T2)/2.0d; | 211 double offset=(T1p-T1-T2p+T2)/2.0d; |
211 printf("-----------OFFSET IS : %04.1f, average: %04.2f\n", offset, movingAverage.add(offset)); | 212 printf("-----------OFFSET IS : %04.4f seconds, average: %04.4f seconds\n", |
213 offset/44100.f, movingAverage.add(offset)/44100.f); | |
212 expectedClockSyncType=kSync; //end of the cycle, wait for next sync. | 214 expectedClockSyncType=kSync; //end of the cycle, wait for next sync. |
213 break; | 215 break; |
214 } | 216 } |
215 default: | 217 default: |
216 //printf("Unexpected message type\n"); // we should never get here | 218 //printf("Unexpected message type\n"); // we should never get here |