-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for 2025 version of URCL (#267)
* Add support for 2025 URCL * Fix formatting * URCL clean up * Fix URCL * Update Spark frame spec * Rename `REVSchemaLegacy`
- Loading branch information
Showing
9 changed files
with
16,729 additions
and
73 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import Log from "../../../shared/log/Log"; | ||
import PhotonSchema from "./PhotonSchema"; | ||
import REVSchemas from "./REVSchema"; | ||
import URCLSchema from "./URCLSchema"; | ||
import URCLSchemaLegacy from "./URCLSchemaLegacy"; | ||
|
||
/** Schemas that require custom handling because they can't be decoded using just the log data. */ | ||
const CustomSchemas: Map<string, (log: Log, key: string, timestamp: number, value: Uint8Array) => void> = new Map(); | ||
export default CustomSchemas; | ||
|
||
CustomSchemas.set("rawBytes", PhotonSchema); // PhotonVision 2023.1.2 | ||
CustomSchemas.set("URCL", REVSchemas.parseURCLr1); | ||
CustomSchemas.set("URCLr2_periodic", REVSchemas.parseURCLr2); | ||
CustomSchemas.set("URCL", URCLSchemaLegacy.parseURCLr1); | ||
CustomSchemas.set("URCLr2_periodic", URCLSchemaLegacy.parseURCLr2); | ||
CustomSchemas.set("URCLr3_periodic", URCLSchema.parseURCLr3); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
import BigNumber from "bignumber.js"; | ||
import Log from "../../../shared/log/Log"; | ||
import { getOrDefault } from "../../../shared/log/LogUtil"; | ||
import LoggableType from "../../../shared/log/LoggableType"; | ||
import { parseCanFrame } from "./spark/can-spec-util"; | ||
import { sparkFramesSpec } from "./spark/spark-frames"; | ||
|
||
type FirmwareVersion = { | ||
major: number; | ||
minor: number; | ||
build: number; | ||
}; | ||
|
||
const PERSISTENT_SIZE = 8; | ||
const PERIODIC_SIZE = 14; | ||
const PERIODIC_API_CLASS = sparkFramesSpec.periodicFrames.STATUS_0.apiClass; | ||
const PERIODIC_FRAME_SPECS = [ | ||
sparkFramesSpec.periodicFrames.STATUS_0, | ||
sparkFramesSpec.periodicFrames.STATUS_1, | ||
sparkFramesSpec.periodicFrames.STATUS_2, | ||
sparkFramesSpec.periodicFrames.STATUS_3, | ||
sparkFramesSpec.periodicFrames.STATUS_4, | ||
sparkFramesSpec.periodicFrames.STATUS_5, | ||
sparkFramesSpec.periodicFrames.STATUS_6, | ||
sparkFramesSpec.periodicFrames.STATUS_7 | ||
]; | ||
const FIRMWARE_FRAME_SPEC = sparkFramesSpec.nonPeriodicFrames.GET_FIRMWARE_VERSION; | ||
const FIRMWARE_API = (FIRMWARE_FRAME_SPEC.apiClass << 4) | FIRMWARE_FRAME_SPEC.apiIndex; | ||
|
||
const DEFAULT_ALIASES = Uint8Array.of(0x7b, 0x7d); | ||
const TEXT_DECODER = new TextDecoder("UTF-8"); | ||
|
||
export default class URCLSchema { | ||
private constructor() {} | ||
|
||
/** | ||
* Parses a set of frames recorded by URCL using revision 3. | ||
*/ | ||
static parseURCLr3(log: Log, key: string, timestamp: number, value: Uint8Array) { | ||
let devices: { [key: string]: { alias?: string; firmware?: FirmwareVersion } } = {}; | ||
if (!key.endsWith("Raw/Periodic")) return; | ||
const rootKey = key.slice(0, key.length - "Raw/Periodic".length); | ||
const aliasKey = rootKey + "Raw/Aliases"; | ||
const persistentKey = rootKey + "Raw/Persistent"; | ||
let getName = (deviceId: string): string => { | ||
if (devices[deviceId].alias === undefined) { | ||
return "Spark-" + deviceId; | ||
} else { | ||
return devices[deviceId].alias!; | ||
} | ||
}; | ||
|
||
// Read aliases | ||
let aliasesRaw = getOrDefault(log, aliasKey, LoggableType.Raw, timestamp, null); | ||
if (aliasesRaw === null) aliasesRaw = DEFAULT_ALIASES; | ||
let aliases = JSON.parse(TEXT_DECODER.decode(aliasesRaw)); | ||
Object.keys(aliases).forEach((idString) => { | ||
devices[idString] = { alias: aliases[idString] }; | ||
}); | ||
|
||
// Read persistent | ||
let persistentRaw: Uint8Array | null = getOrDefault(log, persistentKey, LoggableType.Raw, timestamp, null); | ||
if (persistentRaw === null) return; | ||
const persistentDataView = new DataView(persistentRaw.buffer, persistentRaw.byteOffset, persistentRaw.byteLength); | ||
for (let position = 0; position < persistentRaw.length; position += PERSISTENT_SIZE) { | ||
let messageId = persistentDataView.getUint16(position, true); | ||
let messageValue = persistentRaw.slice(position + 2, position + 8); | ||
let deviceId = messageId & 0x3f; | ||
if (!(deviceId in devices)) { | ||
devices[deviceId] = {}; | ||
} | ||
if (((messageId >> 6) & 0x3ff) === FIRMWARE_API) { | ||
// Firmware frame | ||
let fullMessageValue = new Uint8Array(8); | ||
fullMessageValue.set(messageValue, 0); | ||
let firmwareValues = parseCanFrame(FIRMWARE_FRAME_SPEC, { data: fullMessageValue }); | ||
devices[deviceId].firmware = { | ||
major: Number(firmwareValues.MAJOR), | ||
minor: Number(firmwareValues.MINOR), | ||
build: Number(firmwareValues.FIX) | ||
}; | ||
} | ||
} | ||
|
||
// Write firmware versions to log | ||
Object.keys(devices).forEach((deviceId) => { | ||
if (devices[deviceId].firmware === undefined) { | ||
return; | ||
} | ||
let firmwareString = | ||
devices[deviceId].firmware?.major.toString() + | ||
"." + | ||
devices[deviceId].firmware?.minor.toString() + | ||
"." + | ||
devices[deviceId].firmware?.build.toString(); | ||
let firmwareKey = rootKey + getName(deviceId) + "/Firmware"; | ||
log.putString(firmwareKey, timestamp, firmwareString); | ||
log.createBlankField(rootKey + getName(deviceId), LoggableType.Empty); | ||
log.setGeneratedParent(rootKey + getName(deviceId)); | ||
}); | ||
|
||
// Read periodic frames | ||
const periodicDataView = new DataView(value.buffer, value.byteOffset, value.byteLength); | ||
for (let position = 0; position < value.length; position += PERIODIC_SIZE) { | ||
let messageTimestamp = Number(periodicDataView.getUint32(position, true)) / 1e3; | ||
let messageId = periodicDataView.getUint16(position + 4, true); | ||
let messageValue = value.slice(position + 6, position + 14); | ||
let deviceId = messageId & 0x3f; | ||
if (!(deviceId in devices) || devices[deviceId].firmware === undefined || devices[deviceId].firmware.major < 25) { | ||
continue; | ||
} | ||
|
||
if (((messageId >> 10) & 0x3f) === PERIODIC_API_CLASS) { | ||
// Periodic frame | ||
let frameIndex = (messageId >> 6) & 0xf; | ||
let deviceKey = rootKey + getName(deviceId.toString()); | ||
let frameKey = deviceKey + "/PeriodicFrame/" + frameIndex.toFixed(); | ||
log.putRaw(frameKey, messageTimestamp, messageValue); | ||
if (frameIndex >= 0 && frameIndex < PERIODIC_FRAME_SPECS.length) { | ||
let frameSpec = PERIODIC_FRAME_SPECS[frameIndex]; | ||
let frameValues = parseCanFrame(frameSpec, { data: messageValue }) as { [key: string]: BigNumber | boolean }; | ||
Object.entries(frameValues).forEach(([signalKey, signalValue]) => { | ||
if (!(signalKey in frameSpec.signals)) return; | ||
let signalSpec = (frameSpec.signals as { [key: string]: any })[signalKey]; | ||
if (signalSpec.name.includes("Reserved")) return; | ||
let signalGroup = ""; | ||
if (signalSpec.name.includes("Fault")) { | ||
signalGroup = "Fault"; | ||
} else if (signalSpec.name.includes("Warning")) { | ||
signalGroup = "Warning"; | ||
} | ||
let signalLogKey = | ||
deviceKey + | ||
"/" + | ||
(signalGroup.length === 0 ? "" : signalGroup + "/") + | ||
(signalSpec.name as string).replaceAll(" ", ""); | ||
switch (signalSpec.type as string) { | ||
case "int": | ||
case "uint": | ||
case "float": | ||
log.putNumber(signalLogKey, messageTimestamp, Number(signalValue)); | ||
break; | ||
case "boolean": | ||
log.putBoolean(signalLogKey, messageTimestamp, signalValue as boolean); | ||
break; | ||
} | ||
if ("description" in signalSpec) { | ||
log.setMetadataString(signalLogKey, JSON.stringify({ description: signalSpec.description })); | ||
} | ||
if (signalSpec.name === "Applied Output") { | ||
const voltage = getOrDefault(log, deviceKey + "/Voltage", LoggableType.Number, timestamp, 0); | ||
if (voltage > 0) { | ||
log.putNumber(deviceKey + "/AppliedOutputVoltage", timestamp, Number(signalValue) * voltage); | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
These files are provided by REV and should not be modified, except to remove the dependency on @rev-robotics/can-bridge. |
Oops, something went wrong.