Skip to content

Commit

Permalink
Create new PhotonStructDecoder (#181)
Browse files Browse the repository at this point in the history
* Create Photon schema

* Fix point visualization!

* Fix formatting

* Hide type hash in source list help window

* Remove debug logging calls

---------

Co-authored-by: Jonah <47046556+jwbonner@users.noreply.github.com>
  • Loading branch information
mcm001 and jwbonner authored Nov 12, 2024
1 parent f634ff2 commit b6ab389
Show file tree
Hide file tree
Showing 9 changed files with 418 additions and 151 deletions.
7 changes: 6 additions & 1 deletion src/hub/controllers/PointsController_Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ const PointsController_Config: SourceListConfig = {
showInTypeName: true,
color: "#000000",
darkColor: "#ffffff",
sourceTypes: ["Translation2d", "Translation2d[]", "NumberArray"],
sourceTypes: [
"Translation2d",
"Translation2d[]",
"NumberArray",
"TargetCorner:16f6ac0dedc8eaccb951f4895d9e18b6[]"
],
showDocs: true,
options: [
{
Expand Down
7 changes: 6 additions & 1 deletion src/hub/dataSources/nt4/NT4Source.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Log from "../../../shared/log/Log";
import { PROTO_PREFIX, STRUCT_PREFIX, getEnabledKey, getURCLKeys } from "../../../shared/log/LogUtil";
import { PHOTON_PREFIX, PROTO_PREFIX, STRUCT_PREFIX, getEnabledKey, getURCLKeys } from "../../../shared/log/LogUtil";
import LoggableType from "../../../shared/log/LoggableType";
import ProtoDecoder from "../../../shared/log/ProtoDecoder";
import { checkArrayType } from "../../../shared/util";
Expand Down Expand Up @@ -196,6 +196,8 @@ export default class NT4Source extends LiveDataSource {
}
} else if (topic.type.startsWith(PROTO_PREFIX)) {
structuredType = ProtoDecoder.getFriendlySchemaType(topic.type.split(PROTO_PREFIX)[1]);
} else if (topic.type.startsWith(PHOTON_PREFIX)) {
structuredType = topic.type.split(PHOTON_PREFIX)[1];
} else if (topic.type === "msgpack") {
structuredType = "MessagePack";
} else if (topic.type === "json") {
Expand Down Expand Up @@ -295,6 +297,9 @@ export default class NT4Source extends LiveDataSource {
} else {
this.log?.putStruct(key, timestamp, value, schemaType, false);
}
} else if (topic.type.startsWith(PHOTON_PREFIX)) {
let schemaType = topic.type.split(PHOTON_PREFIX)[1];
this.log?.putPhotonStruct(key, timestamp, value, schemaType);
} else if (topic.type.startsWith(PROTO_PREFIX)) {
let schemaType = topic.type.split(PROTO_PREFIX)[1];
this.log?.putProto(key, timestamp, value, schemaType);
Expand Down
2 changes: 0 additions & 2 deletions src/hub/dataSources/schema/CustomSchemas.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import Log from "../../../shared/log/Log";
import PhotonSchema from "./PhotonSchema";
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", URCLSchemaLegacy.parseURCLr1);
CustomSchemas.set("URCLr2_periodic", URCLSchemaLegacy.parseURCLr2);
CustomSchemas.set("URCLr3_periodic", URCLSchema.parseURCLr3);
145 changes: 0 additions & 145 deletions src/hub/dataSources/schema/PhotonSchema.ts

This file was deleted.

24 changes: 24 additions & 0 deletions src/shared/geometry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ export function grabPosesAuto(
return grabRotation2dArray(log, key, timestamp, uuid);
case "Rotation3d[]":
return grabRotation3dArray(log, key, timestamp, uuid);
case "TargetCorner:16f6ac0dedc8eaccb951f4895d9e18b6[]":
return grabTargetCornerArray(log, key, timestamp, uuid);
case "Translation2d":
return grabTranslation2d(log, key, timestamp, uuid);
case "Translation3d":
Expand Down Expand Up @@ -354,6 +356,28 @@ export function grabRotation3dArray(log: Log, key: string, timestamp: number, uu
);
}

export function grabTargetCornerArray(log: Log, key: string, timestamp: number, uuid?: string): AnnotatedPose3d[] {
return indexArray(getOrDefault(log, key + "/length", LoggableType.Number, timestamp, 0, uuid)).reduce(
(array, index) => array.concat(grabTargetCorner(log, key + "/" + index.toString(), timestamp)),
[] as AnnotatedPose3d[]
);
}

export function grabTargetCorner(log: Log, key: string, timestamp: number, uuid?: string): AnnotatedPose3d[] {
return [
{
pose: {
translation: translation2dTo3d([
getOrDefault(log, key + "/x", LoggableType.Number, timestamp, 0, uuid),
getOrDefault(log, key + "/y", LoggableType.Number, timestamp, 0, uuid)
]),
rotation: Rotation3dZero
},
annotation: { is2DSource: true }
}
];
}

export function grabTranslation2d(log: Log, key: string, timestamp: number, uuid?: string): AnnotatedPose3d[] {
return [
{
Expand Down
42 changes: 41 additions & 1 deletion src/shared/log/Log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Pose2d, Translation2d } from "../geometry";
import { arraysEqual, checkArrayType } from "../util";
import LogField from "./LogField";
import LogFieldTree from "./LogFieldTree";
import { STRUCT_PREFIX, TYPE_KEY, applyKeyPrefix, getEnabledData, splitLogKey } from "./LogUtil";
import { PHOTON_PREFIX, STRUCT_PREFIX, TYPE_KEY, applyKeyPrefix, getEnabledData, splitLogKey } from "./LogUtil";
import {
LogValueSetAny,
LogValueSetBoolean,
Expand All @@ -15,6 +15,7 @@ import {
LogValueSetStringArray
} from "./LogValueSets";
import LoggableType from "./LoggableType";
import PhotonStructDecoder from "./PhotonStructDecoder";
import ProtoDecoder from "./ProtoDecoder";
import StructDecoder from "./StructDecoder";

Expand All @@ -24,6 +25,7 @@ export default class Log {
private msgpackDecoder = new Decoder();
private structDecoder = new StructDecoder();
private protoDecoder = new ProtoDecoder();
private photonDecoder = new PhotonStructDecoder();

private fields: { [id: string]: LogField } = {};
private generatedParents: Set<string> = new Set(); // Children of these fields are generated
Expand Down Expand Up @@ -373,6 +375,11 @@ export default class Log {
// Check for struct schema
if (key.includes("/.schema/" + STRUCT_PREFIX)) {
this.structDecoder.addSchema(key.split(STRUCT_PREFIX)[1], value);
this.photonDecoder.addSchema(key.split(STRUCT_PREFIX)[1], value);
this.attemptQueuedStructures();
}
if (key.includes("/.schema/" + PHOTON_PREFIX)) {
this.photonDecoder.addSchema(key.split(PHOTON_PREFIX)[1], value);
this.attemptQueuedStructures();
}
}
Expand Down Expand Up @@ -586,6 +593,39 @@ export default class Log {
}
}

/** Writes a photonstruct-encoded raw value to the field.
*
* The schema type should not include "photonstruct:"
*/
putPhotonStruct(key: string, timestamp: number, value: Uint8Array, schemaType: string) {
this.putRaw(key, timestamp, value);
if (this.fields[key].getType() === LoggableType.Raw) {
this.setGeneratedParent(key);
this.setStructuredType(key, schemaType);
let decodedData: { data: unknown; schemaTypes: { [key: string]: string } } | null = null;
try {
decodedData = this.photonDecoder.decode(schemaType, value);
} catch {}
if (decodedData !== null) {
this.putUnknownStruct(key, timestamp, decodedData.data);
Object.entries(decodedData.schemaTypes).forEach(([childKey, schemaType]) => {
// Create the key so it can be dragged even though it doesn't have data
let fullChildKey = key + "/" + childKey;
this.createBlankField(fullChildKey, LoggableType.Empty);
this.processTimestamp(fullChildKey, timestamp);
this.setStructuredType(fullChildKey, schemaType);
});
} else {
this.queuedStructs.push({
key: key,
timestamp: timestamp,
value: value,
schemaType: schemaType
});
}
}
}

/** Writes a struct-encoded raw value to the field.
*
* The schema type should not include "struct:" or "[]"
Expand Down
1 change: 1 addition & 0 deletions src/shared/log/LogUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import LoggableType from "./LoggableType";
export const TYPE_KEY = ".type";
export const STRUCT_PREFIX = "struct:";
export const PROTO_PREFIX = "proto:";
export const PHOTON_PREFIX = "photonstruct:";
export const MAX_SEARCH_RESULTS = 128;
export const MERGE_PREFIX = "Log";
export const MERGE_PREFIX_REGEX = new RegExp(/^\/?Log\d+/);
Expand Down
Loading

0 comments on commit b6ab389

Please sign in to comment.