Skip to content

Commit

Permalink
Fix export of negative timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
jwbonner committed Jan 21, 2024
1 parent be45020 commit 2869da0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
21 changes: 11 additions & 10 deletions src/hub/dataSources/wpilog/WPILOGEncoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,15 @@ export class WPILOGEncoderRecord {

/** Encodes an integer using the fewest necessary bytes. */
private encodeInteger(int: number): Uint8Array {
int = Math.floor(int);
if (int === 0) return new Uint8Array(1);
let length = Math.floor(Math.log(int) / Math.log(256)) + 1;
let array = new Uint8Array(length);
for (let i = 0; i < length; i++) {
array[i] = (int >> (i * 8)) & 0xff;
let array = new Uint8Array(8);
let dataView = new DataView(array.buffer);
dataView.setBigInt64(0, BigInt(Math.floor(int)), true);
for (let i = 7; i > 0; i--) {
if (array[i] !== 0) {
return array.slice(0, i + 1);
}
}
return array;
return array.slice(0, 1);
}

/** Returns the timestamp in microseconds. */
Expand All @@ -181,9 +182,9 @@ export class WPILOGEncoderRecord {
let payloadSizeData = this.encodeInteger(this.data.length);
let timestampData = this.encodeInteger(this.timestamp);
let lengthBitfield = 0;
lengthBitfield |= entryData.length - 1;
lengthBitfield |= (payloadSizeData.length - 1) << 2;
lengthBitfield |= (timestampData.length - 1) << 4;
lengthBitfield |= (entryData.length - 1) & 0x3;
lengthBitfield |= ((payloadSizeData.length - 1) & 0x3) << 2;
lengthBitfield |= ((timestampData.length - 1) & 0x7) << 4;

// Combine to single array
let array = new Uint8Array(1 + entryData.length + payloadSizeData.length + timestampData.length + this.data.length);
Expand Down
2 changes: 1 addition & 1 deletion src/hub/exportWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ function generateWPILOG(
}
let startTimestamp = fieldData.timestamps.length > 0 ? fieldData.timestamps[0] : 0;
encoder.add(
WPILOGEncoderRecord.makeControlStart(startTimestamp, {
WPILOGEncoderRecord.makeControlStart(startTimestamp * 1000000, {
entry: entryId, // Entry 0 is reserved
name: field,
type: typeStr,
Expand Down

0 comments on commit 2869da0

Please sign in to comment.