James Kuszmaul | f3ef9e1 | 2022-03-05 17:13:00 -0800 | [diff] [blame] | 1 | import * as web_proxy from 'org_frc971/aos/network/web_proxy_generated'; |
| 2 | import {Connection} from 'org_frc971/aos/network/www/proxy'; |
| 3 | import * as flatbuffers_builder from 'org_frc971/external/com_github_google_flatbuffers/ts/builder'; |
| 4 | import {ByteBuffer} from 'org_frc971/external/com_github_google_flatbuffers/ts/byte-buffer'; |
| 5 | import * as localizer from 'org_frc971/y2022/localizer/localizer_visualization_generated'; |
| 6 | import * as output from 'org_frc971/y2022/localizer/localizer_output_generated'; |
| 7 | import * as ss from 'org_frc971/y2022/control_loops/superstructure/superstructure_status_generated' |
| 8 | |
| 9 | import LocalizerVisualization = localizer.frc971.controls.LocalizerVisualization; |
| 10 | import LocalizerOutput = output.frc971.controls.LocalizerOutput; |
| 11 | import RejectionReason = localizer.frc971.controls.RejectionReason; |
| 12 | import TargetEstimateDebug = localizer.frc971.controls.TargetEstimateDebug; |
| 13 | import SuperstructureStatus = ss.y2022.control_loops.superstructure.Status; |
James Kuszmaul | a087187 | 2022-03-05 22:12:39 -0800 | [diff] [blame^] | 14 | import SuperstructureState = ss.y2022.control_loops.superstructure.SuperstructureState; |
| 15 | import IntakeState = ss.y2022.control_loops.superstructure.IntakeState; |
James Kuszmaul | f3ef9e1 | 2022-03-05 17:13:00 -0800 | [diff] [blame] | 16 | |
| 17 | import {FIELD_LENGTH, FIELD_WIDTH, FT_TO_M, IN_TO_M} from './constants'; |
| 18 | |
| 19 | // (0,0) is field center, +X is toward red DS |
| 20 | const FIELD_SIDE_Y = FIELD_WIDTH / 2; |
| 21 | const FIELD_EDGE_X = FIELD_LENGTH / 2; |
| 22 | |
| 23 | const ROBOT_WIDTH = 34 * IN_TO_M; |
| 24 | const ROBOT_LENGTH = 36 * IN_TO_M; |
| 25 | |
| 26 | const PI_COLORS = ['#ff00ff', '#ffff00', '#00ffff', '#ffa500']; |
| 27 | |
| 28 | export class FieldHandler { |
| 29 | private canvas = document.createElement('canvas'); |
| 30 | private localizerOutput: LocalizerOutput|null = null; |
| 31 | private superstructureStatus: SuperstructureStatus|null = null; |
| 32 | |
| 33 | // Image information indexed by timestamp (seconds since the epoch), so that |
| 34 | // we can stop displaying images after a certain amount of time. |
| 35 | private localizerImageMatches = new Map<number, LocalizerVisualization>(); |
| 36 | private outerTarget: HTMLElement = |
| 37 | (document.getElementById('outer_target') as HTMLElement); |
| 38 | private innerTarget: HTMLElement = |
| 39 | (document.getElementById('inner_target') as HTMLElement); |
| 40 | private x: HTMLElement = (document.getElementById('x') as HTMLElement); |
| 41 | private y: HTMLElement = (document.getElementById('y') as HTMLElement); |
| 42 | private theta: HTMLElement = |
| 43 | (document.getElementById('theta') as HTMLElement); |
| 44 | private shotDistance: HTMLElement = |
| 45 | (document.getElementById('shot_distance') as HTMLElement); |
| 46 | private turret: HTMLElement = |
| 47 | (document.getElementById('turret') as HTMLElement); |
James Kuszmaul | a087187 | 2022-03-05 22:12:39 -0800 | [diff] [blame^] | 48 | private fire: HTMLElement = |
| 49 | (document.getElementById('fire') as HTMLElement); |
| 50 | private mpcSolveTime: HTMLElement = |
| 51 | (document.getElementById('mpc_solve_time') as HTMLElement); |
| 52 | private mpcActive: HTMLElement = |
| 53 | (document.getElementById('mpc_active') as HTMLElement); |
| 54 | private shotCount: HTMLElement = |
| 55 | (document.getElementById('shot_count') as HTMLElement); |
| 56 | private catapult: HTMLElement = |
| 57 | (document.getElementById('catapult') as HTMLElement); |
| 58 | private superstructureState: HTMLElement = |
| 59 | (document.getElementById('superstructure_state') as HTMLElement); |
| 60 | private intakeState: HTMLElement = |
| 61 | (document.getElementById('intake_state') as HTMLElement); |
| 62 | private reseatingInCatapult: HTMLElement = |
| 63 | (document.getElementById('reseating_in_catapult') as HTMLElement); |
| 64 | private flippersOpen: HTMLElement = |
| 65 | (document.getElementById('flippers_open') as HTMLElement); |
| 66 | private climber: HTMLElement = |
| 67 | (document.getElementById('climber') as HTMLElement); |
James Kuszmaul | f3ef9e1 | 2022-03-05 17:13:00 -0800 | [diff] [blame] | 68 | private frontIntake: HTMLElement = |
| 69 | (document.getElementById('front_intake') as HTMLElement); |
| 70 | private backIntake: HTMLElement = |
| 71 | (document.getElementById('back_intake') as HTMLElement); |
| 72 | private imagesAcceptedCounter: HTMLElement = |
| 73 | (document.getElementById('images_accepted') as HTMLElement); |
| 74 | private imagesRejectedCounter: HTMLElement = |
| 75 | (document.getElementById('images_rejected') as HTMLElement); |
| 76 | private rejectionReasonCells: HTMLElement[] = []; |
| 77 | private fieldImage: HTMLImageElement = new Image(); |
| 78 | |
| 79 | constructor(private readonly connection: Connection) { |
| 80 | (document.getElementById('field') as HTMLElement).appendChild(this.canvas); |
| 81 | |
| 82 | this.fieldImage.src = "2022.png"; |
| 83 | |
| 84 | for (const value in RejectionReason) { |
| 85 | // Typescript generates an iterator that produces both numbers and |
| 86 | // strings... don't do anything on the string iterations. |
| 87 | if (isNaN(Number(value))) { |
| 88 | continue; |
| 89 | } |
| 90 | const row = document.createElement('div'); |
| 91 | const nameCell = document.createElement('div'); |
| 92 | nameCell.innerHTML = RejectionReason[value]; |
| 93 | row.appendChild(nameCell); |
| 94 | const valueCell = document.createElement('div'); |
| 95 | valueCell.innerHTML = 'NA'; |
| 96 | this.rejectionReasonCells.push(valueCell); |
| 97 | row.appendChild(valueCell); |
| 98 | document.getElementById('vision_readouts').appendChild(row); |
| 99 | } |
| 100 | |
| 101 | for (let ii = 0; ii < PI_COLORS.length; ++ii) { |
| 102 | const legendEntry = document.createElement('div'); |
| 103 | legendEntry.style.color = PI_COLORS[ii]; |
| 104 | legendEntry.innerHTML = 'PI' + (ii + 1).toString() |
| 105 | document.getElementById('legend').appendChild(legendEntry); |
| 106 | } |
| 107 | |
| 108 | this.connection.addConfigHandler(() => { |
| 109 | this.connection.addHandler( |
| 110 | '/localizer', LocalizerVisualization.getFullyQualifiedName(), |
| 111 | (data) => { |
| 112 | this.handleLocalizerDebug(data); |
| 113 | }); |
| 114 | this.connection.addHandler( |
| 115 | '/localizer', LocalizerOutput.getFullyQualifiedName(), (data) => { |
| 116 | this.handleLocalizerOutput(data); |
| 117 | }); |
| 118 | this.connection.addHandler( |
| 119 | '/superstructure', SuperstructureStatus.getFullyQualifiedName(), |
| 120 | (data) => { |
| 121 | this.handleSuperstructureStatus(data); |
| 122 | }); |
| 123 | }); |
| 124 | } |
| 125 | |
| 126 | private handleLocalizerDebug(data: Uint8Array): void { |
| 127 | const now = Date.now() / 1000.0; |
| 128 | |
| 129 | const fbBuffer = new ByteBuffer(data); |
| 130 | this.localizerImageMatches.set( |
| 131 | now, |
| 132 | LocalizerVisualization.getRootAsLocalizerVisualization( |
| 133 | fbBuffer as unknown as flatbuffers.ByteBuffer)); |
| 134 | |
| 135 | const debug = this.localizerImageMatches.get(now); |
| 136 | |
| 137 | if (debug.statistics()) { |
| 138 | this.imagesAcceptedCounter.innerHTML = |
| 139 | debug.statistics().totalAccepted().toString(); |
| 140 | this.imagesRejectedCounter.innerHTML = |
| 141 | (debug.statistics().totalCandidates() - |
| 142 | debug.statistics().totalAccepted()) |
| 143 | .toString(); |
| 144 | if (debug.statistics().rejectionReasonCountLength() == |
| 145 | this.rejectionReasonCells.length) { |
| 146 | for (let ii = 0; ii < debug.statistics().rejectionReasonCountLength(); |
| 147 | ++ii) { |
| 148 | this.rejectionReasonCells[ii].innerHTML = |
| 149 | debug.statistics().rejectionReasonCount(ii).toString(); |
| 150 | } |
| 151 | } else { |
| 152 | console.error('Unexpected number of rejection reasons in counter.'); |
| 153 | } |
| 154 | this.imagesRejectedCounter.innerHTML = |
| 155 | (debug.statistics().totalCandidates() - |
| 156 | debug.statistics().totalAccepted()) |
| 157 | .toString(); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | private handleLocalizerOutput(data: Uint8Array): void { |
| 162 | const fbBuffer = new ByteBuffer(data); |
| 163 | this.localizerOutput = LocalizerOutput.getRootAsLocalizerOutput( |
| 164 | fbBuffer as unknown as flatbuffers.ByteBuffer); |
| 165 | } |
| 166 | |
| 167 | private handleSuperstructureStatus(data: Uint8Array): void { |
| 168 | const fbBuffer = new ByteBuffer(data); |
| 169 | this.superstructureStatus = SuperstructureStatus.getRootAsStatus( |
| 170 | fbBuffer as unknown as flatbuffers.ByteBuffer); |
| 171 | } |
| 172 | |
| 173 | drawField(): void { |
| 174 | const ctx = this.canvas.getContext('2d'); |
| 175 | ctx.drawImage( |
| 176 | this.fieldImage, 0, 0, this.fieldImage.width, this.fieldImage.height, |
| 177 | -FIELD_EDGE_X, -FIELD_SIDE_Y, FIELD_LENGTH, FIELD_WIDTH); |
| 178 | } |
| 179 | |
| 180 | drawCamera( |
| 181 | x: number, y: number, theta: number, color: string = 'blue', |
| 182 | extendLines: boolean = true): void { |
| 183 | const ctx = this.canvas.getContext('2d'); |
| 184 | ctx.save(); |
| 185 | ctx.translate(x, y); |
| 186 | ctx.rotate(theta); |
| 187 | ctx.strokeStyle = color; |
| 188 | ctx.beginPath(); |
| 189 | ctx.moveTo(0.5, 0.5); |
| 190 | ctx.lineTo(0, 0); |
| 191 | if (extendLines) { |
| 192 | ctx.lineTo(100.0, 0); |
| 193 | ctx.lineTo(0, 0); |
| 194 | } |
| 195 | ctx.lineTo(0.5, -0.5); |
| 196 | ctx.stroke(); |
| 197 | ctx.beginPath(); |
| 198 | ctx.arc(0, 0, 0.25, -Math.PI / 4, Math.PI / 4); |
| 199 | ctx.stroke(); |
| 200 | ctx.restore(); |
| 201 | } |
| 202 | |
| 203 | drawRobot( |
| 204 | x: number, y: number, theta: number, turret: number|null, |
| 205 | color: string = 'blue', dashed: boolean = false, |
| 206 | extendLines: boolean = true): void { |
| 207 | const ctx = this.canvas.getContext('2d'); |
| 208 | ctx.save(); |
| 209 | ctx.translate(x, y); |
| 210 | ctx.rotate(theta); |
| 211 | ctx.strokeStyle = color; |
| 212 | ctx.lineWidth = ROBOT_WIDTH / 10.0; |
| 213 | if (dashed) { |
| 214 | ctx.setLineDash([0.05, 0.05]); |
| 215 | } else { |
| 216 | // Empty array = solid line. |
| 217 | ctx.setLineDash([]); |
| 218 | } |
| 219 | ctx.rect(-ROBOT_LENGTH / 2, -ROBOT_WIDTH / 2, ROBOT_LENGTH, ROBOT_WIDTH); |
| 220 | ctx.stroke(); |
| 221 | |
| 222 | // Draw line indicating which direction is forwards on the robot. |
| 223 | ctx.beginPath(); |
| 224 | ctx.moveTo(0, 0); |
| 225 | if (extendLines) { |
| 226 | ctx.lineTo(1000.0, 0); |
| 227 | } else { |
| 228 | ctx.lineTo(ROBOT_LENGTH / 2.0, 0); |
| 229 | } |
| 230 | ctx.stroke(); |
| 231 | |
| 232 | if (turret !== null) { |
| 233 | ctx.save(); |
| 234 | ctx.rotate(turret); |
| 235 | const turretRadius = ROBOT_WIDTH / 3.0; |
| 236 | ctx.strokeStyle = 'red'; |
| 237 | // Draw circle for turret. |
| 238 | ctx.beginPath(); |
| 239 | ctx.arc(0, 0, turretRadius, 0, 2.0 * Math.PI); |
| 240 | ctx.stroke(); |
| 241 | // Draw line in circle to show forwards. |
| 242 | ctx.beginPath(); |
| 243 | ctx.moveTo(0, 0); |
| 244 | if (extendLines) { |
| 245 | ctx.lineTo(1000.0, 0); |
| 246 | } else { |
| 247 | ctx.lineTo(turretRadius, 0); |
| 248 | } |
| 249 | ctx.stroke(); |
| 250 | ctx.restore(); |
| 251 | } |
| 252 | ctx.restore(); |
| 253 | } |
| 254 | |
| 255 | setZeroing(div: HTMLElement): void { |
| 256 | div.innerHTML = 'zeroing'; |
| 257 | div.classList.remove('faulted'); |
| 258 | div.classList.add('zeroing'); |
| 259 | div.classList.remove('near'); |
| 260 | } |
| 261 | |
| 262 | setEstopped(div: HTMLElement): void { |
| 263 | div.innerHTML = 'estopped'; |
| 264 | div.classList.add('faulted'); |
| 265 | div.classList.remove('zeroing'); |
| 266 | div.classList.remove('near'); |
| 267 | } |
| 268 | |
| 269 | setTargetValue( |
| 270 | div: HTMLElement, target: number, val: number, tolerance: number): void { |
| 271 | div.innerHTML = val.toFixed(4); |
| 272 | div.classList.remove('faulted'); |
| 273 | div.classList.remove('zeroing'); |
| 274 | if (Math.abs(target - val) < tolerance) { |
| 275 | div.classList.add('near'); |
| 276 | } else { |
| 277 | div.classList.remove('near'); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | setValue(div: HTMLElement, val: number): void { |
| 282 | div.innerHTML = val.toFixed(4); |
| 283 | div.classList.remove('faulted'); |
| 284 | div.classList.remove('zeroing'); |
| 285 | div.classList.remove('near'); |
| 286 | } |
| 287 | |
| 288 | draw(): void { |
| 289 | this.reset(); |
| 290 | this.drawField(); |
| 291 | |
| 292 | // Draw the matches with debugging information from the localizer. |
| 293 | const now = Date.now() / 1000.0; |
| 294 | for (const [time, value] of this.localizerImageMatches) { |
| 295 | const age = now - time; |
| 296 | const kRemovalAge = 2.0; |
| 297 | if (age > kRemovalAge) { |
| 298 | this.localizerImageMatches.delete(time); |
| 299 | continue; |
| 300 | } |
| 301 | const ageAlpha = (kRemovalAge - age) / kRemovalAge |
| 302 | for (let i = 0; i < value.targetsLength(); i++) { |
| 303 | const imageDebug = value.targets(i); |
| 304 | const x = imageDebug.impliedRobotX(); |
| 305 | const y = imageDebug.impliedRobotY(); |
| 306 | const theta = imageDebug.impliedRobotTheta(); |
| 307 | const cameraX = imageDebug.cameraX(); |
| 308 | const cameraY = imageDebug.cameraY(); |
| 309 | const cameraTheta = imageDebug.cameraTheta(); |
| 310 | const accepted = imageDebug.accepted(); |
| 311 | // Make camera readings fade over time. |
| 312 | const alpha = Math.round(255 * ageAlpha).toString(16).padStart(2, '0'); |
| 313 | const dashed = false; |
| 314 | const acceptedRgb = accepted ? '#00FF00' : '#FF0000'; |
| 315 | const acceptedRgba = acceptedRgb + alpha; |
| 316 | const cameraRgb = PI_COLORS[imageDebug.camera()]; |
| 317 | const cameraRgba = cameraRgb + alpha; |
| 318 | this.drawRobot(x, y, theta, null, acceptedRgba, dashed, false); |
| 319 | this.drawCamera(cameraX, cameraY, cameraTheta, cameraRgba, false); |
| 320 | } |
| 321 | } |
James Kuszmaul | a087187 | 2022-03-05 22:12:39 -0800 | [diff] [blame^] | 322 | if (this.superstructureStatus) { |
| 323 | this.shotDistance.innerHTML = this.superstructureStatus.aimer() ? |
| 324 | this.superstructureStatus.aimer().shotDistance().toFixed(2) : |
| 325 | 'NA'; |
| 326 | |
| 327 | this.fire.innerHTML = this.superstructureStatus.fire() ? 'true' : 'false'; |
| 328 | |
| 329 | this.mpcActive.innerHTML = |
| 330 | this.superstructureStatus.mpcActive() ? 'true' : 'false'; |
| 331 | |
| 332 | this.setValue(this.mpcSolveTime, this.superstructureStatus.solveTime()); |
| 333 | |
| 334 | this.shotCount.innerHTML = |
| 335 | this.superstructureStatus.shotCount().toFixed(0); |
| 336 | |
| 337 | this.superstructureState.innerHTML = |
| 338 | SuperstructureState[this.superstructureStatus.state()]; |
| 339 | |
| 340 | this.intakeState.innerHTML = |
| 341 | IntakeState[this.superstructureStatus.intakeState()]; |
| 342 | |
| 343 | this.reseatingInCatapult.innerHTML = |
| 344 | this.superstructureStatus.reseatingInCatapult() ? 'true' : 'false'; |
| 345 | |
| 346 | this.flippersOpen.innerHTML = |
| 347 | this.superstructureStatus.flippersOpen() ? 'true' : 'false'; |
| 348 | |
| 349 | if (!this.superstructureStatus.catapult() || |
| 350 | !this.superstructureStatus.catapult().zeroed()) { |
| 351 | this.setZeroing(this.catapult); |
| 352 | } else if (this.superstructureStatus.catapult().estopped()) { |
| 353 | this.setEstopped(this.catapult); |
| 354 | } else { |
| 355 | this.setTargetValue( |
| 356 | this.catapult, |
| 357 | this.superstructureStatus.catapult().unprofiledGoalPosition(), |
| 358 | this.superstructureStatus.catapult().estimatorState().position(), |
| 359 | 1e-3); |
| 360 | } |
| 361 | |
| 362 | if (!this.superstructureStatus.climber() || |
| 363 | !this.superstructureStatus.climber().zeroed()) { |
| 364 | this.setZeroing(this.climber); |
| 365 | } else if (this.superstructureStatus.climber().estopped()) { |
| 366 | this.setEstopped(this.climber); |
| 367 | } else { |
| 368 | this.setTargetValue( |
| 369 | this.climber, |
| 370 | this.superstructureStatus.climber().unprofiledGoalPosition(), |
| 371 | this.superstructureStatus.climber().estimatorState().position(), |
| 372 | 1e-3); |
| 373 | } |
| 374 | |
| 375 | |
| 376 | |
| 377 | if (!this.superstructureStatus.turret() || |
| 378 | !this.superstructureStatus.turret().zeroed()) { |
| 379 | this.setZeroing(this.turret); |
| 380 | } else if (this.superstructureStatus.turret().estopped()) { |
| 381 | this.setEstopped(this.turret); |
| 382 | } else { |
| 383 | this.setTargetValue( |
| 384 | this.turret, |
| 385 | this.superstructureStatus.turret().unprofiledGoalPosition(), |
| 386 | this.superstructureStatus.turret().estimatorState().position(), |
| 387 | 1e-3); |
| 388 | } |
| 389 | |
| 390 | if (!this.superstructureStatus.intakeBack() || |
| 391 | !this.superstructureStatus.intakeBack().zeroed()) { |
| 392 | this.setZeroing(this.backIntake); |
| 393 | } else if (this.superstructureStatus.intakeBack().estopped()) { |
| 394 | this.setEstopped(this.backIntake); |
| 395 | } else { |
| 396 | this.setValue( |
| 397 | this.backIntake, |
| 398 | this.superstructureStatus.intakeBack().estimatorState().position()); |
| 399 | } |
| 400 | |
| 401 | if (!this.superstructureStatus.intakeFront() || |
| 402 | !this.superstructureStatus.intakeFront().zeroed()) { |
| 403 | this.setZeroing(this.frontIntake); |
| 404 | } else if (this.superstructureStatus.intakeFront().estopped()) { |
| 405 | this.setEstopped(this.frontIntake); |
| 406 | } else { |
| 407 | this.setValue( |
| 408 | this.frontIntake, |
| 409 | this.superstructureStatus.intakeFront() |
| 410 | .estimatorState() |
| 411 | .position()); |
| 412 | } |
| 413 | } |
James Kuszmaul | f3ef9e1 | 2022-03-05 17:13:00 -0800 | [diff] [blame] | 414 | |
| 415 | if (this.localizerOutput) { |
| 416 | if (!this.localizerOutput.zeroed()) { |
| 417 | this.setZeroing(this.x); |
| 418 | this.setZeroing(this.y); |
| 419 | this.setZeroing(this.theta); |
| 420 | } else { |
| 421 | this.setValue(this.x, this.localizerOutput.x()); |
| 422 | this.setValue(this.y, this.localizerOutput.y()); |
| 423 | this.setValue(this.theta, this.localizerOutput.theta()); |
| 424 | } |
| 425 | |
James Kuszmaul | f3ef9e1 | 2022-03-05 17:13:00 -0800 | [diff] [blame] | 426 | this.drawRobot( |
| 427 | this.localizerOutput.x(), this.localizerOutput.y(), |
| 428 | this.localizerOutput.theta(), |
| 429 | this.superstructureStatus ? |
| 430 | this.superstructureStatus.turret().position() : |
| 431 | null); |
| 432 | } |
| 433 | |
| 434 | window.requestAnimationFrame(() => this.draw()); |
| 435 | } |
| 436 | |
| 437 | reset(): void { |
| 438 | const ctx = this.canvas.getContext('2d'); |
| 439 | ctx.setTransform(1, 0, 0, 1, 0, 0); |
| 440 | const size = window.innerHeight * 0.9; |
| 441 | ctx.canvas.height = size; |
| 442 | const width = size / 2 + 20; |
| 443 | ctx.canvas.width = width; |
| 444 | ctx.clearRect(0, 0, size, width); |
| 445 | |
| 446 | // Translate to center of display. |
| 447 | ctx.translate(width / 2, size / 2); |
| 448 | // Coordinate system is: |
| 449 | // x -> forward. |
| 450 | // y -> to the left. |
| 451 | ctx.rotate(-Math.PI / 2); |
| 452 | ctx.scale(1, -1); |
| 453 | |
| 454 | const M_TO_PX = (size - 10) / FIELD_LENGTH; |
| 455 | ctx.scale(M_TO_PX, M_TO_PX); |
| 456 | ctx.lineWidth = 1 / M_TO_PX; |
| 457 | } |
| 458 | } |