Commit a0de0122 authored by Andreas Schmidt's avatar Andreas Schmidt

Fix clock problem.

parent 51c6a0b8
Pipeline #143 passed with stage
......@@ -20,9 +20,9 @@ prrtTimestamp_t PrrtClock_get_prrt_time_us(PrrtClock *clock)
if(virtualTime) {
prrtTimestamp_t lastMeasurement = clock->lastMeasurement;
int32_t diff = diff_ts(currentTime, lastMeasurement);
int32_t skew = (diff * clock->skew) / 400;
return virtualTime + clock->meanDeviation + skew;
prrtTimeDifference_t diff = diff_abs_ts(currentTime, lastMeasurement);
prrtTimeDifference_t skew = (diff * clock->skew) / 400;
return (prrtTimestamp_t) virtualTime + clock->meanDeviation + skew;
} else {
return currentTime;
}
......@@ -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 virtualTime = clock->virtualTime;
int32_t clockSkew = clock->skew;
prrtTimeDifference_t clockSkew = clock->skew;
prrtTimedelta_t delay = rtt/2; // half the rtt
int32_t phaseError = (int32_t) diff_ts(referenceTime, virtualTime) + delay;
//debug("Virtual Time: %u, Current Time: %u, Phase Error: %d, Delay: %u, Skew: %d", virtualTime, currentTime, phaseError, delay, clockSkew);
prrtTimeDifference_t phaseError = referenceTime - virtualTime + delay;
debug("V: %13u, C: %13u, S: %13d, R: %13u, E: %13d, D: %3u", virtualTime, currentTime, clockSkew, referenceTime, phaseError, delay);
if(abs(phaseError) > 10000) {
clock->meanDeviation = 0;
......@@ -45,13 +45,13 @@ bool PrrtClock_update(PrrtClock *clock, uint32_t referenceTime, uint32_t rtt)
clock->virtualTime = currentTime;
}
int32_t deviationSum = phaseError + 3 * clock->meanDeviation; // TODO: why 3?
int32_t meanDeviation = deviationSum / 4; // TODO: find out why???
int32_t period = diff_ts(currentTime, clock->lastMeasurement);
prrtTimeDifference_t deviationSum = (prrtTimeDifference_t) phaseError + 3 * clock->meanDeviation; // TODO: why 3?
prrtTimeDifference_t meanDeviation = (prrtTimeDifference_t) (deviationSum / 4); // TODO: find out why???
prrtTimeDifference_t period = diff_abs_ts(currentTime, clock->lastMeasurement);
if(period > 0) {
clockSkew = (meanDeviation / period) + 15 * clock->skew;
clockSkew = clockSkew / 16;
clockSkew = (prrtTimeDifference_t) (clockSkew / 16);
}
virtualTime = virtualTime + meanDeviation + period * clockSkew / 400;
......@@ -61,7 +61,7 @@ bool PrrtClock_update(PrrtClock *clock, uint32_t referenceTime, uint32_t rtt)
clock->lastMeasurement = currentTime;
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;
}
......
......@@ -10,8 +10,8 @@
typedef struct prrtClock {
prrtTimestamp_t lastMeasurement;
prrtTimedelta_t meanDeviation;
int32_t skew;
prrtTimeDifference_t meanDeviation;
prrtTimeDifference_t skew;
prrtTimestamp_t virtualTime;
} PrrtClock;
......@@ -25,6 +25,8 @@ prrtTimestamp_t PrrtClock_get_prrt_time_us(PrrtClock *clock);
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
......@@ -18,6 +18,7 @@ typedef uint16_t prrtSequenceNumber_t;
typedef uint8_t prrtIndex_t;
typedef uint32_t prrtTimestamp_t;
typedef uint32_t prrtTimedelta_t;
typedef int32_t prrtTimeDifference_t;
typedef uint32_t prrtPacketLength_t;
typedef struct prrtIncompleteBlock {
......
Markdown is supported
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