Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: add offset to resync if data is lost #458

Merged
merged 2 commits into from
Apr 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions src/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,15 @@ export interface GlobalOptions {
onSuccess: (writeBuffer: WriteBuffer, absoluteTime: number) => void;
onFail: (error: Error) => void;
}[];
inSyncOffset: number;
lastInSyncTime: number;
}

const options: GlobalOptions = {
samplesPerSecond: initialSamplesPerSecond,
timeReachedTriggers: [],
inSyncOffset: 0,
lastInSyncTime: 0,
};

class FileData {
Expand Down Expand Up @@ -175,11 +179,14 @@ export const DataManager = () => ({

getTimestamp,
isInSync: () => {
const actualTimePassed =
Date.now() -
(options.writeBuffer?.getFirstWriteTime() ??
options.fileBuffer?.getFirstWriteTime() ??
0);
const firstWriteTime =
options.writeBuffer?.getFirstWriteTime() ??
options.fileBuffer?.getFirstWriteTime() ??
0;

if (firstWriteTime === 0) return true;

const actualTimePassed = Date.now() - firstWriteTime;

const processedBytes =
options.writeBuffer?.getBytesWritten() ??
Expand All @@ -194,7 +201,21 @@ export const DataManager = () => ({

// We get serial data every 30 ms regardless of sampling rate.
// If PC is ahead by more then 1.5 samples we are not in sync
return pcAheadDelta <= 45;
let inSync = pcAheadDelta - options.inSyncOffset <= 45;

if (inSync) {
options.lastInSyncTime = Date.now();
}

// If Data is lost in the serial and this was not detected we need to resync the timers so we do not get stuck rendering at 1 FPS
// NOTE: this is temporary fix until PPK protocol can handle data loss better
if (Date.now() - options.lastInSyncTime >= 1000) {
options.lastInSyncTime = Date.now();
options.inSyncOffset = actualTimePassed - simulationDelta;
inSync = true;
}

return inSync;
},
getStartSystemTime: () => options.fileBuffer?.getFirstWriteTime(),

Expand Down Expand Up @@ -258,6 +279,7 @@ export const DataManager = () => ({
options.writeBuffer = undefined;
options.foldingBuffer = undefined;
options.samplesPerSecond = initialSamplesPerSecond;
options.inSyncOffset = 0;
},
initializeLiveSession: (sessionRootPath: string) => {
const sessionPath = path.join(sessionRootPath, v4());
Expand Down