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

Create new PhotonStructDecoder #181

Draft
wants to merge 3 commits into
base: new-ui
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/hub/controllers/PointsController_Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ 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 } from "../../../shared/log/LogUtil";
import { PHOTON_PREFIX, PROTO_PREFIX, STRUCT_PREFIX, getEnabledKey } 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 @@ -185,6 +185,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 @@ -284,6 +286,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,11 +1,9 @@
import Log from "../../../shared/log/Log";
import PhotonSchema from "./PhotonSchema";
import REVSchemas from "./REVSchema";

/** 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);
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 @@ -183,6 +183,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 @@ -353,6 +355,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
44 changes: 43 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 { MERGE_PREFIX, STRUCT_PREFIX, TYPE_KEY, getEnabledData, splitLogKey } from "./LogUtil";
import { MERGE_PREFIX, PHOTON_PREFIX, STRUCT_PREFIX, TYPE_KEY, getEnabledData, splitLogKey } from "./LogUtil";
import {
LogValueSetAny,
LogValueSetBoolean,
Expand All @@ -17,13 +17,15 @@ import {
import LoggableType from "./LoggableType";
import ProtoDecoder from "./ProtoDecoder";
import StructDecoder from "./StructDecoder";
import PhotonStructDecoder from "./PhotonStructDecoder";

/** Represents a collection of log fields. */
export default class Log {
private DEFAULT_TIMESTAMP_RANGE: [number, number] = [0, 10];
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 @@ -344,6 +346,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 @@ -544,6 +551,41 @@ 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);
// console.log("Log: asking to decode " + schemaType)
// console.log(decodedData)
} 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_MAX_FILES = 10;
Expand Down
Loading
Loading