blob: c165bcc2162560c74db2274fceafed3096d739a3 [file] [log] [blame]
James Kuszmauldac091f2022-03-22 09:35:06 -07001import {ByteBuffer} from 'flatbuffers';
Philipp Schradere625ba22020-11-16 20:11:37 -08002import {Connection} from 'org_frc971/aos/network/www/proxy';
3import * as flatbuffers_builder from 'org_frc971/external/com_github_google_flatbuffers/ts/builder';
James Kuszmauldac091f2022-03-22 09:35:06 -07004import {Status as DrivetrainStatus} from 'org_frc971/frc971/control_loops/drivetrain/drivetrain_status_generated';
5import {LocalizerDebug, RejectionReason, ImageMatchDebug} from 'org_frc971/y2020/control_loops/drivetrain/localizer_debug_generated';
6import {Status as SuperstructureStatus, FlywheelControllerStatus} from 'org_frc971/y2020/control_loops/superstructure/superstructure_status_generated'
7import {ImageMatchResult} from 'org_frc971/y2020/vision/sift/sift_generated';
Alex Perry5427c9a2020-02-15 17:43:45 -08008
Alex Perry2124ae82020-03-07 14:19:06 -08009import {FIELD_LENGTH, FIELD_WIDTH, FT_TO_M, IN_TO_M} from './constants';
10
Alex Perryb49a3fb2020-02-29 15:26:54 -080011// (0,0) is field center, +X is toward red DS
Alex Perry5427c9a2020-02-15 17:43:45 -080012const FIELD_SIDE_Y = FIELD_WIDTH / 2;
Alex Perryb49a3fb2020-02-29 15:26:54 -080013const FIELD_EDGE_X = FIELD_LENGTH / 2;
Alex Perry5427c9a2020-02-15 17:43:45 -080014
15const DS_WIDTH = 69 * IN_TO_M;
16const DS_ANGLE = 20 * Math.PI / 180;
Alex Perryb49a3fb2020-02-29 15:26:54 -080017const DS_END_X = FIELD_EDGE_X - DS_WIDTH * Math.sin(DS_ANGLE);
Alex Perry5427c9a2020-02-15 17:43:45 -080018const DS_INSIDE_Y = FIELD_SIDE_Y - DS_WIDTH * Math.cos(DS_ANGLE);
19
Alex Perryb49a3fb2020-02-29 15:26:54 -080020const TRENCH_X = 108 * IN_TO_M;
Alex Perry5427c9a2020-02-15 17:43:45 -080021const TRENCH_WIDTH = 55.5 * IN_TO_M;
22const TRENCH_INSIDE = FIELD_SIDE_Y - TRENCH_WIDTH;
23
24const SPINNER_LENGTH = 30 * IN_TO_M;
Alex Perryb49a3fb2020-02-29 15:26:54 -080025const SPINNER_TOP_X = 374.59 * IN_TO_M - FIELD_EDGE_X;
Alex Perry5427c9a2020-02-15 17:43:45 -080026const SPINNER_BOTTOM_X = SPINNER_TOP_X - SPINNER_LENGTH;
27
Alex Perryb49a3fb2020-02-29 15:26:54 -080028const SHIELD_BOTTOM_X = -116 * IN_TO_M;
Alex Perry5427c9a2020-02-15 17:43:45 -080029const SHIELD_BOTTOM_Y = 43.75 * IN_TO_M;
30
Alex Perryb49a3fb2020-02-29 15:26:54 -080031const SHIELD_TOP_X = 116 * IN_TO_M;
Alex Perry5427c9a2020-02-15 17:43:45 -080032const SHIELD_TOP_Y = -43.75 * IN_TO_M;
33
Alex Perryb49a3fb2020-02-29 15:26:54 -080034const SHIELD_RIGHT_X = -51.06 * IN_TO_M;
Alex Perry5427c9a2020-02-15 17:43:45 -080035const SHIELD_RIGHT_Y = -112.88 * IN_TO_M;
36
Alex Perryb49a3fb2020-02-29 15:26:54 -080037const SHIELD_LEFT_X = 51.06 * IN_TO_M;
Alex Perry5427c9a2020-02-15 17:43:45 -080038const SHIELD_LEFT_Y = 112.88 * IN_TO_M;
39
40const SHIELD_CENTER_TOP_X = (SHIELD_TOP_X + SHIELD_LEFT_X) / 2
41const SHIELD_CENTER_TOP_Y = (SHIELD_TOP_Y + SHIELD_LEFT_Y) / 2
42
43const SHIELD_CENTER_BOTTOM_X = (SHIELD_BOTTOM_X + SHIELD_RIGHT_X) / 2
44const SHIELD_CENTER_BOTTOM_Y = (SHIELD_BOTTOM_Y + SHIELD_RIGHT_Y) / 2
45
Alex Perryb49a3fb2020-02-29 15:26:54 -080046const INITIATION_X = FIELD_EDGE_X - 120 * IN_TO_M;
Alex Perry5427c9a2020-02-15 17:43:45 -080047
Alex Perryb49a3fb2020-02-29 15:26:54 -080048const TARGET_ZONE_TIP_X = FIELD_EDGE_X - 30 * IN_TO_M;
Alex Perry5427c9a2020-02-15 17:43:45 -080049const TARGET_ZONE_WIDTH = 48 * IN_TO_M;
50const LOADING_ZONE_WIDTH = 60 * IN_TO_M;
51
Austin Schuhb863f932021-10-23 23:29:09 -070052const ROBOT_WIDTH = 34 * IN_TO_M;
53const ROBOT_LENGTH = 36 * IN_TO_M;
Alex Perry2124ae82020-03-07 14:19:06 -080054
Alex Perry87cc63d2021-10-27 21:18:42 -070055const PI_COLORS = ['#ff00ff', '#ffff00', '#000000', '#00ffff', '#ffa500'];
James Kuszmaul5e6aa252021-08-28 22:19:29 -070056
Alex Perry5427c9a2020-02-15 17:43:45 -080057export class FieldHandler {
58 private canvas = document.createElement('canvas');
Alex Perry87cc63d2021-10-27 21:18:42 -070059 private imageMatchResult = new Map<string, ImageMatchResult>();
Philipp Schradera227d042020-11-14 17:33:52 -080060 private drivetrainStatus: DrivetrainStatus|null = null;
James Kuszmaul5e6aa252021-08-28 22:19:29 -070061 private superstructureStatus: SuperstructureStatus|null = null;
Alex Perry87cc63d2021-10-27 21:18:42 -070062
James Kuszmaulf75ecd62021-10-23 14:33:46 -070063 // Image information indexed by timestamp (seconds since the epoch), so that
64 // we can stop displaying images after a certain amount of time.
65 private localizerImageMatches = new Map<number, LocalizerDebug>();
Austin Schuh146c0bc2021-11-07 22:33:28 -080066 private outerTarget: HTMLElement =
Alex Perry87cc63d2021-10-27 21:18:42 -070067 (document.getElementById('outer_target') as HTMLElement);
Austin Schuh146c0bc2021-11-07 22:33:28 -080068 private innerTarget: HTMLElement =
Alex Perry87cc63d2021-10-27 21:18:42 -070069 (document.getElementById('inner_target') as HTMLElement);
70 private x: HTMLElement = (document.getElementById('x') as HTMLElement);
71 private y: HTMLElement = (document.getElementById('y') as HTMLElement);
72 private theta: HTMLElement =
73 (document.getElementById('theta') as HTMLElement);
74 private shotDistance: HTMLElement =
75 (document.getElementById('shot_distance') as HTMLElement);
76 private finisher: HTMLElement =
77 (document.getElementById('finisher') as HTMLElement);
78 private leftAccelerator: HTMLElement =
79 (document.getElementById('left_accelerator') as HTMLElement);
80 private rightAccelerator: HTMLElement =
81 (document.getElementById('right_accelerator') as HTMLElement);
82 private innerPort: HTMLElement =
83 (document.getElementById('inner_port') as HTMLElement);
84 private hood: HTMLElement = (document.getElementById('hood') as HTMLElement);
85 private turret: HTMLElement =
86 (document.getElementById('turret') as HTMLElement);
Austin Schuh146c0bc2021-11-07 22:33:28 -080087 private ballsShot: HTMLElement =
88 (document.getElementById('balls_shot') as HTMLElement);
Alex Perry87cc63d2021-10-27 21:18:42 -070089 private intake: HTMLElement =
90 (document.getElementById('intake') as HTMLElement);
91 private imagesAcceptedCounter: HTMLElement =
92 (document.getElementById('images_accepted') as HTMLElement);
93 private imagesRejectedCounter: HTMLElement =
94 (document.getElementById('images_rejected') as HTMLElement);
95 private rejectionReasonCells: HTMLElement[] = [];
Alex Perry5427c9a2020-02-15 17:43:45 -080096
Alex Perryb49a3fb2020-02-29 15:26:54 -080097 constructor(private readonly connection: Connection) {
Austin Schuh840132b2021-10-17 17:40:14 -070098 (document.getElementById('field') as HTMLElement).appendChild(this.canvas);
Alex Perryb49a3fb2020-02-29 15:26:54 -080099
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700100 for (const value in RejectionReason) {
101 // Typescript generates an iterator that produces both numbers and
102 // strings... don't do anything on the string iterations.
103 if (isNaN(Number(value))) {
104 continue;
105 }
Alex Perry87cc63d2021-10-27 21:18:42 -0700106 const row = document.createElement('div');
107 const nameCell = document.createElement('div');
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700108 nameCell.innerHTML = RejectionReason[value];
109 row.appendChild(nameCell);
Alex Perry87cc63d2021-10-27 21:18:42 -0700110 const valueCell = document.createElement('div');
111 valueCell.innerHTML = 'NA';
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700112 this.rejectionReasonCells.push(valueCell);
113 row.appendChild(valueCell);
Alex Perry87cc63d2021-10-27 21:18:42 -0700114 document.getElementById('vision_readouts').appendChild(row);
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700115 }
116
James Kuszmaul286b80c2021-10-23 21:56:33 -0700117 for (let ii = 0; ii < PI_COLORS.length; ++ii) {
Alex Perry87cc63d2021-10-27 21:18:42 -0700118 const legendEntry = document.createElement('div');
James Kuszmaul286b80c2021-10-23 21:56:33 -0700119 legendEntry.style.color = PI_COLORS[ii];
Alex Perry87cc63d2021-10-27 21:18:42 -0700120 legendEntry.innerHTML = 'PI' + (ii + 1).toString()
121 document.getElementById('legend').appendChild(legendEntry);
James Kuszmaul286b80c2021-10-23 21:56:33 -0700122 }
123
Alex Perryb49a3fb2020-02-29 15:26:54 -0800124 this.connection.addConfigHandler(() => {
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700125 // Go through and register handlers for both all the individual pis as
126 // well as the local pi. Depending on the node that we are running on,
127 // different subsets of these will be available.
James Kuszmaul286b80c2021-10-23 21:56:33 -0700128 for (const prefix of ['', '/pi1', '/pi2', '/pi3', '/pi4', '/pi5']) {
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700129 this.connection.addHandler(
James Kuszmaul286b80c2021-10-23 21:56:33 -0700130 prefix + '/camera', ImageMatchResult.getFullyQualifiedName(),
131 (res) => {
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700132 this.handleImageMatchResult(prefix, res);
133 });
134 }
James Kuszmaul527038a2020-12-21 23:40:44 -0800135 this.connection.addHandler(
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700136 '/drivetrain', LocalizerDebug.getFullyQualifiedName(), (data) => {
137 this.handleLocalizerDebug(data);
138 });
139 this.connection.addHandler(
James Kuszmaul527038a2020-12-21 23:40:44 -0800140 '/drivetrain', DrivetrainStatus.getFullyQualifiedName(), (data) => {
141 this.handleDrivetrainStatus(data);
142 });
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700143 this.connection.addHandler(
144 '/superstructure', SuperstructureStatus.getFullyQualifiedName(),
145 (data) => {
146 this.handleSuperstructureStatus(data);
147 });
Alex Perryb49a3fb2020-02-29 15:26:54 -0800148 });
Alex Perryb49a3fb2020-02-29 15:26:54 -0800149 }
150
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700151 private handleImageMatchResult(prefix: string, data: Uint8Array): void {
Philipp Schradere625ba22020-11-16 20:11:37 -0800152 const fbBuffer = new ByteBuffer(data);
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700153 this.imageMatchResult.set(
James Kuszmauldac091f2022-03-22 09:35:06 -0700154 prefix, ImageMatchResult.getRootAsImageMatchResult(fbBuffer));
Alex Perryb49a3fb2020-02-29 15:26:54 -0800155 }
156
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700157 private handleLocalizerDebug(data: Uint8Array): void {
158 const now = Date.now() / 1000.0;
159
160 const fbBuffer = new ByteBuffer(data);
161 this.localizerImageMatches.set(
James Kuszmauldac091f2022-03-22 09:35:06 -0700162 now, LocalizerDebug.getRootAsLocalizerDebug(fbBuffer));
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700163
164 const debug = this.localizerImageMatches.get(now);
165
166 if (debug.statistics()) {
167 this.imagesAcceptedCounter.innerHTML =
168 debug.statistics().totalAccepted().toString();
169 this.imagesRejectedCounter.innerHTML =
170 (debug.statistics().totalCandidates() -
171 debug.statistics().totalAccepted())
172 .toString();
173 if (debug.statistics().rejectionReasonCountLength() ==
174 this.rejectionReasonCells.length) {
175 for (let ii = 0; ii < debug.statistics().rejectionReasonCountLength();
176 ++ii) {
177 this.rejectionReasonCells[ii].innerHTML =
178 debug.statistics().rejectionReasonCount(ii).toString();
179 }
180 } else {
Alex Perry87cc63d2021-10-27 21:18:42 -0700181 console.error('Unexpected number of rejection reasons in counter.');
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700182 }
183 this.imagesRejectedCounter.innerHTML =
184 (debug.statistics().totalCandidates() -
185 debug.statistics().totalAccepted())
186 .toString();
187 }
188 }
189
Alex Perry2124ae82020-03-07 14:19:06 -0800190 private handleDrivetrainStatus(data: Uint8Array): void {
Philipp Schradere625ba22020-11-16 20:11:37 -0800191 const fbBuffer = new ByteBuffer(data);
James Kuszmauldac091f2022-03-22 09:35:06 -0700192 this.drivetrainStatus = DrivetrainStatus.getRootAsStatus(fbBuffer);
Alex Perry2124ae82020-03-07 14:19:06 -0800193 }
194
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700195 private handleSuperstructureStatus(data: Uint8Array): void {
196 const fbBuffer = new ByteBuffer(data);
James Kuszmauldac091f2022-03-22 09:35:06 -0700197 this.superstructureStatus = SuperstructureStatus.getRootAsStatus(fbBuffer);
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700198 }
199
Alex Perry5427c9a2020-02-15 17:43:45 -0800200 drawField(): void {
201 const MY_COLOR = 'red';
202 const OTHER_COLOR = 'blue';
203 const ctx = this.canvas.getContext('2d');
204 // draw perimiter
205 ctx.beginPath();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800206 ctx.moveTo(FIELD_EDGE_X, DS_INSIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800207 ctx.lineTo(DS_END_X, FIELD_SIDE_Y);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800208 ctx.lineTo(-DS_END_X, FIELD_SIDE_Y);
209 ctx.lineTo(-FIELD_EDGE_X, DS_INSIDE_Y);
210 ctx.lineTo(-FIELD_EDGE_X, -DS_INSIDE_Y);
211 ctx.lineTo(-DS_END_X, -FIELD_SIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800212 ctx.lineTo(DS_END_X, -FIELD_SIDE_Y);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800213 ctx.lineTo(FIELD_EDGE_X, -DS_INSIDE_Y);
214 ctx.lineTo(FIELD_EDGE_X, DS_INSIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800215 ctx.stroke();
216
217 // draw shield generator
218 ctx.beginPath();
219 ctx.moveTo(SHIELD_BOTTOM_X, SHIELD_BOTTOM_Y);
220 ctx.lineTo(SHIELD_RIGHT_X, SHIELD_RIGHT_Y);
221 ctx.lineTo(SHIELD_TOP_X, SHIELD_TOP_Y);
222 ctx.lineTo(SHIELD_LEFT_X, SHIELD_LEFT_Y);
223 ctx.lineTo(SHIELD_BOTTOM_X, SHIELD_BOTTOM_Y);
224 ctx.moveTo(SHIELD_CENTER_TOP_X, SHIELD_CENTER_TOP_Y);
225 ctx.lineTo(SHIELD_CENTER_BOTTOM_X, SHIELD_CENTER_BOTTOM_Y);
226 ctx.stroke();
227
Alex Perryb49a3fb2020-02-29 15:26:54 -0800228 this.drawHalfField(ctx, 'red');
229 ctx.rotate(Math.PI);
230 this.drawHalfField(ctx, 'blue');
231 ctx.rotate(Math.PI);
232 }
Alex Perry5427c9a2020-02-15 17:43:45 -0800233
Alex Perryb49a3fb2020-02-29 15:26:54 -0800234 drawHalfField(ctx, color: string): void {
235 // trenches
236 ctx.strokeStyle = color;
Alex Perry5427c9a2020-02-15 17:43:45 -0800237 ctx.beginPath();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800238 ctx.moveTo(TRENCH_X, FIELD_SIDE_Y);
239 ctx.lineTo(TRENCH_X, TRENCH_INSIDE);
240 ctx.lineTo(-TRENCH_X, TRENCH_INSIDE);
241 ctx.lineTo(-TRENCH_X, FIELD_SIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800242 ctx.stroke();
243
244 ctx.strokeStyle = 'black';
245 ctx.beginPath();
246 ctx.moveTo(SPINNER_TOP_X, FIELD_SIDE_Y);
247 ctx.lineTo(SPINNER_TOP_X, TRENCH_INSIDE);
248 ctx.lineTo(SPINNER_BOTTOM_X, TRENCH_INSIDE);
249 ctx.lineTo(SPINNER_BOTTOM_X, FIELD_SIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800250 ctx.stroke();
251
Alex Perry5427c9a2020-02-15 17:43:45 -0800252 ctx.beginPath();
253 ctx.moveTo(INITIATION_X, FIELD_SIDE_Y);
254 ctx.lineTo(INITIATION_X, -FIELD_SIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800255 ctx.stroke();
256
Alex Perryb49a3fb2020-02-29 15:26:54 -0800257 // target/loading
258 ctx.strokeStyle = color;
Alex Perry5427c9a2020-02-15 17:43:45 -0800259 ctx.beginPath();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800260 ctx.moveTo(FIELD_EDGE_X, DS_INSIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800261 ctx.lineTo(TARGET_ZONE_TIP_X, DS_INSIDE_Y - 0.5 * TARGET_ZONE_WIDTH);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800262 ctx.lineTo(FIELD_EDGE_X, DS_INSIDE_Y - TARGET_ZONE_WIDTH);
Alex Perry5427c9a2020-02-15 17:43:45 -0800263
Alex Perryb49a3fb2020-02-29 15:26:54 -0800264 ctx.moveTo(-FIELD_EDGE_X, DS_INSIDE_Y);
265 ctx.lineTo(-TARGET_ZONE_TIP_X, DS_INSIDE_Y - 0.5 * LOADING_ZONE_WIDTH);
266 ctx.lineTo(-FIELD_EDGE_X, DS_INSIDE_Y - LOADING_ZONE_WIDTH);
Alex Perry5427c9a2020-02-15 17:43:45 -0800267 ctx.stroke();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800268 }
Alex Perry5427c9a2020-02-15 17:43:45 -0800269
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700270 drawCamera(
271 x: number, y: number, theta: number, color: string = 'blue',
272 extendLines: boolean = true): void {
Alex Perryb49a3fb2020-02-29 15:26:54 -0800273 const ctx = this.canvas.getContext('2d');
274 ctx.save();
275 ctx.translate(x, y);
276 ctx.rotate(theta);
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700277 ctx.strokeStyle = color;
Alex Perry5427c9a2020-02-15 17:43:45 -0800278 ctx.beginPath();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800279 ctx.moveTo(0.5, 0.5);
280 ctx.lineTo(0, 0);
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700281 if (extendLines) {
282 ctx.lineTo(100.0, 0);
283 ctx.lineTo(0, 0);
284 }
Alex Perryb49a3fb2020-02-29 15:26:54 -0800285 ctx.lineTo(0.5, -0.5);
Alex Perry5427c9a2020-02-15 17:43:45 -0800286 ctx.stroke();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800287 ctx.beginPath();
Philipp Schradere625ba22020-11-16 20:11:37 -0800288 ctx.arc(0, 0, 0.25, -Math.PI / 4, Math.PI / 4);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800289 ctx.stroke();
290 ctx.restore();
291 }
292
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700293 drawRobot(
294 x: number, y: number, theta: number, turret: number|null,
295 color: string = 'blue', dashed: boolean = false,
296 extendLines: boolean = true): void {
Alex Perry2124ae82020-03-07 14:19:06 -0800297 const ctx = this.canvas.getContext('2d');
298 ctx.save();
299 ctx.translate(x, y);
300 ctx.rotate(theta);
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700301 ctx.strokeStyle = color;
302 if (dashed) {
303 ctx.setLineDash([0.05, 0.05]);
304 } else {
305 // Empty array = solid line.
306 ctx.setLineDash([]);
307 }
Alex Perry2124ae82020-03-07 14:19:06 -0800308 ctx.rect(-ROBOT_LENGTH / 2, -ROBOT_WIDTH / 2, ROBOT_LENGTH, ROBOT_WIDTH);
309 ctx.stroke();
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700310
311 // Draw line indicating which direction is forwards on the robot.
312 ctx.beginPath();
313 ctx.moveTo(0, 0);
314 if (extendLines) {
315 ctx.lineTo(1000.0, 0);
316 } else {
317 ctx.lineTo(ROBOT_LENGTH / 2.0, 0);
318 }
319 ctx.stroke();
320
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700321 if (turret) {
322 ctx.save();
323 ctx.rotate(turret + Math.PI);
324 const turretRadius = ROBOT_WIDTH / 4.0;
Alex Perry87cc63d2021-10-27 21:18:42 -0700325 ctx.strokeStyle = 'red';
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700326 // Draw circle for turret.
327 ctx.beginPath();
328 ctx.arc(0, 0, turretRadius, 0, 2.0 * Math.PI);
329 ctx.stroke();
330 // Draw line in circle to show forwards.
331 ctx.beginPath();
332 ctx.moveTo(0, 0);
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700333 if (extendLines) {
334 ctx.lineTo(1000.0, 0);
335 } else {
336 ctx.lineTo(turretRadius, 0);
337 }
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700338 ctx.stroke();
339 ctx.restore();
340 }
Alex Perry2124ae82020-03-07 14:19:06 -0800341 ctx.restore();
342 }
343
Alex Perry87cc63d2021-10-27 21:18:42 -0700344 setShooter(div: HTMLElement, flywheelStatus: FlywheelControllerStatus): void {
345 const currentVelocity = flywheelStatus.angularVelocity();
346 const goal = flywheelStatus.angularVelocityGoal();
347 // Show it as 'near' if difference between
348 if (Math.abs(currentVelocity - goal) < 5) {
349 this.setNear(div, currentVelocity.toFixed(2));
350 return;
351 }
352 div.classList.remove('faulted');
353 div.classList.remove('zeroing');
354 div.classList.remove('near');
355 div.innerHTML = currentVelocity.toFixed(2);
Austin Schuh840132b2021-10-17 17:40:14 -0700356 }
Alex Perry87cc63d2021-10-27 21:18:42 -0700357
358 setZeroing(div: HTMLElement): void {
359 div.innerHTML = 'zeroing';
360 div.classList.remove('faulted');
361 div.classList.add('zeroing');
362 div.classList.remove('near');
Austin Schuh840132b2021-10-17 17:40:14 -0700363 }
Alex Perry87cc63d2021-10-27 21:18:42 -0700364
365 setEstopped(div: HTMLElement): void {
366 div.innerHTML = 'estopped';
367 div.classList.add('faulted');
368 div.classList.remove('zeroing');
369 div.classList.remove('near');
370 }
371
372 setNear(div: HTMLElement, val: string): void {
373 div.innerHTML = val;
374 div.classList.remove('faulted');
375 div.classList.remove('zeroing');
376 div.classList.add('near');
377 }
378
379 setTargetValue(
380 div: HTMLElement, target: number, val: number, tolerance: number): void {
381 div.innerHTML = val.toFixed(4);
382 div.classList.remove('faulted');
383 div.classList.remove('zeroing');
384 if (Math.abs(target - val) < tolerance) {
385 div.classList.add('near');
386 } else {
387 div.classList.remove('near');
388 }
389 }
390
391 setValue(div: HTMLElement, val: number): void {
392 div.innerHTML = val.toFixed(4);
393 div.classList.remove('faulted');
394 div.classList.remove('zeroing');
395 div.classList.remove('near');
Austin Schuh840132b2021-10-17 17:40:14 -0700396 }
397
Philipp Schradere625ba22020-11-16 20:11:37 -0800398 draw(): void {
Alex Perryb49a3fb2020-02-29 15:26:54 -0800399 this.reset();
400 this.drawField();
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700401
402 // Draw the matches with debugging information from the localizer.
403 const now = Date.now() / 1000.0;
404 for (const [time, value] of this.localizerImageMatches) {
405 const age = now - time;
406 const kRemovalAge = 2.0;
407 if (age > kRemovalAge) {
408 this.localizerImageMatches.delete(time);
409 continue;
410 }
411 const ageAlpha = (kRemovalAge - age) / kRemovalAge
412 for (let i = 0; i < value.matchesLength(); i++) {
413 const imageDebug = value.matches(i);
414 const x = imageDebug.impliedRobotX();
415 const y = imageDebug.impliedRobotY();
416 const theta = imageDebug.impliedRobotTheta();
417 const cameraX = imageDebug.cameraX();
418 const cameraY = imageDebug.cameraY();
419 const cameraTheta = imageDebug.cameraTheta();
420 const accepted = imageDebug.accepted();
421 // Make camera readings fade over time.
422 const alpha = Math.round(255 * ageAlpha).toString(16).padStart(2, '0');
423 const dashed = false;
Alex Perry87cc63d2021-10-27 21:18:42 -0700424 const acceptedRgb = accepted ? '#00FF00' : '#FF0000';
James Kuszmaul286b80c2021-10-23 21:56:33 -0700425 const acceptedRgba = acceptedRgb + alpha;
426 const cameraRgb = PI_COLORS[imageDebug.camera()];
427 const cameraRgba = cameraRgb + alpha;
428 this.drawRobot(x, y, theta, null, acceptedRgba, dashed, false);
429 this.drawCamera(cameraX, cameraY, cameraTheta, cameraRgba, false);
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700430 }
431 }
432
433 // draw cameras from ImageMatchResults directly (helpful when viewing page
434 // on the pis individually).
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700435 for (const keyPair of this.imageMatchResult) {
436 const value = keyPair[1];
437 for (let i = 0; i < value.cameraPosesLength(); i++) {
438 const pose = value.cameraPoses(i);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800439 const mat = pose.fieldToCamera();
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700440 // Matrix layout:
441 // [0, 1, 2, 3]
442 // [4, 5, 6, 7]
443 // [8, 9, 10, 11]
444 // [12, 13, 14, 15]
Alex Perryb49a3fb2020-02-29 15:26:54 -0800445 const x = mat.data(3);
446 const y = mat.data(7);
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700447 const theta = Math.atan2(mat.data(6), mat.data(2));
James Kuszmaul286b80c2021-10-23 21:56:33 -0700448 const cameraColor = (keyPair[0].length > 0) ?
449 PI_COLORS[Number(keyPair[0][3]) - 1] :
450 'blue';
451 this.drawCamera(x, y, theta, cameraColor);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800452 }
453 }
454
Alex Perry2124ae82020-03-07 14:19:06 -0800455 if (this.drivetrainStatus) {
Austin Schuh840132b2021-10-17 17:40:14 -0700456 if (!this.drivetrainStatus.zeroing().zeroed()) {
457 this.setZeroing(this.x);
458 this.setZeroing(this.y);
459 this.setZeroing(this.theta);
460 } else if (this.drivetrainStatus.zeroing().faulted()) {
461 this.setEstopped(this.x);
462 this.setEstopped(this.y);
463 this.setEstopped(this.theta);
464 } else {
465 this.setValue(this.x, this.drivetrainStatus.x());
466 this.setValue(this.y, this.drivetrainStatus.y());
467 this.setValue(this.theta, this.drivetrainStatus.theta());
468 }
469
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700470 if (this.superstructureStatus) {
471 this.shotDistance.innerHTML =
472 this.superstructureStatus.aimer().shotDistance().toFixed(2);
Alex Perry87cc63d2021-10-27 21:18:42 -0700473 this.setShooter(
474 this.finisher, this.superstructureStatus.shooter().finisher());
475 this.setShooter(
476 this.leftAccelerator,
477 this.superstructureStatus.shooter().acceleratorLeft());
478 this.setShooter(
479 this.rightAccelerator,
480 this.superstructureStatus.shooter().acceleratorRight());
481
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700482 if (this.superstructureStatus.aimer().aimingForInnerPort()) {
483 this.innerPort.innerHTML = 'true';
Austin Schuh146c0bc2021-11-07 22:33:28 -0800484 this.outerTarget.classList.remove('targetted');
485 this.innerTarget.classList.add('targetted');
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700486 } else {
487 this.innerPort.innerHTML = 'false';
Austin Schuh146c0bc2021-11-07 22:33:28 -0800488 this.outerTarget.classList.add('targetted');
489 this.innerTarget.classList.remove('targetted');
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700490 }
Alex Perry87cc63d2021-10-27 21:18:42 -0700491
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700492 if (!this.superstructureStatus.hood().zeroed()) {
493 this.setZeroing(this.hood);
494 } else if (this.superstructureStatus.hood().estopped()) {
495 this.setEstopped(this.hood);
496 } else {
Alex Perry87cc63d2021-10-27 21:18:42 -0700497 this.setTargetValue(
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700498 this.hood,
Alex Perry87cc63d2021-10-27 21:18:42 -0700499 this.superstructureStatus.hood().unprofiledGoalPosition(),
500 this.superstructureStatus.hood().estimatorState().position(),
501 1e-3);
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700502 }
Alex Perry87cc63d2021-10-27 21:18:42 -0700503
Austin Schuh146c0bc2021-11-07 22:33:28 -0800504 this.ballsShot.innerHTML =
505 this.superstructureStatus.shooter().ballsShot().toString();
506
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700507 if (!this.superstructureStatus.turret().zeroed()) {
508 this.setZeroing(this.turret);
509 } else if (this.superstructureStatus.turret().estopped()) {
510 this.setEstopped(this.turret);
511 } else {
Alex Perry87cc63d2021-10-27 21:18:42 -0700512 this.setTargetValue(
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700513 this.turret,
Alex Perry87cc63d2021-10-27 21:18:42 -0700514 this.superstructureStatus.turret().unprofiledGoalPosition(),
515 this.superstructureStatus.turret().estimatorState().position(),
516 1e-3);
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700517 }
Alex Perry87cc63d2021-10-27 21:18:42 -0700518
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700519 if (!this.superstructureStatus.intake().zeroed()) {
520 this.setZeroing(this.intake);
521 } else if (this.superstructureStatus.intake().estopped()) {
522 this.setEstopped(this.intake);
523 } else {
524 this.setValue(
525 this.intake,
526 this.superstructureStatus.intake().estimatorState().position());
527 }
Austin Schuh840132b2021-10-17 17:40:14 -0700528 }
Alex Perry87cc63d2021-10-27 21:18:42 -0700529
Alex Perry2124ae82020-03-07 14:19:06 -0800530 this.drawRobot(
531 this.drivetrainStatus.x(), this.drivetrainStatus.y(),
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700532 this.drivetrainStatus.theta(),
533 this.superstructureStatus ?
534 this.superstructureStatus.turret().position() :
535 null);
Alex Perry2124ae82020-03-07 14:19:06 -0800536 }
537
Alex Perryb49a3fb2020-02-29 15:26:54 -0800538 window.requestAnimationFrame(() => this.draw());
Alex Perry5427c9a2020-02-15 17:43:45 -0800539 }
540
541 reset(): void {
542 const ctx = this.canvas.getContext('2d');
543 ctx.setTransform(1, 0, 0, 1, 0, 0);
544 const size = window.innerHeight * 0.9;
545 ctx.canvas.height = size;
Alex Perryb49a3fb2020-02-29 15:26:54 -0800546 const width = size / 2 + 20;
547 ctx.canvas.width = width;
548 ctx.clearRect(0, 0, size, width);
Alex Perry5427c9a2020-02-15 17:43:45 -0800549
Alex Perryb49a3fb2020-02-29 15:26:54 -0800550 // Translate to center of display.
551 ctx.translate(width / 2, size / 2);
Alex Perry5427c9a2020-02-15 17:43:45 -0800552 // Coordinate system is:
553 // x -> forward.
554 // y -> to the left.
555 ctx.rotate(-Math.PI / 2);
556 ctx.scale(1, -1);
Alex Perry5427c9a2020-02-15 17:43:45 -0800557
558 const M_TO_PX = (size - 10) / FIELD_LENGTH;
559 ctx.scale(M_TO_PX, M_TO_PX);
560 ctx.lineWidth = 1 / M_TO_PX;
561 }
562}