Commit a0de0122 authored by Andreas Schmidt's avatar Andreas Schmidt
Browse files

Fix clock problem.

parent 51c6a0b8
Pipeline #143 passed with stage
...@@ -20,9 +20,9 @@ prrtTimestamp_t PrrtClock_get_prrt_time_us(PrrtClock *clock) ...@@ -20,9 +20,9 @@ prrtTimestamp_t PrrtClock_get_prrt_time_us(PrrtClock *clock)
if(virtualTime) { if(virtualTime) {
prrtTimestamp_t lastMeasurement = clock->lastMeasurement; prrtTimestamp_t lastMeasurement = clock->lastMeasurement;
int32_t diff = diff_ts(currentTime, lastMeasurement); prrtTimeDifference_t diff = diff_abs_ts(currentTime, lastMeasurement);
int32_t skew = (diff * clock->skew) / 400; prrtTimeDifference_t skew = (diff * clock->skew) / 400;
return virtualTime + clock->meanDeviation + skew; return (prrtTimestamp_t) virtualTime + clock->meanDeviation + skew;
} else { } else {
return currentTime; return currentTime;
} }
...@@ -33,10 +33,10 @@ bool PrrtClock_update(PrrtClock *clock, uint32_t referenceTime, uint32_t rtt) ...@@ -33,10 +33,10 @@ bool PrrtClock_update(PrrtClock *clock, uint32_t referenceTime, uint32_t rtt)
{ {
prrtTimestamp_t currentTime = PrrtClock_get_current_time_us(); prrtTimestamp_t currentTime = PrrtClock_get_current_time_us();
prrtTimestamp_t virtualTime = clock->virtualTime; prrtTimestamp_t virtualTime = clock->virtualTime;
int32_t clockSkew = clock->skew; prrtTimeDifference_t clockSkew = clock->skew;
prrtTimedelta_t delay = rtt/2; // half the rtt prrtTimedelta_t delay = rtt/2; // half the rtt
int32_t phaseError = (int32_t) diff_ts(referenceTime, virtualTime) + delay; prrtTimeDifference_t phaseError = referenceTime - virtualTime + delay;
//debug("Virtual Time: %u, Current Time: %u, Phase Error: %d, Delay: %u, Skew: %d", virtualTime, currentTime, phaseError, delay, clockSkew); debug("V: %13u, C: %13u, S: %13d, R: %13u, E: %13d, D: %3u", virtualTime, currentTime, clockSkew, referenceTime, phaseError, delay);
if(abs(phaseError) > 10000) { if(abs(phaseError) > 10000) {
clock->meanDeviation = 0; clock->meanDeviation = 0;
...@@ -45,13 +45,13 @@ bool PrrtClock_update(PrrtClock *clock, uint32_t referenceTime, uint32_t rtt) ...@@ -45,13 +45,13 @@ bool PrrtClock_update(PrrtClock *clock, uint32_t referenceTime, uint32_t rtt)
clock->virtualTime = currentTime; clock->virtualTime = currentTime;
} }
int32_t deviationSum = phaseError + 3 * clock->meanDeviation; // TODO: why 3? prrtTimeDifference_t deviationSum = (prrtTimeDifference_t) phaseError + 3 * clock->meanDeviation; // TODO: why 3?
int32_t meanDeviation = deviationSum / 4; // TODO: find out why??? prrtTimeDifference_t meanDeviation = (prrtTimeDifference_t) (deviationSum / 4); // TODO: find out why???
int32_t period = diff_ts(currentTime, clock->lastMeasurement); prrtTimeDifference_t period = diff_abs_ts(currentTime, clock->lastMeasurement);
if(period > 0) { if(period > 0) {
clockSkew = (meanDeviation / period) + 15 * clock->skew; clockSkew = (meanDeviation / period) + 15 * clock->skew;
clockSkew = clockSkew / 16; clockSkew = (prrtTimeDifference_t) (clockSkew / 16);
} }
virtualTime = virtualTime + meanDeviation + period * clockSkew / 400; virtualTime = virtualTime + meanDeviation + period * clockSkew / 400;
...@@ -61,7 +61,7 @@ bool PrrtClock_update(PrrtClock *clock, uint32_t referenceTime, uint32_t rtt) ...@@ -61,7 +61,7 @@ bool PrrtClock_update(PrrtClock *clock, uint32_t referenceTime, uint32_t rtt)
clock->lastMeasurement = currentTime; clock->lastMeasurement = currentTime;
clock->virtualTime = virtualTime; clock->virtualTime = virtualTime;
//debug("Current Time: %u; Virtual Time %u; Skew: %d; Mean Deviation: %u", currentTime, virtualTime, clockSkew, meanDeviation); //debug("V: %13u, C: %13u; S: %13d; M: %13u", virtualTime, currentTime, clockSkew, meanDeviation);
return true; return true;
} }
......
...@@ -10,8 +10,8 @@ ...@@ -10,8 +10,8 @@
typedef struct prrtClock { typedef struct prrtClock {
prrtTimestamp_t lastMeasurement; prrtTimestamp_t lastMeasurement;
prrtTimedelta_t meanDeviation; prrtTimeDifference_t meanDeviation;
int32_t skew; prrtTimeDifference_t skew;
prrtTimestamp_t virtualTime; prrtTimestamp_t virtualTime;
} PrrtClock; } PrrtClock;
...@@ -25,6 +25,8 @@ prrtTimestamp_t PrrtClock_get_prrt_time_us(PrrtClock *clock); ...@@ -25,6 +25,8 @@ prrtTimestamp_t PrrtClock_get_prrt_time_us(PrrtClock *clock);
bool PrrtClock_update(PrrtClock *clock, prrtTimestamp_t referenceTime, prrtTimedelta_t rtt); bool PrrtClock_update(PrrtClock *clock, prrtTimestamp_t referenceTime, prrtTimedelta_t rtt);
#define diff_ts(timeA, timeB) abs(((int32_t) timeA) - ((int32_t)timeB))
#define diff_abs_ts(timeA, timeB) (prrtTimeDifference_t) abs(((prrtTimeDifference_t) timeA) - ((prrtTimeDifference_t)timeB))
#endif //PRRT_CLOCK_H #endif //PRRT_CLOCK_H
...@@ -18,6 +18,7 @@ typedef uint16_t prrtSequenceNumber_t; ...@@ -18,6 +18,7 @@ typedef uint16_t prrtSequenceNumber_t;
typedef uint8_t prrtIndex_t; typedef uint8_t prrtIndex_t;
typedef uint32_t prrtTimestamp_t; typedef uint32_t prrtTimestamp_t;
typedef uint32_t prrtTimedelta_t; typedef uint32_t prrtTimedelta_t;
typedef int32_t prrtTimeDifference_t;
typedef uint32_t prrtPacketLength_t; typedef uint32_t prrtPacketLength_t;
typedef struct prrtIncompleteBlock { typedef struct prrtIncompleteBlock {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment