diff --git a/src/utils/interface/lighthouse/pulseProcessor.h b/src/utils/interface/lighthouse/pulseProcessor.h index e409ef09ff..10311baa69 100644 --- a/src/utils/interface/lighthouse/pulseProcessor.h +++ b/src/utils/interface/lighthouse/pulseProcessor.h @@ -2,10 +2,13 @@ #include #include +#include #define PULSE_PROCESSOR_N_SENSORS 8 #define PULSE_PROCESSOR_HISTORY_LENGTH 8 #define TIMESTAMP_BITWIDTH 29 +#define TIMESTAMP_MAX ((1< (FRAME_LENGTH - MAX_FRAME_LENGTH_NOISE)); +} + +bool getSystemSyncTime(const uint32_t syncTimes[], int nSyncTimes, uint32_t *syncTime) +{ + if (nSyncTimes == 0) { + return false; + } + + // Detect if samples are wrapping + // If the samples are wrapping, all samples bellow TIMESTAMP_MAX/2 will + // be pushed by (1< (TIMESTAMP_MAX/2)) { + isWrapping = true; + } + } + + int32_t differenceSum = 0; + int32_t reference = syncTimes[0] % FRAME_LENGTH; + if (isWrapping && syncTimes[0] < (TIMESTAMP_MAX/2)) { + reference = (syncTimes[0] + (1< maxDiff) { + maxDiff = diff; + } + + differenceSum += diff; + } + + if ((maxDiff - minDiff) > MAX_FRAME_LENGTH_NOISE) { + return false; + } + + *syncTime = ((int)syncTimes[0] + (differenceSum / (nSyncTimes))) & (TIMESTAMP_MAX); + + + return true; } \ No newline at end of file diff --git a/test/utils/src/lighthouse/TestPulseProcessor.c b/test/utils/src/lighthouse/TestPulseProcessor.c index 903e7b189c..a415d17ceb 100644 --- a/test/utils/src/lighthouse/TestPulseProcessor.c +++ b/test/utils/src/lighthouse/TestPulseProcessor.c @@ -250,6 +250,81 @@ void testThatFindSyncCanDetectSync0FromTwoBasestationsWithTimestampWrapping() assertSyncTimeIsMultipleOfFrameLength(expectedSyncTime, actualSyncTime); } +void testThatGetSystemSyncTimeReturnsTheAverageForGoodSyncData() +{ + // Fixture + uint32_t actualSyncTime = 0; + uint32_t expectedSyncTime = 2; + uint32_t syncTimes[8] = {1, 2, 3}; + size_t nSyncTimes = 3; + + // Test + bool found = getSystemSyncTime(syncTimes, nSyncTimes, &actualSyncTime); + + // Assert + TEST_ASSERT_TRUE(found); + TEST_ASSERT_EQUAL(expectedSyncTime, actualSyncTime); +} + +void testThatGetSystemSyncTimeHandlesTimestampsFromMultipleFrames() +{ + // Fixture + uint32_t actualSyncTime = 0; + uint32_t expectedSyncTime = 2; + uint32_t syncTimes[8] = {1 + FRAME_LENGTH, 2 + (2*FRAME_LENGTH), 3, 2 + (2*FRAME_LENGTH)}; + size_t nSyncTimes = 4; + + // Test + bool found = getSystemSyncTime(syncTimes, nSyncTimes, &actualSyncTime); + + // Assert + TEST_ASSERT_TRUE(found); + assertSyncTimeIsMultipleOfFrameLength(expectedSyncTime, actualSyncTime); +} + +void testThatGetSystemSyncTimeDoesNotReturnTimestampFor0Samples() +{ + // Fixture + uint32_t unused = 0; + uint32_t syncTimes[8] = {}; + size_t nSyncTimes = 0; + + // Test + bool found = getSystemSyncTime(syncTimes, nSyncTimes, &unused); + + // Assert + TEST_ASSERT_FALSE(found); +} + +void testThatGetSystemSyncTimeDoesNotReturnTimestampIfTooMuchTimestampsSpread() +{ + // Fixture + uint32_t unused = 0; + uint32_t syncTimes[8] = {1, 50, 3}; + size_t nSyncTimes = 3; + + // Test + bool found = getSystemSyncTime(syncTimes, nSyncTimes, &unused); + + // Assert + TEST_ASSERT_FALSE(found); +} + +void testThatGetSystemSyncTimeHandlesTimestampsWithWrapping() +{ + // Fixture + uint32_t actualSyncTime = 0; + uint32_t expectedSyncTime = TIMESTAMP_MAX; + uint32_t syncTimes[8] = {0, TIMESTAMP_MAX - 1, TIMESTAMP_MAX}; + size_t nSyncTimes = 3; + + // Test + bool found = getSystemSyncTime(syncTimes, nSyncTimes, &actualSyncTime); + + // Assert + TEST_ASSERT_TRUE(found); + TEST_ASSERT_EQUAL(expectedSyncTime, actualSyncTime); +} // Test helpers @@ -257,7 +332,7 @@ void assertSyncTimeIsMultipleOfFrameLength(uint32_t expectedSyncTime, uint32_t a { uint32_t diff = actualSyncTime - expectedSyncTime; - TEST_ASSERT_TRUE_MESSAGE(diff % FRAME_LENGTH < MAX_FRAME_LENGTH_NOISE, "Sync time out of bound"); + TEST_ASSERT_LESS_THAN_MESSAGE(MAX_FRAME_LENGTH_NOISE, diff % FRAME_LENGTH, "Sync time out of bound"); } void limitTimestamps(pulseProcessorPulse_t history[])