blob: fc70d99c0369cdfee3e2c081d7e2a93ed0a099b8 [file] [log] [blame]
Philipp Schradere625ba22020-11-16 20:11:37 -08001import * as configuration from 'org_frc971/aos/configuration_generated';
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';
4import {ByteBuffer} from 'org_frc971/external/com_github_google_flatbuffers/ts/byte-buffer';
5import * as drivetrain from 'org_frc971/frc971/control_loops/drivetrain/drivetrain_status_generated';
James Kuszmaulf75ecd62021-10-23 14:33:46 -07006import * as localizer from 'org_frc971/y2020/control_loops/drivetrain/localizer_debug_generated';
Philipp Schradere625ba22020-11-16 20:11:37 -08007import * as sift from 'org_frc971/y2020/vision/sift/sift_generated';
James Kuszmaul71a81932020-12-15 21:08:01 -08008import * as web_proxy from 'org_frc971/aos/network/web_proxy_generated';
James Kuszmaul5e6aa252021-08-28 22:19:29 -07009import * as ss from 'org_frc971/y2020/control_loops/superstructure/superstructure_status_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;
Philipp Schradere625ba22020-11-16 20:11:37 -080016import ImageMatchResult = sift.frc971.vision.sift.ImageMatchResult;
Philipp Schradere625ba22020-11-16 20:11:37 -080017import Channel = configuration.aos.Channel;
James Kuszmaul71a81932020-12-15 21:08:01 -080018import SubscriberRequest = web_proxy.aos.web_proxy.SubscriberRequest;
19import ChannelRequest = web_proxy.aos.web_proxy.ChannelRequest;
20import TransferMethod = web_proxy.aos.web_proxy.TransferMethod;
Alex Perry5427c9a2020-02-15 17:43:45 -080021
Alex Perry2124ae82020-03-07 14:19:06 -080022import {FIELD_LENGTH, FIELD_WIDTH, FT_TO_M, IN_TO_M} from './constants';
23
Alex Perryb49a3fb2020-02-29 15:26:54 -080024// (0,0) is field center, +X is toward red DS
Alex Perry5427c9a2020-02-15 17:43:45 -080025const FIELD_SIDE_Y = FIELD_WIDTH / 2;
Alex Perryb49a3fb2020-02-29 15:26:54 -080026const FIELD_EDGE_X = FIELD_LENGTH / 2;
Alex Perry5427c9a2020-02-15 17:43:45 -080027
28const DS_WIDTH = 69 * IN_TO_M;
29const DS_ANGLE = 20 * Math.PI / 180;
Alex Perryb49a3fb2020-02-29 15:26:54 -080030const DS_END_X = FIELD_EDGE_X - DS_WIDTH * Math.sin(DS_ANGLE);
Alex Perry5427c9a2020-02-15 17:43:45 -080031const DS_INSIDE_Y = FIELD_SIDE_Y - DS_WIDTH * Math.cos(DS_ANGLE);
32
Alex Perryb49a3fb2020-02-29 15:26:54 -080033const TRENCH_X = 108 * IN_TO_M;
Alex Perry5427c9a2020-02-15 17:43:45 -080034const TRENCH_WIDTH = 55.5 * IN_TO_M;
35const TRENCH_INSIDE = FIELD_SIDE_Y - TRENCH_WIDTH;
36
37const SPINNER_LENGTH = 30 * IN_TO_M;
Alex Perryb49a3fb2020-02-29 15:26:54 -080038const SPINNER_TOP_X = 374.59 * IN_TO_M - FIELD_EDGE_X;
Alex Perry5427c9a2020-02-15 17:43:45 -080039const SPINNER_BOTTOM_X = SPINNER_TOP_X - SPINNER_LENGTH;
40
Alex Perryb49a3fb2020-02-29 15:26:54 -080041const SHIELD_BOTTOM_X = -116 * IN_TO_M;
Alex Perry5427c9a2020-02-15 17:43:45 -080042const SHIELD_BOTTOM_Y = 43.75 * IN_TO_M;
43
Alex Perryb49a3fb2020-02-29 15:26:54 -080044const SHIELD_TOP_X = 116 * IN_TO_M;
Alex Perry5427c9a2020-02-15 17:43:45 -080045const SHIELD_TOP_Y = -43.75 * IN_TO_M;
46
Alex Perryb49a3fb2020-02-29 15:26:54 -080047const SHIELD_RIGHT_X = -51.06 * IN_TO_M;
Alex Perry5427c9a2020-02-15 17:43:45 -080048const SHIELD_RIGHT_Y = -112.88 * IN_TO_M;
49
Alex Perryb49a3fb2020-02-29 15:26:54 -080050const SHIELD_LEFT_X = 51.06 * IN_TO_M;
Alex Perry5427c9a2020-02-15 17:43:45 -080051const SHIELD_LEFT_Y = 112.88 * IN_TO_M;
52
53const SHIELD_CENTER_TOP_X = (SHIELD_TOP_X + SHIELD_LEFT_X) / 2
54const SHIELD_CENTER_TOP_Y = (SHIELD_TOP_Y + SHIELD_LEFT_Y) / 2
55
56const SHIELD_CENTER_BOTTOM_X = (SHIELD_BOTTOM_X + SHIELD_RIGHT_X) / 2
57const SHIELD_CENTER_BOTTOM_Y = (SHIELD_BOTTOM_Y + SHIELD_RIGHT_Y) / 2
58
Alex Perryb49a3fb2020-02-29 15:26:54 -080059const INITIATION_X = FIELD_EDGE_X - 120 * IN_TO_M;
Alex Perry5427c9a2020-02-15 17:43:45 -080060
Alex Perryb49a3fb2020-02-29 15:26:54 -080061const TARGET_ZONE_TIP_X = FIELD_EDGE_X - 30 * IN_TO_M;
Alex Perry5427c9a2020-02-15 17:43:45 -080062const TARGET_ZONE_WIDTH = 48 * IN_TO_M;
63const LOADING_ZONE_WIDTH = 60 * IN_TO_M;
64
Austin Schuhb863f932021-10-23 23:29:09 -070065const ROBOT_WIDTH = 34 * IN_TO_M;
66const ROBOT_LENGTH = 36 * IN_TO_M;
Alex Perry2124ae82020-03-07 14:19:06 -080067
James Kuszmaul286b80c2021-10-23 21:56:33 -070068const PI_COLORS = ["#ff00ff", "#ffff00", "#000000", "#00ffff", "#ffa500"];
James Kuszmaul5e6aa252021-08-28 22:19:29 -070069
Alex Perry5427c9a2020-02-15 17:43:45 -080070export class FieldHandler {
71 private canvas = document.createElement('canvas');
James Kuszmaul5e6aa252021-08-28 22:19:29 -070072 private imageMatchResult = new Map<string, ImageMatchResult>();
Philipp Schradera227d042020-11-14 17:33:52 -080073 private drivetrainStatus: DrivetrainStatus|null = null;
James Kuszmaul5e6aa252021-08-28 22:19:29 -070074 private superstructureStatus: SuperstructureStatus|null = null;
James Kuszmaulf75ecd62021-10-23 14:33:46 -070075 // Image information indexed by timestamp (seconds since the epoch), so that
76 // we can stop displaying images after a certain amount of time.
77 private localizerImageMatches = new Map<number, LocalizerDebug>();
Austin Schuh840132b2021-10-17 17:40:14 -070078 private x: HTMLDivElement = (document.getElementById('x') as HTMLDivElement);
79 private y: HTMLDivElement = (document.getElementById('y') as HTMLDivElement);
80 private theta: HTMLDivElement = (document.getElementById('theta') as HTMLDivElement);
81 private shotDistance: HTMLDivElement = (document.getElementById('shot_distance') as HTMLDivElement);
82 private finisher: HTMLDivElement = (document.getElementById('finisher') as HTMLDivElement);
83 private leftAccelerator: HTMLDivElement = (document.getElementById('left_accelerator') as HTMLDivElement);
84 private rightAccelerator: HTMLDivElement = (document.getElementById('right_accelerator') as HTMLDivElement);
85 private innerPort: HTMLDivElement = (document.getElementById('inner_port') as HTMLDivElement);
86 private hood: HTMLDivElement = (document.getElementById('hood') as HTMLDivElement);
87 private turret: HTMLDivElement = (document.getElementById('turret') as HTMLDivElement);
88 private intake: HTMLDivElement = (document.getElementById('intake') as HTMLDivElement);
James Kuszmaulf75ecd62021-10-23 14:33:46 -070089 private imagesAcceptedCounter: HTMLDivElement = (document.getElementById('images_accepted') as HTMLDivElement);
90 private imagesRejectedCounter: HTMLDivElement = (document.getElementById('images_rejected') as HTMLDivElement);
91 private rejectionReasonCells: HTMLDivElement[] = [];
Alex Perry5427c9a2020-02-15 17:43:45 -080092
Alex Perryb49a3fb2020-02-29 15:26:54 -080093 constructor(private readonly connection: Connection) {
Austin Schuh840132b2021-10-17 17:40:14 -070094 (document.getElementById('field') as HTMLElement).appendChild(this.canvas);
Alex Perryb49a3fb2020-02-29 15:26:54 -080095
James Kuszmaulf75ecd62021-10-23 14:33:46 -070096 for (const value in RejectionReason) {
97 // Typescript generates an iterator that produces both numbers and
98 // strings... don't do anything on the string iterations.
99 if (isNaN(Number(value))) {
100 continue;
101 }
102 const row = document.createElement("div");
103 const nameCell = document.createElement("div");
104 nameCell.innerHTML = RejectionReason[value];
105 row.appendChild(nameCell);
106 const valueCell = document.createElement("div");
107 valueCell.innerHTML = "NA";
108 this.rejectionReasonCells.push(valueCell);
109 row.appendChild(valueCell);
110 document.getElementById('readouts').appendChild(row);
111 }
112
James Kuszmaul286b80c2021-10-23 21:56:33 -0700113 for (let ii = 0; ii < PI_COLORS.length; ++ii) {
114 const legendEntry = document.createElement("div");
115 legendEntry.style.color = PI_COLORS[ii];
116 legendEntry.innerHTML = "PI" + (ii + 1).toString()
117 document.getElementById("legend").appendChild(legendEntry);
118 }
119
Alex Perryb49a3fb2020-02-29 15:26:54 -0800120 this.connection.addConfigHandler(() => {
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700121 // Go through and register handlers for both all the individual pis as
122 // well as the local pi. Depending on the node that we are running on,
123 // different subsets of these will be available.
James Kuszmaul286b80c2021-10-23 21:56:33 -0700124 for (const prefix of ['', '/pi1', '/pi2', '/pi3', '/pi4', '/pi5']) {
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700125 this.connection.addHandler(
James Kuszmaul286b80c2021-10-23 21:56:33 -0700126 prefix + '/camera', ImageMatchResult.getFullyQualifiedName(),
127 (res) => {
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700128 this.handleImageMatchResult(prefix, res);
129 });
130 }
James Kuszmaul527038a2020-12-21 23:40:44 -0800131 this.connection.addHandler(
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700132 '/drivetrain', LocalizerDebug.getFullyQualifiedName(), (data) => {
133 this.handleLocalizerDebug(data);
134 });
135 this.connection.addHandler(
James Kuszmaul527038a2020-12-21 23:40:44 -0800136 '/drivetrain', DrivetrainStatus.getFullyQualifiedName(), (data) => {
137 this.handleDrivetrainStatus(data);
138 });
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700139 this.connection.addHandler(
140 '/superstructure', SuperstructureStatus.getFullyQualifiedName(),
141 (data) => {
142 this.handleSuperstructureStatus(data);
143 });
Alex Perryb49a3fb2020-02-29 15:26:54 -0800144 });
Alex Perryb49a3fb2020-02-29 15:26:54 -0800145 }
146
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700147 private handleImageMatchResult(prefix: string, data: Uint8Array): void {
Philipp Schradere625ba22020-11-16 20:11:37 -0800148 const fbBuffer = new ByteBuffer(data);
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700149 this.imageMatchResult.set(
150 prefix,
151 ImageMatchResult.getRootAsImageMatchResult(
152 fbBuffer as unknown as flatbuffers.ByteBuffer));
Alex Perryb49a3fb2020-02-29 15:26:54 -0800153 }
154
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700155 private handleLocalizerDebug(data: Uint8Array): void {
156 const now = Date.now() / 1000.0;
157
158 const fbBuffer = new ByteBuffer(data);
159 this.localizerImageMatches.set(
160 now,
161 LocalizerDebug.getRootAsLocalizerDebug(
162 fbBuffer as unknown as flatbuffers.ByteBuffer));
163
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 {
181 console.error("Unexpected number of rejection reasons in counter.");
182 }
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);
192 this.drivetrainStatus = DrivetrainStatus.getRootAsStatus(
193 fbBuffer as unknown as flatbuffers.ByteBuffer);
Alex Perry2124ae82020-03-07 14:19:06 -0800194 }
195
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700196 private handleSuperstructureStatus(data: Uint8Array): void {
197 const fbBuffer = new ByteBuffer(data);
198 this.superstructureStatus = SuperstructureStatus.getRootAsStatus(
199 fbBuffer as unknown as flatbuffers.ByteBuffer);
200 }
201
Alex Perry5427c9a2020-02-15 17:43:45 -0800202 drawField(): void {
203 const MY_COLOR = 'red';
204 const OTHER_COLOR = 'blue';
205 const ctx = this.canvas.getContext('2d');
206 // draw perimiter
207 ctx.beginPath();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800208 ctx.moveTo(FIELD_EDGE_X, DS_INSIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800209 ctx.lineTo(DS_END_X, FIELD_SIDE_Y);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800210 ctx.lineTo(-DS_END_X, FIELD_SIDE_Y);
211 ctx.lineTo(-FIELD_EDGE_X, DS_INSIDE_Y);
212 ctx.lineTo(-FIELD_EDGE_X, -DS_INSIDE_Y);
213 ctx.lineTo(-DS_END_X, -FIELD_SIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800214 ctx.lineTo(DS_END_X, -FIELD_SIDE_Y);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800215 ctx.lineTo(FIELD_EDGE_X, -DS_INSIDE_Y);
216 ctx.lineTo(FIELD_EDGE_X, DS_INSIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800217 ctx.stroke();
218
219 // draw shield generator
220 ctx.beginPath();
221 ctx.moveTo(SHIELD_BOTTOM_X, SHIELD_BOTTOM_Y);
222 ctx.lineTo(SHIELD_RIGHT_X, SHIELD_RIGHT_Y);
223 ctx.lineTo(SHIELD_TOP_X, SHIELD_TOP_Y);
224 ctx.lineTo(SHIELD_LEFT_X, SHIELD_LEFT_Y);
225 ctx.lineTo(SHIELD_BOTTOM_X, SHIELD_BOTTOM_Y);
226 ctx.moveTo(SHIELD_CENTER_TOP_X, SHIELD_CENTER_TOP_Y);
227 ctx.lineTo(SHIELD_CENTER_BOTTOM_X, SHIELD_CENTER_BOTTOM_Y);
228 ctx.stroke();
229
Alex Perryb49a3fb2020-02-29 15:26:54 -0800230 this.drawHalfField(ctx, 'red');
231 ctx.rotate(Math.PI);
232 this.drawHalfField(ctx, 'blue');
233 ctx.rotate(Math.PI);
234 }
Alex Perry5427c9a2020-02-15 17:43:45 -0800235
Alex Perryb49a3fb2020-02-29 15:26:54 -0800236 drawHalfField(ctx, color: string): void {
237 // trenches
238 ctx.strokeStyle = color;
Alex Perry5427c9a2020-02-15 17:43:45 -0800239 ctx.beginPath();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800240 ctx.moveTo(TRENCH_X, FIELD_SIDE_Y);
241 ctx.lineTo(TRENCH_X, TRENCH_INSIDE);
242 ctx.lineTo(-TRENCH_X, TRENCH_INSIDE);
243 ctx.lineTo(-TRENCH_X, FIELD_SIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800244 ctx.stroke();
245
246 ctx.strokeStyle = 'black';
247 ctx.beginPath();
248 ctx.moveTo(SPINNER_TOP_X, FIELD_SIDE_Y);
249 ctx.lineTo(SPINNER_TOP_X, TRENCH_INSIDE);
250 ctx.lineTo(SPINNER_BOTTOM_X, TRENCH_INSIDE);
251 ctx.lineTo(SPINNER_BOTTOM_X, FIELD_SIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800252 ctx.stroke();
253
Alex Perry5427c9a2020-02-15 17:43:45 -0800254 ctx.beginPath();
255 ctx.moveTo(INITIATION_X, FIELD_SIDE_Y);
256 ctx.lineTo(INITIATION_X, -FIELD_SIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800257 ctx.stroke();
258
Alex Perryb49a3fb2020-02-29 15:26:54 -0800259 // target/loading
260 ctx.strokeStyle = color;
Alex Perry5427c9a2020-02-15 17:43:45 -0800261 ctx.beginPath();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800262 ctx.moveTo(FIELD_EDGE_X, DS_INSIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800263 ctx.lineTo(TARGET_ZONE_TIP_X, DS_INSIDE_Y - 0.5 * TARGET_ZONE_WIDTH);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800264 ctx.lineTo(FIELD_EDGE_X, DS_INSIDE_Y - TARGET_ZONE_WIDTH);
Alex Perry5427c9a2020-02-15 17:43:45 -0800265
Alex Perryb49a3fb2020-02-29 15:26:54 -0800266 ctx.moveTo(-FIELD_EDGE_X, DS_INSIDE_Y);
267 ctx.lineTo(-TARGET_ZONE_TIP_X, DS_INSIDE_Y - 0.5 * LOADING_ZONE_WIDTH);
268 ctx.lineTo(-FIELD_EDGE_X, DS_INSIDE_Y - LOADING_ZONE_WIDTH);
Alex Perry5427c9a2020-02-15 17:43:45 -0800269 ctx.stroke();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800270 }
Alex Perry5427c9a2020-02-15 17:43:45 -0800271
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700272 drawCamera(
273 x: number, y: number, theta: number, color: string = 'blue',
274 extendLines: boolean = true): void {
Alex Perryb49a3fb2020-02-29 15:26:54 -0800275 const ctx = this.canvas.getContext('2d');
276 ctx.save();
277 ctx.translate(x, y);
278 ctx.rotate(theta);
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700279 ctx.strokeStyle = color;
Alex Perry5427c9a2020-02-15 17:43:45 -0800280 ctx.beginPath();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800281 ctx.moveTo(0.5, 0.5);
282 ctx.lineTo(0, 0);
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700283 if (extendLines) {
284 ctx.lineTo(100.0, 0);
285 ctx.lineTo(0, 0);
286 }
Alex Perryb49a3fb2020-02-29 15:26:54 -0800287 ctx.lineTo(0.5, -0.5);
Alex Perry5427c9a2020-02-15 17:43:45 -0800288 ctx.stroke();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800289 ctx.beginPath();
Philipp Schradere625ba22020-11-16 20:11:37 -0800290 ctx.arc(0, 0, 0.25, -Math.PI / 4, Math.PI / 4);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800291 ctx.stroke();
292 ctx.restore();
293 }
294
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700295 drawRobot(
296 x: number, y: number, theta: number, turret: number|null,
297 color: string = 'blue', dashed: boolean = false,
298 extendLines: boolean = true): void {
Alex Perry2124ae82020-03-07 14:19:06 -0800299 const ctx = this.canvas.getContext('2d');
300 ctx.save();
301 ctx.translate(x, y);
302 ctx.rotate(theta);
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700303 ctx.strokeStyle = color;
304 if (dashed) {
305 ctx.setLineDash([0.05, 0.05]);
306 } else {
307 // Empty array = solid line.
308 ctx.setLineDash([]);
309 }
Alex Perry2124ae82020-03-07 14:19:06 -0800310 ctx.rect(-ROBOT_LENGTH / 2, -ROBOT_WIDTH / 2, ROBOT_LENGTH, ROBOT_WIDTH);
311 ctx.stroke();
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700312
313 // Draw line indicating which direction is forwards on the robot.
314 ctx.beginPath();
315 ctx.moveTo(0, 0);
316 if (extendLines) {
317 ctx.lineTo(1000.0, 0);
318 } else {
319 ctx.lineTo(ROBOT_LENGTH / 2.0, 0);
320 }
321 ctx.stroke();
322
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700323 if (turret) {
324 ctx.save();
325 ctx.rotate(turret + Math.PI);
326 const turretRadius = ROBOT_WIDTH / 4.0;
327 ctx.strokeStyle = "red";
328 // Draw circle for turret.
329 ctx.beginPath();
330 ctx.arc(0, 0, turretRadius, 0, 2.0 * Math.PI);
331 ctx.stroke();
332 // Draw line in circle to show forwards.
333 ctx.beginPath();
334 ctx.moveTo(0, 0);
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700335 if (extendLines) {
336 ctx.lineTo(1000.0, 0);
337 } else {
338 ctx.lineTo(turretRadius, 0);
339 }
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700340 ctx.stroke();
341 ctx.restore();
342 }
Alex Perry2124ae82020-03-07 14:19:06 -0800343 ctx.restore();
344 }
345
Austin Schuh840132b2021-10-17 17:40:14 -0700346 setZeroing(div: HTMLDivElement): void {
347 div.innerHTML = "zeroing";
348 div.classList.remove("faulted");
349 div.classList.add("zeroing");
350 }
351 setEstopped(div: HTMLDivElement): void {
352 div.innerHTML = "estopped";
353 div.classList.add("faulted");
354 div.classList.remove("zeroing");
355 }
356 setValue(div: HTMLDivElement, val: Number): void {
357 div.innerHTML = val.toFixed(4);
358 div.classList.remove("faulted");
359 div.classList.remove("zeroing");
360 }
361
Philipp Schradere625ba22020-11-16 20:11:37 -0800362 draw(): void {
Alex Perryb49a3fb2020-02-29 15:26:54 -0800363 this.reset();
364 this.drawField();
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700365
366 // Draw the matches with debugging information from the localizer.
367 const now = Date.now() / 1000.0;
368 for (const [time, value] of this.localizerImageMatches) {
369 const age = now - time;
370 const kRemovalAge = 2.0;
371 if (age > kRemovalAge) {
372 this.localizerImageMatches.delete(time);
373 continue;
374 }
375 const ageAlpha = (kRemovalAge - age) / kRemovalAge
376 for (let i = 0; i < value.matchesLength(); i++) {
377 const imageDebug = value.matches(i);
378 const x = imageDebug.impliedRobotX();
379 const y = imageDebug.impliedRobotY();
380 const theta = imageDebug.impliedRobotTheta();
381 const cameraX = imageDebug.cameraX();
382 const cameraY = imageDebug.cameraY();
383 const cameraTheta = imageDebug.cameraTheta();
384 const accepted = imageDebug.accepted();
385 // Make camera readings fade over time.
386 const alpha = Math.round(255 * ageAlpha).toString(16).padStart(2, '0');
387 const dashed = false;
James Kuszmaul286b80c2021-10-23 21:56:33 -0700388 const acceptedRgb = accepted ? "#00FF00" : "#FF0000";
389 const acceptedRgba = acceptedRgb + alpha;
390 const cameraRgb = PI_COLORS[imageDebug.camera()];
391 const cameraRgba = cameraRgb + alpha;
392 this.drawRobot(x, y, theta, null, acceptedRgba, dashed, false);
393 this.drawCamera(cameraX, cameraY, cameraTheta, cameraRgba, false);
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700394 }
395 }
396
397 // draw cameras from ImageMatchResults directly (helpful when viewing page
398 // on the pis individually).
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700399 for (const keyPair of this.imageMatchResult) {
400 const value = keyPair[1];
401 for (let i = 0; i < value.cameraPosesLength(); i++) {
402 const pose = value.cameraPoses(i);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800403 const mat = pose.fieldToCamera();
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700404 // Matrix layout:
405 // [0, 1, 2, 3]
406 // [4, 5, 6, 7]
407 // [8, 9, 10, 11]
408 // [12, 13, 14, 15]
Alex Perryb49a3fb2020-02-29 15:26:54 -0800409 const x = mat.data(3);
410 const y = mat.data(7);
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700411 const theta = Math.atan2(mat.data(6), mat.data(2));
James Kuszmaul286b80c2021-10-23 21:56:33 -0700412 const cameraColor = (keyPair[0].length > 0) ?
413 PI_COLORS[Number(keyPair[0][3]) - 1] :
414 'blue';
415 this.drawCamera(x, y, theta, cameraColor);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800416 }
417 }
418
Alex Perry2124ae82020-03-07 14:19:06 -0800419 if (this.drivetrainStatus) {
Austin Schuh840132b2021-10-17 17:40:14 -0700420 if (!this.drivetrainStatus.zeroing().zeroed()) {
421 this.setZeroing(this.x);
422 this.setZeroing(this.y);
423 this.setZeroing(this.theta);
424 } else if (this.drivetrainStatus.zeroing().faulted()) {
425 this.setEstopped(this.x);
426 this.setEstopped(this.y);
427 this.setEstopped(this.theta);
428 } else {
429 this.setValue(this.x, this.drivetrainStatus.x());
430 this.setValue(this.y, this.drivetrainStatus.y());
431 this.setValue(this.theta, this.drivetrainStatus.theta());
432 }
433
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700434 if (this.superstructureStatus) {
435 this.shotDistance.innerHTML =
436 this.superstructureStatus.aimer().shotDistance().toFixed(2);
437 this.finisher.innerHTML = this.superstructureStatus.shooter()
438 .finisher()
439 .angularVelocity()
440 .toFixed(2);
441 this.leftAccelerator.innerHTML = this.superstructureStatus.shooter()
442 .acceleratorLeft()
443 .angularVelocity()
444 .toFixed(2);
445 this.rightAccelerator.innerHTML = this.superstructureStatus.shooter()
446 .acceleratorRight()
447 .angularVelocity()
448 .toFixed(2);
449 if (this.superstructureStatus.aimer().aimingForInnerPort()) {
450 this.innerPort.innerHTML = 'true';
451 } else {
452 this.innerPort.innerHTML = 'false';
453 }
454 if (!this.superstructureStatus.hood().zeroed()) {
455 this.setZeroing(this.hood);
456 } else if (this.superstructureStatus.hood().estopped()) {
457 this.setEstopped(this.hood);
458 } else {
459 this.setValue(
460 this.hood,
461 this.superstructureStatus.hood().estimatorState().position());
462 }
463 if (!this.superstructureStatus.turret().zeroed()) {
464 this.setZeroing(this.turret);
465 } else if (this.superstructureStatus.turret().estopped()) {
466 this.setEstopped(this.turret);
467 } else {
468 this.setValue(
469 this.turret,
470 this.superstructureStatus.turret().estimatorState().position());
471 }
472 if (!this.superstructureStatus.intake().zeroed()) {
473 this.setZeroing(this.intake);
474 } else if (this.superstructureStatus.intake().estopped()) {
475 this.setEstopped(this.intake);
476 } else {
477 this.setValue(
478 this.intake,
479 this.superstructureStatus.intake().estimatorState().position());
480 }
Austin Schuh840132b2021-10-17 17:40:14 -0700481 }
Alex Perry2124ae82020-03-07 14:19:06 -0800482 this.drawRobot(
483 this.drivetrainStatus.x(), this.drivetrainStatus.y(),
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700484 this.drivetrainStatus.theta(),
485 this.superstructureStatus ?
486 this.superstructureStatus.turret().position() :
487 null);
Alex Perry2124ae82020-03-07 14:19:06 -0800488 }
489
Alex Perryb49a3fb2020-02-29 15:26:54 -0800490 window.requestAnimationFrame(() => this.draw());
Alex Perry5427c9a2020-02-15 17:43:45 -0800491 }
492
493 reset(): void {
494 const ctx = this.canvas.getContext('2d');
495 ctx.setTransform(1, 0, 0, 1, 0, 0);
496 const size = window.innerHeight * 0.9;
497 ctx.canvas.height = size;
Alex Perryb49a3fb2020-02-29 15:26:54 -0800498 const width = size / 2 + 20;
499 ctx.canvas.width = width;
500 ctx.clearRect(0, 0, size, width);
Alex Perry5427c9a2020-02-15 17:43:45 -0800501
Alex Perryb49a3fb2020-02-29 15:26:54 -0800502 // Translate to center of display.
503 ctx.translate(width / 2, size / 2);
Alex Perry5427c9a2020-02-15 17:43:45 -0800504 // Coordinate system is:
505 // x -> forward.
506 // y -> to the left.
507 ctx.rotate(-Math.PI / 2);
508 ctx.scale(1, -1);
Alex Perry5427c9a2020-02-15 17:43:45 -0800509
510 const M_TO_PX = (size - 10) / FIELD_LENGTH;
511 ctx.scale(M_TO_PX, M_TO_PX);
512 ctx.lineWidth = 1 / M_TO_PX;
513 }
514}