Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 1 | import {ByteBuffer} from 'flatbuffers'; |
| 2 | import {Connection} from '../../aos/network/www/proxy'; |
James Kuszmaul | fb89457 | 2023-02-23 17:25:06 -0800 | [diff] [blame] | 3 | import {LocalizerOutput} from '../../frc971/control_loops/drivetrain/localization/localizer_output_generated'; |
| 4 | import {RejectionReason} from '../localizer/status_generated'; |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 5 | import {Status as DrivetrainStatus} from '../../frc971/control_loops/drivetrain/drivetrain_status_generated'; |
Milo Lin | e6571c0 | 2023-03-04 21:08:20 -0800 | [diff] [blame^] | 6 | import {Status as SuperstructureStatus, EndEffectorState, ArmState, ArmStatus} from '../control_loops/superstructure/superstructure_status_generated' |
| 7 | import {Class} from '../vision/game_pieces_generated' |
James Kuszmaul | fb89457 | 2023-02-23 17:25:06 -0800 | [diff] [blame] | 8 | import {Visualization, TargetEstimateDebug} from '../localizer/visualization_generated'; |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 9 | |
| 10 | import {FIELD_LENGTH, FIELD_WIDTH, FT_TO_M, IN_TO_M} from './constants'; |
| 11 | |
| 12 | // (0,0) is field center, +X is toward red DS |
| 13 | const FIELD_SIDE_Y = FIELD_WIDTH / 2; |
| 14 | const FIELD_EDGE_X = FIELD_LENGTH / 2; |
| 15 | |
James Kuszmaul | fb89457 | 2023-02-23 17:25:06 -0800 | [diff] [blame] | 16 | const ROBOT_WIDTH = 25 * IN_TO_M; |
| 17 | const ROBOT_LENGTH = 32 * IN_TO_M; |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 18 | |
| 19 | const PI_COLORS = ['#ff00ff', '#ffff00', '#00ffff', '#ffa500']; |
James Kuszmaul | fb89457 | 2023-02-23 17:25:06 -0800 | [diff] [blame] | 20 | const PIS = ['pi1', 'pi2', 'pi3', 'pi4']; |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 21 | |
| 22 | export class FieldHandler { |
| 23 | private canvas = document.createElement('canvas'); |
James Kuszmaul | fb89457 | 2023-02-23 17:25:06 -0800 | [diff] [blame] | 24 | private localizerOutput: LocalizerOutput|null = null; |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 25 | private drivetrainStatus: DrivetrainStatus|null = null; |
Milo Lin | e6571c0 | 2023-03-04 21:08:20 -0800 | [diff] [blame^] | 26 | private superstructureStatus: SuperstructureStatus|null = null; |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 27 | |
| 28 | // Image information indexed by timestamp (seconds since the epoch), so that |
| 29 | // we can stop displaying images after a certain amount of time. |
James Kuszmaul | fb89457 | 2023-02-23 17:25:06 -0800 | [diff] [blame] | 30 | private localizerImageMatches = new Map<number, Visualization>(); |
| 31 | private x: HTMLElement = (document.getElementById('x') as HTMLElement); |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 32 | private y: HTMLElement = (document.getElementById('y') as HTMLElement); |
| 33 | private theta: HTMLElement = |
| 34 | (document.getElementById('theta') as HTMLElement); |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 35 | private imagesAcceptedCounter: HTMLElement = |
| 36 | (document.getElementById('images_accepted') as HTMLElement); |
James Kuszmaul | fb89457 | 2023-02-23 17:25:06 -0800 | [diff] [blame] | 37 | private rejectionReasonCells: HTMLElement[] = []; |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 38 | private fieldImage: HTMLImageElement = new Image(); |
Milo Lin | e6571c0 | 2023-03-04 21:08:20 -0800 | [diff] [blame^] | 39 | private endEffectorState: HTMLElement = |
| 40 | (document.getElementById('end_effector_state') as HTMLElement); |
| 41 | private wrist: HTMLElement = |
| 42 | (document.getElementById('wrist') as HTMLElement); |
| 43 | private armState: HTMLElement = |
| 44 | (document.getElementById('arm_state') as HTMLElement); |
| 45 | private gamePiece: HTMLElement = |
| 46 | (document.getElementById('game_piece') as HTMLElement); |
| 47 | private armX: HTMLElement = |
| 48 | (document.getElementById('arm_x') as HTMLElement); |
| 49 | private armY: HTMLElement = |
| 50 | (document.getElementById('arm_y') as HTMLElement); |
| 51 | private circularIndex: HTMLElement = |
| 52 | (document.getElementById('arm_circular_index') as HTMLElement); |
| 53 | private roll: HTMLElement = |
| 54 | (document.getElementById('arm_roll') as HTMLElement); |
| 55 | private proximal: HTMLElement = |
| 56 | (document.getElementById('arm_proximal') as HTMLElement); |
| 57 | private distal: HTMLElement = |
| 58 | (document.getElementById('arm_distal') as HTMLElement); |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 59 | |
| 60 | constructor(private readonly connection: Connection) { |
| 61 | (document.getElementById('field') as HTMLElement).appendChild(this.canvas); |
| 62 | |
James Kuszmaul | fb89457 | 2023-02-23 17:25:06 -0800 | [diff] [blame] | 63 | this.fieldImage.src = "2023.png"; |
| 64 | |
| 65 | for (const value in RejectionReason) { |
| 66 | // Typescript generates an iterator that produces both numbers and |
| 67 | // strings... don't do anything on the string iterations. |
| 68 | if (isNaN(Number(value))) { |
| 69 | continue; |
| 70 | } |
| 71 | const row = document.createElement('div'); |
| 72 | const nameCell = document.createElement('div'); |
| 73 | nameCell.innerHTML = RejectionReason[value]; |
| 74 | row.appendChild(nameCell); |
| 75 | const valueCell = document.createElement('div'); |
| 76 | valueCell.innerHTML = 'NA'; |
| 77 | this.rejectionReasonCells.push(valueCell); |
| 78 | row.appendChild(valueCell); |
| 79 | document.getElementById('vision_readouts').appendChild(row); |
| 80 | } |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 81 | |
| 82 | for (let ii = 0; ii < PI_COLORS.length; ++ii) { |
| 83 | const legendEntry = document.createElement('div'); |
| 84 | legendEntry.style.color = PI_COLORS[ii]; |
| 85 | legendEntry.innerHTML = 'PI' + (ii + 1).toString() |
| 86 | document.getElementById('legend').appendChild(legendEntry); |
| 87 | } |
| 88 | |
| 89 | this.connection.addConfigHandler(() => { |
| 90 | // Visualization message is reliable so that we can see *all* the vision |
| 91 | // matches. |
James Kuszmaul | fb89457 | 2023-02-23 17:25:06 -0800 | [diff] [blame] | 92 | for (const pi in PIS) { |
| 93 | this.connection.addReliableHandler( |
| 94 | '/' + PIS[pi] + '/camera', "y2023.localizer.Visualization", |
| 95 | (data) => { |
| 96 | this.handleLocalizerDebug(pi, data); |
| 97 | }); |
| 98 | } |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 99 | this.connection.addHandler( |
| 100 | '/drivetrain', "frc971.control_loops.drivetrain.Status", (data) => { |
| 101 | this.handleDrivetrainStatus(data); |
| 102 | }); |
| 103 | this.connection.addHandler( |
James Kuszmaul | fb89457 | 2023-02-23 17:25:06 -0800 | [diff] [blame] | 104 | '/localizer', "frc971.controls.LocalizerOutput", (data) => { |
| 105 | this.handleLocalizerOutput(data); |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 106 | }); |
Milo Lin | e6571c0 | 2023-03-04 21:08:20 -0800 | [diff] [blame^] | 107 | this.connection.addHandler( |
| 108 | '/superstructure', "y2023.control_loops.superstructure.Status", |
| 109 | (data) => { |
| 110 | this.handleSuperstructureStatus(data) |
| 111 | }); |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 112 | }); |
| 113 | } |
| 114 | |
James Kuszmaul | fb89457 | 2023-02-23 17:25:06 -0800 | [diff] [blame] | 115 | private handleLocalizerDebug(pi: string, data: Uint8Array): void { |
| 116 | const now = Date.now() / 1000.0; |
| 117 | |
| 118 | const fbBuffer = new ByteBuffer(data); |
| 119 | this.localizerImageMatches.set( |
| 120 | now, Visualization.getRootAsVisualization(fbBuffer)); |
| 121 | |
| 122 | const debug = this.localizerImageMatches.get(now); |
| 123 | |
| 124 | if (debug.statistics()) { |
| 125 | if (debug.statistics().rejectionReasonsLength() == |
| 126 | this.rejectionReasonCells.length) { |
| 127 | for (let ii = 0; ii < debug.statistics().rejectionReasonsLength(); |
| 128 | ++ii) { |
| 129 | this.rejectionReasonCells[ii].innerHTML = |
| 130 | debug.statistics().rejectionReasons(ii).count().toString(); |
| 131 | } |
| 132 | } else { |
| 133 | console.error('Unexpected number of rejection reasons in counter.'); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | private handleLocalizerOutput(data: Uint8Array): void { |
| 139 | const fbBuffer = new ByteBuffer(data); |
| 140 | this.localizerOutput = LocalizerOutput.getRootAsLocalizerOutput(fbBuffer); |
| 141 | } |
| 142 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 143 | private handleDrivetrainStatus(data: Uint8Array): void { |
| 144 | const fbBuffer = new ByteBuffer(data); |
| 145 | this.drivetrainStatus = DrivetrainStatus.getRootAsStatus(fbBuffer); |
| 146 | } |
| 147 | |
Milo Lin | e6571c0 | 2023-03-04 21:08:20 -0800 | [diff] [blame^] | 148 | private handleSuperstructureStatus(data: Uint8Array): void { |
| 149 | const fbBuffer = new ByteBuffer(data); |
| 150 | this.superstructureStatus = SuperstructureStatus.getRootAsStatus(fbBuffer); |
| 151 | } |
| 152 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 153 | drawField(): void { |
| 154 | const ctx = this.canvas.getContext('2d'); |
| 155 | ctx.save(); |
James Kuszmaul | fb89457 | 2023-02-23 17:25:06 -0800 | [diff] [blame] | 156 | ctx.scale(1.0, -1.0); |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 157 | ctx.drawImage( |
| 158 | this.fieldImage, 0, 0, this.fieldImage.width, this.fieldImage.height, |
| 159 | -FIELD_EDGE_X, -FIELD_SIDE_Y, FIELD_LENGTH, FIELD_WIDTH); |
| 160 | ctx.restore(); |
| 161 | } |
| 162 | |
| 163 | drawCamera( |
James Kuszmaul | fb89457 | 2023-02-23 17:25:06 -0800 | [diff] [blame] | 164 | x: number, y: number, theta: number, color: string = 'blue'): void { |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 165 | const ctx = this.canvas.getContext('2d'); |
| 166 | ctx.save(); |
| 167 | ctx.translate(x, y); |
| 168 | ctx.rotate(theta); |
| 169 | ctx.strokeStyle = color; |
| 170 | ctx.beginPath(); |
| 171 | ctx.moveTo(0.5, 0.5); |
| 172 | ctx.lineTo(0, 0); |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 173 | ctx.lineTo(0.5, -0.5); |
| 174 | ctx.stroke(); |
| 175 | ctx.beginPath(); |
| 176 | ctx.arc(0, 0, 0.25, -Math.PI / 4, Math.PI / 4); |
| 177 | ctx.stroke(); |
| 178 | ctx.restore(); |
| 179 | } |
| 180 | |
| 181 | drawRobot( |
James Kuszmaul | fb89457 | 2023-02-23 17:25:06 -0800 | [diff] [blame] | 182 | x: number, y: number, theta: number, |
| 183 | color: string = 'blue', dashed: boolean = false): void { |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 184 | const ctx = this.canvas.getContext('2d'); |
| 185 | ctx.save(); |
| 186 | ctx.translate(x, y); |
| 187 | ctx.rotate(theta); |
| 188 | ctx.strokeStyle = color; |
| 189 | ctx.lineWidth = ROBOT_WIDTH / 10.0; |
| 190 | if (dashed) { |
| 191 | ctx.setLineDash([0.05, 0.05]); |
| 192 | } else { |
| 193 | // Empty array = solid line. |
| 194 | ctx.setLineDash([]); |
| 195 | } |
| 196 | ctx.rect(-ROBOT_LENGTH / 2, -ROBOT_WIDTH / 2, ROBOT_LENGTH, ROBOT_WIDTH); |
| 197 | ctx.stroke(); |
| 198 | |
| 199 | // Draw line indicating which direction is forwards on the robot. |
| 200 | ctx.beginPath(); |
| 201 | ctx.moveTo(0, 0); |
James Kuszmaul | fb89457 | 2023-02-23 17:25:06 -0800 | [diff] [blame] | 202 | ctx.lineTo(ROBOT_LENGTH / 2.0, 0); |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 203 | ctx.stroke(); |
| 204 | |
| 205 | ctx.restore(); |
| 206 | } |
| 207 | |
| 208 | setZeroing(div: HTMLElement): void { |
| 209 | div.innerHTML = 'zeroing'; |
| 210 | div.classList.remove('faulted'); |
| 211 | div.classList.add('zeroing'); |
| 212 | div.classList.remove('near'); |
| 213 | } |
| 214 | |
Milo Lin | e6571c0 | 2023-03-04 21:08:20 -0800 | [diff] [blame^] | 215 | setEstopped(div: HTMLElement): void { |
| 216 | div.innerHTML = 'estopped'; |
| 217 | div.classList.add('faulted'); |
| 218 | div.classList.remove('zeroing'); |
| 219 | div.classList.remove('near'); |
| 220 | } |
| 221 | |
| 222 | setTargetValue( |
| 223 | div: HTMLElement, target: number, val: number, tolerance: number): void { |
| 224 | div.innerHTML = val.toFixed(4); |
| 225 | div.classList.remove('faulted'); |
| 226 | div.classList.remove('zeroing'); |
| 227 | if (Math.abs(target - val) < tolerance) { |
| 228 | div.classList.add('near'); |
| 229 | } else { |
| 230 | div.classList.remove('near'); |
| 231 | } |
| 232 | } |
| 233 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 234 | setValue(div: HTMLElement, val: number): void { |
| 235 | div.innerHTML = val.toFixed(4); |
| 236 | div.classList.remove('faulted'); |
| 237 | div.classList.remove('zeroing'); |
| 238 | div.classList.remove('near'); |
| 239 | } |
| 240 | |
| 241 | draw(): void { |
| 242 | this.reset(); |
| 243 | this.drawField(); |
| 244 | |
| 245 | // Draw the matches with debugging information from the localizer. |
| 246 | const now = Date.now() / 1000.0; |
James Kuszmaul | fb89457 | 2023-02-23 17:25:06 -0800 | [diff] [blame] | 247 | |
Milo Lin | e6571c0 | 2023-03-04 21:08:20 -0800 | [diff] [blame^] | 248 | if (this.superstructureStatus) { |
| 249 | this.endEffectorState.innerHTML = |
| 250 | EndEffectorState[this.superstructureStatus.endEffectorState()]; |
| 251 | if (!this.superstructureStatus.wrist() || |
| 252 | !this.superstructureStatus.wrist().zeroed()) { |
| 253 | this.setZeroing(this.wrist); |
| 254 | } else if (this.superstructureStatus.wrist().estopped()) { |
| 255 | this.setEstopped(this.wrist); |
| 256 | } else { |
| 257 | this.setTargetValue( |
| 258 | this.wrist, |
| 259 | this.superstructureStatus.wrist().unprofiledGoalPosition(), |
| 260 | this.superstructureStatus.wrist().estimatorState().position(), |
| 261 | 1e-3); |
| 262 | } |
| 263 | this.armState.innerHTML = |
| 264 | ArmState[this.superstructureStatus.arm().state()]; |
| 265 | this.gamePiece.innerHTML = |
| 266 | Class[this.superstructureStatus.gamePiece()]; |
| 267 | this.armX.innerHTML = |
| 268 | this.superstructureStatus.arm().armX().toFixed(2); |
| 269 | this.armY.innerHTML = |
| 270 | this.superstructureStatus.arm().armY().toFixed(2); |
| 271 | this.circularIndex.innerHTML = |
| 272 | this.superstructureStatus.arm().armCircularIndex().toFixed(0); |
| 273 | this.roll.innerHTML = |
| 274 | this.superstructureStatus.arm().rollJointEstimatorState().position().toFixed(2); |
| 275 | this.proximal.innerHTML = |
| 276 | this.superstructureStatus.arm().proximalEstimatorState().position().toFixed(2); |
| 277 | this.distal.innerHTML = |
| 278 | this.superstructureStatus.arm().distalEstimatorState().position().toFixed(2); |
| 279 | } |
| 280 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 281 | if (this.drivetrainStatus && this.drivetrainStatus.trajectoryLogging()) { |
| 282 | this.drawRobot( |
| 283 | this.drivetrainStatus.trajectoryLogging().x(), |
| 284 | this.drivetrainStatus.trajectoryLogging().y(), |
James Kuszmaul | fb89457 | 2023-02-23 17:25:06 -0800 | [diff] [blame] | 285 | this.drivetrainStatus.trajectoryLogging().theta(), "#000000FF", |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 286 | false); |
| 287 | } |
| 288 | |
James Kuszmaul | fb89457 | 2023-02-23 17:25:06 -0800 | [diff] [blame] | 289 | if (this.localizerOutput) { |
| 290 | if (!this.localizerOutput.zeroed()) { |
| 291 | this.setZeroing(this.x); |
| 292 | this.setZeroing(this.y); |
| 293 | this.setZeroing(this.theta); |
| 294 | } else { |
| 295 | this.setValue(this.x, this.localizerOutput.x()); |
| 296 | this.setValue(this.y, this.localizerOutput.y()); |
| 297 | this.setValue(this.theta, this.localizerOutput.theta()); |
| 298 | } |
| 299 | |
| 300 | this.drawRobot( |
| 301 | this.localizerOutput.x(), this.localizerOutput.y(), |
| 302 | this.localizerOutput.theta()); |
| 303 | |
| 304 | this.imagesAcceptedCounter.innerHTML = |
| 305 | this.localizerOutput.imageAcceptedCount().toString(); |
| 306 | } |
| 307 | |
| 308 | for (const [time, value] of this.localizerImageMatches) { |
| 309 | const age = now - time; |
| 310 | const kRemovalAge = 1.0; |
| 311 | if (age > kRemovalAge) { |
| 312 | this.localizerImageMatches.delete(time); |
| 313 | continue; |
| 314 | } |
| 315 | const kMaxImageAlpha = 0.5; |
| 316 | const ageAlpha = kMaxImageAlpha * (kRemovalAge - age) / kRemovalAge |
| 317 | for (let i = 0; i < value.targetsLength(); i++) { |
| 318 | const imageDebug = value.targets(i); |
| 319 | const x = imageDebug.impliedRobotX(); |
| 320 | const y = imageDebug.impliedRobotY(); |
| 321 | const theta = imageDebug.impliedRobotTheta(); |
| 322 | const cameraX = imageDebug.cameraX(); |
| 323 | const cameraY = imageDebug.cameraY(); |
| 324 | const cameraTheta = imageDebug.cameraTheta(); |
| 325 | const accepted = imageDebug.accepted(); |
| 326 | // Make camera readings fade over time. |
| 327 | const alpha = Math.round(255 * ageAlpha).toString(16).padStart(2, '0'); |
| 328 | const dashed = false; |
James Kuszmaul | fb89457 | 2023-02-23 17:25:06 -0800 | [diff] [blame] | 329 | const cameraRgb = PI_COLORS[imageDebug.camera()]; |
| 330 | const cameraRgba = cameraRgb + alpha; |
James Kuszmaul | 122a22b | 2023-02-25 18:14:15 -0800 | [diff] [blame] | 331 | this.drawRobot(x, y, theta, cameraRgba, dashed); |
James Kuszmaul | fb89457 | 2023-02-23 17:25:06 -0800 | [diff] [blame] | 332 | this.drawCamera(cameraX, cameraY, cameraTheta, cameraRgba); |
| 333 | } |
| 334 | } |
| 335 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 336 | window.requestAnimationFrame(() => this.draw()); |
| 337 | } |
| 338 | |
| 339 | reset(): void { |
| 340 | const ctx = this.canvas.getContext('2d'); |
| 341 | ctx.setTransform(1, 0, 0, 1, 0, 0); |
| 342 | const size = window.innerHeight * 0.9; |
| 343 | ctx.canvas.height = size; |
| 344 | const width = size / 2 + 20; |
| 345 | ctx.canvas.width = width; |
| 346 | ctx.clearRect(0, 0, size, width); |
| 347 | |
| 348 | // Translate to center of display. |
| 349 | ctx.translate(width / 2, size / 2); |
| 350 | // Coordinate system is: |
| 351 | // x -> forward. |
| 352 | // y -> to the left. |
| 353 | ctx.rotate(-Math.PI / 2); |
| 354 | ctx.scale(1, -1); |
| 355 | |
| 356 | const M_TO_PX = (size - 10) / FIELD_LENGTH; |
| 357 | ctx.scale(M_TO_PX, M_TO_PX); |
| 358 | ctx.lineWidth = 1 / M_TO_PX; |
| 359 | } |
| 360 | } |