James (Peilun) Li | a70e575 | 2024-09-18 20:43:00 -0700 | [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 {Position as DrivetrainPosition} from '../../frc971/control_loops/drivetrain/drivetrain_position_generated' |
| 7 | import {CANPosition as DrivetrainCANPosition} from '../../frc971/control_loops/drivetrain/drivetrain_can_position_generated' |
| 8 | import {Status as DrivetrainStatus} from '../../frc971/control_loops/drivetrain/drivetrain_status_generated' |
| 9 | import {Position as SuperstructurePosition} from '../control_loops/superstructure/superstructure_position_generated' |
| 10 | import {Status as SuperstructureStatus} from '../control_loops/superstructure/superstructure_status_generated' |
| 11 | import {TargetMap} from '../../frc971/vision/target_map_generated' |
| 12 | |
| 13 | |
| 14 | import {FIELD_LENGTH, FIELD_WIDTH, FT_TO_M, IN_TO_M} from './constants'; |
| 15 | |
| 16 | // (0,0) is field center, +X is toward red DS |
| 17 | const FIELD_SIDE_Y = FIELD_WIDTH / 2; |
| 18 | const FIELD_EDGE_X = FIELD_LENGTH / 2; |
| 19 | |
| 20 | const ROBOT_WIDTH = 29 * IN_TO_M; |
| 21 | const ROBOT_LENGTH = 32 * IN_TO_M; |
| 22 | |
| 23 | const CAMERA_COLORS = ['#ff00ff', '#ffff00', '#00ffff', '#ffa500']; |
| 24 | const CAMERAS = ['/orin1/camera0', '/orin1/camera1', '/imu/camera0', '/imu/camera1']; |
| 25 | |
| 26 | export class FieldHandler { |
| 27 | private canvas = document.createElement('canvas'); |
| 28 | private superstructureStatus: SuperstructureStatus|null = null; |
| 29 | private superstructurePosition: SuperstructurePosition|null = null; |
| 30 | |
| 31 | // Image information indexed by timestamp (seconds since the epoch), so that |
| 32 | // we can stop displaying images after a certain amount of time. |
| 33 | private x: HTMLElement = (document.getElementById('x') as HTMLElement); |
| 34 | private y: HTMLElement = (document.getElementById('y') as HTMLElement); |
| 35 | private theta: HTMLElement = |
| 36 | (document.getElementById('theta') as HTMLElement); |
| 37 | |
| 38 | private imagesAcceptedCounter: HTMLElement = |
| 39 | (document.getElementById('images_accepted') as HTMLElement); |
| 40 | // HTML elements for rejection reasons for individual cameras. Indices |
| 41 | // corresponding to RejectionReason enum values will be for those reasons. The |
| 42 | // final row will account for images rejected by the aprilrobotics detector |
| 43 | // instead of the localizer. |
| 44 | private rejectionReasonCells: HTMLElement[][] = []; |
| 45 | private messageBridgeDiv: HTMLElement = |
| 46 | (document.getElementById('message_bridge_status') as HTMLElement); |
| 47 | private clientStatuses = new Map<string, HTMLElement>(); |
| 48 | private serverStatuses = new Map<string, HTMLElement>(); |
| 49 | |
| 50 | private fieldImage: HTMLImageElement = new Image(); |
| 51 | |
| 52 | private zeroingFaults: HTMLElement = |
| 53 | (document.getElementById('zeroing_faults') as HTMLElement); |
| 54 | |
| 55 | private leftDrivetrainEncoder: HTMLElement = |
| 56 | (document.getElementById('left_drivetrain_encoder') as HTMLElement); |
| 57 | private rightDrivetrainEncoder: HTMLElement = |
| 58 | (document.getElementById('right_drivetrain_encoder') as HTMLElement); |
| 59 | private falconRightFrontPosition: HTMLElement = |
| 60 | (document.getElementById('falcon_right_front') as HTMLElement); |
| 61 | private falconRightBackPosition: HTMLElement = |
| 62 | (document.getElementById('falcon_right_back') as HTMLElement); |
| 63 | private falconLeftFrontPosition: HTMLElement = |
| 64 | (document.getElementById('falcon_left_front') as HTMLElement); |
| 65 | private falconLeftBackPosition: HTMLElement = |
| 66 | (document.getElementById('falcon_left_back') as HTMLElement); |
| 67 | |
| 68 | constructor(private readonly connection: Connection) { |
| 69 | (document.getElementById('field') as HTMLElement).appendChild(this.canvas); |
| 70 | |
| 71 | this.fieldImage.src = '2024.png'; |
| 72 | |
| 73 | // Construct a table header. |
| 74 | { |
| 75 | const row = document.createElement('div'); |
| 76 | const nameCell = document.createElement('div'); |
| 77 | nameCell.innerHTML = 'Rejection Reason'; |
| 78 | row.appendChild(nameCell); |
| 79 | for (const camera of CAMERAS) { |
| 80 | const nodeCell = document.createElement('div'); |
| 81 | nodeCell.innerHTML = camera; |
| 82 | row.appendChild(nodeCell); |
| 83 | } |
| 84 | document.getElementById('vision_readouts').appendChild(row); |
| 85 | } |
| 86 | |
| 87 | // Add rejection reason row for aprilrobotics rejections. |
| 88 | { |
| 89 | const row = document.createElement('div'); |
| 90 | const nameCell = document.createElement('div'); |
| 91 | nameCell.innerHTML = 'Rejected by aprilrobotics'; |
| 92 | row.appendChild(nameCell); |
| 93 | this.rejectionReasonCells.push([]); |
| 94 | for (const camera of CAMERAS) { |
| 95 | const valueCell = document.createElement('div'); |
| 96 | valueCell.innerHTML = 'NA'; |
| 97 | this.rejectionReasonCells[this.rejectionReasonCells.length - 1].push( |
| 98 | valueCell); |
| 99 | row.appendChild(valueCell); |
| 100 | } |
| 101 | document.getElementById('vision_readouts').appendChild(row); |
| 102 | } |
| 103 | |
| 104 | for (let ii = 0; ii < CAMERA_COLORS.length; ++ii) { |
| 105 | const legendEntry = document.createElement('div'); |
| 106 | legendEntry.style.color = CAMERA_COLORS[ii]; |
| 107 | legendEntry.innerHTML = CAMERAS[ii]; |
| 108 | document.getElementById('legend').appendChild(legendEntry); |
| 109 | } |
| 110 | |
| 111 | this.connection.addConfigHandler(() => { |
| 112 | // Visualization message is reliable so that we can see *all* the vision |
| 113 | // matches. |
| 114 | |
| 115 | for (const camera in CAMERAS) { |
| 116 | // Make unreliable to reduce network spam. |
| 117 | this.connection.addHandler( |
| 118 | CAMERAS[camera], 'frc971.vision.TargetMap', (data) => { |
| 119 | this.handleCameraTargetMap(camera, data); |
| 120 | }); |
| 121 | } |
| 122 | |
| 123 | this.connection.addHandler( |
| 124 | '/superstructure', "y2024_bot3.control_loops.superstructure.Status", |
| 125 | (data) => { |
| 126 | this.handleSuperstructureStatus(data) |
| 127 | }); |
| 128 | this.connection.addHandler( |
| 129 | '/superstructure', "y2024_bot3.control_loops.superstructure.Positon", |
| 130 | (data) => { |
| 131 | this.handleSuperstructurePosition(data) |
| 132 | }); |
| 133 | this.connection.addHandler( |
| 134 | '/aos', 'aos.message_bridge.ServerStatistics', |
| 135 | (data) => {this.handleServerStatistics(data)}); |
| 136 | this.connection.addHandler( |
| 137 | '/aos', 'aos.message_bridge.ClientStatistics', |
| 138 | (data) => {this.handleClientStatistics(data)}); |
| 139 | }); |
| 140 | } |
| 141 | private handleCameraTargetMap(pi: string, data: Uint8Array): void { |
| 142 | const fbBuffer = new ByteBuffer(data); |
| 143 | const targetMap = TargetMap.getRootAsTargetMap(fbBuffer); |
| 144 | this.rejectionReasonCells[this.rejectionReasonCells.length - 1][pi] |
| 145 | .innerHTML = targetMap.rejections().toString(); |
| 146 | } |
| 147 | |
| 148 | private handleSuperstructureStatus(data: Uint8Array): void { |
| 149 | const fbBuffer = new ByteBuffer(data); |
| 150 | this.superstructureStatus = SuperstructureStatus.getRootAsStatus(fbBuffer); |
| 151 | } |
| 152 | |
| 153 | private handleSuperstructurePosition(data: Uint8Array): void { |
| 154 | const fbBuffer = new ByteBuffer(data); |
| 155 | this.superstructurePosition = SuperstructurePosition.getRootAsPosition(fbBuffer); |
| 156 | } |
| 157 | |
| 158 | private populateNodeConnections(nodeName: string): void { |
| 159 | const row = document.createElement('div'); |
| 160 | this.messageBridgeDiv.appendChild(row); |
| 161 | const nodeDiv = document.createElement('div'); |
| 162 | nodeDiv.innerHTML = nodeName; |
| 163 | row.appendChild(nodeDiv); |
| 164 | const clientDiv = document.createElement('div'); |
| 165 | clientDiv.innerHTML = 'N/A'; |
| 166 | row.appendChild(clientDiv); |
| 167 | const serverDiv = document.createElement('div'); |
| 168 | serverDiv.innerHTML = 'N/A'; |
| 169 | row.appendChild(serverDiv); |
| 170 | this.serverStatuses.set(nodeName, serverDiv); |
| 171 | this.clientStatuses.set(nodeName, clientDiv); |
| 172 | } |
| 173 | |
| 174 | private setCurrentNodeState(element: HTMLElement, state: ConnectionState): |
| 175 | void { |
| 176 | if (state === ConnectionState.CONNECTED) { |
| 177 | element.innerHTML = ConnectionState[state]; |
| 178 | element.classList.remove('faulted'); |
| 179 | element.classList.add('connected'); |
| 180 | } else { |
| 181 | element.innerHTML = ConnectionState[state]; |
| 182 | element.classList.remove('connected'); |
| 183 | element.classList.add('faulted'); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | private handleServerStatistics(data: Uint8Array): void { |
| 188 | const fbBuffer = new ByteBuffer(data); |
| 189 | const serverStatistics = |
| 190 | ServerStatistics.getRootAsServerStatistics(fbBuffer); |
| 191 | |
| 192 | for (let ii = 0; ii < serverStatistics.connectionsLength(); ++ii) { |
| 193 | const connection = serverStatistics.connections(ii); |
| 194 | const nodeName = connection.node().name(); |
| 195 | if (!this.serverStatuses.has(nodeName)) { |
| 196 | this.populateNodeConnections(nodeName); |
| 197 | } |
| 198 | this.setCurrentNodeState( |
| 199 | this.serverStatuses.get(nodeName), connection.state()); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | private handleClientStatistics(data: Uint8Array): void { |
| 204 | const fbBuffer = new ByteBuffer(data); |
| 205 | const clientStatistics = |
| 206 | ClientStatistics.getRootAsClientStatistics(fbBuffer); |
| 207 | |
| 208 | for (let ii = 0; ii < clientStatistics.connectionsLength(); ++ii) { |
| 209 | const connection = clientStatistics.connections(ii); |
| 210 | const nodeName = connection.node().name(); |
| 211 | if (!this.clientStatuses.has(nodeName)) { |
| 212 | this.populateNodeConnections(nodeName); |
| 213 | } |
| 214 | this.setCurrentNodeState( |
| 215 | this.clientStatuses.get(nodeName), connection.state()); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | drawField(): void { |
| 220 | const ctx = this.canvas.getContext('2d'); |
| 221 | ctx.save(); |
| 222 | ctx.scale(1.0, -1.0); |
| 223 | ctx.drawImage( |
| 224 | this.fieldImage, 0, 0, this.fieldImage.width, this.fieldImage.height, |
| 225 | -FIELD_EDGE_X, -FIELD_SIDE_Y, FIELD_LENGTH, FIELD_WIDTH); |
| 226 | ctx.restore(); |
| 227 | } |
| 228 | |
| 229 | drawCamera(x: number, y: number, theta: number, color: string = 'blue'): |
| 230 | void { |
| 231 | const ctx = this.canvas.getContext('2d'); |
| 232 | ctx.save(); |
| 233 | ctx.translate(x, y); |
| 234 | ctx.rotate(theta); |
| 235 | ctx.strokeStyle = color; |
| 236 | ctx.beginPath(); |
| 237 | ctx.moveTo(0.5, 0.5); |
| 238 | ctx.lineTo(0, 0); |
| 239 | ctx.lineTo(0.5, -0.5); |
| 240 | ctx.stroke(); |
| 241 | ctx.beginPath(); |
| 242 | ctx.arc(0, 0, 0.25, -Math.PI / 4, Math.PI / 4); |
| 243 | ctx.stroke(); |
| 244 | ctx.restore(); |
| 245 | } |
| 246 | |
| 247 | drawRobot( |
| 248 | x: number, y: number, theta: number, color: string = 'blue', |
| 249 | dashed: boolean = false): void { |
| 250 | const ctx = this.canvas.getContext('2d'); |
| 251 | ctx.save(); |
| 252 | ctx.translate(x, y); |
| 253 | ctx.rotate(theta); |
| 254 | ctx.strokeStyle = color; |
| 255 | ctx.lineWidth = ROBOT_WIDTH / 10.0; |
| 256 | if (dashed) { |
| 257 | ctx.setLineDash([0.05, 0.05]); |
| 258 | } else { |
| 259 | // Empty array = solid line. |
| 260 | ctx.setLineDash([]); |
| 261 | } |
| 262 | ctx.rect(-ROBOT_LENGTH / 2, -ROBOT_WIDTH / 2, ROBOT_LENGTH, ROBOT_WIDTH); |
| 263 | ctx.stroke(); |
| 264 | |
| 265 | // Draw line indicating which direction is forwards on the robot. |
| 266 | ctx.beginPath(); |
| 267 | ctx.moveTo(0, 0); |
| 268 | ctx.lineTo(ROBOT_LENGTH / 2.0, 0); |
| 269 | ctx.stroke(); |
| 270 | |
| 271 | ctx.restore(); |
| 272 | } |
| 273 | |
| 274 | setZeroing(div: HTMLElement): void { |
| 275 | div.innerHTML = 'zeroing'; |
| 276 | div.classList.remove('faulted'); |
| 277 | div.classList.add('zeroing'); |
| 278 | div.classList.remove('near'); |
| 279 | } |
| 280 | |
| 281 | setEstopped(div: HTMLElement): void { |
| 282 | div.innerHTML = 'estopped'; |
| 283 | div.classList.add('faulted'); |
| 284 | div.classList.remove('zeroing'); |
| 285 | div.classList.remove('near'); |
| 286 | } |
| 287 | |
| 288 | setTargetValue( |
| 289 | div: HTMLElement, target: number, val: number, tolerance: number): void { |
| 290 | div.innerHTML = val.toFixed(4); |
| 291 | div.classList.remove('faulted'); |
| 292 | div.classList.remove('zeroing'); |
| 293 | if (Math.abs(target - val) < tolerance) { |
| 294 | div.classList.add('near'); |
| 295 | } else { |
| 296 | div.classList.remove('near'); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | setValue(div: HTMLElement, val: number): void { |
| 301 | div.innerHTML = val.toFixed(4); |
| 302 | div.classList.remove('faulted'); |
| 303 | div.classList.remove('zeroing'); |
| 304 | div.classList.remove('near'); |
| 305 | } |
| 306 | |
| 307 | setBoolean(div: HTMLElement, triggered: boolean): void { |
| 308 | div.innerHTML = ((triggered) ? "TRUE" : "FALSE") |
| 309 | div.className = ''; |
| 310 | if (triggered) { |
| 311 | div.classList.add('lightgreen'); |
| 312 | } else { |
| 313 | div.classList.add('lightcoral'); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | draw(): void { |
| 318 | this.reset(); |
| 319 | this.drawField(); |
| 320 | |
| 321 | window.requestAnimationFrame(() => this.draw()); |
| 322 | } |
| 323 | |
| 324 | reset(): void { |
| 325 | const ctx = this.canvas.getContext('2d'); |
| 326 | // Empty space from the canvas boundary to the image |
| 327 | const IMAGE_PADDING = 10; |
| 328 | ctx.setTransform(1, 0, 0, 1, 0, 0); |
| 329 | const size = window.innerHeight * 0.9; |
| 330 | ctx.canvas.height = size; |
| 331 | const width = size / 2 + 20; |
| 332 | ctx.canvas.width = width; |
| 333 | ctx.clearRect(0, 0, size, width); |
| 334 | |
| 335 | // Translate to center of display. |
| 336 | ctx.translate(width / 2, size / 2); |
| 337 | // Coordinate system is: |
| 338 | // x -> forward. |
| 339 | // y -> to the left. |
| 340 | ctx.rotate(-Math.PI / 2); |
| 341 | ctx.scale(1, -1); |
| 342 | |
| 343 | const M_TO_PX = (size - IMAGE_PADDING) / FIELD_LENGTH; |
| 344 | ctx.scale(M_TO_PX, M_TO_PX); |
| 345 | ctx.lineWidth = 1 / M_TO_PX; |
| 346 | } |
| 347 | } |