Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -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 {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' |
Filip Kujawa | 1a2e9e0 | 2024-02-24 18:30:29 -0800 | [diff] [blame] | 9 | import {SuperstructureState, IntakeRollerStatus, CatapultState, TransferRollerStatus, ExtendRollerStatus, ExtendStatus, Status as SuperstructureStatus} from '../control_loops/superstructure/superstructure_status_generated' |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 10 | import {LocalizerOutput} from '../../frc971/control_loops/drivetrain/localization/localizer_output_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 | |
Niko Sohmers | 2d10876 | 2024-02-02 20:21:14 -0800 | [diff] [blame] | 20 | const ROBOT_WIDTH = 29 * IN_TO_M; |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 21 | const ROBOT_LENGTH = 32 * IN_TO_M; |
| 22 | |
| 23 | export class FieldHandler { |
Niko Sohmers | 2d10876 | 2024-02-02 20:21:14 -0800 | [diff] [blame] | 24 | private canvas = document.createElement('canvas'); |
Mirabel Wang | 6654664 | 2024-02-10 16:37:05 -0800 | [diff] [blame] | 25 | private localizerOutput: LocalizerOutput|null = null; |
| 26 | private drivetrainStatus: DrivetrainStatus|null = null; |
| 27 | private drivetrainPosition: DrivetrainPosition|null = null; |
| 28 | private drivetrainCANPosition: DrivetrainCANPosition|null = null; |
Filip Kujawa | 1a2e9e0 | 2024-02-24 18:30:29 -0800 | [diff] [blame] | 29 | private superstructureStatus: SuperstructureStatus|null = null; |
Mirabel Wang | 6654664 | 2024-02-10 16:37:05 -0800 | [diff] [blame] | 30 | |
| 31 | private x: HTMLElement = (document.getElementById('x') as HTMLElement); |
| 32 | private y: HTMLElement = (document.getElementById('y') as HTMLElement); |
| 33 | private theta: HTMLElement = |
| 34 | (document.getElementById('theta') as HTMLElement); |
| 35 | |
Niko Sohmers | 2d10876 | 2024-02-02 20:21:14 -0800 | [diff] [blame] | 36 | private fieldImage: HTMLImageElement = new Image(); |
Mirabel Wang | 6654664 | 2024-02-10 16:37:05 -0800 | [diff] [blame] | 37 | |
Filip Kujawa | 1a2e9e0 | 2024-02-24 18:30:29 -0800 | [diff] [blame] | 38 | private zeroingFaults: HTMLElement = |
| 39 | (document.getElementById('zeroing_faults') as HTMLElement); |
| 40 | |
| 41 | private superstructureState: HTMLElement = |
| 42 | (document.getElementById('superstructure_state') as HTMLElement); |
| 43 | |
| 44 | private intakeRollerState: HTMLElement = |
| 45 | (document.getElementById('intake_roller_state') as HTMLElement); |
| 46 | private transferRollerState: HTMLElement = |
| 47 | (document.getElementById('transfer_roller_state') as HTMLElement); |
| 48 | private extendState: HTMLElement = |
| 49 | (document.getElementById('extend_state') as HTMLElement); |
| 50 | private extendRollerState: HTMLElement = |
| 51 | (document.getElementById('extend_roller_state') as HTMLElement); |
| 52 | private catapultState: HTMLElement = |
| 53 | (document.getElementById('catapult_state') as HTMLElement); |
| 54 | |
| 55 | private intakePivot: HTMLElement = |
| 56 | (document.getElementById('intake_pivot') as HTMLElement); |
| 57 | private intakePivotAbs: HTMLElement = |
| 58 | (document.getElementById('intake_pivot_abs') as HTMLElement); |
| 59 | |
| 60 | private climber: HTMLElement = |
| 61 | (document.getElementById('climber') as HTMLElement); |
| 62 | private climberAbs: HTMLElement = |
| 63 | (document.getElementById('climber_abs') as HTMLElement); |
| 64 | private climberPot: HTMLElement = |
| 65 | (document.getElementById('climber_pot') as HTMLElement); |
| 66 | |
| 67 | private extend: HTMLElement = |
| 68 | (document.getElementById('extend') as HTMLElement); |
| 69 | private extendAbs: HTMLElement = |
| 70 | (document.getElementById('extend_abs') as HTMLElement); |
| 71 | private extendPot: HTMLElement = |
| 72 | (document.getElementById('extend_pot') as HTMLElement); |
| 73 | |
| 74 | private turret: HTMLElement = |
| 75 | (document.getElementById('turret') as HTMLElement); |
| 76 | private turretAbs: HTMLElement = |
| 77 | (document.getElementById('turret_abs') as HTMLElement); |
| 78 | private turretPot: HTMLElement = |
| 79 | (document.getElementById('turret_pot') as HTMLElement); |
| 80 | |
| 81 | private catapult: HTMLElement = |
| 82 | (document.getElementById('catapult') as HTMLElement); |
| 83 | private catapultAbs: HTMLElement = |
James Kuszmaul | 51a677e | 2024-03-01 19:59:00 -0800 | [diff] [blame] | 84 | (document.getElementById('catapult_abs') as HTMLElement); |
Filip Kujawa | 1a2e9e0 | 2024-02-24 18:30:29 -0800 | [diff] [blame] | 85 | private catapultPot: HTMLElement = |
| 86 | (document.getElementById('catapult_pot') as HTMLElement); |
| 87 | |
| 88 | private altitude: HTMLElement = |
| 89 | (document.getElementById('altitude') as HTMLElement); |
| 90 | private altitudeAbs: HTMLElement = |
| 91 | (document.getElementById('altitude_abs') as HTMLElement); |
| 92 | private altitudePot: HTMLElement = |
| 93 | (document.getElementById('altitude_pot') as HTMLElement); |
| 94 | |
| 95 | private turret_position: HTMLElement = |
| 96 | (document.getElementById('turret_position') as HTMLElement); |
| 97 | private turret_velocity: HTMLElement = |
| 98 | (document.getElementById('turret_velocity') as HTMLElement); |
| 99 | private target_distance: HTMLElement = |
| 100 | (document.getElementById('target_distance') as HTMLElement); |
| 101 | private shot_distance: HTMLElement = |
| 102 | (document.getElementById('shot_distance') as HTMLElement); |
| 103 | |
Mirabel Wang | 6654664 | 2024-02-10 16:37:05 -0800 | [diff] [blame] | 104 | private leftDrivetrainEncoder: HTMLElement = |
| 105 | (document.getElementById('left_drivetrain_encoder') as HTMLElement); |
| 106 | private rightDrivetrainEncoder: HTMLElement = |
| 107 | (document.getElementById('right_drivetrain_encoder') as HTMLElement); |
| 108 | private falconRightFrontPosition: HTMLElement = |
| 109 | (document.getElementById('falcon_right_front') as HTMLElement); |
| 110 | private falconRightBackPosition: HTMLElement = |
| 111 | (document.getElementById('falcon_right_back') as HTMLElement); |
| 112 | private falconLeftFrontPosition: HTMLElement = |
| 113 | (document.getElementById('falcon_left_front') as HTMLElement); |
| 114 | private falconLeftBackPosition: HTMLElement = |
| 115 | (document.getElementById('falcon_left_back') as HTMLElement); |
| 116 | |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 117 | constructor(private readonly connection: Connection) { |
Niko Sohmers | 2d10876 | 2024-02-02 20:21:14 -0800 | [diff] [blame] | 118 | (document.getElementById('field') as HTMLElement).appendChild(this.canvas); |
| 119 | |
| 120 | this.fieldImage.src = '2024.png'; |
Mirabel Wang | 6654664 | 2024-02-10 16:37:05 -0800 | [diff] [blame] | 121 | |
| 122 | this.connection.addConfigHandler(() => { |
| 123 | |
| 124 | this.connection.addHandler( |
| 125 | '/drivetrain', 'frc971.control_loops.drivetrain.Status', (data) => { |
| 126 | this.handleDrivetrainStatus(data); |
| 127 | }); |
| 128 | this.connection.addHandler( |
| 129 | '/drivetrain', 'frc971.control_loops.drivetrain.Position', (data) => { |
| 130 | this.handleDrivetrainPosition(data); |
| 131 | }); |
| 132 | this.connection.addHandler( |
| 133 | '/drivetrain', 'frc971.control_loops.drivetrain.CANPosition', (data) => { |
| 134 | this.handleDrivetrainCANPosition(data); |
| 135 | }); |
| 136 | this.connection.addHandler( |
| 137 | '/localizer', 'frc971.controls.LocalizerOutput', (data) => { |
| 138 | this.handleLocalizerOutput(data); |
| 139 | }); |
Filip Kujawa | 1a2e9e0 | 2024-02-24 18:30:29 -0800 | [diff] [blame] | 140 | this.connection.addHandler( |
| 141 | '/superstructure', "y2024.control_loops.superstructure.Status", |
| 142 | (data) => { |
| 143 | this.handleSuperstructureStatus(data) |
| 144 | }); |
Mirabel Wang | 6654664 | 2024-02-10 16:37:05 -0800 | [diff] [blame] | 145 | }); |
| 146 | } |
| 147 | |
| 148 | private handleDrivetrainStatus(data: Uint8Array): void { |
| 149 | const fbBuffer = new ByteBuffer(data); |
| 150 | this.drivetrainStatus = DrivetrainStatus.getRootAsStatus(fbBuffer); |
| 151 | } |
| 152 | |
| 153 | private handleDrivetrainPosition(data: Uint8Array): void { |
| 154 | const fbBuffer = new ByteBuffer(data); |
| 155 | this.drivetrainPosition = DrivetrainPosition.getRootAsPosition(fbBuffer); |
| 156 | } |
| 157 | |
| 158 | private handleDrivetrainCANPosition(data: Uint8Array): void { |
| 159 | const fbBuffer = new ByteBuffer(data); |
| 160 | this.drivetrainCANPosition = DrivetrainCANPosition.getRootAsCANPosition(fbBuffer); |
| 161 | } |
| 162 | |
| 163 | private handleLocalizerOutput(data: Uint8Array): void { |
| 164 | const fbBuffer = new ByteBuffer(data); |
| 165 | this.localizerOutput = LocalizerOutput.getRootAsLocalizerOutput(fbBuffer); |
Niko Sohmers | 2d10876 | 2024-02-02 20:21:14 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Filip Kujawa | 1a2e9e0 | 2024-02-24 18:30:29 -0800 | [diff] [blame] | 168 | private handleSuperstructureStatus(data: Uint8Array): void { |
| 169 | const fbBuffer = new ByteBuffer(data); |
| 170 | this.superstructureStatus = SuperstructureStatus.getRootAsStatus(fbBuffer); |
| 171 | } |
| 172 | |
Niko Sohmers | 2d10876 | 2024-02-02 20:21:14 -0800 | [diff] [blame] | 173 | drawField(): void { |
| 174 | const ctx = this.canvas.getContext('2d'); |
| 175 | ctx.save(); |
| 176 | ctx.scale(1.0, -1.0); |
| 177 | ctx.drawImage( |
| 178 | this.fieldImage, 0, 0, this.fieldImage.width, this.fieldImage.height, |
| 179 | -FIELD_EDGE_X, -FIELD_SIDE_Y, FIELD_LENGTH, FIELD_WIDTH); |
| 180 | ctx.restore(); |
| 181 | } |
| 182 | |
Filip Kujawa | 1a2e9e0 | 2024-02-24 18:30:29 -0800 | [diff] [blame] | 183 | drawCamera(x: number, y: number, theta: number, color: string = 'blue'): |
| 184 | void { |
| 185 | const ctx = this.canvas.getContext('2d'); |
| 186 | ctx.save(); |
| 187 | ctx.translate(x, y); |
| 188 | ctx.rotate(theta); |
| 189 | ctx.strokeStyle = color; |
| 190 | ctx.beginPath(); |
| 191 | ctx.moveTo(0.5, 0.5); |
| 192 | ctx.lineTo(0, 0); |
| 193 | ctx.lineTo(0.5, -0.5); |
| 194 | ctx.stroke(); |
| 195 | ctx.beginPath(); |
| 196 | ctx.arc(0, 0, 0.25, -Math.PI / 4, Math.PI / 4); |
| 197 | ctx.stroke(); |
| 198 | ctx.restore(); |
| 199 | } |
| 200 | |
Mirabel Wang | 6654664 | 2024-02-10 16:37:05 -0800 | [diff] [blame] | 201 | drawRobot( |
| 202 | x: number, y: number, theta: number, color: string = 'blue', |
| 203 | dashed: boolean = false): void { |
| 204 | const ctx = this.canvas.getContext('2d'); |
| 205 | ctx.save(); |
| 206 | ctx.translate(x, y); |
| 207 | ctx.rotate(theta); |
| 208 | ctx.strokeStyle = color; |
| 209 | ctx.lineWidth = ROBOT_WIDTH / 10.0; |
| 210 | if (dashed) { |
| 211 | ctx.setLineDash([0.05, 0.05]); |
| 212 | } else { |
| 213 | // Empty array = solid line. |
| 214 | ctx.setLineDash([]); |
| 215 | } |
| 216 | ctx.rect(-ROBOT_LENGTH / 2, -ROBOT_WIDTH / 2, ROBOT_LENGTH, ROBOT_WIDTH); |
| 217 | ctx.stroke(); |
| 218 | |
| 219 | // Draw line indicating which direction is forwards on the robot. |
| 220 | ctx.beginPath(); |
| 221 | ctx.moveTo(0, 0); |
| 222 | ctx.lineTo(ROBOT_LENGTH / 2.0, 0); |
| 223 | ctx.stroke(); |
| 224 | |
| 225 | ctx.restore(); |
| 226 | } |
| 227 | |
| 228 | setZeroing(div: HTMLElement): void { |
| 229 | div.innerHTML = 'zeroing'; |
| 230 | div.classList.remove('faulted'); |
| 231 | div.classList.add('zeroing'); |
| 232 | div.classList.remove('near'); |
Filip Kujawa | 1a2e9e0 | 2024-02-24 18:30:29 -0800 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | setEstopped(div: HTMLElement): void { |
| 236 | div.innerHTML = 'estopped'; |
| 237 | div.classList.add('faulted'); |
| 238 | div.classList.remove('zeroing'); |
| 239 | div.classList.remove('near'); |
| 240 | } |
| 241 | |
| 242 | setTargetValue( |
| 243 | div: HTMLElement, target: number, val: number, tolerance: number): void { |
| 244 | div.innerHTML = val.toFixed(4); |
| 245 | div.classList.remove('faulted'); |
| 246 | div.classList.remove('zeroing'); |
| 247 | if (Math.abs(target - val) < tolerance) { |
| 248 | div.classList.add('near'); |
| 249 | } else { |
| 250 | div.classList.remove('near'); |
| 251 | } |
| 252 | } |
Mirabel Wang | 6654664 | 2024-02-10 16:37:05 -0800 | [diff] [blame] | 253 | |
| 254 | setValue(div: HTMLElement, val: number): void { |
| 255 | div.innerHTML = val.toFixed(4); |
| 256 | div.classList.remove('faulted'); |
| 257 | div.classList.remove('zeroing'); |
| 258 | div.classList.remove('near'); |
Filip Kujawa | 1a2e9e0 | 2024-02-24 18:30:29 -0800 | [diff] [blame] | 259 | } |
Niko Sohmers | 2d10876 | 2024-02-02 20:21:14 -0800 | [diff] [blame] | 260 | draw(): void { |
| 261 | this.reset(); |
| 262 | this.drawField(); |
| 263 | |
Filip Kujawa | 1a2e9e0 | 2024-02-24 18:30:29 -0800 | [diff] [blame] | 264 | if (this.superstructureStatus) { |
| 265 | this.superstructureState.innerHTML = |
| 266 | SuperstructureState[this.superstructureStatus.state()]; |
Mirabel Wang | 6654664 | 2024-02-10 16:37:05 -0800 | [diff] [blame] | 267 | |
Filip Kujawa | 1a2e9e0 | 2024-02-24 18:30:29 -0800 | [diff] [blame] | 268 | this.intakeRollerState.innerHTML = |
| 269 | IntakeRollerStatus[this.superstructureStatus.intakeRoller()]; |
| 270 | this.transferRollerState.innerHTML = |
| 271 | TransferRollerStatus[this.superstructureStatus.transferRoller()]; |
| 272 | this.extendState.innerHTML = |
| 273 | ExtendStatus[this.superstructureStatus.extendStatus()]; |
| 274 | this.extendRollerState.innerHTML = |
| 275 | ExtendRollerStatus[this.superstructureStatus.extendRoller()]; |
| 276 | this.catapultState.innerHTML = |
| 277 | CatapultState[this.superstructureStatus.shooter().catapultState()]; |
| 278 | |
James Kuszmaul | 5d1b75e | 2024-02-25 14:37:00 -0800 | [diff] [blame] | 279 | if (this.superstructureStatus.shooter() && |
| 280 | this.superstructureStatus.shooter().aimer()) { |
| 281 | this.turret_position.innerHTML = this.superstructureStatus.shooter() |
| 282 | .aimer() |
| 283 | .turretPosition() |
| 284 | .toString(); |
| 285 | this.turret_velocity.innerHTML = this.superstructureStatus.shooter() |
| 286 | .aimer() |
| 287 | .turretVelocity() |
| 288 | .toString(); |
| 289 | this.target_distance.innerHTML = this.superstructureStatus.shooter() |
| 290 | .aimer() |
| 291 | .targetDistance() |
| 292 | .toString(); |
| 293 | this.shot_distance.innerHTML = this.superstructureStatus.shooter() |
| 294 | .aimer() |
| 295 | .shotDistance() |
| 296 | .toString(); |
| 297 | } |
Filip Kujawa | 1a2e9e0 | 2024-02-24 18:30:29 -0800 | [diff] [blame] | 298 | |
| 299 | if (!this.superstructureStatus.intakePivot() || |
| 300 | !this.superstructureStatus.intakePivot().zeroed()) { |
| 301 | this.setZeroing(this.intakePivot); |
| 302 | } else if (this.superstructureStatus.intakePivot().estopped()) { |
| 303 | this.setEstopped(this.intakePivot); |
| 304 | } else { |
| 305 | this.setTargetValue( |
| 306 | this.intakePivot, |
| 307 | this.superstructureStatus.intakePivot().unprofiledGoalPosition(), |
| 308 | this.superstructureStatus.intakePivot().estimatorState().position(), |
| 309 | 1e-3); |
| 310 | } |
| 311 | |
| 312 | this.intakePivotAbs.innerHTML = this.superstructureStatus.intakePivot().estimatorState().absolutePosition().toString(); |
| 313 | |
| 314 | if (!this.superstructureStatus.climber() || |
| 315 | !this.superstructureStatus.climber().zeroed()) { |
| 316 | this.setZeroing(this.climber); |
| 317 | } else if (this.superstructureStatus.climber().estopped()) { |
| 318 | this.setEstopped(this.climber); |
| 319 | } else { |
| 320 | this.setTargetValue( |
| 321 | this.climber, |
| 322 | this.superstructureStatus.climber().unprofiledGoalPosition(), |
| 323 | this.superstructureStatus.climber().estimatorState().position(), |
| 324 | 1e-3); |
| 325 | } |
| 326 | |
| 327 | this.climberAbs.innerHTML = this.superstructureStatus.climber().estimatorState().absolutePosition().toString(); |
| 328 | this.climberPot.innerHTML = this.superstructureStatus.climber().estimatorState().potPosition().toString(); |
| 329 | |
| 330 | if (!this.superstructureStatus.extend() || |
| 331 | !this.superstructureStatus.extend().zeroed()) { |
| 332 | this.setZeroing(this.extend); |
| 333 | } else if (this.superstructureStatus.extend().estopped()) { |
| 334 | this.setEstopped(this.extend); |
| 335 | } else { |
| 336 | this.setTargetValue( |
James Kuszmaul | 5d1b75e | 2024-02-25 14:37:00 -0800 | [diff] [blame] | 337 | this.extend, |
Filip Kujawa | 1a2e9e0 | 2024-02-24 18:30:29 -0800 | [diff] [blame] | 338 | this.superstructureStatus.extend().unprofiledGoalPosition(), |
| 339 | this.superstructureStatus.extend().estimatorState().position(), |
| 340 | 1e-3); |
| 341 | } |
| 342 | |
| 343 | this.extendAbs.innerHTML = this.superstructureStatus.extend().estimatorState().absolutePosition().toString(); |
| 344 | this.extendPot.innerHTML = this.superstructureStatus.extend().estimatorState().potPosition().toString(); |
| 345 | |
| 346 | if (!this.superstructureStatus.shooter().turret() || |
| 347 | !this.superstructureStatus.shooter().turret().zeroed()) { |
| 348 | this.setZeroing(this.turret); |
| 349 | } else if (this.superstructureStatus.shooter().turret().estopped()) { |
| 350 | this.setEstopped(this.turret); |
| 351 | } else { |
| 352 | this.setTargetValue( |
| 353 | this.turret, |
| 354 | this.superstructureStatus.shooter().turret().unprofiledGoalPosition(), |
| 355 | this.superstructureStatus.shooter().turret().estimatorState().position(), |
| 356 | 1e-3); |
| 357 | } |
| 358 | |
| 359 | this.turretAbs.innerHTML = this.superstructureStatus.shooter().turret().estimatorState().absolutePosition().toString(); |
| 360 | this.turretPot.innerHTML = this.superstructureStatus.shooter().turret().estimatorState().potPosition().toString(); |
| 361 | |
| 362 | if (!this.superstructureStatus.shooter().catapult() || |
| 363 | !this.superstructureStatus.shooter().catapult().zeroed()) { |
| 364 | this.setZeroing(this.catapult); |
| 365 | } else if (this.superstructureStatus.shooter().catapult().estopped()) { |
| 366 | this.setEstopped(this.catapult); |
| 367 | } else { |
| 368 | this.setTargetValue( |
| 369 | this.catapult, |
| 370 | this.superstructureStatus.shooter().catapult().unprofiledGoalPosition(), |
| 371 | this.superstructureStatus.shooter().catapult().estimatorState().position(), |
| 372 | 1e-3); |
| 373 | } |
| 374 | |
| 375 | this.catapultAbs.innerHTML = this.superstructureStatus.shooter().catapult().estimatorState().absolutePosition().toString(); |
| 376 | this.catapultPot.innerHTML = this.superstructureStatus.shooter().catapult().estimatorState().potPosition().toString(); |
| 377 | |
| 378 | if (!this.superstructureStatus.shooter().altitude() || |
| 379 | !this.superstructureStatus.shooter().altitude().zeroed()) { |
| 380 | this.setZeroing(this.altitude); |
| 381 | } else if (this.superstructureStatus.shooter().altitude().estopped()) { |
| 382 | this.setEstopped(this.altitude); |
| 383 | } else { |
| 384 | this.setTargetValue( |
| 385 | this.altitude, |
| 386 | this.superstructureStatus.shooter().altitude().unprofiledGoalPosition(), |
| 387 | this.superstructureStatus.shooter().altitude().estimatorState().position(), |
| 388 | 1e-3); |
| 389 | } |
| 390 | |
| 391 | this.altitudeAbs.innerHTML = this.superstructureStatus.shooter().altitude().estimatorState().absolutePosition().toString(); |
| 392 | this.altitudePot.innerHTML = this.superstructureStatus.shooter().altitude().estimatorState().potPosition().toString(); |
| 393 | |
| 394 | let zeroingErrors: string = 'Intake Pivot Errors:' + |
| 395 | '<br/>'; |
| 396 | for (let i = 0; i < this.superstructureStatus.intakePivot() |
| 397 | .estimatorState() |
| 398 | .errorsLength(); |
| 399 | i++) { |
| 400 | zeroingErrors += ZeroingError[this.superstructureStatus.intakePivot() |
| 401 | .estimatorState() |
| 402 | .errors(i)] + |
| 403 | '<br/>'; |
| 404 | } |
| 405 | zeroingErrors += '<br/>' + |
| 406 | 'Climber Errors:' + |
| 407 | '<br/>'; |
| 408 | for (let i = 0; i < this.superstructureStatus.climber().estimatorState().errorsLength(); |
| 409 | i++) { |
| 410 | zeroingErrors += ZeroingError[this.superstructureStatus.climber().estimatorState().errors(i)] + |
| 411 | '<br/>'; |
| 412 | } |
| 413 | zeroingErrors += '<br/>' + |
| 414 | 'Extend Errors:' + |
| 415 | '<br/>'; |
| 416 | for (let i = 0; i < this.superstructureStatus.extend().estimatorState().errorsLength(); |
| 417 | i++) { |
| 418 | zeroingErrors += ZeroingError[this.superstructureStatus.extend().estimatorState().errors(i)] + |
| 419 | '<br/>'; |
| 420 | } |
| 421 | zeroingErrors += '<br/>' + |
| 422 | 'Turret Errors:' + |
| 423 | '<br/>'; |
| 424 | for (let i = 0; i < this.superstructureStatus.shooter().turret().estimatorState().errorsLength(); |
| 425 | i++) { |
| 426 | zeroingErrors += ZeroingError[this.superstructureStatus.shooter().turret().estimatorState().errors(i)] + |
| 427 | '<br/>'; |
| 428 | } |
| 429 | zeroingErrors += '<br/>' + |
| 430 | 'Catapult Errors:' + |
| 431 | '<br/>'; |
| 432 | for (let i = 0; i < this.superstructureStatus.shooter().catapult().estimatorState().errorsLength(); |
| 433 | i++) { |
| 434 | zeroingErrors += ZeroingError[this.superstructureStatus.shooter().catapult().estimatorState().errors(i)] + |
| 435 | '<br/>'; |
| 436 | } |
| 437 | zeroingErrors += '<br/>' + |
| 438 | 'Altitude Errors:' + |
| 439 | '<br/>'; |
| 440 | for (let i = 0; i < this.superstructureStatus.shooter().altitude().estimatorState().errorsLength(); |
| 441 | i++) { |
| 442 | zeroingErrors += ZeroingError[this.superstructureStatus.shooter().altitude().estimatorState().errors(i)] + |
| 443 | '<br/>'; |
| 444 | } |
| 445 | this.zeroingFaults.innerHTML = zeroingErrors; |
| 446 | } |
| 447 | |
| 448 | if (this.drivetrainPosition) { |
| 449 | this.leftDrivetrainEncoder.innerHTML = |
| 450 | this.drivetrainPosition.leftEncoder().toString(); |
| 451 | |
| 452 | this.rightDrivetrainEncoder.innerHTML = |
| 453 | this.drivetrainPosition.rightEncoder().toString(); |
Mirabel Wang | 6654664 | 2024-02-10 16:37:05 -0800 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | if (this.drivetrainCANPosition) { |
| 457 | this.falconRightFrontPosition.innerHTML = |
| 458 | this.drivetrainCANPosition.talonfxs(0).position().toString(); |
| 459 | |
| 460 | this.falconRightBackPosition.innerHTML = |
| 461 | this.drivetrainCANPosition.talonfxs(1).position().toString(); |
| 462 | |
| 463 | this.falconLeftFrontPosition.innerHTML = |
| 464 | this.drivetrainCANPosition.talonfxs(2).position().toString(); |
| 465 | |
| 466 | this.falconLeftBackPosition.innerHTML = |
| 467 | this.drivetrainCANPosition.talonfxs(3).position().toString(); |
| 468 | } |
| 469 | |
| 470 | if (this.drivetrainStatus && this.drivetrainStatus.trajectoryLogging()) { |
| 471 | this.drawRobot( |
| 472 | this.drivetrainStatus.trajectoryLogging().x(), |
| 473 | this.drivetrainStatus.trajectoryLogging().y(), |
| 474 | this.drivetrainStatus.trajectoryLogging().theta(), '#000000FF', |
| 475 | false); |
| 476 | } |
| 477 | |
| 478 | if (this.localizerOutput) { |
| 479 | if (!this.localizerOutput.zeroed()) { |
| 480 | this.setZeroing(this.x); |
| 481 | this.setZeroing(this.y); |
| 482 | this.setZeroing(this.theta); |
| 483 | } else { |
| 484 | this.setValue(this.x, this.localizerOutput.x()); |
| 485 | this.setValue(this.y, this.localizerOutput.y()); |
| 486 | this.setValue(this.theta, this.localizerOutput.theta()); |
| 487 | } |
| 488 | |
| 489 | this.drawRobot( |
| 490 | this.localizerOutput.x(), this.localizerOutput.y(), |
| 491 | this.localizerOutput.theta()); |
| 492 | } |
Filip Kujawa | 1a2e9e0 | 2024-02-24 18:30:29 -0800 | [diff] [blame] | 493 | |
Niko Sohmers | 2d10876 | 2024-02-02 20:21:14 -0800 | [diff] [blame] | 494 | window.requestAnimationFrame(() => this.draw()); |
| 495 | } |
| 496 | |
| 497 | reset(): void { |
| 498 | const ctx = this.canvas.getContext('2d'); |
| 499 | // Empty space from the canvas boundary to the image |
| 500 | const IMAGE_PADDING = 10; |
| 501 | ctx.setTransform(1, 0, 0, 1, 0, 0); |
| 502 | const size = window.innerHeight * 0.9; |
| 503 | ctx.canvas.height = size; |
| 504 | const width = size / 2 + 20; |
| 505 | ctx.canvas.width = width; |
| 506 | ctx.clearRect(0, 0, size, width); |
| 507 | |
| 508 | // Translate to center of display. |
| 509 | ctx.translate(width / 2, size / 2); |
| 510 | // Coordinate system is: |
| 511 | // x -> forward. |
| 512 | // y -> to the left. |
| 513 | ctx.rotate(-Math.PI / 2); |
| 514 | ctx.scale(1, -1); |
| 515 | |
| 516 | const M_TO_PX = (size - IMAGE_PADDING) / FIELD_LENGTH; |
| 517 | ctx.scale(M_TO_PX, M_TO_PX); |
| 518 | ctx.lineWidth = 1 / M_TO_PX; |
Niko Sohmers | 3860f8a | 2024-01-12 21:05:19 -0800 | [diff] [blame] | 519 | } |
| 520 | } |