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