Skip to content

Commit

Permalink
Export WPILOGs in timestamp order
Browse files Browse the repository at this point in the history
  • Loading branch information
jwbonner committed Jan 10, 2024
1 parent 3784742 commit 665ee5a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/hub/dataSources/wpilog/WPILOGEncoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ export class WPILOGEncoderRecord {
return array;
}

/** Returns the timestamp in microseconds. */
getTimestamp() {
return this.timestamp;
}

/** Encodes the full contents of this record and returns the result. */
getEncoded(): Uint8Array {
// Generate length bitfield
Expand Down Expand Up @@ -207,9 +212,10 @@ export class WPILOGEncoder {
}

/** Encodes the full data log. */
getEncoded(): Uint8Array {
getEncoded(sortByTimestamp = false): Uint8Array {
// Encode all records and header data
let encodedRecords = this.records.map((record) => record.getEncoded());
let records = sortByTimestamp ? this.records.sort((a, b) => a.getTimestamp() - b.getTimestamp()) : this.records;
let encodedRecords = records.map((record) => record.getEncoded());
let totalRecordLength = encodedRecords.reduce((previous, current) => previous + current.length, 0);
let encodedHeader = TEXT_ENCODER.encode(HEADER_STRING);
let encodedExtraHeader = TEXT_ENCODER.encode(this.extraHeader);
Expand Down
5 changes: 3 additions & 2 deletions src/hub/exportWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,9 @@ function generateWPILOG(log: Log, fields: string[], progress: (progress: number)
metadata = JSON.stringify(metadataParsed);
} catch {}
}
let startTimestamp = fieldData.timestamps.length > 0 ? fieldData.timestamps[0] : 0;
encoder.add(
WPILOGEncoderRecord.makeControlStart(0, {
WPILOGEncoderRecord.makeControlStart(startTimestamp, {
entry: entryId, // Entry 0 is reserved
name: field,
type: typeStr,
Expand Down Expand Up @@ -260,7 +261,7 @@ function generateWPILOG(log: Log, fields: string[], progress: (progress: number)

// Encode full log
progress(1);
return encoder.getEncoded();
return encoder.getEncoded(true);
}

async function generateMCAP(log: Log, fields: string[], progress: (progress: number) => void): Promise<Uint8Array> {
Expand Down

0 comments on commit 665ee5a

Please sign in to comment.