blob: 28c35aaf40945e469fafa2963e8193ca83103aaf [file] [log] [blame]
James Kuszmauldac091f2022-03-22 09:35:06 -07001import {ByteBuffer} from 'flatbuffers';
James Kuszmaulb84c0912022-04-13 19:44:52 -07002import {Connection} from '../../aos/network/www/proxy';
3import {Status as DrivetrainStatus} from '../../frc971/control_loops/drivetrain/drivetrain_status_generated';
4import {LocalizerDebug, RejectionReason, ImageMatchDebug} from '../control_loops/drivetrain/localizer_debug_generated';
5import {Status as SuperstructureStatus, FlywheelControllerStatus} from '../control_loops/superstructure/superstructure_status_generated'
6import {ImageMatchResult} from '../vision/sift/sift_generated';
Alex Perry5427c9a2020-02-15 17:43:45 -08007
Alex Perry2124ae82020-03-07 14:19:06 -08008import {FIELD_LENGTH, FIELD_WIDTH, FT_TO_M, IN_TO_M} from './constants';
9
Alex Perryb49a3fb2020-02-29 15:26:54 -080010// (0,0) is field center, +X is toward red DS
Alex Perry5427c9a2020-02-15 17:43:45 -080011const FIELD_SIDE_Y = FIELD_WIDTH / 2;
Alex Perryb49a3fb2020-02-29 15:26:54 -080012const FIELD_EDGE_X = FIELD_LENGTH / 2;
Alex Perry5427c9a2020-02-15 17:43:45 -080013
14const DS_WIDTH = 69 * IN_TO_M;
15const DS_ANGLE = 20 * Math.PI / 180;
Alex Perryb49a3fb2020-02-29 15:26:54 -080016const DS_END_X = FIELD_EDGE_X - DS_WIDTH * Math.sin(DS_ANGLE);
Alex Perry5427c9a2020-02-15 17:43:45 -080017const DS_INSIDE_Y = FIELD_SIDE_Y - DS_WIDTH * Math.cos(DS_ANGLE);
18
Alex Perryb49a3fb2020-02-29 15:26:54 -080019const TRENCH_X = 108 * IN_TO_M;
Alex Perry5427c9a2020-02-15 17:43:45 -080020const TRENCH_WIDTH = 55.5 * IN_TO_M;
21const TRENCH_INSIDE = FIELD_SIDE_Y - TRENCH_WIDTH;
22
23const SPINNER_LENGTH = 30 * IN_TO_M;
Alex Perryb49a3fb2020-02-29 15:26:54 -080024const SPINNER_TOP_X = 374.59 * IN_TO_M - FIELD_EDGE_X;
Alex Perry5427c9a2020-02-15 17:43:45 -080025const SPINNER_BOTTOM_X = SPINNER_TOP_X - SPINNER_LENGTH;
26
Alex Perryb49a3fb2020-02-29 15:26:54 -080027const SHIELD_BOTTOM_X = -116 * IN_TO_M;
Alex Perry5427c9a2020-02-15 17:43:45 -080028const SHIELD_BOTTOM_Y = 43.75 * IN_TO_M;
29
Alex Perryb49a3fb2020-02-29 15:26:54 -080030const SHIELD_TOP_X = 116 * IN_TO_M;
Alex Perry5427c9a2020-02-15 17:43:45 -080031const SHIELD_TOP_Y = -43.75 * IN_TO_M;
32
Alex Perryb49a3fb2020-02-29 15:26:54 -080033const SHIELD_RIGHT_X = -51.06 * IN_TO_M;
Alex Perry5427c9a2020-02-15 17:43:45 -080034const SHIELD_RIGHT_Y = -112.88 * IN_TO_M;
35
Alex Perryb49a3fb2020-02-29 15:26:54 -080036const SHIELD_LEFT_X = 51.06 * IN_TO_M;
Alex Perry5427c9a2020-02-15 17:43:45 -080037const SHIELD_LEFT_Y = 112.88 * IN_TO_M;
38
39const SHIELD_CENTER_TOP_X = (SHIELD_TOP_X + SHIELD_LEFT_X) / 2
40const SHIELD_CENTER_TOP_Y = (SHIELD_TOP_Y + SHIELD_LEFT_Y) / 2
41
42const SHIELD_CENTER_BOTTOM_X = (SHIELD_BOTTOM_X + SHIELD_RIGHT_X) / 2
43const SHIELD_CENTER_BOTTOM_Y = (SHIELD_BOTTOM_Y + SHIELD_RIGHT_Y) / 2
44
Alex Perryb49a3fb2020-02-29 15:26:54 -080045const INITIATION_X = FIELD_EDGE_X - 120 * IN_TO_M;
Alex Perry5427c9a2020-02-15 17:43:45 -080046
Alex Perryb49a3fb2020-02-29 15:26:54 -080047const TARGET_ZONE_TIP_X = FIELD_EDGE_X - 30 * IN_TO_M;
Alex Perry5427c9a2020-02-15 17:43:45 -080048const TARGET_ZONE_WIDTH = 48 * IN_TO_M;
49const LOADING_ZONE_WIDTH = 60 * IN_TO_M;
50
Austin Schuhb863f932021-10-23 23:29:09 -070051const ROBOT_WIDTH = 34 * IN_TO_M;
52const ROBOT_LENGTH = 36 * IN_TO_M;
Alex Perry2124ae82020-03-07 14:19:06 -080053
Alex Perry87cc63d2021-10-27 21:18:42 -070054const PI_COLORS = ['#ff00ff', '#ffff00', '#000000', '#00ffff', '#ffa500'];
James Kuszmaul5e6aa252021-08-28 22:19:29 -070055
Alex Perry5427c9a2020-02-15 17:43:45 -080056export class FieldHandler {
57 private canvas = document.createElement('canvas');
Alex Perry87cc63d2021-10-27 21:18:42 -070058 private imageMatchResult = new Map<string, ImageMatchResult>();
Philipp Schradera227d042020-11-14 17:33:52 -080059 private drivetrainStatus: DrivetrainStatus|null = null;
James Kuszmaul5e6aa252021-08-28 22:19:29 -070060 private superstructureStatus: SuperstructureStatus|null = null;
Alex Perry87cc63d2021-10-27 21:18:42 -070061
James Kuszmaulf75ecd62021-10-23 14:33:46 -070062 // Image information indexed by timestamp (seconds since the epoch), so that
63 // we can stop displaying images after a certain amount of time.
64 private localizerImageMatches = new Map<number, LocalizerDebug>();
Austin Schuh146c0bc2021-11-07 22:33:28 -080065 private outerTarget: HTMLElement =
Alex Perry87cc63d2021-10-27 21:18:42 -070066 (document.getElementById('outer_target') as HTMLElement);
Austin Schuh146c0bc2021-11-07 22:33:28 -080067 private innerTarget: HTMLElement =
Alex Perry87cc63d2021-10-27 21:18:42 -070068 (document.getElementById('inner_target') as HTMLElement);
69 private x: HTMLElement = (document.getElementById('x') as HTMLElement);
70 private y: HTMLElement = (document.getElementById('y') as HTMLElement);
71 private theta: HTMLElement =
72 (document.getElementById('theta') as HTMLElement);
73 private shotDistance: HTMLElement =
74 (document.getElementById('shot_distance') as HTMLElement);
75 private finisher: HTMLElement =
76 (document.getElementById('finisher') as HTMLElement);
77 private leftAccelerator: HTMLElement =
78 (document.getElementById('left_accelerator') as HTMLElement);
79 private rightAccelerator: HTMLElement =
80 (document.getElementById('right_accelerator') as HTMLElement);
81 private innerPort: HTMLElement =
82 (document.getElementById('inner_port') as HTMLElement);
83 private hood: HTMLElement = (document.getElementById('hood') as HTMLElement);
84 private turret: HTMLElement =
85 (document.getElementById('turret') as HTMLElement);
Austin Schuh146c0bc2021-11-07 22:33:28 -080086 private ballsShot: HTMLElement =
87 (document.getElementById('balls_shot') as HTMLElement);
Alex Perry87cc63d2021-10-27 21:18:42 -070088 private intake: HTMLElement =
89 (document.getElementById('intake') as HTMLElement);
90 private imagesAcceptedCounter: HTMLElement =
91 (document.getElementById('images_accepted') as HTMLElement);
92 private imagesRejectedCounter: HTMLElement =
93 (document.getElementById('images_rejected') as HTMLElement);
94 private rejectionReasonCells: HTMLElement[] = [];
Alex Perry5427c9a2020-02-15 17:43:45 -080095
Alex Perryb49a3fb2020-02-29 15:26:54 -080096 constructor(private readonly connection: Connection) {
Austin Schuh840132b2021-10-17 17:40:14 -070097 (document.getElementById('field') as HTMLElement).appendChild(this.canvas);
Alex Perryb49a3fb2020-02-29 15:26:54 -080098
James Kuszmaulf75ecd62021-10-23 14:33:46 -070099 for (const value in RejectionReason) {
100 // Typescript generates an iterator that produces both numbers and
101 // strings... don't do anything on the string iterations.
102 if (isNaN(Number(value))) {
103 continue;
104 }
Alex Perry87cc63d2021-10-27 21:18:42 -0700105 const row = document.createElement('div');
106 const nameCell = document.createElement('div');
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700107 nameCell.innerHTML = RejectionReason[value];
108 row.appendChild(nameCell);
Alex Perry87cc63d2021-10-27 21:18:42 -0700109 const valueCell = document.createElement('div');
110 valueCell.innerHTML = 'NA';
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700111 this.rejectionReasonCells.push(valueCell);
112 row.appendChild(valueCell);
Alex Perry87cc63d2021-10-27 21:18:42 -0700113 document.getElementById('vision_readouts').appendChild(row);
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700114 }
115
James Kuszmaul286b80c2021-10-23 21:56:33 -0700116 for (let ii = 0; ii < PI_COLORS.length; ++ii) {
Alex Perry87cc63d2021-10-27 21:18:42 -0700117 const legendEntry = document.createElement('div');
James Kuszmaul286b80c2021-10-23 21:56:33 -0700118 legendEntry.style.color = PI_COLORS[ii];
Alex Perry87cc63d2021-10-27 21:18:42 -0700119 legendEntry.innerHTML = 'PI' + (ii + 1).toString()
120 document.getElementById('legend').appendChild(legendEntry);
James Kuszmaul286b80c2021-10-23 21:56:33 -0700121 }
122
Alex Perryb49a3fb2020-02-29 15:26:54 -0800123 this.connection.addConfigHandler(() => {
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700124 // Go through and register handlers for both all the individual pis as
125 // well as the local pi. Depending on the node that we are running on,
126 // different subsets of these will be available.
James Kuszmaul286b80c2021-10-23 21:56:33 -0700127 for (const prefix of ['', '/pi1', '/pi2', '/pi3', '/pi4', '/pi5']) {
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700128 this.connection.addHandler(
James Kuszmaul286b80c2021-10-23 21:56:33 -0700129 prefix + '/camera', ImageMatchResult.getFullyQualifiedName(),
130 (res) => {
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700131 this.handleImageMatchResult(prefix, res);
132 });
133 }
James Kuszmaul527038a2020-12-21 23:40:44 -0800134 this.connection.addHandler(
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700135 '/drivetrain', LocalizerDebug.getFullyQualifiedName(), (data) => {
136 this.handleLocalizerDebug(data);
137 });
138 this.connection.addHandler(
James Kuszmaul527038a2020-12-21 23:40:44 -0800139 '/drivetrain', DrivetrainStatus.getFullyQualifiedName(), (data) => {
140 this.handleDrivetrainStatus(data);
141 });
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700142 this.connection.addHandler(
143 '/superstructure', SuperstructureStatus.getFullyQualifiedName(),
144 (data) => {
145 this.handleSuperstructureStatus(data);
146 });
Alex Perryb49a3fb2020-02-29 15:26:54 -0800147 });
Alex Perryb49a3fb2020-02-29 15:26:54 -0800148 }
149
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700150 private handleImageMatchResult(prefix: string, data: Uint8Array): void {
Philipp Schradere625ba22020-11-16 20:11:37 -0800151 const fbBuffer = new ByteBuffer(data);
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700152 this.imageMatchResult.set(
James Kuszmauldac091f2022-03-22 09:35:06 -0700153 prefix, ImageMatchResult.getRootAsImageMatchResult(fbBuffer));
Alex Perryb49a3fb2020-02-29 15:26:54 -0800154 }
155
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700156 private handleLocalizerDebug(data: Uint8Array): void {
157 const now = Date.now() / 1000.0;
158
159 const fbBuffer = new ByteBuffer(data);
160 this.localizerImageMatches.set(
James Kuszmauldac091f2022-03-22 09:35:06 -0700161 now, LocalizerDebug.getRootAsLocalizerDebug(fbBuffer));
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700162
163 const debug = this.localizerImageMatches.get(now);
164
165 if (debug.statistics()) {
166 this.imagesAcceptedCounter.innerHTML =
167 debug.statistics().totalAccepted().toString();
168 this.imagesRejectedCounter.innerHTML =
169 (debug.statistics().totalCandidates() -
170 debug.statistics().totalAccepted())
171 .toString();
172 if (debug.statistics().rejectionReasonCountLength() ==
173 this.rejectionReasonCells.length) {
174 for (let ii = 0; ii < debug.statistics().rejectionReasonCountLength();
175 ++ii) {
176 this.rejectionReasonCells[ii].innerHTML =
177 debug.statistics().rejectionReasonCount(ii).toString();
178 }
179 } else {
Alex Perry87cc63d2021-10-27 21:18:42 -0700180 console.error('Unexpected number of rejection reasons in counter.');
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700181 }
182 this.imagesRejectedCounter.innerHTML =
183 (debug.statistics().totalCandidates() -
184 debug.statistics().totalAccepted())
185 .toString();
186 }
187 }
188
Alex Perry2124ae82020-03-07 14:19:06 -0800189 private handleDrivetrainStatus(data: Uint8Array): void {
Philipp Schradere625ba22020-11-16 20:11:37 -0800190 const fbBuffer = new ByteBuffer(data);
James Kuszmauldac091f2022-03-22 09:35:06 -0700191 this.drivetrainStatus = DrivetrainStatus.getRootAsStatus(fbBuffer);
Alex Perry2124ae82020-03-07 14:19:06 -0800192 }
193
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700194 private handleSuperstructureStatus(data: Uint8Array): void {
195 const fbBuffer = new ByteBuffer(data);
James Kuszmauldac091f2022-03-22 09:35:06 -0700196 this.superstructureStatus = SuperstructureStatus.getRootAsStatus(fbBuffer);
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700197 }
198
Alex Perry5427c9a2020-02-15 17:43:45 -0800199 drawField(): void {
200 const MY_COLOR = 'red';
201 const OTHER_COLOR = 'blue';
202 const ctx = this.canvas.getContext('2d');
203 // draw perimiter
204 ctx.beginPath();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800205 ctx.moveTo(FIELD_EDGE_X, DS_INSIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800206 ctx.lineTo(DS_END_X, FIELD_SIDE_Y);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800207 ctx.lineTo(-DS_END_X, FIELD_SIDE_Y);
208 ctx.lineTo(-FIELD_EDGE_X, DS_INSIDE_Y);
209 ctx.lineTo(-FIELD_EDGE_X, -DS_INSIDE_Y);
210 ctx.lineTo(-DS_END_X, -FIELD_SIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800211 ctx.lineTo(DS_END_X, -FIELD_SIDE_Y);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800212 ctx.lineTo(FIELD_EDGE_X, -DS_INSIDE_Y);
213 ctx.lineTo(FIELD_EDGE_X, DS_INSIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800214 ctx.stroke();
215
216 // draw shield generator
217 ctx.beginPath();
218 ctx.moveTo(SHIELD_BOTTOM_X, SHIELD_BOTTOM_Y);
219 ctx.lineTo(SHIELD_RIGHT_X, SHIELD_RIGHT_Y);
220 ctx.lineTo(SHIELD_TOP_X, SHIELD_TOP_Y);
221 ctx.lineTo(SHIELD_LEFT_X, SHIELD_LEFT_Y);
222 ctx.lineTo(SHIELD_BOTTOM_X, SHIELD_BOTTOM_Y);
223 ctx.moveTo(SHIELD_CENTER_TOP_X, SHIELD_CENTER_TOP_Y);
224 ctx.lineTo(SHIELD_CENTER_BOTTOM_X, SHIELD_CENTER_BOTTOM_Y);
225 ctx.stroke();
226
Alex Perryb49a3fb2020-02-29 15:26:54 -0800227 this.drawHalfField(ctx, 'red');
228 ctx.rotate(Math.PI);
229 this.drawHalfField(ctx, 'blue');
230 ctx.rotate(Math.PI);
231 }
Alex Perry5427c9a2020-02-15 17:43:45 -0800232
Alex Perryb49a3fb2020-02-29 15:26:54 -0800233 drawHalfField(ctx, color: string): void {
234 // trenches
235 ctx.strokeStyle = color;
Alex Perry5427c9a2020-02-15 17:43:45 -0800236 ctx.beginPath();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800237 ctx.moveTo(TRENCH_X, FIELD_SIDE_Y);
238 ctx.lineTo(TRENCH_X, TRENCH_INSIDE);
239 ctx.lineTo(-TRENCH_X, TRENCH_INSIDE);
240 ctx.lineTo(-TRENCH_X, FIELD_SIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800241 ctx.stroke();
242
243 ctx.strokeStyle = 'black';
244 ctx.beginPath();
245 ctx.moveTo(SPINNER_TOP_X, FIELD_SIDE_Y);
246 ctx.lineTo(SPINNER_TOP_X, TRENCH_INSIDE);
247 ctx.lineTo(SPINNER_BOTTOM_X, TRENCH_INSIDE);
248 ctx.lineTo(SPINNER_BOTTOM_X, FIELD_SIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800249 ctx.stroke();
250
Alex Perry5427c9a2020-02-15 17:43:45 -0800251 ctx.beginPath();
252 ctx.moveTo(INITIATION_X, FIELD_SIDE_Y);
253 ctx.lineTo(INITIATION_X, -FIELD_SIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800254 ctx.stroke();
255
Alex Perryb49a3fb2020-02-29 15:26:54 -0800256 // target/loading
257 ctx.strokeStyle = color;
Alex Perry5427c9a2020-02-15 17:43:45 -0800258 ctx.beginPath();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800259 ctx.moveTo(FIELD_EDGE_X, DS_INSIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800260 ctx.lineTo(TARGET_ZONE_TIP_X, DS_INSIDE_Y - 0.5 * TARGET_ZONE_WIDTH);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800261 ctx.lineTo(FIELD_EDGE_X, DS_INSIDE_Y - TARGET_ZONE_WIDTH);
Alex Perry5427c9a2020-02-15 17:43:45 -0800262
Alex Perryb49a3fb2020-02-29 15:26:54 -0800263 ctx.moveTo(-FIELD_EDGE_X, DS_INSIDE_Y);
264 ctx.lineTo(-TARGET_ZONE_TIP_X, DS_INSIDE_Y - 0.5 * LOADING_ZONE_WIDTH);
265 ctx.lineTo(-FIELD_EDGE_X, DS_INSIDE_Y - LOADING_ZONE_WIDTH);
Alex Perry5427c9a2020-02-15 17:43:45 -0800266 ctx.stroke();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800267 }
Alex Perry5427c9a2020-02-15 17:43:45 -0800268
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700269 drawCamera(
270 x: number, y: number, theta: number, color: string = 'blue',
271 extendLines: boolean = true): void {
Alex Perryb49a3fb2020-02-29 15:26:54 -0800272 const ctx = this.canvas.getContext('2d');
273 ctx.save();
274 ctx.translate(x, y);
275 ctx.rotate(theta);
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700276 ctx.strokeStyle = color;
Alex Perry5427c9a2020-02-15 17:43:45 -0800277 ctx.beginPath();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800278 ctx.moveTo(0.5, 0.5);
279 ctx.lineTo(0, 0);
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700280 if (extendLines) {
281 ctx.lineTo(100.0, 0);
282 ctx.lineTo(0, 0);
283 }
Alex Perryb49a3fb2020-02-29 15:26:54 -0800284 ctx.lineTo(0.5, -0.5);
Alex Perry5427c9a2020-02-15 17:43:45 -0800285 ctx.stroke();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800286 ctx.beginPath();
Philipp Schradere625ba22020-11-16 20:11:37 -0800287 ctx.arc(0, 0, 0.25, -Math.PI / 4, Math.PI / 4);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800288 ctx.stroke();
289 ctx.restore();
290 }
291
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700292 drawRobot(
293 x: number, y: number, theta: number, turret: number|null,
294 color: string = 'blue', dashed: boolean = false,
295 extendLines: boolean = true): void {
Alex Perry2124ae82020-03-07 14:19:06 -0800296 const ctx = this.canvas.getContext('2d');
297 ctx.save();
298 ctx.translate(x, y);
299 ctx.rotate(theta);
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700300 ctx.strokeStyle = color;
301 if (dashed) {
302 ctx.setLineDash([0.05, 0.05]);
303 } else {
304 // Empty array = solid line.
305 ctx.setLineDash([]);
306 }
Alex Perry2124ae82020-03-07 14:19:06 -0800307 ctx.rect(-ROBOT_LENGTH / 2, -ROBOT_WIDTH / 2, ROBOT_LENGTH, ROBOT_WIDTH);
308 ctx.stroke();
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700309
310 // Draw line indicating which direction is forwards on the robot.
311 ctx.beginPath();
312 ctx.moveTo(0, 0);
313 if (extendLines) {
314 ctx.lineTo(1000.0, 0);
315 } else {
316 ctx.lineTo(ROBOT_LENGTH / 2.0, 0);
317 }
318 ctx.stroke();
319
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700320 if (turret) {
321 ctx.save();
322 ctx.rotate(turret + Math.PI);
323 const turretRadius = ROBOT_WIDTH / 4.0;
Alex Perry87cc63d2021-10-27 21:18:42 -0700324 ctx.strokeStyle = 'red';
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700325 // Draw circle for turret.
326 ctx.beginPath();
327 ctx.arc(0, 0, turretRadius, 0, 2.0 * Math.PI);
328 ctx.stroke();
329 // Draw line in circle to show forwards.
330 ctx.beginPath();
331 ctx.moveTo(0, 0);
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700332 if (extendLines) {
333 ctx.lineTo(1000.0, 0);
334 } else {
335 ctx.lineTo(turretRadius, 0);
336 }
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700337 ctx.stroke();
338 ctx.restore();
339 }
Alex Perry2124ae82020-03-07 14:19:06 -0800340 ctx.restore();
341 }
342
Alex Perry87cc63d2021-10-27 21:18:42 -0700343 setShooter(div: HTMLElement, flywheelStatus: FlywheelControllerStatus): void {
344 const currentVelocity = flywheelStatus.angularVelocity();
345 const goal = flywheelStatus.angularVelocityGoal();
346 // Show it as 'near' if difference between
347 if (Math.abs(currentVelocity - goal) < 5) {
348 this.setNear(div, currentVelocity.toFixed(2));
349 return;
350 }
351 div.classList.remove('faulted');
352 div.classList.remove('zeroing');
353 div.classList.remove('near');
354 div.innerHTML = currentVelocity.toFixed(2);
Austin Schuh840132b2021-10-17 17:40:14 -0700355 }
Alex Perry87cc63d2021-10-27 21:18:42 -0700356
357 setZeroing(div: HTMLElement): void {
358 div.innerHTML = 'zeroing';
359 div.classList.remove('faulted');
360 div.classList.add('zeroing');
361 div.classList.remove('near');
Austin Schuh840132b2021-10-17 17:40:14 -0700362 }
Alex Perry87cc63d2021-10-27 21:18:42 -0700363
364 setEstopped(div: HTMLElement): void {
365 div.innerHTML = 'estopped';
366 div.classList.add('faulted');
367 div.classList.remove('zeroing');
368 div.classList.remove('near');
369 }
370
371 setNear(div: HTMLElement, val: string): void {
372 div.innerHTML = val;
373 div.classList.remove('faulted');
374 div.classList.remove('zeroing');
375 div.classList.add('near');
376 }
377
378 setTargetValue(
379 div: HTMLElement, target: number, val: number, tolerance: number): void {
380 div.innerHTML = val.toFixed(4);
381 div.classList.remove('faulted');
382 div.classList.remove('zeroing');
383 if (Math.abs(target - val) < tolerance) {
384 div.classList.add('near');
385 } else {
386 div.classList.remove('near');
387 }
388 }
389
390 setValue(div: HTMLElement, val: number): void {
391 div.innerHTML = val.toFixed(4);
392 div.classList.remove('faulted');
393 div.classList.remove('zeroing');
394 div.classList.remove('near');
Austin Schuh840132b2021-10-17 17:40:14 -0700395 }
396
Philipp Schradere625ba22020-11-16 20:11:37 -0800397 draw(): void {
Alex Perryb49a3fb2020-02-29 15:26:54 -0800398 this.reset();
399 this.drawField();
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700400
401 // Draw the matches with debugging information from the localizer.
402 const now = Date.now() / 1000.0;
403 for (const [time, value] of this.localizerImageMatches) {
404 const age = now - time;
405 const kRemovalAge = 2.0;
406 if (age > kRemovalAge) {
407 this.localizerImageMatches.delete(time);
408 continue;
409 }
410 const ageAlpha = (kRemovalAge - age) / kRemovalAge
411 for (let i = 0; i < value.matchesLength(); i++) {
412 const imageDebug = value.matches(i);
413 const x = imageDebug.impliedRobotX();
414 const y = imageDebug.impliedRobotY();
415 const theta = imageDebug.impliedRobotTheta();
416 const cameraX = imageDebug.cameraX();
417 const cameraY = imageDebug.cameraY();
418 const cameraTheta = imageDebug.cameraTheta();
419 const accepted = imageDebug.accepted();
420 // Make camera readings fade over time.
421 const alpha = Math.round(255 * ageAlpha).toString(16).padStart(2, '0');
422 const dashed = false;
Alex Perry87cc63d2021-10-27 21:18:42 -0700423 const acceptedRgb = accepted ? '#00FF00' : '#FF0000';
James Kuszmaul286b80c2021-10-23 21:56:33 -0700424 const acceptedRgba = acceptedRgb + alpha;
425 const cameraRgb = PI_COLORS[imageDebug.camera()];
426 const cameraRgba = cameraRgb + alpha;
427 this.drawRobot(x, y, theta, null, acceptedRgba, dashed, false);
428 this.drawCamera(cameraX, cameraY, cameraTheta, cameraRgba, false);
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700429 }
430 }
431
432 // draw cameras from ImageMatchResults directly (helpful when viewing page
433 // on the pis individually).
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700434 for (const keyPair of this.imageMatchResult) {
435 const value = keyPair[1];
436 for (let i = 0; i < value.cameraPosesLength(); i++) {
437 const pose = value.cameraPoses(i);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800438 const mat = pose.fieldToCamera();
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700439 // Matrix layout:
440 // [0, 1, 2, 3]
441 // [4, 5, 6, 7]
442 // [8, 9, 10, 11]
443 // [12, 13, 14, 15]
Alex Perryb49a3fb2020-02-29 15:26:54 -0800444 const x = mat.data(3);
445 const y = mat.data(7);
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700446 const theta = Math.atan2(mat.data(6), mat.data(2));
James Kuszmaul286b80c2021-10-23 21:56:33 -0700447 const cameraColor = (keyPair[0].length > 0) ?
448 PI_COLORS[Number(keyPair[0][3]) - 1] :
449 'blue';
450 this.drawCamera(x, y, theta, cameraColor);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800451 }
452 }
453
Alex Perry2124ae82020-03-07 14:19:06 -0800454 if (this.drivetrainStatus) {
Austin Schuh840132b2021-10-17 17:40:14 -0700455 if (!this.drivetrainStatus.zeroing().zeroed()) {
456 this.setZeroing(this.x);
457 this.setZeroing(this.y);
458 this.setZeroing(this.theta);
459 } else if (this.drivetrainStatus.zeroing().faulted()) {
460 this.setEstopped(this.x);
461 this.setEstopped(this.y);
462 this.setEstopped(this.theta);
463 } else {
464 this.setValue(this.x, this.drivetrainStatus.x());
465 this.setValue(this.y, this.drivetrainStatus.y());
466 this.setValue(this.theta, this.drivetrainStatus.theta());
467 }
468
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700469 if (this.superstructureStatus) {
470 this.shotDistance.innerHTML =
471 this.superstructureStatus.aimer().shotDistance().toFixed(2);
Alex Perry87cc63d2021-10-27 21:18:42 -0700472 this.setShooter(
473 this.finisher, this.superstructureStatus.shooter().finisher());
474 this.setShooter(
475 this.leftAccelerator,
476 this.superstructureStatus.shooter().acceleratorLeft());
477 this.setShooter(
478 this.rightAccelerator,
479 this.superstructureStatus.shooter().acceleratorRight());
480
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700481 if (this.superstructureStatus.aimer().aimingForInnerPort()) {
482 this.innerPort.innerHTML = 'true';
Austin Schuh146c0bc2021-11-07 22:33:28 -0800483 this.outerTarget.classList.remove('targetted');
484 this.innerTarget.classList.add('targetted');
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700485 } else {
486 this.innerPort.innerHTML = 'false';
Austin Schuh146c0bc2021-11-07 22:33:28 -0800487 this.outerTarget.classList.add('targetted');
488 this.innerTarget.classList.remove('targetted');
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700489 }
Alex Perry87cc63d2021-10-27 21:18:42 -0700490
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700491 if (!this.superstructureStatus.hood().zeroed()) {
492 this.setZeroing(this.hood);
493 } else if (this.superstructureStatus.hood().estopped()) {
494 this.setEstopped(this.hood);
495 } else {
Alex Perry87cc63d2021-10-27 21:18:42 -0700496 this.setTargetValue(
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700497 this.hood,
Alex Perry87cc63d2021-10-27 21:18:42 -0700498 this.superstructureStatus.hood().unprofiledGoalPosition(),
499 this.superstructureStatus.hood().estimatorState().position(),
500 1e-3);
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700501 }
Alex Perry87cc63d2021-10-27 21:18:42 -0700502
Austin Schuh146c0bc2021-11-07 22:33:28 -0800503 this.ballsShot.innerHTML =
504 this.superstructureStatus.shooter().ballsShot().toString();
505
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700506 if (!this.superstructureStatus.turret().zeroed()) {
507 this.setZeroing(this.turret);
508 } else if (this.superstructureStatus.turret().estopped()) {
509 this.setEstopped(this.turret);
510 } else {
Alex Perry87cc63d2021-10-27 21:18:42 -0700511 this.setTargetValue(
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700512 this.turret,
Alex Perry87cc63d2021-10-27 21:18:42 -0700513 this.superstructureStatus.turret().unprofiledGoalPosition(),
514 this.superstructureStatus.turret().estimatorState().position(),
515 1e-3);
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700516 }
Alex Perry87cc63d2021-10-27 21:18:42 -0700517
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700518 if (!this.superstructureStatus.intake().zeroed()) {
519 this.setZeroing(this.intake);
520 } else if (this.superstructureStatus.intake().estopped()) {
521 this.setEstopped(this.intake);
522 } else {
523 this.setValue(
524 this.intake,
525 this.superstructureStatus.intake().estimatorState().position());
526 }
Austin Schuh840132b2021-10-17 17:40:14 -0700527 }
Alex Perry87cc63d2021-10-27 21:18:42 -0700528
Alex Perry2124ae82020-03-07 14:19:06 -0800529 this.drawRobot(
530 this.drivetrainStatus.x(), this.drivetrainStatus.y(),
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700531 this.drivetrainStatus.theta(),
532 this.superstructureStatus ?
533 this.superstructureStatus.turret().position() :
534 null);
Alex Perry2124ae82020-03-07 14:19:06 -0800535 }
536
Alex Perryb49a3fb2020-02-29 15:26:54 -0800537 window.requestAnimationFrame(() => this.draw());
Alex Perry5427c9a2020-02-15 17:43:45 -0800538 }
539
540 reset(): void {
541 const ctx = this.canvas.getContext('2d');
542 ctx.setTransform(1, 0, 0, 1, 0, 0);
543 const size = window.innerHeight * 0.9;
544 ctx.canvas.height = size;
Alex Perryb49a3fb2020-02-29 15:26:54 -0800545 const width = size / 2 + 20;
546 ctx.canvas.width = width;
547 ctx.clearRect(0, 0, size, width);
Alex Perry5427c9a2020-02-15 17:43:45 -0800548
Alex Perryb49a3fb2020-02-29 15:26:54 -0800549 // Translate to center of display.
550 ctx.translate(width / 2, size / 2);
Alex Perry5427c9a2020-02-15 17:43:45 -0800551 // Coordinate system is:
552 // x -> forward.
553 // y -> to the left.
554 ctx.rotate(-Math.PI / 2);
555 ctx.scale(1, -1);
Alex Perry5427c9a2020-02-15 17:43:45 -0800556
557 const M_TO_PX = (size - 10) / FIELD_LENGTH;
558 ctx.scale(M_TO_PX, M_TO_PX);
559 ctx.lineWidth = 1 / M_TO_PX;
560 }
561}