Maxwell Henderson | c3d063a | 2023-12-26 17:37:22 -0800 | [diff] [blame^] | 1 | import {ByteBuffer} from 'flatbuffers' |
| 2 | import {ClientStatistics} from '../../aos/network/message_bridge_client_generated' |
| 3 | import {ServerStatistics, State as ConnectionState} from '../../aos/network/message_bridge_server_generated' |
| 4 | import {Connection} from '../../aos/network/www/proxy' |
| 5 | import {ZeroingError} from '../../frc971/control_loops/control_loops_generated' |
| 6 | import {Status as DrivetrainStatus} from '../../frc971/control_loops/drivetrain/drivetrain_status_generated' |
| 7 | import {LocalizerOutput} from '../../frc971/control_loops/drivetrain/localization/localizer_output_generated' |
| 8 | |
| 9 | import {FIELD_LENGTH, FIELD_WIDTH, FT_TO_M, IN_TO_M} from './constants'; |
| 10 | |
| 11 | // (0,0) is field center, +X is toward red DS |
| 12 | const FIELD_SIDE_Y = FIELD_WIDTH / 2; |
| 13 | const FIELD_EDGE_X = FIELD_LENGTH / 2; |
| 14 | |
| 15 | const ROBOT_WIDTH = 25 * IN_TO_M; |
| 16 | const ROBOT_LENGTH = 32 * IN_TO_M; |
| 17 | |
| 18 | const PI_COLORS = ['#ff00ff', '#ffff00', '#00ffff', '#ffa500']; |
| 19 | const PIS = ['pi1', 'pi2', 'pi3', 'pi4']; |
| 20 | |
| 21 | export class FieldHandler { |
| 22 | private canvas = document.createElement('canvas'); |
| 23 | private localizerOutput: LocalizerOutput|null = null; |
| 24 | private drivetrainStatus: DrivetrainStatus|null = null; |
| 25 | |
| 26 | private x: HTMLElement = (document.getElementById('x') as HTMLElement); |
| 27 | private y: HTMLElement = (document.getElementById('y') as HTMLElement); |
| 28 | private theta: HTMLElement = |
| 29 | (document.getElementById('theta') as HTMLElement); |
| 30 | // HTML elements for rejection reasons for individual pis. Indices |
| 31 | // corresponding to RejectionReason enum values will be for those reasons. The |
| 32 | // final row will account for images rejected by the aprilrobotics detector |
| 33 | // instead of the localizer. |
| 34 | private messageBridgeDiv: HTMLElement = |
| 35 | (document.getElementById('message_bridge_status') as HTMLElement); |
| 36 | private clientStatuses = new Map<string, HTMLElement>(); |
| 37 | private serverStatuses = new Map<string, HTMLElement>(); |
| 38 | private fieldImage: HTMLImageElement = new Image(); |
| 39 | |
| 40 | constructor(private readonly connection: Connection) { |
| 41 | (document.getElementById('field') as HTMLElement).appendChild(this.canvas); |
| 42 | |
| 43 | this.fieldImage.src = '2023.png'; |
| 44 | |
| 45 | this.connection.addConfigHandler(() => { |
| 46 | // Visualization message is reliable so that we can see *all* the vision |
| 47 | // matches. |
| 48 | this.connection.addHandler( |
| 49 | '/drivetrain', 'frc971.control_loops.drivetrain.Status', (data) => { |
| 50 | this.handleDrivetrainStatus(data); |
| 51 | }); |
| 52 | this.connection.addHandler( |
| 53 | '/localizer', 'frc971.controls.LocalizerOutput', (data) => { |
| 54 | this.handleLocalizerOutput(data); |
| 55 | }); |
| 56 | this.connection.addHandler( |
| 57 | '/aos', 'aos.message_bridge.ServerStatistics', |
| 58 | (data) => {this.handleServerStatistics(data)}); |
| 59 | this.connection.addHandler( |
| 60 | '/aos', 'aos.message_bridge.ClientStatistics', |
| 61 | (data) => {this.handleClientStatistics(data)}); |
| 62 | }); |
| 63 | } |
| 64 | |
| 65 | private handleLocalizerOutput(data: Uint8Array): void { |
| 66 | const fbBuffer = new ByteBuffer(data); |
| 67 | this.localizerOutput = LocalizerOutput.getRootAsLocalizerOutput(fbBuffer); |
| 68 | } |
| 69 | |
| 70 | private handleDrivetrainStatus(data: Uint8Array): void { |
| 71 | const fbBuffer = new ByteBuffer(data); |
| 72 | this.drivetrainStatus = DrivetrainStatus.getRootAsStatus(fbBuffer); |
| 73 | } |
| 74 | |
| 75 | private populateNodeConnections(nodeName: string): void { |
| 76 | const row = document.createElement('div'); |
| 77 | this.messageBridgeDiv.appendChild(row); |
| 78 | const nodeDiv = document.createElement('div'); |
| 79 | nodeDiv.innerHTML = nodeName; |
| 80 | row.appendChild(nodeDiv); |
| 81 | const clientDiv = document.createElement('div'); |
| 82 | clientDiv.innerHTML = 'N/A'; |
| 83 | row.appendChild(clientDiv); |
| 84 | const serverDiv = document.createElement('div'); |
| 85 | serverDiv.innerHTML = 'N/A'; |
| 86 | row.appendChild(serverDiv); |
| 87 | this.serverStatuses.set(nodeName, serverDiv); |
| 88 | this.clientStatuses.set(nodeName, clientDiv); |
| 89 | } |
| 90 | |
| 91 | private setCurrentNodeState(element: HTMLElement, state: ConnectionState): |
| 92 | void { |
| 93 | if (state === ConnectionState.CONNECTED) { |
| 94 | element.innerHTML = ConnectionState[state]; |
| 95 | element.classList.remove('faulted'); |
| 96 | element.classList.add('connected'); |
| 97 | } else { |
| 98 | element.innerHTML = ConnectionState[state]; |
| 99 | element.classList.remove('connected'); |
| 100 | element.classList.add('faulted'); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | private handleServerStatistics(data: Uint8Array): void { |
| 105 | const fbBuffer = new ByteBuffer(data); |
| 106 | const serverStatistics = |
| 107 | ServerStatistics.getRootAsServerStatistics(fbBuffer); |
| 108 | |
| 109 | for (let ii = 0; ii < serverStatistics.connectionsLength(); ++ii) { |
| 110 | const connection = serverStatistics.connections(ii); |
| 111 | const nodeName = connection.node().name(); |
| 112 | if (!this.serverStatuses.has(nodeName)) { |
| 113 | this.populateNodeConnections(nodeName); |
| 114 | } |
| 115 | this.setCurrentNodeState( |
| 116 | this.serverStatuses.get(nodeName), connection.state()); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | private handleClientStatistics(data: Uint8Array): void { |
| 121 | const fbBuffer = new ByteBuffer(data); |
| 122 | const clientStatistics = |
| 123 | ClientStatistics.getRootAsClientStatistics(fbBuffer); |
| 124 | |
| 125 | for (let ii = 0; ii < clientStatistics.connectionsLength(); ++ii) { |
| 126 | const connection = clientStatistics.connections(ii); |
| 127 | const nodeName = connection.node().name(); |
| 128 | if (!this.clientStatuses.has(nodeName)) { |
| 129 | this.populateNodeConnections(nodeName); |
| 130 | } |
| 131 | this.setCurrentNodeState( |
| 132 | this.clientStatuses.get(nodeName), connection.state()); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | setZeroing(div: HTMLElement): void { |
| 137 | div.innerHTML = 'zeroing'; |
| 138 | div.classList.remove('faulted'); |
| 139 | div.classList.add('zeroing'); |
| 140 | div.classList.remove('near'); |
| 141 | } |
| 142 | |
| 143 | setValue(div: HTMLElement, val: number): void { |
| 144 | div.innerHTML = val.toFixed(4); |
| 145 | div.classList.remove('faulted'); |
| 146 | div.classList.remove('zeroing'); |
| 147 | div.classList.remove('near'); |
| 148 | } |
| 149 | |
| 150 | drawField(): void { |
| 151 | const ctx = this.canvas.getContext('2d'); |
| 152 | ctx.save(); |
| 153 | ctx.scale(1.0, -1.0); |
| 154 | ctx.drawImage( |
| 155 | this.fieldImage, 0, 0, this.fieldImage.width, this.fieldImage.height, |
| 156 | -FIELD_EDGE_X, -FIELD_SIDE_Y, FIELD_LENGTH, FIELD_WIDTH); |
| 157 | ctx.restore(); |
| 158 | } |
| 159 | |
| 160 | drawCamera(x: number, y: number, theta: number, color: string = 'blue'): |
| 161 | void { |
| 162 | const ctx = this.canvas.getContext('2d'); |
| 163 | ctx.save(); |
| 164 | ctx.translate(x, y); |
| 165 | ctx.rotate(theta); |
| 166 | ctx.strokeStyle = color; |
| 167 | ctx.beginPath(); |
| 168 | ctx.moveTo(0.5, 0.5); |
| 169 | ctx.lineTo(0, 0); |
| 170 | ctx.lineTo(0.5, -0.5); |
| 171 | ctx.stroke(); |
| 172 | ctx.beginPath(); |
| 173 | ctx.arc(0, 0, 0.25, -Math.PI / 4, Math.PI / 4); |
| 174 | ctx.stroke(); |
| 175 | ctx.restore(); |
| 176 | } |
| 177 | |
| 178 | drawRobot( |
| 179 | x: number, y: number, theta: number, color: string = 'blue', |
| 180 | dashed: boolean = false): void { |
| 181 | const ctx = this.canvas.getContext('2d'); |
| 182 | ctx.save(); |
| 183 | ctx.translate(x, y); |
| 184 | ctx.rotate(theta); |
| 185 | ctx.strokeStyle = color; |
| 186 | ctx.lineWidth = ROBOT_WIDTH / 10.0; |
| 187 | if (dashed) { |
| 188 | ctx.setLineDash([0.05, 0.05]); |
| 189 | } else { |
| 190 | // Empty array = solid line. |
| 191 | ctx.setLineDash([]); |
| 192 | } |
| 193 | ctx.rect(-ROBOT_LENGTH / 2, -ROBOT_WIDTH / 2, ROBOT_LENGTH, ROBOT_WIDTH); |
| 194 | ctx.stroke(); |
| 195 | |
| 196 | // Draw line indicating which direction is forwards on the robot. |
| 197 | ctx.beginPath(); |
| 198 | ctx.moveTo(0, 0); |
| 199 | ctx.lineTo(ROBOT_LENGTH / 2.0, 0); |
| 200 | ctx.stroke(); |
| 201 | |
| 202 | ctx.restore(); |
| 203 | } |
| 204 | |
| 205 | draw(): void { |
| 206 | this.reset(); |
| 207 | this.drawField(); |
| 208 | |
| 209 | // Draw the matches with debugging information from the localizer. |
| 210 | const now = Date.now() / 1000.0; |
| 211 | |
| 212 | if (this.drivetrainStatus && this.drivetrainStatus.trajectoryLogging()) { |
| 213 | this.drawRobot( |
| 214 | this.drivetrainStatus.trajectoryLogging().x(), |
| 215 | this.drivetrainStatus.trajectoryLogging().y(), |
| 216 | this.drivetrainStatus.trajectoryLogging().theta(), '#000000FF', |
| 217 | false); |
| 218 | } |
| 219 | |
| 220 | if (this.localizerOutput) { |
| 221 | if (!this.localizerOutput.zeroed()) { |
| 222 | this.setZeroing(this.x); |
| 223 | this.setZeroing(this.y); |
| 224 | this.setZeroing(this.theta); |
| 225 | } else { |
| 226 | this.setValue(this.x, this.localizerOutput.x()); |
| 227 | this.setValue(this.y, this.localizerOutput.y()); |
| 228 | this.setValue(this.theta, this.localizerOutput.theta()); |
| 229 | } |
| 230 | |
| 231 | this.drawRobot( |
| 232 | this.localizerOutput.x(), this.localizerOutput.y(), |
| 233 | this.localizerOutput.theta()); |
| 234 | } |
| 235 | |
| 236 | |
| 237 | window.requestAnimationFrame(() => this.draw()); |
| 238 | } |
| 239 | |
| 240 | reset(): void { |
| 241 | const ctx = this.canvas.getContext('2d'); |
| 242 | ctx.setTransform(1, 0, 0, 1, 0, 0); |
| 243 | const size = window.innerHeight * 0.9; |
| 244 | ctx.canvas.height = size; |
| 245 | const width = size / 2 + 20; |
| 246 | ctx.canvas.width = width; |
| 247 | ctx.clearRect(0, 0, size, width); |
| 248 | |
| 249 | // Translate to center of display. |
| 250 | ctx.translate(width / 2, size / 2); |
| 251 | // Coordinate system is: |
| 252 | // x -> forward. |
| 253 | // y -> to the left. |
| 254 | ctx.rotate(-Math.PI / 2); |
| 255 | ctx.scale(1, -1); |
| 256 | |
| 257 | const M_TO_PX = (size - 10) / FIELD_LENGTH; |
| 258 | ctx.scale(M_TO_PX, M_TO_PX); |
| 259 | ctx.lineWidth = 1 / M_TO_PX; |
| 260 | } |
| 261 | } |