blob: fe13fcb261fb6838c8dbfc8d0a28a2a040ac6b96 [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'
Filip Kujawa1a2e9e02024-02-24 18:30:29 -08009import {SuperstructureState, IntakeRollerStatus, CatapultState, TransferRollerStatus, ExtendRollerStatus, ExtendStatus, 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);
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 =
84 (document.getElementById('turret_abs') as HTMLElement);
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 Wang66546642024-02-10 16:37:05 -0800104 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 Sohmers3860f8a2024-01-12 21:05:19 -0800117 constructor(private readonly connection: Connection) {
Niko Sohmers2d108762024-02-02 20:21:14 -0800118 (document.getElementById('field') as HTMLElement).appendChild(this.canvas);
119
120 this.fieldImage.src = '2024.png';
Mirabel Wang66546642024-02-10 16:37:05 -0800121
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 Kujawa1a2e9e02024-02-24 18:30:29 -0800140 this.connection.addHandler(
141 '/superstructure', "y2024.control_loops.superstructure.Status",
142 (data) => {
143 this.handleSuperstructureStatus(data)
144 });
Mirabel Wang66546642024-02-10 16:37:05 -0800145 });
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 Sohmers2d108762024-02-02 20:21:14 -0800166 }
167
Filip Kujawa1a2e9e02024-02-24 18:30:29 -0800168 private handleSuperstructureStatus(data: Uint8Array): void {
169 const fbBuffer = new ByteBuffer(data);
170 this.superstructureStatus = SuperstructureStatus.getRootAsStatus(fbBuffer);
171 }
172
Niko Sohmers2d108762024-02-02 20:21:14 -0800173 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 Kujawa1a2e9e02024-02-24 18:30:29 -0800183 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 Wang66546642024-02-10 16:37:05 -0800201 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 Kujawa1a2e9e02024-02-24 18:30:29 -0800233 }
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 Wang66546642024-02-10 16:37:05 -0800253
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 Kujawa1a2e9e02024-02-24 18:30:29 -0800259 }
Niko Sohmers2d108762024-02-02 20:21:14 -0800260 draw(): void {
261 this.reset();
262 this.drawField();
263
Filip Kujawa1a2e9e02024-02-24 18:30:29 -0800264 if (this.superstructureStatus) {
265 this.superstructureState.innerHTML =
266 SuperstructureState[this.superstructureStatus.state()];
Mirabel Wang66546642024-02-10 16:37:05 -0800267
Filip Kujawa1a2e9e02024-02-24 18:30:29 -0800268 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
279 this.turret_position.innerHTML = this.superstructureStatus.shooter().aimer().turretPosition().toString();
280 this.turret_velocity.innerHTML = this.superstructureStatus.shooter().aimer().turretVelocity().toString();
281 this.target_distance.innerHTML = this.superstructureStatus.shooter().aimer().targetDistance().toString();
282 this.shot_distance.innerHTML = this.superstructureStatus.shooter().aimer().shotDistance().toString();
283
284 if (!this.superstructureStatus.intakePivot() ||
285 !this.superstructureStatus.intakePivot().zeroed()) {
286 this.setZeroing(this.intakePivot);
287 } else if (this.superstructureStatus.intakePivot().estopped()) {
288 this.setEstopped(this.intakePivot);
289 } else {
290 this.setTargetValue(
291 this.intakePivot,
292 this.superstructureStatus.intakePivot().unprofiledGoalPosition(),
293 this.superstructureStatus.intakePivot().estimatorState().position(),
294 1e-3);
295 }
296
297 this.intakePivotAbs.innerHTML = this.superstructureStatus.intakePivot().estimatorState().absolutePosition().toString();
298
299 if (!this.superstructureStatus.climber() ||
300 !this.superstructureStatus.climber().zeroed()) {
301 this.setZeroing(this.climber);
302 } else if (this.superstructureStatus.climber().estopped()) {
303 this.setEstopped(this.climber);
304 } else {
305 this.setTargetValue(
306 this.climber,
307 this.superstructureStatus.climber().unprofiledGoalPosition(),
308 this.superstructureStatus.climber().estimatorState().position(),
309 1e-3);
310 }
311
312 this.climberAbs.innerHTML = this.superstructureStatus.climber().estimatorState().absolutePosition().toString();
313 this.climberPot.innerHTML = this.superstructureStatus.climber().estimatorState().potPosition().toString();
314
315 if (!this.superstructureStatus.extend() ||
316 !this.superstructureStatus.extend().zeroed()) {
317 this.setZeroing(this.extend);
318 } else if (this.superstructureStatus.extend().estopped()) {
319 this.setEstopped(this.extend);
320 } else {
321 this.setTargetValue(
322 this.climber,
323 this.superstructureStatus.extend().unprofiledGoalPosition(),
324 this.superstructureStatus.extend().estimatorState().position(),
325 1e-3);
326 }
327
328 this.extendAbs.innerHTML = this.superstructureStatus.extend().estimatorState().absolutePosition().toString();
329 this.extendPot.innerHTML = this.superstructureStatus.extend().estimatorState().potPosition().toString();
330
331 if (!this.superstructureStatus.shooter().turret() ||
332 !this.superstructureStatus.shooter().turret().zeroed()) {
333 this.setZeroing(this.turret);
334 } else if (this.superstructureStatus.shooter().turret().estopped()) {
335 this.setEstopped(this.turret);
336 } else {
337 this.setTargetValue(
338 this.turret,
339 this.superstructureStatus.shooter().turret().unprofiledGoalPosition(),
340 this.superstructureStatus.shooter().turret().estimatorState().position(),
341 1e-3);
342 }
343
344 this.turretAbs.innerHTML = this.superstructureStatus.shooter().turret().estimatorState().absolutePosition().toString();
345 this.turretPot.innerHTML = this.superstructureStatus.shooter().turret().estimatorState().potPosition().toString();
346
347 if (!this.superstructureStatus.shooter().catapult() ||
348 !this.superstructureStatus.shooter().catapult().zeroed()) {
349 this.setZeroing(this.catapult);
350 } else if (this.superstructureStatus.shooter().catapult().estopped()) {
351 this.setEstopped(this.catapult);
352 } else {
353 this.setTargetValue(
354 this.catapult,
355 this.superstructureStatus.shooter().catapult().unprofiledGoalPosition(),
356 this.superstructureStatus.shooter().catapult().estimatorState().position(),
357 1e-3);
358 }
359
360 this.catapultAbs.innerHTML = this.superstructureStatus.shooter().catapult().estimatorState().absolutePosition().toString();
361 this.catapultPot.innerHTML = this.superstructureStatus.shooter().catapult().estimatorState().potPosition().toString();
362
363 if (!this.superstructureStatus.shooter().altitude() ||
364 !this.superstructureStatus.shooter().altitude().zeroed()) {
365 this.setZeroing(this.altitude);
366 } else if (this.superstructureStatus.shooter().altitude().estopped()) {
367 this.setEstopped(this.altitude);
368 } else {
369 this.setTargetValue(
370 this.altitude,
371 this.superstructureStatus.shooter().altitude().unprofiledGoalPosition(),
372 this.superstructureStatus.shooter().altitude().estimatorState().position(),
373 1e-3);
374 }
375
376 this.altitudeAbs.innerHTML = this.superstructureStatus.shooter().altitude().estimatorState().absolutePosition().toString();
377 this.altitudePot.innerHTML = this.superstructureStatus.shooter().altitude().estimatorState().potPosition().toString();
378
379 let zeroingErrors: string = 'Intake Pivot Errors:' +
380 '<br/>';
381 for (let i = 0; i < this.superstructureStatus.intakePivot()
382 .estimatorState()
383 .errorsLength();
384 i++) {
385 zeroingErrors += ZeroingError[this.superstructureStatus.intakePivot()
386 .estimatorState()
387 .errors(i)] +
388 '<br/>';
389 }
390 zeroingErrors += '<br/>' +
391 'Climber Errors:' +
392 '<br/>';
393 for (let i = 0; i < this.superstructureStatus.climber().estimatorState().errorsLength();
394 i++) {
395 zeroingErrors += ZeroingError[this.superstructureStatus.climber().estimatorState().errors(i)] +
396 '<br/>';
397 }
398 zeroingErrors += '<br/>' +
399 'Extend Errors:' +
400 '<br/>';
401 for (let i = 0; i < this.superstructureStatus.extend().estimatorState().errorsLength();
402 i++) {
403 zeroingErrors += ZeroingError[this.superstructureStatus.extend().estimatorState().errors(i)] +
404 '<br/>';
405 }
406 zeroingErrors += '<br/>' +
407 'Turret Errors:' +
408 '<br/>';
409 for (let i = 0; i < this.superstructureStatus.shooter().turret().estimatorState().errorsLength();
410 i++) {
411 zeroingErrors += ZeroingError[this.superstructureStatus.shooter().turret().estimatorState().errors(i)] +
412 '<br/>';
413 }
414 zeroingErrors += '<br/>' +
415 'Catapult Errors:' +
416 '<br/>';
417 for (let i = 0; i < this.superstructureStatus.shooter().catapult().estimatorState().errorsLength();
418 i++) {
419 zeroingErrors += ZeroingError[this.superstructureStatus.shooter().catapult().estimatorState().errors(i)] +
420 '<br/>';
421 }
422 zeroingErrors += '<br/>' +
423 'Altitude Errors:' +
424 '<br/>';
425 for (let i = 0; i < this.superstructureStatus.shooter().altitude().estimatorState().errorsLength();
426 i++) {
427 zeroingErrors += ZeroingError[this.superstructureStatus.shooter().altitude().estimatorState().errors(i)] +
428 '<br/>';
429 }
430 this.zeroingFaults.innerHTML = zeroingErrors;
431 }
432
433 if (this.drivetrainPosition) {
434 this.leftDrivetrainEncoder.innerHTML =
435 this.drivetrainPosition.leftEncoder().toString();
436
437 this.rightDrivetrainEncoder.innerHTML =
438 this.drivetrainPosition.rightEncoder().toString();
Mirabel Wang66546642024-02-10 16:37:05 -0800439 }
440
441 if (this.drivetrainCANPosition) {
442 this.falconRightFrontPosition.innerHTML =
443 this.drivetrainCANPosition.talonfxs(0).position().toString();
444
445 this.falconRightBackPosition.innerHTML =
446 this.drivetrainCANPosition.talonfxs(1).position().toString();
447
448 this.falconLeftFrontPosition.innerHTML =
449 this.drivetrainCANPosition.talonfxs(2).position().toString();
450
451 this.falconLeftBackPosition.innerHTML =
452 this.drivetrainCANPosition.talonfxs(3).position().toString();
453 }
454
455 if (this.drivetrainStatus && this.drivetrainStatus.trajectoryLogging()) {
456 this.drawRobot(
457 this.drivetrainStatus.trajectoryLogging().x(),
458 this.drivetrainStatus.trajectoryLogging().y(),
459 this.drivetrainStatus.trajectoryLogging().theta(), '#000000FF',
460 false);
461 }
462
463 if (this.localizerOutput) {
464 if (!this.localizerOutput.zeroed()) {
465 this.setZeroing(this.x);
466 this.setZeroing(this.y);
467 this.setZeroing(this.theta);
468 } else {
469 this.setValue(this.x, this.localizerOutput.x());
470 this.setValue(this.y, this.localizerOutput.y());
471 this.setValue(this.theta, this.localizerOutput.theta());
472 }
473
474 this.drawRobot(
475 this.localizerOutput.x(), this.localizerOutput.y(),
476 this.localizerOutput.theta());
477 }
Filip Kujawa1a2e9e02024-02-24 18:30:29 -0800478
Niko Sohmers2d108762024-02-02 20:21:14 -0800479 window.requestAnimationFrame(() => this.draw());
480 }
481
482 reset(): void {
483 const ctx = this.canvas.getContext('2d');
484 // Empty space from the canvas boundary to the image
485 const IMAGE_PADDING = 10;
486 ctx.setTransform(1, 0, 0, 1, 0, 0);
487 const size = window.innerHeight * 0.9;
488 ctx.canvas.height = size;
489 const width = size / 2 + 20;
490 ctx.canvas.width = width;
491 ctx.clearRect(0, 0, size, width);
492
493 // Translate to center of display.
494 ctx.translate(width / 2, size / 2);
495 // Coordinate system is:
496 // x -> forward.
497 // y -> to the left.
498 ctx.rotate(-Math.PI / 2);
499 ctx.scale(1, -1);
500
501 const M_TO_PX = (size - IMAGE_PADDING) / FIELD_LENGTH;
502 ctx.scale(M_TO_PX, M_TO_PX);
503 ctx.lineWidth = 1 / M_TO_PX;
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800504 }
505}