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