blob: adbf90078047c2b6f6240fc2605beb52294664b3 [file] [log] [blame]
Milind Upadhyayd836bce2022-03-25 21:49:57 -07001import {ByteBuffer} from 'flatbuffers';
James Kuszmaulb35e2342022-03-06 15:44:00 -08002import {AosPlotter} from 'org_frc971/aos/network/www/aos_plotter';
Milind Upadhyayd836bce2022-03-25 21:49:57 -07003import {MessageHandler, TimestampedMessage} from 'org_frc971/aos/network/www/aos_plotter';
James Kuszmaulb35e2342022-03-06 15:44:00 -08004import {BLUE, BROWN, CYAN, GREEN, PINK, RED, WHITE} from 'org_frc971/aos/network/www/colors';
Milind Upadhyayd836bce2022-03-25 21:49:57 -07005import {Connection} from 'org_frc971/aos/network/www/proxy';
6import {Table} from 'org_frc971/aos/network/www/reflection';
James Kuszmaul136aa2b2022-04-02 14:50:56 -07007import {Schema} from 'flatbuffers_reflection/reflection_generated';
Milind Upadhyayd836bce2022-03-25 21:49:57 -07008import {TargetEstimate} from 'org_frc971/y2022/vision/target_estimate_generated';
James Kuszmaulb35e2342022-03-06 15:44:00 -08009
James Kuszmaulb35e2342022-03-06 15:44:00 -080010
11const TIME = AosPlotter.TIME;
Milind Upadhyaye3215862022-03-24 19:59:19 -070012// magenta, yellow, cyan, black
13const PI_COLORS = [[255, 0, 255], [255, 255, 0], [0, 255, 255], [0, 0, 0]];
James Kuszmaulb35e2342022-03-06 15:44:00 -080014
Milind Upadhyayd836bce2022-03-25 21:49:57 -070015class VisionMessageHandler extends MessageHandler {
16 constructor(private readonly schema: Schema) {
17 super(schema);
18 }
19
20 private readScalar(table: Table, fieldName: string): number|BigInt|null {
21 return this.parser.readScalar(table, fieldName);
22 }
23
24 addMessage(data: Uint8Array, time: number): void {
25 const target = TargetEstimate.getRootAsTargetEstimate(new ByteBuffer(data));
26 // Copied from localizer.cc
27 const MIN_TARGET_ESTIMATE_CONFIDENCE = 0.75;
28 // Only add estimates with decent confidences - these are what the localizer
29 // uses
30 if (target.confidence() >= MIN_TARGET_ESTIMATE_CONFIDENCE) {
31 const table = Table.getNamedTable(
32 target.bb, this.schema, 'y2022.vision.TargetEstimate', target.bb_pos);
33 this.messages.push(new TimestampedMessage(table, time));
34 }
35 }
36}
37
James Kuszmaulb35e2342022-03-06 15:44:00 -080038export function plotVision(conn: Connection, element: Element): void {
39 const aosPlotter = new AosPlotter(conn);
40
41 const targets = [];
Milind Upadhyayd836bce2022-03-25 21:49:57 -070042 for (const pi of ['pi1', 'pi2', 'pi3', 'pi4']) {
43 targets.push(aosPlotter.addRawMessageSource(
44 '/' + pi + '/camera', 'y2022.vision.TargetEstimate',
45 new VisionMessageHandler(
46 conn.getSchema('y2022.vision.TargetEstimate'))));
James Kuszmaulb35e2342022-03-06 15:44:00 -080047 }
48 const localizer = aosPlotter.addMessageSource(
49 '/localizer', 'frc971.controls.LocalizerVisualization');
50 const localizerOutput = aosPlotter.addMessageSource(
51 '/localizer', 'frc971.controls.LocalizerOutput');
52 const superstructureStatus = aosPlotter.addMessageSource(
53 '/superstructure', 'y2022.control_loops.superstructure.Status');
54
55 const rejectionPlot = aosPlotter.addPlot(element);
Milind Upadhyayd836bce2022-03-25 21:49:57 -070056 rejectionPlot.plot.getAxisLabels().setTitle('Rejection Reasons');
James Kuszmaulb35e2342022-03-06 15:44:00 -080057 rejectionPlot.plot.getAxisLabels().setXLabel(TIME);
Milind Upadhyayd836bce2022-03-25 21:49:57 -070058 rejectionPlot.plot.getAxisLabels().setYLabel('[bool, enum]');
James Kuszmaulb35e2342022-03-06 15:44:00 -080059
60 rejectionPlot.addMessageLine(localizer, ['targets[]', 'accepted'])
61 .setDrawLine(false)
62 .setColor(BLUE);
63 rejectionPlot.addMessageLine(localizer, ['targets[]', 'rejection_reason'])
64 .setDrawLine(false)
65 .setColor(RED);
66
67 const xPlot = aosPlotter.addPlot(element);
Milind Upadhyayd836bce2022-03-25 21:49:57 -070068 xPlot.plot.getAxisLabels().setTitle('X Position');
James Kuszmaulb35e2342022-03-06 15:44:00 -080069 xPlot.plot.getAxisLabels().setXLabel(TIME);
Milind Upadhyayd836bce2022-03-25 21:49:57 -070070 xPlot.plot.getAxisLabels().setYLabel('[m]');
James Kuszmaulb35e2342022-03-06 15:44:00 -080071
72 xPlot.addMessageLine(localizer, ['targets[]', 'implied_robot_x'])
73 .setDrawLine(false)
74 .setColor(RED);
75 xPlot.addMessageLine(localizerOutput, ['x'])
76 .setDrawLine(false)
77 .setColor(BLUE);
78
79 const yPlot = aosPlotter.addPlot(element);
Milind Upadhyayd836bce2022-03-25 21:49:57 -070080 yPlot.plot.getAxisLabels().setTitle('Y Position');
James Kuszmaulb35e2342022-03-06 15:44:00 -080081 yPlot.plot.getAxisLabels().setXLabel(TIME);
Milind Upadhyayd836bce2022-03-25 21:49:57 -070082 yPlot.plot.getAxisLabels().setYLabel('[m]');
James Kuszmaulb35e2342022-03-06 15:44:00 -080083
84 yPlot.addMessageLine(localizer, ['targets[]', 'implied_robot_y'])
85 .setDrawLine(false)
86 .setColor(RED);
87 yPlot.addMessageLine(localizerOutput, ['y'])
88 .setDrawLine(false)
89 .setColor(BLUE);
90
91 const turretPlot = aosPlotter.addPlot(element);
Milind Upadhyayd836bce2022-03-25 21:49:57 -070092 turretPlot.plot.getAxisLabels().setTitle('Turret Position');
James Kuszmaulb35e2342022-03-06 15:44:00 -080093 turretPlot.plot.getAxisLabels().setXLabel(TIME);
Milind Upadhyayd836bce2022-03-25 21:49:57 -070094 turretPlot.plot.getAxisLabels().setYLabel('[rad]');
James Kuszmaulb35e2342022-03-06 15:44:00 -080095
96 turretPlot.addMessageLine(localizer, ['targets[]', 'implied_turret_goal'])
97 .setDrawLine(false)
98 .setColor(RED);
99 turretPlot.addMessageLine(superstructureStatus, ['turret', 'position'])
100 .setPointSize(0.0)
101 .setColor(BLUE);
Milind Upadhyayd836bce2022-03-25 21:49:57 -0700102 turretPlot.addMessageLine(superstructureStatus, ['aimer', 'turret_position'])
James Kuszmaulb35e2342022-03-06 15:44:00 -0800103 .setPointSize(0.0)
104 .setColor(GREEN);
105
106 const anglePlot = aosPlotter.addPlot(element);
Milind Upadhyayd836bce2022-03-25 21:49:57 -0700107 anglePlot.plot.getAxisLabels().setTitle('TargetEstimate Angle');
James Kuszmaulb35e2342022-03-06 15:44:00 -0800108 anglePlot.plot.getAxisLabels().setXLabel(TIME);
Milind Upadhyayd836bce2022-03-25 21:49:57 -0700109 anglePlot.plot.getAxisLabels().setYLabel('[rad]');
James Kuszmaulb35e2342022-03-06 15:44:00 -0800110
111 for (let ii = 0; ii < targets.length; ++ii) {
112 anglePlot.addMessageLine(targets[ii], ['angle_to_target'])
113 .setDrawLine(false)
114 .setColor(PI_COLORS[ii])
Milind Upadhyayd836bce2022-03-25 21:49:57 -0700115 .setLabel('pi' + (ii + 1));
James Kuszmaulb35e2342022-03-06 15:44:00 -0800116 }
117
118 const distancePlot = aosPlotter.addPlot(element);
Milind Upadhyayd836bce2022-03-25 21:49:57 -0700119 distancePlot.plot.getAxisLabels().setTitle('TargetEstimate Distance');
James Kuszmaulb35e2342022-03-06 15:44:00 -0800120 distancePlot.plot.getAxisLabels().setXLabel(TIME);
Milind Upadhyayd836bce2022-03-25 21:49:57 -0700121 distancePlot.plot.getAxisLabels().setYLabel('[m]');
James Kuszmaulb35e2342022-03-06 15:44:00 -0800122
123 for (let ii = 0; ii < targets.length; ++ii) {
124 distancePlot.addMessageLine(targets[ii], ['distance'])
125 .setDrawLine(false)
126 .setColor(PI_COLORS[ii])
Milind Upadhyayd836bce2022-03-25 21:49:57 -0700127 .setLabel('pi' + (ii + 1));
James Kuszmaulb35e2342022-03-06 15:44:00 -0800128 }
129
130 const confidencePlot = aosPlotter.addPlot(element);
Milind Upadhyayd836bce2022-03-25 21:49:57 -0700131 confidencePlot.plot.getAxisLabels().setTitle('TargetEstimate Confidence');
James Kuszmaulb35e2342022-03-06 15:44:00 -0800132 confidencePlot.plot.getAxisLabels().setXLabel(TIME);
Milind Upadhyayd836bce2022-03-25 21:49:57 -0700133 confidencePlot.plot.getAxisLabels().setYLabel('[0-1]');
James Kuszmaulb35e2342022-03-06 15:44:00 -0800134
135 for (let ii = 0; ii < targets.length; ++ii) {
136 confidencePlot.addMessageLine(targets[ii], ['confidence'])
137 .setDrawLine(false)
138 .setColor(PI_COLORS[ii])
Milind Upadhyayd836bce2022-03-25 21:49:57 -0700139 .setLabel('pi' + (ii + 1));
James Kuszmaulb35e2342022-03-06 15:44:00 -0800140 }
141}