blob: c1170c47f1a61a9919c7138e691e6e6d8f372ce3 [file] [log] [blame]
Philipp Schradere625ba22020-11-16 20:11:37 -08001import * as configuration from 'org_frc971/aos/configuration_generated';
Alex Perry87cc63d2021-10-27 21:18:42 -07002import * as web_proxy from 'org_frc971/aos/network/web_proxy_generated';
Philipp Schradere625ba22020-11-16 20:11:37 -08003import {Connection} from 'org_frc971/aos/network/www/proxy';
4import * as flatbuffers_builder from 'org_frc971/external/com_github_google_flatbuffers/ts/builder';
5import {ByteBuffer} from 'org_frc971/external/com_github_google_flatbuffers/ts/byte-buffer';
6import * as drivetrain from 'org_frc971/frc971/control_loops/drivetrain/drivetrain_status_generated';
James Kuszmaulf75ecd62021-10-23 14:33:46 -07007import * as localizer from 'org_frc971/y2020/control_loops/drivetrain/localizer_debug_generated';
James Kuszmaul5e6aa252021-08-28 22:19:29 -07008import * as ss from 'org_frc971/y2020/control_loops/superstructure/superstructure_status_generated'
Alex Perry87cc63d2021-10-27 21:18:42 -07009import * as sift from 'org_frc971/y2020/vision/sift/sift_generated';
Philipp Schradere625ba22020-11-16 20:11:37 -080010
11import DrivetrainStatus = drivetrain.frc971.control_loops.drivetrain.Status;
James Kuszmaulf75ecd62021-10-23 14:33:46 -070012import LocalizerDebug = localizer.y2020.control_loops.drivetrain.LocalizerDebug;
13import RejectionReason = localizer.y2020.control_loops.drivetrain.RejectionReason;
14import ImageMatchDebug = localizer.y2020.control_loops.drivetrain.ImageMatchDebug;
James Kuszmaul5e6aa252021-08-28 22:19:29 -070015import SuperstructureStatus = ss.y2020.control_loops.superstructure.Status;
Alex Perry87cc63d2021-10-27 21:18:42 -070016import FlywheelControllerStatus = ss.y2020.control_loops.superstructure.FlywheelControllerStatus;
Philipp Schradere625ba22020-11-16 20:11:37 -080017import ImageMatchResult = sift.frc971.vision.sift.ImageMatchResult;
Philipp Schradere625ba22020-11-16 20:11:37 -080018import Channel = configuration.aos.Channel;
James Kuszmaul71a81932020-12-15 21:08:01 -080019import SubscriberRequest = web_proxy.aos.web_proxy.SubscriberRequest;
20import ChannelRequest = web_proxy.aos.web_proxy.ChannelRequest;
21import TransferMethod = web_proxy.aos.web_proxy.TransferMethod;
Alex Perry5427c9a2020-02-15 17:43:45 -080022
Alex Perry2124ae82020-03-07 14:19:06 -080023import {FIELD_LENGTH, FIELD_WIDTH, FT_TO_M, IN_TO_M} from './constants';
24
Alex Perryb49a3fb2020-02-29 15:26:54 -080025// (0,0) is field center, +X is toward red DS
Alex Perry5427c9a2020-02-15 17:43:45 -080026const FIELD_SIDE_Y = FIELD_WIDTH / 2;
Alex Perryb49a3fb2020-02-29 15:26:54 -080027const FIELD_EDGE_X = FIELD_LENGTH / 2;
Alex Perry5427c9a2020-02-15 17:43:45 -080028
29const DS_WIDTH = 69 * IN_TO_M;
30const DS_ANGLE = 20 * Math.PI / 180;
Alex Perryb49a3fb2020-02-29 15:26:54 -080031const DS_END_X = FIELD_EDGE_X - DS_WIDTH * Math.sin(DS_ANGLE);
Alex Perry5427c9a2020-02-15 17:43:45 -080032const DS_INSIDE_Y = FIELD_SIDE_Y - DS_WIDTH * Math.cos(DS_ANGLE);
33
Alex Perryb49a3fb2020-02-29 15:26:54 -080034const TRENCH_X = 108 * IN_TO_M;
Alex Perry5427c9a2020-02-15 17:43:45 -080035const TRENCH_WIDTH = 55.5 * IN_TO_M;
36const TRENCH_INSIDE = FIELD_SIDE_Y - TRENCH_WIDTH;
37
38const SPINNER_LENGTH = 30 * IN_TO_M;
Alex Perryb49a3fb2020-02-29 15:26:54 -080039const SPINNER_TOP_X = 374.59 * IN_TO_M - FIELD_EDGE_X;
Alex Perry5427c9a2020-02-15 17:43:45 -080040const SPINNER_BOTTOM_X = SPINNER_TOP_X - SPINNER_LENGTH;
41
Alex Perryb49a3fb2020-02-29 15:26:54 -080042const SHIELD_BOTTOM_X = -116 * IN_TO_M;
Alex Perry5427c9a2020-02-15 17:43:45 -080043const SHIELD_BOTTOM_Y = 43.75 * IN_TO_M;
44
Alex Perryb49a3fb2020-02-29 15:26:54 -080045const SHIELD_TOP_X = 116 * IN_TO_M;
Alex Perry5427c9a2020-02-15 17:43:45 -080046const SHIELD_TOP_Y = -43.75 * IN_TO_M;
47
Alex Perryb49a3fb2020-02-29 15:26:54 -080048const SHIELD_RIGHT_X = -51.06 * IN_TO_M;
Alex Perry5427c9a2020-02-15 17:43:45 -080049const SHIELD_RIGHT_Y = -112.88 * IN_TO_M;
50
Alex Perryb49a3fb2020-02-29 15:26:54 -080051const SHIELD_LEFT_X = 51.06 * IN_TO_M;
Alex Perry5427c9a2020-02-15 17:43:45 -080052const SHIELD_LEFT_Y = 112.88 * IN_TO_M;
53
54const SHIELD_CENTER_TOP_X = (SHIELD_TOP_X + SHIELD_LEFT_X) / 2
55const SHIELD_CENTER_TOP_Y = (SHIELD_TOP_Y + SHIELD_LEFT_Y) / 2
56
57const SHIELD_CENTER_BOTTOM_X = (SHIELD_BOTTOM_X + SHIELD_RIGHT_X) / 2
58const SHIELD_CENTER_BOTTOM_Y = (SHIELD_BOTTOM_Y + SHIELD_RIGHT_Y) / 2
59
Alex Perryb49a3fb2020-02-29 15:26:54 -080060const INITIATION_X = FIELD_EDGE_X - 120 * IN_TO_M;
Alex Perry5427c9a2020-02-15 17:43:45 -080061
Alex Perryb49a3fb2020-02-29 15:26:54 -080062const TARGET_ZONE_TIP_X = FIELD_EDGE_X - 30 * IN_TO_M;
Alex Perry5427c9a2020-02-15 17:43:45 -080063const TARGET_ZONE_WIDTH = 48 * IN_TO_M;
64const LOADING_ZONE_WIDTH = 60 * IN_TO_M;
65
Austin Schuhb863f932021-10-23 23:29:09 -070066const ROBOT_WIDTH = 34 * IN_TO_M;
67const ROBOT_LENGTH = 36 * IN_TO_M;
Alex Perry2124ae82020-03-07 14:19:06 -080068
Alex Perry87cc63d2021-10-27 21:18:42 -070069const PI_COLORS = ['#ff00ff', '#ffff00', '#000000', '#00ffff', '#ffa500'];
James Kuszmaul5e6aa252021-08-28 22:19:29 -070070
Alex Perry5427c9a2020-02-15 17:43:45 -080071export class FieldHandler {
72 private canvas = document.createElement('canvas');
Alex Perry87cc63d2021-10-27 21:18:42 -070073 private imageMatchResult = new Map<string, ImageMatchResult>();
Philipp Schradera227d042020-11-14 17:33:52 -080074 private drivetrainStatus: DrivetrainStatus|null = null;
James Kuszmaul5e6aa252021-08-28 22:19:29 -070075 private superstructureStatus: SuperstructureStatus|null = null;
Alex Perry87cc63d2021-10-27 21:18:42 -070076
James Kuszmaulf75ecd62021-10-23 14:33:46 -070077 // Image information indexed by timestamp (seconds since the epoch), so that
78 // we can stop displaying images after a certain amount of time.
79 private localizerImageMatches = new Map<number, LocalizerDebug>();
Austin Schuh146c0bc2021-11-07 22:33:28 -080080 private outerTarget: HTMLElement =
Alex Perry87cc63d2021-10-27 21:18:42 -070081 (document.getElementById('outer_target') as HTMLElement);
Austin Schuh146c0bc2021-11-07 22:33:28 -080082 private innerTarget: HTMLElement =
Alex Perry87cc63d2021-10-27 21:18:42 -070083 (document.getElementById('inner_target') as HTMLElement);
84 private x: HTMLElement = (document.getElementById('x') as HTMLElement);
85 private y: HTMLElement = (document.getElementById('y') as HTMLElement);
86 private theta: HTMLElement =
87 (document.getElementById('theta') as HTMLElement);
88 private shotDistance: HTMLElement =
89 (document.getElementById('shot_distance') as HTMLElement);
90 private finisher: HTMLElement =
91 (document.getElementById('finisher') as HTMLElement);
92 private leftAccelerator: HTMLElement =
93 (document.getElementById('left_accelerator') as HTMLElement);
94 private rightAccelerator: HTMLElement =
95 (document.getElementById('right_accelerator') as HTMLElement);
96 private innerPort: HTMLElement =
97 (document.getElementById('inner_port') as HTMLElement);
98 private hood: HTMLElement = (document.getElementById('hood') as HTMLElement);
99 private turret: HTMLElement =
100 (document.getElementById('turret') as HTMLElement);
Austin Schuh146c0bc2021-11-07 22:33:28 -0800101 private ballsShot: HTMLElement =
102 (document.getElementById('balls_shot') as HTMLElement);
Alex Perry87cc63d2021-10-27 21:18:42 -0700103 private intake: HTMLElement =
104 (document.getElementById('intake') as HTMLElement);
105 private imagesAcceptedCounter: HTMLElement =
106 (document.getElementById('images_accepted') as HTMLElement);
107 private imagesRejectedCounter: HTMLElement =
108 (document.getElementById('images_rejected') as HTMLElement);
109 private rejectionReasonCells: HTMLElement[] = [];
Alex Perry5427c9a2020-02-15 17:43:45 -0800110
Alex Perryb49a3fb2020-02-29 15:26:54 -0800111 constructor(private readonly connection: Connection) {
Austin Schuh840132b2021-10-17 17:40:14 -0700112 (document.getElementById('field') as HTMLElement).appendChild(this.canvas);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800113
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700114 for (const value in RejectionReason) {
115 // Typescript generates an iterator that produces both numbers and
116 // strings... don't do anything on the string iterations.
117 if (isNaN(Number(value))) {
118 continue;
119 }
Alex Perry87cc63d2021-10-27 21:18:42 -0700120 const row = document.createElement('div');
121 const nameCell = document.createElement('div');
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700122 nameCell.innerHTML = RejectionReason[value];
123 row.appendChild(nameCell);
Alex Perry87cc63d2021-10-27 21:18:42 -0700124 const valueCell = document.createElement('div');
125 valueCell.innerHTML = 'NA';
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700126 this.rejectionReasonCells.push(valueCell);
127 row.appendChild(valueCell);
Alex Perry87cc63d2021-10-27 21:18:42 -0700128 document.getElementById('vision_readouts').appendChild(row);
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700129 }
130
James Kuszmaul286b80c2021-10-23 21:56:33 -0700131 for (let ii = 0; ii < PI_COLORS.length; ++ii) {
Alex Perry87cc63d2021-10-27 21:18:42 -0700132 const legendEntry = document.createElement('div');
James Kuszmaul286b80c2021-10-23 21:56:33 -0700133 legendEntry.style.color = PI_COLORS[ii];
Alex Perry87cc63d2021-10-27 21:18:42 -0700134 legendEntry.innerHTML = 'PI' + (ii + 1).toString()
135 document.getElementById('legend').appendChild(legendEntry);
James Kuszmaul286b80c2021-10-23 21:56:33 -0700136 }
137
Alex Perryb49a3fb2020-02-29 15:26:54 -0800138 this.connection.addConfigHandler(() => {
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700139 // Go through and register handlers for both all the individual pis as
140 // well as the local pi. Depending on the node that we are running on,
141 // different subsets of these will be available.
James Kuszmaul286b80c2021-10-23 21:56:33 -0700142 for (const prefix of ['', '/pi1', '/pi2', '/pi3', '/pi4', '/pi5']) {
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700143 this.connection.addHandler(
James Kuszmaul286b80c2021-10-23 21:56:33 -0700144 prefix + '/camera', ImageMatchResult.getFullyQualifiedName(),
145 (res) => {
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700146 this.handleImageMatchResult(prefix, res);
147 });
148 }
James Kuszmaul527038a2020-12-21 23:40:44 -0800149 this.connection.addHandler(
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700150 '/drivetrain', LocalizerDebug.getFullyQualifiedName(), (data) => {
151 this.handleLocalizerDebug(data);
152 });
153 this.connection.addHandler(
James Kuszmaul527038a2020-12-21 23:40:44 -0800154 '/drivetrain', DrivetrainStatus.getFullyQualifiedName(), (data) => {
155 this.handleDrivetrainStatus(data);
156 });
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700157 this.connection.addHandler(
158 '/superstructure', SuperstructureStatus.getFullyQualifiedName(),
159 (data) => {
160 this.handleSuperstructureStatus(data);
161 });
Alex Perryb49a3fb2020-02-29 15:26:54 -0800162 });
Alex Perryb49a3fb2020-02-29 15:26:54 -0800163 }
164
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700165 private handleImageMatchResult(prefix: string, data: Uint8Array): void {
Philipp Schradere625ba22020-11-16 20:11:37 -0800166 const fbBuffer = new ByteBuffer(data);
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700167 this.imageMatchResult.set(
168 prefix,
169 ImageMatchResult.getRootAsImageMatchResult(
170 fbBuffer as unknown as flatbuffers.ByteBuffer));
Alex Perryb49a3fb2020-02-29 15:26:54 -0800171 }
172
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700173 private handleLocalizerDebug(data: Uint8Array): void {
174 const now = Date.now() / 1000.0;
175
176 const fbBuffer = new ByteBuffer(data);
177 this.localizerImageMatches.set(
178 now,
179 LocalizerDebug.getRootAsLocalizerDebug(
180 fbBuffer as unknown as flatbuffers.ByteBuffer));
181
182 const debug = this.localizerImageMatches.get(now);
183
184 if (debug.statistics()) {
185 this.imagesAcceptedCounter.innerHTML =
186 debug.statistics().totalAccepted().toString();
187 this.imagesRejectedCounter.innerHTML =
188 (debug.statistics().totalCandidates() -
189 debug.statistics().totalAccepted())
190 .toString();
191 if (debug.statistics().rejectionReasonCountLength() ==
192 this.rejectionReasonCells.length) {
193 for (let ii = 0; ii < debug.statistics().rejectionReasonCountLength();
194 ++ii) {
195 this.rejectionReasonCells[ii].innerHTML =
196 debug.statistics().rejectionReasonCount(ii).toString();
197 }
198 } else {
Alex Perry87cc63d2021-10-27 21:18:42 -0700199 console.error('Unexpected number of rejection reasons in counter.');
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700200 }
201 this.imagesRejectedCounter.innerHTML =
202 (debug.statistics().totalCandidates() -
203 debug.statistics().totalAccepted())
204 .toString();
205 }
206 }
207
Alex Perry2124ae82020-03-07 14:19:06 -0800208 private handleDrivetrainStatus(data: Uint8Array): void {
Philipp Schradere625ba22020-11-16 20:11:37 -0800209 const fbBuffer = new ByteBuffer(data);
210 this.drivetrainStatus = DrivetrainStatus.getRootAsStatus(
211 fbBuffer as unknown as flatbuffers.ByteBuffer);
Alex Perry2124ae82020-03-07 14:19:06 -0800212 }
213
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700214 private handleSuperstructureStatus(data: Uint8Array): void {
215 const fbBuffer = new ByteBuffer(data);
216 this.superstructureStatus = SuperstructureStatus.getRootAsStatus(
217 fbBuffer as unknown as flatbuffers.ByteBuffer);
218 }
219
Alex Perry5427c9a2020-02-15 17:43:45 -0800220 drawField(): void {
221 const MY_COLOR = 'red';
222 const OTHER_COLOR = 'blue';
223 const ctx = this.canvas.getContext('2d');
224 // draw perimiter
225 ctx.beginPath();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800226 ctx.moveTo(FIELD_EDGE_X, DS_INSIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800227 ctx.lineTo(DS_END_X, FIELD_SIDE_Y);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800228 ctx.lineTo(-DS_END_X, FIELD_SIDE_Y);
229 ctx.lineTo(-FIELD_EDGE_X, DS_INSIDE_Y);
230 ctx.lineTo(-FIELD_EDGE_X, -DS_INSIDE_Y);
231 ctx.lineTo(-DS_END_X, -FIELD_SIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800232 ctx.lineTo(DS_END_X, -FIELD_SIDE_Y);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800233 ctx.lineTo(FIELD_EDGE_X, -DS_INSIDE_Y);
234 ctx.lineTo(FIELD_EDGE_X, DS_INSIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800235 ctx.stroke();
236
237 // draw shield generator
238 ctx.beginPath();
239 ctx.moveTo(SHIELD_BOTTOM_X, SHIELD_BOTTOM_Y);
240 ctx.lineTo(SHIELD_RIGHT_X, SHIELD_RIGHT_Y);
241 ctx.lineTo(SHIELD_TOP_X, SHIELD_TOP_Y);
242 ctx.lineTo(SHIELD_LEFT_X, SHIELD_LEFT_Y);
243 ctx.lineTo(SHIELD_BOTTOM_X, SHIELD_BOTTOM_Y);
244 ctx.moveTo(SHIELD_CENTER_TOP_X, SHIELD_CENTER_TOP_Y);
245 ctx.lineTo(SHIELD_CENTER_BOTTOM_X, SHIELD_CENTER_BOTTOM_Y);
246 ctx.stroke();
247
Alex Perryb49a3fb2020-02-29 15:26:54 -0800248 this.drawHalfField(ctx, 'red');
249 ctx.rotate(Math.PI);
250 this.drawHalfField(ctx, 'blue');
251 ctx.rotate(Math.PI);
252 }
Alex Perry5427c9a2020-02-15 17:43:45 -0800253
Alex Perryb49a3fb2020-02-29 15:26:54 -0800254 drawHalfField(ctx, color: string): void {
255 // trenches
256 ctx.strokeStyle = color;
Alex Perry5427c9a2020-02-15 17:43:45 -0800257 ctx.beginPath();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800258 ctx.moveTo(TRENCH_X, FIELD_SIDE_Y);
259 ctx.lineTo(TRENCH_X, TRENCH_INSIDE);
260 ctx.lineTo(-TRENCH_X, TRENCH_INSIDE);
261 ctx.lineTo(-TRENCH_X, FIELD_SIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800262 ctx.stroke();
263
264 ctx.strokeStyle = 'black';
265 ctx.beginPath();
266 ctx.moveTo(SPINNER_TOP_X, FIELD_SIDE_Y);
267 ctx.lineTo(SPINNER_TOP_X, TRENCH_INSIDE);
268 ctx.lineTo(SPINNER_BOTTOM_X, TRENCH_INSIDE);
269 ctx.lineTo(SPINNER_BOTTOM_X, FIELD_SIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800270 ctx.stroke();
271
Alex Perry5427c9a2020-02-15 17:43:45 -0800272 ctx.beginPath();
273 ctx.moveTo(INITIATION_X, FIELD_SIDE_Y);
274 ctx.lineTo(INITIATION_X, -FIELD_SIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800275 ctx.stroke();
276
Alex Perryb49a3fb2020-02-29 15:26:54 -0800277 // target/loading
278 ctx.strokeStyle = color;
Alex Perry5427c9a2020-02-15 17:43:45 -0800279 ctx.beginPath();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800280 ctx.moveTo(FIELD_EDGE_X, DS_INSIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800281 ctx.lineTo(TARGET_ZONE_TIP_X, DS_INSIDE_Y - 0.5 * TARGET_ZONE_WIDTH);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800282 ctx.lineTo(FIELD_EDGE_X, DS_INSIDE_Y - TARGET_ZONE_WIDTH);
Alex Perry5427c9a2020-02-15 17:43:45 -0800283
Alex Perryb49a3fb2020-02-29 15:26:54 -0800284 ctx.moveTo(-FIELD_EDGE_X, DS_INSIDE_Y);
285 ctx.lineTo(-TARGET_ZONE_TIP_X, DS_INSIDE_Y - 0.5 * LOADING_ZONE_WIDTH);
286 ctx.lineTo(-FIELD_EDGE_X, DS_INSIDE_Y - LOADING_ZONE_WIDTH);
Alex Perry5427c9a2020-02-15 17:43:45 -0800287 ctx.stroke();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800288 }
Alex Perry5427c9a2020-02-15 17:43:45 -0800289
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700290 drawCamera(
291 x: number, y: number, theta: number, color: string = 'blue',
292 extendLines: boolean = true): void {
Alex Perryb49a3fb2020-02-29 15:26:54 -0800293 const ctx = this.canvas.getContext('2d');
294 ctx.save();
295 ctx.translate(x, y);
296 ctx.rotate(theta);
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700297 ctx.strokeStyle = color;
Alex Perry5427c9a2020-02-15 17:43:45 -0800298 ctx.beginPath();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800299 ctx.moveTo(0.5, 0.5);
300 ctx.lineTo(0, 0);
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700301 if (extendLines) {
302 ctx.lineTo(100.0, 0);
303 ctx.lineTo(0, 0);
304 }
Alex Perryb49a3fb2020-02-29 15:26:54 -0800305 ctx.lineTo(0.5, -0.5);
Alex Perry5427c9a2020-02-15 17:43:45 -0800306 ctx.stroke();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800307 ctx.beginPath();
Philipp Schradere625ba22020-11-16 20:11:37 -0800308 ctx.arc(0, 0, 0.25, -Math.PI / 4, Math.PI / 4);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800309 ctx.stroke();
310 ctx.restore();
311 }
312
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700313 drawRobot(
314 x: number, y: number, theta: number, turret: number|null,
315 color: string = 'blue', dashed: boolean = false,
316 extendLines: boolean = true): void {
Alex Perry2124ae82020-03-07 14:19:06 -0800317 const ctx = this.canvas.getContext('2d');
318 ctx.save();
319 ctx.translate(x, y);
320 ctx.rotate(theta);
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700321 ctx.strokeStyle = color;
322 if (dashed) {
323 ctx.setLineDash([0.05, 0.05]);
324 } else {
325 // Empty array = solid line.
326 ctx.setLineDash([]);
327 }
Alex Perry2124ae82020-03-07 14:19:06 -0800328 ctx.rect(-ROBOT_LENGTH / 2, -ROBOT_WIDTH / 2, ROBOT_LENGTH, ROBOT_WIDTH);
329 ctx.stroke();
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700330
331 // Draw line indicating which direction is forwards on the robot.
332 ctx.beginPath();
333 ctx.moveTo(0, 0);
334 if (extendLines) {
335 ctx.lineTo(1000.0, 0);
336 } else {
337 ctx.lineTo(ROBOT_LENGTH / 2.0, 0);
338 }
339 ctx.stroke();
340
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700341 if (turret) {
342 ctx.save();
343 ctx.rotate(turret + Math.PI);
344 const turretRadius = ROBOT_WIDTH / 4.0;
Alex Perry87cc63d2021-10-27 21:18:42 -0700345 ctx.strokeStyle = 'red';
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700346 // Draw circle for turret.
347 ctx.beginPath();
348 ctx.arc(0, 0, turretRadius, 0, 2.0 * Math.PI);
349 ctx.stroke();
350 // Draw line in circle to show forwards.
351 ctx.beginPath();
352 ctx.moveTo(0, 0);
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700353 if (extendLines) {
354 ctx.lineTo(1000.0, 0);
355 } else {
356 ctx.lineTo(turretRadius, 0);
357 }
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700358 ctx.stroke();
359 ctx.restore();
360 }
Alex Perry2124ae82020-03-07 14:19:06 -0800361 ctx.restore();
362 }
363
Alex Perry87cc63d2021-10-27 21:18:42 -0700364 setShooter(div: HTMLElement, flywheelStatus: FlywheelControllerStatus): void {
365 const currentVelocity = flywheelStatus.angularVelocity();
366 const goal = flywheelStatus.angularVelocityGoal();
367 // Show it as 'near' if difference between
368 if (Math.abs(currentVelocity - goal) < 5) {
369 this.setNear(div, currentVelocity.toFixed(2));
370 return;
371 }
372 div.classList.remove('faulted');
373 div.classList.remove('zeroing');
374 div.classList.remove('near');
375 div.innerHTML = currentVelocity.toFixed(2);
Austin Schuh840132b2021-10-17 17:40:14 -0700376 }
Alex Perry87cc63d2021-10-27 21:18:42 -0700377
378 setZeroing(div: HTMLElement): void {
379 div.innerHTML = 'zeroing';
380 div.classList.remove('faulted');
381 div.classList.add('zeroing');
382 div.classList.remove('near');
Austin Schuh840132b2021-10-17 17:40:14 -0700383 }
Alex Perry87cc63d2021-10-27 21:18:42 -0700384
385 setEstopped(div: HTMLElement): void {
386 div.innerHTML = 'estopped';
387 div.classList.add('faulted');
388 div.classList.remove('zeroing');
389 div.classList.remove('near');
390 }
391
392 setNear(div: HTMLElement, val: string): void {
393 div.innerHTML = val;
394 div.classList.remove('faulted');
395 div.classList.remove('zeroing');
396 div.classList.add('near');
397 }
398
399 setTargetValue(
400 div: HTMLElement, target: number, val: number, tolerance: number): void {
401 div.innerHTML = val.toFixed(4);
402 div.classList.remove('faulted');
403 div.classList.remove('zeroing');
404 if (Math.abs(target - val) < tolerance) {
405 div.classList.add('near');
406 } else {
407 div.classList.remove('near');
408 }
409 }
410
411 setValue(div: HTMLElement, val: number): void {
412 div.innerHTML = val.toFixed(4);
413 div.classList.remove('faulted');
414 div.classList.remove('zeroing');
415 div.classList.remove('near');
Austin Schuh840132b2021-10-17 17:40:14 -0700416 }
417
Philipp Schradere625ba22020-11-16 20:11:37 -0800418 draw(): void {
Alex Perryb49a3fb2020-02-29 15:26:54 -0800419 this.reset();
420 this.drawField();
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700421
422 // Draw the matches with debugging information from the localizer.
423 const now = Date.now() / 1000.0;
424 for (const [time, value] of this.localizerImageMatches) {
425 const age = now - time;
426 const kRemovalAge = 2.0;
427 if (age > kRemovalAge) {
428 this.localizerImageMatches.delete(time);
429 continue;
430 }
431 const ageAlpha = (kRemovalAge - age) / kRemovalAge
432 for (let i = 0; i < value.matchesLength(); i++) {
433 const imageDebug = value.matches(i);
434 const x = imageDebug.impliedRobotX();
435 const y = imageDebug.impliedRobotY();
436 const theta = imageDebug.impliedRobotTheta();
437 const cameraX = imageDebug.cameraX();
438 const cameraY = imageDebug.cameraY();
439 const cameraTheta = imageDebug.cameraTheta();
440 const accepted = imageDebug.accepted();
441 // Make camera readings fade over time.
442 const alpha = Math.round(255 * ageAlpha).toString(16).padStart(2, '0');
443 const dashed = false;
Alex Perry87cc63d2021-10-27 21:18:42 -0700444 const acceptedRgb = accepted ? '#00FF00' : '#FF0000';
James Kuszmaul286b80c2021-10-23 21:56:33 -0700445 const acceptedRgba = acceptedRgb + alpha;
446 const cameraRgb = PI_COLORS[imageDebug.camera()];
447 const cameraRgba = cameraRgb + alpha;
448 this.drawRobot(x, y, theta, null, acceptedRgba, dashed, false);
449 this.drawCamera(cameraX, cameraY, cameraTheta, cameraRgba, false);
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700450 }
451 }
452
453 // draw cameras from ImageMatchResults directly (helpful when viewing page
454 // on the pis individually).
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700455 for (const keyPair of this.imageMatchResult) {
456 const value = keyPair[1];
457 for (let i = 0; i < value.cameraPosesLength(); i++) {
458 const pose = value.cameraPoses(i);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800459 const mat = pose.fieldToCamera();
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700460 // Matrix layout:
461 // [0, 1, 2, 3]
462 // [4, 5, 6, 7]
463 // [8, 9, 10, 11]
464 // [12, 13, 14, 15]
Alex Perryb49a3fb2020-02-29 15:26:54 -0800465 const x = mat.data(3);
466 const y = mat.data(7);
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700467 const theta = Math.atan2(mat.data(6), mat.data(2));
James Kuszmaul286b80c2021-10-23 21:56:33 -0700468 const cameraColor = (keyPair[0].length > 0) ?
469 PI_COLORS[Number(keyPair[0][3]) - 1] :
470 'blue';
471 this.drawCamera(x, y, theta, cameraColor);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800472 }
473 }
474
Alex Perry2124ae82020-03-07 14:19:06 -0800475 if (this.drivetrainStatus) {
Austin Schuh840132b2021-10-17 17:40:14 -0700476 if (!this.drivetrainStatus.zeroing().zeroed()) {
477 this.setZeroing(this.x);
478 this.setZeroing(this.y);
479 this.setZeroing(this.theta);
480 } else if (this.drivetrainStatus.zeroing().faulted()) {
481 this.setEstopped(this.x);
482 this.setEstopped(this.y);
483 this.setEstopped(this.theta);
484 } else {
485 this.setValue(this.x, this.drivetrainStatus.x());
486 this.setValue(this.y, this.drivetrainStatus.y());
487 this.setValue(this.theta, this.drivetrainStatus.theta());
488 }
489
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700490 if (this.superstructureStatus) {
491 this.shotDistance.innerHTML =
492 this.superstructureStatus.aimer().shotDistance().toFixed(2);
Alex Perry87cc63d2021-10-27 21:18:42 -0700493 this.setShooter(
494 this.finisher, this.superstructureStatus.shooter().finisher());
495 this.setShooter(
496 this.leftAccelerator,
497 this.superstructureStatus.shooter().acceleratorLeft());
498 this.setShooter(
499 this.rightAccelerator,
500 this.superstructureStatus.shooter().acceleratorRight());
501
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700502 if (this.superstructureStatus.aimer().aimingForInnerPort()) {
503 this.innerPort.innerHTML = 'true';
Austin Schuh146c0bc2021-11-07 22:33:28 -0800504 this.outerTarget.classList.remove('targetted');
505 this.innerTarget.classList.add('targetted');
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700506 } else {
507 this.innerPort.innerHTML = 'false';
Austin Schuh146c0bc2021-11-07 22:33:28 -0800508 this.outerTarget.classList.add('targetted');
509 this.innerTarget.classList.remove('targetted');
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700510 }
Alex Perry87cc63d2021-10-27 21:18:42 -0700511
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700512 if (!this.superstructureStatus.hood().zeroed()) {
513 this.setZeroing(this.hood);
514 } else if (this.superstructureStatus.hood().estopped()) {
515 this.setEstopped(this.hood);
516 } else {
Alex Perry87cc63d2021-10-27 21:18:42 -0700517 this.setTargetValue(
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700518 this.hood,
Alex Perry87cc63d2021-10-27 21:18:42 -0700519 this.superstructureStatus.hood().unprofiledGoalPosition(),
520 this.superstructureStatus.hood().estimatorState().position(),
521 1e-3);
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700522 }
Alex Perry87cc63d2021-10-27 21:18:42 -0700523
Austin Schuh146c0bc2021-11-07 22:33:28 -0800524 this.ballsShot.innerHTML =
525 this.superstructureStatus.shooter().ballsShot().toString();
526
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700527 if (!this.superstructureStatus.turret().zeroed()) {
528 this.setZeroing(this.turret);
529 } else if (this.superstructureStatus.turret().estopped()) {
530 this.setEstopped(this.turret);
531 } else {
Alex Perry87cc63d2021-10-27 21:18:42 -0700532 this.setTargetValue(
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700533 this.turret,
Alex Perry87cc63d2021-10-27 21:18:42 -0700534 this.superstructureStatus.turret().unprofiledGoalPosition(),
535 this.superstructureStatus.turret().estimatorState().position(),
536 1e-3);
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700537 }
Alex Perry87cc63d2021-10-27 21:18:42 -0700538
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700539 if (!this.superstructureStatus.intake().zeroed()) {
540 this.setZeroing(this.intake);
541 } else if (this.superstructureStatus.intake().estopped()) {
542 this.setEstopped(this.intake);
543 } else {
544 this.setValue(
545 this.intake,
546 this.superstructureStatus.intake().estimatorState().position());
547 }
Austin Schuh840132b2021-10-17 17:40:14 -0700548 }
Alex Perry87cc63d2021-10-27 21:18:42 -0700549
Alex Perry2124ae82020-03-07 14:19:06 -0800550 this.drawRobot(
551 this.drivetrainStatus.x(), this.drivetrainStatus.y(),
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700552 this.drivetrainStatus.theta(),
553 this.superstructureStatus ?
554 this.superstructureStatus.turret().position() :
555 null);
Alex Perry2124ae82020-03-07 14:19:06 -0800556 }
557
Alex Perryb49a3fb2020-02-29 15:26:54 -0800558 window.requestAnimationFrame(() => this.draw());
Alex Perry5427c9a2020-02-15 17:43:45 -0800559 }
560
561 reset(): void {
562 const ctx = this.canvas.getContext('2d');
563 ctx.setTransform(1, 0, 0, 1, 0, 0);
564 const size = window.innerHeight * 0.9;
565 ctx.canvas.height = size;
Alex Perryb49a3fb2020-02-29 15:26:54 -0800566 const width = size / 2 + 20;
567 ctx.canvas.width = width;
568 ctx.clearRect(0, 0, size, width);
Alex Perry5427c9a2020-02-15 17:43:45 -0800569
Alex Perryb49a3fb2020-02-29 15:26:54 -0800570 // Translate to center of display.
571 ctx.translate(width / 2, size / 2);
Alex Perry5427c9a2020-02-15 17:43:45 -0800572 // Coordinate system is:
573 // x -> forward.
574 // y -> to the left.
575 ctx.rotate(-Math.PI / 2);
576 ctx.scale(1, -1);
Alex Perry5427c9a2020-02-15 17:43:45 -0800577
578 const M_TO_PX = (size - 10) / FIELD_LENGTH;
579 ctx.scale(M_TO_PX, M_TO_PX);
580 ctx.lineWidth = 1 / M_TO_PX;
581 }
582}