blob: eb5ece53cbb1208c3f7b06e5d8f41b9caf80fcb5 [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>();
Alex Perry87cc63d2021-10-27 21:18:42 -070080 private outer_target: HTMLElement =
81 (document.getElementById('outer_target') as HTMLElement);
82 private inner_target: HTMLElement =
83 (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);
101 private intake: HTMLElement =
102 (document.getElementById('intake') as HTMLElement);
103 private imagesAcceptedCounter: HTMLElement =
104 (document.getElementById('images_accepted') as HTMLElement);
105 private imagesRejectedCounter: HTMLElement =
106 (document.getElementById('images_rejected') as HTMLElement);
107 private rejectionReasonCells: HTMLElement[] = [];
Alex Perry5427c9a2020-02-15 17:43:45 -0800108
Alex Perryb49a3fb2020-02-29 15:26:54 -0800109 constructor(private readonly connection: Connection) {
Austin Schuh840132b2021-10-17 17:40:14 -0700110 (document.getElementById('field') as HTMLElement).appendChild(this.canvas);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800111
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700112 for (const value in RejectionReason) {
113 // Typescript generates an iterator that produces both numbers and
114 // strings... don't do anything on the string iterations.
115 if (isNaN(Number(value))) {
116 continue;
117 }
Alex Perry87cc63d2021-10-27 21:18:42 -0700118 const row = document.createElement('div');
119 const nameCell = document.createElement('div');
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700120 nameCell.innerHTML = RejectionReason[value];
121 row.appendChild(nameCell);
Alex Perry87cc63d2021-10-27 21:18:42 -0700122 const valueCell = document.createElement('div');
123 valueCell.innerHTML = 'NA';
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700124 this.rejectionReasonCells.push(valueCell);
125 row.appendChild(valueCell);
Alex Perry87cc63d2021-10-27 21:18:42 -0700126 document.getElementById('vision_readouts').appendChild(row);
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700127 }
128
James Kuszmaul286b80c2021-10-23 21:56:33 -0700129 for (let ii = 0; ii < PI_COLORS.length; ++ii) {
Alex Perry87cc63d2021-10-27 21:18:42 -0700130 const legendEntry = document.createElement('div');
James Kuszmaul286b80c2021-10-23 21:56:33 -0700131 legendEntry.style.color = PI_COLORS[ii];
Alex Perry87cc63d2021-10-27 21:18:42 -0700132 legendEntry.innerHTML = 'PI' + (ii + 1).toString()
133 document.getElementById('legend').appendChild(legendEntry);
James Kuszmaul286b80c2021-10-23 21:56:33 -0700134 }
135
Alex Perryb49a3fb2020-02-29 15:26:54 -0800136 this.connection.addConfigHandler(() => {
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700137 // Go through and register handlers for both all the individual pis as
138 // well as the local pi. Depending on the node that we are running on,
139 // different subsets of these will be available.
James Kuszmaul286b80c2021-10-23 21:56:33 -0700140 for (const prefix of ['', '/pi1', '/pi2', '/pi3', '/pi4', '/pi5']) {
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700141 this.connection.addHandler(
James Kuszmaul286b80c2021-10-23 21:56:33 -0700142 prefix + '/camera', ImageMatchResult.getFullyQualifiedName(),
143 (res) => {
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700144 this.handleImageMatchResult(prefix, res);
145 });
146 }
James Kuszmaul527038a2020-12-21 23:40:44 -0800147 this.connection.addHandler(
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700148 '/drivetrain', LocalizerDebug.getFullyQualifiedName(), (data) => {
149 this.handleLocalizerDebug(data);
150 });
151 this.connection.addHandler(
James Kuszmaul527038a2020-12-21 23:40:44 -0800152 '/drivetrain', DrivetrainStatus.getFullyQualifiedName(), (data) => {
153 this.handleDrivetrainStatus(data);
154 });
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700155 this.connection.addHandler(
156 '/superstructure', SuperstructureStatus.getFullyQualifiedName(),
157 (data) => {
158 this.handleSuperstructureStatus(data);
159 });
Alex Perryb49a3fb2020-02-29 15:26:54 -0800160 });
Alex Perryb49a3fb2020-02-29 15:26:54 -0800161 }
162
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700163 private handleImageMatchResult(prefix: string, data: Uint8Array): void {
Philipp Schradere625ba22020-11-16 20:11:37 -0800164 const fbBuffer = new ByteBuffer(data);
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700165 this.imageMatchResult.set(
166 prefix,
167 ImageMatchResult.getRootAsImageMatchResult(
168 fbBuffer as unknown as flatbuffers.ByteBuffer));
Alex Perryb49a3fb2020-02-29 15:26:54 -0800169 }
170
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700171 private handleLocalizerDebug(data: Uint8Array): void {
172 const now = Date.now() / 1000.0;
173
174 const fbBuffer = new ByteBuffer(data);
175 this.localizerImageMatches.set(
176 now,
177 LocalizerDebug.getRootAsLocalizerDebug(
178 fbBuffer as unknown as flatbuffers.ByteBuffer));
179
180 const debug = this.localizerImageMatches.get(now);
181
182 if (debug.statistics()) {
183 this.imagesAcceptedCounter.innerHTML =
184 debug.statistics().totalAccepted().toString();
185 this.imagesRejectedCounter.innerHTML =
186 (debug.statistics().totalCandidates() -
187 debug.statistics().totalAccepted())
188 .toString();
189 if (debug.statistics().rejectionReasonCountLength() ==
190 this.rejectionReasonCells.length) {
191 for (let ii = 0; ii < debug.statistics().rejectionReasonCountLength();
192 ++ii) {
193 this.rejectionReasonCells[ii].innerHTML =
194 debug.statistics().rejectionReasonCount(ii).toString();
195 }
196 } else {
Alex Perry87cc63d2021-10-27 21:18:42 -0700197 console.error('Unexpected number of rejection reasons in counter.');
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700198 }
199 this.imagesRejectedCounter.innerHTML =
200 (debug.statistics().totalCandidates() -
201 debug.statistics().totalAccepted())
202 .toString();
203 }
204 }
205
Alex Perry2124ae82020-03-07 14:19:06 -0800206 private handleDrivetrainStatus(data: Uint8Array): void {
Philipp Schradere625ba22020-11-16 20:11:37 -0800207 const fbBuffer = new ByteBuffer(data);
208 this.drivetrainStatus = DrivetrainStatus.getRootAsStatus(
209 fbBuffer as unknown as flatbuffers.ByteBuffer);
Alex Perry2124ae82020-03-07 14:19:06 -0800210 }
211
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700212 private handleSuperstructureStatus(data: Uint8Array): void {
213 const fbBuffer = new ByteBuffer(data);
214 this.superstructureStatus = SuperstructureStatus.getRootAsStatus(
215 fbBuffer as unknown as flatbuffers.ByteBuffer);
216 }
217
Alex Perry5427c9a2020-02-15 17:43:45 -0800218 drawField(): void {
219 const MY_COLOR = 'red';
220 const OTHER_COLOR = 'blue';
221 const ctx = this.canvas.getContext('2d');
222 // draw perimiter
223 ctx.beginPath();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800224 ctx.moveTo(FIELD_EDGE_X, DS_INSIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800225 ctx.lineTo(DS_END_X, FIELD_SIDE_Y);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800226 ctx.lineTo(-DS_END_X, FIELD_SIDE_Y);
227 ctx.lineTo(-FIELD_EDGE_X, DS_INSIDE_Y);
228 ctx.lineTo(-FIELD_EDGE_X, -DS_INSIDE_Y);
229 ctx.lineTo(-DS_END_X, -FIELD_SIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800230 ctx.lineTo(DS_END_X, -FIELD_SIDE_Y);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800231 ctx.lineTo(FIELD_EDGE_X, -DS_INSIDE_Y);
232 ctx.lineTo(FIELD_EDGE_X, DS_INSIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800233 ctx.stroke();
234
235 // draw shield generator
236 ctx.beginPath();
237 ctx.moveTo(SHIELD_BOTTOM_X, SHIELD_BOTTOM_Y);
238 ctx.lineTo(SHIELD_RIGHT_X, SHIELD_RIGHT_Y);
239 ctx.lineTo(SHIELD_TOP_X, SHIELD_TOP_Y);
240 ctx.lineTo(SHIELD_LEFT_X, SHIELD_LEFT_Y);
241 ctx.lineTo(SHIELD_BOTTOM_X, SHIELD_BOTTOM_Y);
242 ctx.moveTo(SHIELD_CENTER_TOP_X, SHIELD_CENTER_TOP_Y);
243 ctx.lineTo(SHIELD_CENTER_BOTTOM_X, SHIELD_CENTER_BOTTOM_Y);
244 ctx.stroke();
245
Alex Perryb49a3fb2020-02-29 15:26:54 -0800246 this.drawHalfField(ctx, 'red');
247 ctx.rotate(Math.PI);
248 this.drawHalfField(ctx, 'blue');
249 ctx.rotate(Math.PI);
250 }
Alex Perry5427c9a2020-02-15 17:43:45 -0800251
Alex Perryb49a3fb2020-02-29 15:26:54 -0800252 drawHalfField(ctx, color: string): void {
253 // trenches
254 ctx.strokeStyle = color;
Alex Perry5427c9a2020-02-15 17:43:45 -0800255 ctx.beginPath();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800256 ctx.moveTo(TRENCH_X, FIELD_SIDE_Y);
257 ctx.lineTo(TRENCH_X, TRENCH_INSIDE);
258 ctx.lineTo(-TRENCH_X, TRENCH_INSIDE);
259 ctx.lineTo(-TRENCH_X, FIELD_SIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800260 ctx.stroke();
261
262 ctx.strokeStyle = 'black';
263 ctx.beginPath();
264 ctx.moveTo(SPINNER_TOP_X, FIELD_SIDE_Y);
265 ctx.lineTo(SPINNER_TOP_X, TRENCH_INSIDE);
266 ctx.lineTo(SPINNER_BOTTOM_X, TRENCH_INSIDE);
267 ctx.lineTo(SPINNER_BOTTOM_X, FIELD_SIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800268 ctx.stroke();
269
Alex Perry5427c9a2020-02-15 17:43:45 -0800270 ctx.beginPath();
271 ctx.moveTo(INITIATION_X, FIELD_SIDE_Y);
272 ctx.lineTo(INITIATION_X, -FIELD_SIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800273 ctx.stroke();
274
Alex Perryb49a3fb2020-02-29 15:26:54 -0800275 // target/loading
276 ctx.strokeStyle = color;
Alex Perry5427c9a2020-02-15 17:43:45 -0800277 ctx.beginPath();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800278 ctx.moveTo(FIELD_EDGE_X, DS_INSIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800279 ctx.lineTo(TARGET_ZONE_TIP_X, DS_INSIDE_Y - 0.5 * TARGET_ZONE_WIDTH);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800280 ctx.lineTo(FIELD_EDGE_X, DS_INSIDE_Y - TARGET_ZONE_WIDTH);
Alex Perry5427c9a2020-02-15 17:43:45 -0800281
Alex Perryb49a3fb2020-02-29 15:26:54 -0800282 ctx.moveTo(-FIELD_EDGE_X, DS_INSIDE_Y);
283 ctx.lineTo(-TARGET_ZONE_TIP_X, DS_INSIDE_Y - 0.5 * LOADING_ZONE_WIDTH);
284 ctx.lineTo(-FIELD_EDGE_X, DS_INSIDE_Y - LOADING_ZONE_WIDTH);
Alex Perry5427c9a2020-02-15 17:43:45 -0800285 ctx.stroke();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800286 }
Alex Perry5427c9a2020-02-15 17:43:45 -0800287
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700288 drawCamera(
289 x: number, y: number, theta: number, color: string = 'blue',
290 extendLines: boolean = true): void {
Alex Perryb49a3fb2020-02-29 15:26:54 -0800291 const ctx = this.canvas.getContext('2d');
292 ctx.save();
293 ctx.translate(x, y);
294 ctx.rotate(theta);
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700295 ctx.strokeStyle = color;
Alex Perry5427c9a2020-02-15 17:43:45 -0800296 ctx.beginPath();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800297 ctx.moveTo(0.5, 0.5);
298 ctx.lineTo(0, 0);
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700299 if (extendLines) {
300 ctx.lineTo(100.0, 0);
301 ctx.lineTo(0, 0);
302 }
Alex Perryb49a3fb2020-02-29 15:26:54 -0800303 ctx.lineTo(0.5, -0.5);
Alex Perry5427c9a2020-02-15 17:43:45 -0800304 ctx.stroke();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800305 ctx.beginPath();
Philipp Schradere625ba22020-11-16 20:11:37 -0800306 ctx.arc(0, 0, 0.25, -Math.PI / 4, Math.PI / 4);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800307 ctx.stroke();
308 ctx.restore();
309 }
310
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700311 drawRobot(
312 x: number, y: number, theta: number, turret: number|null,
313 color: string = 'blue', dashed: boolean = false,
314 extendLines: boolean = true): void {
Alex Perry2124ae82020-03-07 14:19:06 -0800315 const ctx = this.canvas.getContext('2d');
316 ctx.save();
317 ctx.translate(x, y);
318 ctx.rotate(theta);
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700319 ctx.strokeStyle = color;
320 if (dashed) {
321 ctx.setLineDash([0.05, 0.05]);
322 } else {
323 // Empty array = solid line.
324 ctx.setLineDash([]);
325 }
Alex Perry2124ae82020-03-07 14:19:06 -0800326 ctx.rect(-ROBOT_LENGTH / 2, -ROBOT_WIDTH / 2, ROBOT_LENGTH, ROBOT_WIDTH);
327 ctx.stroke();
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700328
329 // Draw line indicating which direction is forwards on the robot.
330 ctx.beginPath();
331 ctx.moveTo(0, 0);
332 if (extendLines) {
333 ctx.lineTo(1000.0, 0);
334 } else {
335 ctx.lineTo(ROBOT_LENGTH / 2.0, 0);
336 }
337 ctx.stroke();
338
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700339 if (turret) {
340 ctx.save();
341 ctx.rotate(turret + Math.PI);
342 const turretRadius = ROBOT_WIDTH / 4.0;
Alex Perry87cc63d2021-10-27 21:18:42 -0700343 ctx.strokeStyle = 'red';
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700344 // Draw circle for turret.
345 ctx.beginPath();
346 ctx.arc(0, 0, turretRadius, 0, 2.0 * Math.PI);
347 ctx.stroke();
348 // Draw line in circle to show forwards.
349 ctx.beginPath();
350 ctx.moveTo(0, 0);
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700351 if (extendLines) {
352 ctx.lineTo(1000.0, 0);
353 } else {
354 ctx.lineTo(turretRadius, 0);
355 }
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700356 ctx.stroke();
357 ctx.restore();
358 }
Alex Perry2124ae82020-03-07 14:19:06 -0800359 ctx.restore();
360 }
361
Alex Perry87cc63d2021-10-27 21:18:42 -0700362 setShooter(div: HTMLElement, flywheelStatus: FlywheelControllerStatus): void {
363 const currentVelocity = flywheelStatus.angularVelocity();
364 const goal = flywheelStatus.angularVelocityGoal();
365 // Show it as 'near' if difference between
366 if (Math.abs(currentVelocity - goal) < 5) {
367 this.setNear(div, currentVelocity.toFixed(2));
368 return;
369 }
370 div.classList.remove('faulted');
371 div.classList.remove('zeroing');
372 div.classList.remove('near');
373 div.innerHTML = currentVelocity.toFixed(2);
Austin Schuh840132b2021-10-17 17:40:14 -0700374 }
Alex Perry87cc63d2021-10-27 21:18:42 -0700375
376 setZeroing(div: HTMLElement): void {
377 div.innerHTML = 'zeroing';
378 div.classList.remove('faulted');
379 div.classList.add('zeroing');
380 div.classList.remove('near');
Austin Schuh840132b2021-10-17 17:40:14 -0700381 }
Alex Perry87cc63d2021-10-27 21:18:42 -0700382
383 setEstopped(div: HTMLElement): void {
384 div.innerHTML = 'estopped';
385 div.classList.add('faulted');
386 div.classList.remove('zeroing');
387 div.classList.remove('near');
388 }
389
390 setNear(div: HTMLElement, val: string): void {
391 div.innerHTML = val;
392 div.classList.remove('faulted');
393 div.classList.remove('zeroing');
394 div.classList.add('near');
395 }
396
397 setTargetValue(
398 div: HTMLElement, target: number, val: number, tolerance: number): void {
399 div.innerHTML = val.toFixed(4);
400 div.classList.remove('faulted');
401 div.classList.remove('zeroing');
402 if (Math.abs(target - val) < tolerance) {
403 div.classList.add('near');
404 } else {
405 div.classList.remove('near');
406 }
407 }
408
409 setValue(div: HTMLElement, val: number): void {
410 div.innerHTML = val.toFixed(4);
411 div.classList.remove('faulted');
412 div.classList.remove('zeroing');
413 div.classList.remove('near');
Austin Schuh840132b2021-10-17 17:40:14 -0700414 }
415
Philipp Schradere625ba22020-11-16 20:11:37 -0800416 draw(): void {
Alex Perryb49a3fb2020-02-29 15:26:54 -0800417 this.reset();
418 this.drawField();
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700419
420 // Draw the matches with debugging information from the localizer.
421 const now = Date.now() / 1000.0;
422 for (const [time, value] of this.localizerImageMatches) {
423 const age = now - time;
424 const kRemovalAge = 2.0;
425 if (age > kRemovalAge) {
426 this.localizerImageMatches.delete(time);
427 continue;
428 }
429 const ageAlpha = (kRemovalAge - age) / kRemovalAge
430 for (let i = 0; i < value.matchesLength(); i++) {
431 const imageDebug = value.matches(i);
432 const x = imageDebug.impliedRobotX();
433 const y = imageDebug.impliedRobotY();
434 const theta = imageDebug.impliedRobotTheta();
435 const cameraX = imageDebug.cameraX();
436 const cameraY = imageDebug.cameraY();
437 const cameraTheta = imageDebug.cameraTheta();
438 const accepted = imageDebug.accepted();
439 // Make camera readings fade over time.
440 const alpha = Math.round(255 * ageAlpha).toString(16).padStart(2, '0');
441 const dashed = false;
Alex Perry87cc63d2021-10-27 21:18:42 -0700442 const acceptedRgb = accepted ? '#00FF00' : '#FF0000';
James Kuszmaul286b80c2021-10-23 21:56:33 -0700443 const acceptedRgba = acceptedRgb + alpha;
444 const cameraRgb = PI_COLORS[imageDebug.camera()];
445 const cameraRgba = cameraRgb + alpha;
446 this.drawRobot(x, y, theta, null, acceptedRgba, dashed, false);
447 this.drawCamera(cameraX, cameraY, cameraTheta, cameraRgba, false);
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700448 }
449 }
450
451 // draw cameras from ImageMatchResults directly (helpful when viewing page
452 // on the pis individually).
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700453 for (const keyPair of this.imageMatchResult) {
454 const value = keyPair[1];
455 for (let i = 0; i < value.cameraPosesLength(); i++) {
456 const pose = value.cameraPoses(i);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800457 const mat = pose.fieldToCamera();
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700458 // Matrix layout:
459 // [0, 1, 2, 3]
460 // [4, 5, 6, 7]
461 // [8, 9, 10, 11]
462 // [12, 13, 14, 15]
Alex Perryb49a3fb2020-02-29 15:26:54 -0800463 const x = mat.data(3);
464 const y = mat.data(7);
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700465 const theta = Math.atan2(mat.data(6), mat.data(2));
James Kuszmaul286b80c2021-10-23 21:56:33 -0700466 const cameraColor = (keyPair[0].length > 0) ?
467 PI_COLORS[Number(keyPair[0][3]) - 1] :
468 'blue';
469 this.drawCamera(x, y, theta, cameraColor);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800470 }
471 }
472
Alex Perry2124ae82020-03-07 14:19:06 -0800473 if (this.drivetrainStatus) {
Austin Schuh840132b2021-10-17 17:40:14 -0700474 if (!this.drivetrainStatus.zeroing().zeroed()) {
475 this.setZeroing(this.x);
476 this.setZeroing(this.y);
477 this.setZeroing(this.theta);
478 } else if (this.drivetrainStatus.zeroing().faulted()) {
479 this.setEstopped(this.x);
480 this.setEstopped(this.y);
481 this.setEstopped(this.theta);
482 } else {
483 this.setValue(this.x, this.drivetrainStatus.x());
484 this.setValue(this.y, this.drivetrainStatus.y());
485 this.setValue(this.theta, this.drivetrainStatus.theta());
486 }
487
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700488 if (this.superstructureStatus) {
489 this.shotDistance.innerHTML =
490 this.superstructureStatus.aimer().shotDistance().toFixed(2);
Alex Perry87cc63d2021-10-27 21:18:42 -0700491 this.setShooter(
492 this.finisher, this.superstructureStatus.shooter().finisher());
493 this.setShooter(
494 this.leftAccelerator,
495 this.superstructureStatus.shooter().acceleratorLeft());
496 this.setShooter(
497 this.rightAccelerator,
498 this.superstructureStatus.shooter().acceleratorRight());
499
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700500 if (this.superstructureStatus.aimer().aimingForInnerPort()) {
501 this.innerPort.innerHTML = 'true';
Alex Perry87cc63d2021-10-27 21:18:42 -0700502 this.outer_target.classList.remove('targetted');
503 this.inner_target.classList.add('targetted');
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700504 } else {
505 this.innerPort.innerHTML = 'false';
Alex Perry87cc63d2021-10-27 21:18:42 -0700506 this.outer_target.classList.add('targetted');
507 this.inner_target.classList.remove('targetted');
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700508 }
Alex Perry87cc63d2021-10-27 21:18:42 -0700509
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700510 if (!this.superstructureStatus.hood().zeroed()) {
511 this.setZeroing(this.hood);
512 } else if (this.superstructureStatus.hood().estopped()) {
513 this.setEstopped(this.hood);
514 } else {
Alex Perry87cc63d2021-10-27 21:18:42 -0700515 this.setTargetValue(
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700516 this.hood,
Alex Perry87cc63d2021-10-27 21:18:42 -0700517 this.superstructureStatus.hood().unprofiledGoalPosition(),
518 this.superstructureStatus.hood().estimatorState().position(),
519 1e-3);
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700520 }
Alex Perry87cc63d2021-10-27 21:18:42 -0700521
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700522 if (!this.superstructureStatus.turret().zeroed()) {
523 this.setZeroing(this.turret);
524 } else if (this.superstructureStatus.turret().estopped()) {
525 this.setEstopped(this.turret);
526 } else {
Alex Perry87cc63d2021-10-27 21:18:42 -0700527 this.setTargetValue(
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700528 this.turret,
Alex Perry87cc63d2021-10-27 21:18:42 -0700529 this.superstructureStatus.turret().unprofiledGoalPosition(),
530 this.superstructureStatus.turret().estimatorState().position(),
531 1e-3);
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700532 }
Alex Perry87cc63d2021-10-27 21:18:42 -0700533
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700534 if (!this.superstructureStatus.intake().zeroed()) {
535 this.setZeroing(this.intake);
536 } else if (this.superstructureStatus.intake().estopped()) {
537 this.setEstopped(this.intake);
538 } else {
539 this.setValue(
540 this.intake,
541 this.superstructureStatus.intake().estimatorState().position());
542 }
Austin Schuh840132b2021-10-17 17:40:14 -0700543 }
Alex Perry87cc63d2021-10-27 21:18:42 -0700544
Alex Perry2124ae82020-03-07 14:19:06 -0800545 this.drawRobot(
546 this.drivetrainStatus.x(), this.drivetrainStatus.y(),
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700547 this.drivetrainStatus.theta(),
548 this.superstructureStatus ?
549 this.superstructureStatus.turret().position() :
550 null);
Alex Perry2124ae82020-03-07 14:19:06 -0800551 }
552
Alex Perryb49a3fb2020-02-29 15:26:54 -0800553 window.requestAnimationFrame(() => this.draw());
Alex Perry5427c9a2020-02-15 17:43:45 -0800554 }
555
556 reset(): void {
557 const ctx = this.canvas.getContext('2d');
558 ctx.setTransform(1, 0, 0, 1, 0, 0);
559 const size = window.innerHeight * 0.9;
560 ctx.canvas.height = size;
Alex Perryb49a3fb2020-02-29 15:26:54 -0800561 const width = size / 2 + 20;
562 ctx.canvas.width = width;
563 ctx.clearRect(0, 0, size, width);
Alex Perry5427c9a2020-02-15 17:43:45 -0800564
Alex Perryb49a3fb2020-02-29 15:26:54 -0800565 // Translate to center of display.
566 ctx.translate(width / 2, size / 2);
Alex Perry5427c9a2020-02-15 17:43:45 -0800567 // Coordinate system is:
568 // x -> forward.
569 // y -> to the left.
570 ctx.rotate(-Math.PI / 2);
571 ctx.scale(1, -1);
Alex Perry5427c9a2020-02-15 17:43:45 -0800572
573 const M_TO_PX = (size - 10) / FIELD_LENGTH;
574 ctx.scale(M_TO_PX, M_TO_PX);
575 ctx.lineWidth = 1 / M_TO_PX;
576 }
577}