Milind Upadhyay | d836bce | 2022-03-25 21:49:57 -0700 | [diff] [blame] | 1 | import {ByteBuffer} from 'flatbuffers'; |
James Kuszmaul | b35e234 | 2022-03-06 15:44:00 -0800 | [diff] [blame] | 2 | import {AosPlotter} from 'org_frc971/aos/network/www/aos_plotter'; |
Milind Upadhyay | d836bce | 2022-03-25 21:49:57 -0700 | [diff] [blame] | 3 | import {MessageHandler, TimestampedMessage} from 'org_frc971/aos/network/www/aos_plotter'; |
James Kuszmaul | b35e234 | 2022-03-06 15:44:00 -0800 | [diff] [blame] | 4 | import {BLUE, BROWN, CYAN, GREEN, PINK, RED, WHITE} from 'org_frc971/aos/network/www/colors'; |
Milind Upadhyay | d836bce | 2022-03-25 21:49:57 -0700 | [diff] [blame] | 5 | import {Connection} from 'org_frc971/aos/network/www/proxy'; |
| 6 | import {Table} from 'org_frc971/aos/network/www/reflection'; |
| 7 | import {Schema} from 'org_frc971/external/com_github_google_flatbuffers/reflection/reflection_generated'; |
| 8 | import {TargetEstimate} from 'org_frc971/y2022/vision/target_estimate_generated'; |
James Kuszmaul | b35e234 | 2022-03-06 15:44:00 -0800 | [diff] [blame] | 9 | |
James Kuszmaul | b35e234 | 2022-03-06 15:44:00 -0800 | [diff] [blame] | 10 | |
| 11 | const TIME = AosPlotter.TIME; |
Milind Upadhyay | e321586 | 2022-03-24 19:59:19 -0700 | [diff] [blame] | 12 | // magenta, yellow, cyan, black |
| 13 | const PI_COLORS = [[255, 0, 255], [255, 255, 0], [0, 255, 255], [0, 0, 0]]; |
James Kuszmaul | b35e234 | 2022-03-06 15:44:00 -0800 | [diff] [blame] | 14 | |
Milind Upadhyay | d836bce | 2022-03-25 21:49:57 -0700 | [diff] [blame] | 15 | class 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 Kuszmaul | b35e234 | 2022-03-06 15:44:00 -0800 | [diff] [blame] | 38 | export function plotVision(conn: Connection, element: Element): void { |
| 39 | const aosPlotter = new AosPlotter(conn); |
| 40 | |
| 41 | const targets = []; |
Milind Upadhyay | d836bce | 2022-03-25 21:49:57 -0700 | [diff] [blame] | 42 | 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 Kuszmaul | b35e234 | 2022-03-06 15:44:00 -0800 | [diff] [blame] | 47 | } |
| 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 Upadhyay | d836bce | 2022-03-25 21:49:57 -0700 | [diff] [blame] | 56 | rejectionPlot.plot.getAxisLabels().setTitle('Rejection Reasons'); |
James Kuszmaul | b35e234 | 2022-03-06 15:44:00 -0800 | [diff] [blame] | 57 | rejectionPlot.plot.getAxisLabels().setXLabel(TIME); |
Milind Upadhyay | d836bce | 2022-03-25 21:49:57 -0700 | [diff] [blame] | 58 | rejectionPlot.plot.getAxisLabels().setYLabel('[bool, enum]'); |
James Kuszmaul | b35e234 | 2022-03-06 15:44:00 -0800 | [diff] [blame] | 59 | |
| 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 Upadhyay | d836bce | 2022-03-25 21:49:57 -0700 | [diff] [blame] | 68 | xPlot.plot.getAxisLabels().setTitle('X Position'); |
James Kuszmaul | b35e234 | 2022-03-06 15:44:00 -0800 | [diff] [blame] | 69 | xPlot.plot.getAxisLabels().setXLabel(TIME); |
Milind Upadhyay | d836bce | 2022-03-25 21:49:57 -0700 | [diff] [blame] | 70 | xPlot.plot.getAxisLabels().setYLabel('[m]'); |
James Kuszmaul | b35e234 | 2022-03-06 15:44:00 -0800 | [diff] [blame] | 71 | |
| 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 Upadhyay | d836bce | 2022-03-25 21:49:57 -0700 | [diff] [blame] | 80 | yPlot.plot.getAxisLabels().setTitle('Y Position'); |
James Kuszmaul | b35e234 | 2022-03-06 15:44:00 -0800 | [diff] [blame] | 81 | yPlot.plot.getAxisLabels().setXLabel(TIME); |
Milind Upadhyay | d836bce | 2022-03-25 21:49:57 -0700 | [diff] [blame] | 82 | yPlot.plot.getAxisLabels().setYLabel('[m]'); |
James Kuszmaul | b35e234 | 2022-03-06 15:44:00 -0800 | [diff] [blame] | 83 | |
| 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 Upadhyay | d836bce | 2022-03-25 21:49:57 -0700 | [diff] [blame] | 92 | turretPlot.plot.getAxisLabels().setTitle('Turret Position'); |
James Kuszmaul | b35e234 | 2022-03-06 15:44:00 -0800 | [diff] [blame] | 93 | turretPlot.plot.getAxisLabels().setXLabel(TIME); |
Milind Upadhyay | d836bce | 2022-03-25 21:49:57 -0700 | [diff] [blame] | 94 | turretPlot.plot.getAxisLabels().setYLabel('[rad]'); |
James Kuszmaul | b35e234 | 2022-03-06 15:44:00 -0800 | [diff] [blame] | 95 | |
| 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 Upadhyay | d836bce | 2022-03-25 21:49:57 -0700 | [diff] [blame] | 102 | turretPlot.addMessageLine(superstructureStatus, ['aimer', 'turret_position']) |
James Kuszmaul | b35e234 | 2022-03-06 15:44:00 -0800 | [diff] [blame] | 103 | .setPointSize(0.0) |
| 104 | .setColor(GREEN); |
| 105 | |
| 106 | const anglePlot = aosPlotter.addPlot(element); |
Milind Upadhyay | d836bce | 2022-03-25 21:49:57 -0700 | [diff] [blame] | 107 | anglePlot.plot.getAxisLabels().setTitle('TargetEstimate Angle'); |
James Kuszmaul | b35e234 | 2022-03-06 15:44:00 -0800 | [diff] [blame] | 108 | anglePlot.plot.getAxisLabels().setXLabel(TIME); |
Milind Upadhyay | d836bce | 2022-03-25 21:49:57 -0700 | [diff] [blame] | 109 | anglePlot.plot.getAxisLabels().setYLabel('[rad]'); |
James Kuszmaul | b35e234 | 2022-03-06 15:44:00 -0800 | [diff] [blame] | 110 | |
| 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 Upadhyay | d836bce | 2022-03-25 21:49:57 -0700 | [diff] [blame] | 115 | .setLabel('pi' + (ii + 1)); |
James Kuszmaul | b35e234 | 2022-03-06 15:44:00 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | const distancePlot = aosPlotter.addPlot(element); |
Milind Upadhyay | d836bce | 2022-03-25 21:49:57 -0700 | [diff] [blame] | 119 | distancePlot.plot.getAxisLabels().setTitle('TargetEstimate Distance'); |
James Kuszmaul | b35e234 | 2022-03-06 15:44:00 -0800 | [diff] [blame] | 120 | distancePlot.plot.getAxisLabels().setXLabel(TIME); |
Milind Upadhyay | d836bce | 2022-03-25 21:49:57 -0700 | [diff] [blame] | 121 | distancePlot.plot.getAxisLabels().setYLabel('[m]'); |
James Kuszmaul | b35e234 | 2022-03-06 15:44:00 -0800 | [diff] [blame] | 122 | |
| 123 | for (let ii = 0; ii < targets.length; ++ii) { |
| 124 | distancePlot.addMessageLine(targets[ii], ['distance']) |
| 125 | .setDrawLine(false) |
| 126 | .setColor(PI_COLORS[ii]) |
Milind Upadhyay | d836bce | 2022-03-25 21:49:57 -0700 | [diff] [blame] | 127 | .setLabel('pi' + (ii + 1)); |
James Kuszmaul | b35e234 | 2022-03-06 15:44:00 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | const confidencePlot = aosPlotter.addPlot(element); |
Milind Upadhyay | d836bce | 2022-03-25 21:49:57 -0700 | [diff] [blame] | 131 | confidencePlot.plot.getAxisLabels().setTitle('TargetEstimate Confidence'); |
James Kuszmaul | b35e234 | 2022-03-06 15:44:00 -0800 | [diff] [blame] | 132 | confidencePlot.plot.getAxisLabels().setXLabel(TIME); |
Milind Upadhyay | d836bce | 2022-03-25 21:49:57 -0700 | [diff] [blame] | 133 | confidencePlot.plot.getAxisLabels().setYLabel('[0-1]'); |
James Kuszmaul | b35e234 | 2022-03-06 15:44:00 -0800 | [diff] [blame] | 134 | |
| 135 | for (let ii = 0; ii < targets.length; ++ii) { |
| 136 | confidencePlot.addMessageLine(targets[ii], ['confidence']) |
| 137 | .setDrawLine(false) |
| 138 | .setColor(PI_COLORS[ii]) |
Milind Upadhyay | d836bce | 2022-03-25 21:49:57 -0700 | [diff] [blame] | 139 | .setLabel('pi' + (ii + 1)); |
James Kuszmaul | b35e234 | 2022-03-06 15:44:00 -0800 | [diff] [blame] | 140 | } |
| 141 | } |