blob: dec4b34fa8feb22a568b1c7fa36cd8c81872a8c8 [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
Alex Perry2124ae82020-03-07 14:19:06 -080065const ROBOT_WIDTH = 28 * IN_TO_M;
66const ROBOT_LENGTH = 30 * IN_TO_M;
67
James Kuszmaul5e6aa252021-08-28 22:19:29 -070068
Alex Perry5427c9a2020-02-15 17:43:45 -080069export class FieldHandler {
70 private canvas = document.createElement('canvas');
James Kuszmaul5e6aa252021-08-28 22:19:29 -070071 private imageMatchResult = new Map<string, ImageMatchResult>();
Philipp Schradera227d042020-11-14 17:33:52 -080072 private drivetrainStatus: DrivetrainStatus|null = null;
James Kuszmaul5e6aa252021-08-28 22:19:29 -070073 private superstructureStatus: SuperstructureStatus|null = null;
James Kuszmaulf75ecd62021-10-23 14:33:46 -070074 // Image information indexed by timestamp (seconds since the epoch), so that
75 // we can stop displaying images after a certain amount of time.
76 private localizerImageMatches = new Map<number, LocalizerDebug>();
Austin Schuh840132b2021-10-17 17:40:14 -070077 private x: HTMLDivElement = (document.getElementById('x') as HTMLDivElement);
78 private y: HTMLDivElement = (document.getElementById('y') as HTMLDivElement);
79 private theta: HTMLDivElement = (document.getElementById('theta') as HTMLDivElement);
80 private shotDistance: HTMLDivElement = (document.getElementById('shot_distance') as HTMLDivElement);
81 private finisher: HTMLDivElement = (document.getElementById('finisher') as HTMLDivElement);
82 private leftAccelerator: HTMLDivElement = (document.getElementById('left_accelerator') as HTMLDivElement);
83 private rightAccelerator: HTMLDivElement = (document.getElementById('right_accelerator') as HTMLDivElement);
84 private innerPort: HTMLDivElement = (document.getElementById('inner_port') as HTMLDivElement);
85 private hood: HTMLDivElement = (document.getElementById('hood') as HTMLDivElement);
86 private turret: HTMLDivElement = (document.getElementById('turret') as HTMLDivElement);
87 private intake: HTMLDivElement = (document.getElementById('intake') as HTMLDivElement);
James Kuszmaulf75ecd62021-10-23 14:33:46 -070088 private imagesAcceptedCounter: HTMLDivElement = (document.getElementById('images_accepted') as HTMLDivElement);
89 private imagesRejectedCounter: HTMLDivElement = (document.getElementById('images_rejected') as HTMLDivElement);
90 private rejectionReasonCells: HTMLDivElement[] = [];
Alex Perry5427c9a2020-02-15 17:43:45 -080091
Alex Perryb49a3fb2020-02-29 15:26:54 -080092 constructor(private readonly connection: Connection) {
Austin Schuh840132b2021-10-17 17:40:14 -070093 (document.getElementById('field') as HTMLElement).appendChild(this.canvas);
Alex Perryb49a3fb2020-02-29 15:26:54 -080094
James Kuszmaulf75ecd62021-10-23 14:33:46 -070095 for (const value in RejectionReason) {
96 // Typescript generates an iterator that produces both numbers and
97 // strings... don't do anything on the string iterations.
98 if (isNaN(Number(value))) {
99 continue;
100 }
101 const row = document.createElement("div");
102 const nameCell = document.createElement("div");
103 nameCell.innerHTML = RejectionReason[value];
104 row.appendChild(nameCell);
105 const valueCell = document.createElement("div");
106 valueCell.innerHTML = "NA";
107 this.rejectionReasonCells.push(valueCell);
108 row.appendChild(valueCell);
109 document.getElementById('readouts').appendChild(row);
110 }
111
Alex Perryb49a3fb2020-02-29 15:26:54 -0800112 this.connection.addConfigHandler(() => {
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700113 // Go through and register handlers for both all the individual pis as
114 // well as the local pi. Depending on the node that we are running on,
115 // different subsets of these will be available.
116 for (const prefix of ['', '/pi1', '/pi2', '/pi3', '/pi4']) {
117 this.connection.addHandler(
118 prefix + '/camera', ImageMatchResult.getFullyQualifiedName(), (res) => {
119 this.handleImageMatchResult(prefix, res);
120 });
121 }
James Kuszmaul527038a2020-12-21 23:40:44 -0800122 this.connection.addHandler(
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700123 '/drivetrain', LocalizerDebug.getFullyQualifiedName(), (data) => {
124 this.handleLocalizerDebug(data);
125 });
126 this.connection.addHandler(
James Kuszmaul527038a2020-12-21 23:40:44 -0800127 '/drivetrain', DrivetrainStatus.getFullyQualifiedName(), (data) => {
128 this.handleDrivetrainStatus(data);
129 });
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700130 this.connection.addHandler(
131 '/superstructure', SuperstructureStatus.getFullyQualifiedName(),
132 (data) => {
133 this.handleSuperstructureStatus(data);
134 });
Alex Perryb49a3fb2020-02-29 15:26:54 -0800135 });
Alex Perryb49a3fb2020-02-29 15:26:54 -0800136 }
137
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700138 private handleImageMatchResult(prefix: string, data: Uint8Array): void {
Philipp Schradere625ba22020-11-16 20:11:37 -0800139 const fbBuffer = new ByteBuffer(data);
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700140 this.imageMatchResult.set(
141 prefix,
142 ImageMatchResult.getRootAsImageMatchResult(
143 fbBuffer as unknown as flatbuffers.ByteBuffer));
Alex Perryb49a3fb2020-02-29 15:26:54 -0800144 }
145
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700146 private handleLocalizerDebug(data: Uint8Array): void {
147 const now = Date.now() / 1000.0;
148
149 const fbBuffer = new ByteBuffer(data);
150 this.localizerImageMatches.set(
151 now,
152 LocalizerDebug.getRootAsLocalizerDebug(
153 fbBuffer as unknown as flatbuffers.ByteBuffer));
154
155 const debug = this.localizerImageMatches.get(now);
156
157 if (debug.statistics()) {
158 this.imagesAcceptedCounter.innerHTML =
159 debug.statistics().totalAccepted().toString();
160 this.imagesRejectedCounter.innerHTML =
161 (debug.statistics().totalCandidates() -
162 debug.statistics().totalAccepted())
163 .toString();
164 if (debug.statistics().rejectionReasonCountLength() ==
165 this.rejectionReasonCells.length) {
166 for (let ii = 0; ii < debug.statistics().rejectionReasonCountLength();
167 ++ii) {
168 this.rejectionReasonCells[ii].innerHTML =
169 debug.statistics().rejectionReasonCount(ii).toString();
170 }
171 } else {
172 console.error("Unexpected number of rejection reasons in counter.");
173 }
174 this.imagesRejectedCounter.innerHTML =
175 (debug.statistics().totalCandidates() -
176 debug.statistics().totalAccepted())
177 .toString();
178 }
179 }
180
Alex Perry2124ae82020-03-07 14:19:06 -0800181 private handleDrivetrainStatus(data: Uint8Array): void {
Philipp Schradere625ba22020-11-16 20:11:37 -0800182 const fbBuffer = new ByteBuffer(data);
183 this.drivetrainStatus = DrivetrainStatus.getRootAsStatus(
184 fbBuffer as unknown as flatbuffers.ByteBuffer);
Alex Perry2124ae82020-03-07 14:19:06 -0800185 }
186
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700187 private handleSuperstructureStatus(data: Uint8Array): void {
188 const fbBuffer = new ByteBuffer(data);
189 this.superstructureStatus = SuperstructureStatus.getRootAsStatus(
190 fbBuffer as unknown as flatbuffers.ByteBuffer);
191 }
192
Alex Perry5427c9a2020-02-15 17:43:45 -0800193 drawField(): void {
194 const MY_COLOR = 'red';
195 const OTHER_COLOR = 'blue';
196 const ctx = this.canvas.getContext('2d');
197 // draw perimiter
198 ctx.beginPath();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800199 ctx.moveTo(FIELD_EDGE_X, DS_INSIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800200 ctx.lineTo(DS_END_X, FIELD_SIDE_Y);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800201 ctx.lineTo(-DS_END_X, FIELD_SIDE_Y);
202 ctx.lineTo(-FIELD_EDGE_X, DS_INSIDE_Y);
203 ctx.lineTo(-FIELD_EDGE_X, -DS_INSIDE_Y);
204 ctx.lineTo(-DS_END_X, -FIELD_SIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800205 ctx.lineTo(DS_END_X, -FIELD_SIDE_Y);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800206 ctx.lineTo(FIELD_EDGE_X, -DS_INSIDE_Y);
207 ctx.lineTo(FIELD_EDGE_X, DS_INSIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800208 ctx.stroke();
209
210 // draw shield generator
211 ctx.beginPath();
212 ctx.moveTo(SHIELD_BOTTOM_X, SHIELD_BOTTOM_Y);
213 ctx.lineTo(SHIELD_RIGHT_X, SHIELD_RIGHT_Y);
214 ctx.lineTo(SHIELD_TOP_X, SHIELD_TOP_Y);
215 ctx.lineTo(SHIELD_LEFT_X, SHIELD_LEFT_Y);
216 ctx.lineTo(SHIELD_BOTTOM_X, SHIELD_BOTTOM_Y);
217 ctx.moveTo(SHIELD_CENTER_TOP_X, SHIELD_CENTER_TOP_Y);
218 ctx.lineTo(SHIELD_CENTER_BOTTOM_X, SHIELD_CENTER_BOTTOM_Y);
219 ctx.stroke();
220
Alex Perryb49a3fb2020-02-29 15:26:54 -0800221 this.drawHalfField(ctx, 'red');
222 ctx.rotate(Math.PI);
223 this.drawHalfField(ctx, 'blue');
224 ctx.rotate(Math.PI);
225 }
Alex Perry5427c9a2020-02-15 17:43:45 -0800226
Alex Perryb49a3fb2020-02-29 15:26:54 -0800227 drawHalfField(ctx, color: string): void {
228 // trenches
229 ctx.strokeStyle = color;
Alex Perry5427c9a2020-02-15 17:43:45 -0800230 ctx.beginPath();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800231 ctx.moveTo(TRENCH_X, FIELD_SIDE_Y);
232 ctx.lineTo(TRENCH_X, TRENCH_INSIDE);
233 ctx.lineTo(-TRENCH_X, TRENCH_INSIDE);
234 ctx.lineTo(-TRENCH_X, FIELD_SIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800235 ctx.stroke();
236
237 ctx.strokeStyle = 'black';
238 ctx.beginPath();
239 ctx.moveTo(SPINNER_TOP_X, FIELD_SIDE_Y);
240 ctx.lineTo(SPINNER_TOP_X, TRENCH_INSIDE);
241 ctx.lineTo(SPINNER_BOTTOM_X, TRENCH_INSIDE);
242 ctx.lineTo(SPINNER_BOTTOM_X, FIELD_SIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800243 ctx.stroke();
244
Alex Perry5427c9a2020-02-15 17:43:45 -0800245 ctx.beginPath();
246 ctx.moveTo(INITIATION_X, FIELD_SIDE_Y);
247 ctx.lineTo(INITIATION_X, -FIELD_SIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800248 ctx.stroke();
249
Alex Perryb49a3fb2020-02-29 15:26:54 -0800250 // target/loading
251 ctx.strokeStyle = color;
Alex Perry5427c9a2020-02-15 17:43:45 -0800252 ctx.beginPath();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800253 ctx.moveTo(FIELD_EDGE_X, DS_INSIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800254 ctx.lineTo(TARGET_ZONE_TIP_X, DS_INSIDE_Y - 0.5 * TARGET_ZONE_WIDTH);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800255 ctx.lineTo(FIELD_EDGE_X, DS_INSIDE_Y - TARGET_ZONE_WIDTH);
Alex Perry5427c9a2020-02-15 17:43:45 -0800256
Alex Perryb49a3fb2020-02-29 15:26:54 -0800257 ctx.moveTo(-FIELD_EDGE_X, DS_INSIDE_Y);
258 ctx.lineTo(-TARGET_ZONE_TIP_X, DS_INSIDE_Y - 0.5 * LOADING_ZONE_WIDTH);
259 ctx.lineTo(-FIELD_EDGE_X, DS_INSIDE_Y - LOADING_ZONE_WIDTH);
Alex Perry5427c9a2020-02-15 17:43:45 -0800260 ctx.stroke();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800261 }
Alex Perry5427c9a2020-02-15 17:43:45 -0800262
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700263 drawCamera(
264 x: number, y: number, theta: number, color: string = 'blue',
265 extendLines: boolean = true): void {
Alex Perryb49a3fb2020-02-29 15:26:54 -0800266 const ctx = this.canvas.getContext('2d');
267 ctx.save();
268 ctx.translate(x, y);
269 ctx.rotate(theta);
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700270 ctx.strokeStyle = color;
Alex Perry5427c9a2020-02-15 17:43:45 -0800271 ctx.beginPath();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800272 ctx.moveTo(0.5, 0.5);
273 ctx.lineTo(0, 0);
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700274 if (extendLines) {
275 ctx.lineTo(100.0, 0);
276 ctx.lineTo(0, 0);
277 }
Alex Perryb49a3fb2020-02-29 15:26:54 -0800278 ctx.lineTo(0.5, -0.5);
Alex Perry5427c9a2020-02-15 17:43:45 -0800279 ctx.stroke();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800280 ctx.beginPath();
Philipp Schradere625ba22020-11-16 20:11:37 -0800281 ctx.arc(0, 0, 0.25, -Math.PI / 4, Math.PI / 4);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800282 ctx.stroke();
283 ctx.restore();
284 }
285
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700286 drawRobot(
287 x: number, y: number, theta: number, turret: number|null,
288 color: string = 'blue', dashed: boolean = false,
289 extendLines: boolean = true): void {
Alex Perry2124ae82020-03-07 14:19:06 -0800290 const ctx = this.canvas.getContext('2d');
291 ctx.save();
292 ctx.translate(x, y);
293 ctx.rotate(theta);
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700294 ctx.strokeStyle = color;
295 if (dashed) {
296 ctx.setLineDash([0.05, 0.05]);
297 } else {
298 // Empty array = solid line.
299 ctx.setLineDash([]);
300 }
Alex Perry2124ae82020-03-07 14:19:06 -0800301 ctx.rect(-ROBOT_LENGTH / 2, -ROBOT_WIDTH / 2, ROBOT_LENGTH, ROBOT_WIDTH);
302 ctx.stroke();
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700303
304 // Draw line indicating which direction is forwards on the robot.
305 ctx.beginPath();
306 ctx.moveTo(0, 0);
307 if (extendLines) {
308 ctx.lineTo(1000.0, 0);
309 } else {
310 ctx.lineTo(ROBOT_LENGTH / 2.0, 0);
311 }
312 ctx.stroke();
313
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700314 if (turret) {
315 ctx.save();
316 ctx.rotate(turret + Math.PI);
317 const turretRadius = ROBOT_WIDTH / 4.0;
318 ctx.strokeStyle = "red";
319 // Draw circle for turret.
320 ctx.beginPath();
321 ctx.arc(0, 0, turretRadius, 0, 2.0 * Math.PI);
322 ctx.stroke();
323 // Draw line in circle to show forwards.
324 ctx.beginPath();
325 ctx.moveTo(0, 0);
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700326 if (extendLines) {
327 ctx.lineTo(1000.0, 0);
328 } else {
329 ctx.lineTo(turretRadius, 0);
330 }
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700331 ctx.stroke();
332 ctx.restore();
333 }
Alex Perry2124ae82020-03-07 14:19:06 -0800334 ctx.restore();
335 }
336
Austin Schuh840132b2021-10-17 17:40:14 -0700337 setZeroing(div: HTMLDivElement): void {
338 div.innerHTML = "zeroing";
339 div.classList.remove("faulted");
340 div.classList.add("zeroing");
341 }
342 setEstopped(div: HTMLDivElement): void {
343 div.innerHTML = "estopped";
344 div.classList.add("faulted");
345 div.classList.remove("zeroing");
346 }
347 setValue(div: HTMLDivElement, val: Number): void {
348 div.innerHTML = val.toFixed(4);
349 div.classList.remove("faulted");
350 div.classList.remove("zeroing");
351 }
352
Philipp Schradere625ba22020-11-16 20:11:37 -0800353 draw(): void {
Alex Perryb49a3fb2020-02-29 15:26:54 -0800354 this.reset();
355 this.drawField();
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700356
357 // Draw the matches with debugging information from the localizer.
358 const now = Date.now() / 1000.0;
359 for (const [time, value] of this.localizerImageMatches) {
360 const age = now - time;
361 const kRemovalAge = 2.0;
362 if (age > kRemovalAge) {
363 this.localizerImageMatches.delete(time);
364 continue;
365 }
366 const ageAlpha = (kRemovalAge - age) / kRemovalAge
367 for (let i = 0; i < value.matchesLength(); i++) {
368 const imageDebug = value.matches(i);
369 const x = imageDebug.impliedRobotX();
370 const y = imageDebug.impliedRobotY();
371 const theta = imageDebug.impliedRobotTheta();
372 const cameraX = imageDebug.cameraX();
373 const cameraY = imageDebug.cameraY();
374 const cameraTheta = imageDebug.cameraTheta();
375 const accepted = imageDebug.accepted();
376 // Make camera readings fade over time.
377 const alpha = Math.round(255 * ageAlpha).toString(16).padStart(2, '0');
378 const dashed = false;
379 const rgb = accepted ? "#00FF00" : "#FF0000";
380 const rgba = rgb + alpha;
381 this.drawRobot(x, y, theta, null, rgba, dashed, false);
382 this.drawCamera(cameraX, cameraY, cameraTheta, rgba, false);
383 }
384 }
385
386 // draw cameras from ImageMatchResults directly (helpful when viewing page
387 // on the pis individually).
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700388 for (const keyPair of this.imageMatchResult) {
389 const value = keyPair[1];
390 for (let i = 0; i < value.cameraPosesLength(); i++) {
391 const pose = value.cameraPoses(i);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800392 const mat = pose.fieldToCamera();
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700393 // Matrix layout:
394 // [0, 1, 2, 3]
395 // [4, 5, 6, 7]
396 // [8, 9, 10, 11]
397 // [12, 13, 14, 15]
Alex Perryb49a3fb2020-02-29 15:26:54 -0800398 const x = mat.data(3);
399 const y = mat.data(7);
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700400 const theta = Math.atan2(mat.data(6), mat.data(2));
Alex Perry2124ae82020-03-07 14:19:06 -0800401 this.drawCamera(x, y, theta);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800402 }
403 }
404
Alex Perry2124ae82020-03-07 14:19:06 -0800405 if (this.drivetrainStatus) {
Austin Schuh840132b2021-10-17 17:40:14 -0700406 if (!this.drivetrainStatus.zeroing().zeroed()) {
407 this.setZeroing(this.x);
408 this.setZeroing(this.y);
409 this.setZeroing(this.theta);
410 } else if (this.drivetrainStatus.zeroing().faulted()) {
411 this.setEstopped(this.x);
412 this.setEstopped(this.y);
413 this.setEstopped(this.theta);
414 } else {
415 this.setValue(this.x, this.drivetrainStatus.x());
416 this.setValue(this.y, this.drivetrainStatus.y());
417 this.setValue(this.theta, this.drivetrainStatus.theta());
418 }
419
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700420 if (this.superstructureStatus) {
421 this.shotDistance.innerHTML =
422 this.superstructureStatus.aimer().shotDistance().toFixed(2);
423 this.finisher.innerHTML = this.superstructureStatus.shooter()
424 .finisher()
425 .angularVelocity()
426 .toFixed(2);
427 this.leftAccelerator.innerHTML = this.superstructureStatus.shooter()
428 .acceleratorLeft()
429 .angularVelocity()
430 .toFixed(2);
431 this.rightAccelerator.innerHTML = this.superstructureStatus.shooter()
432 .acceleratorRight()
433 .angularVelocity()
434 .toFixed(2);
435 if (this.superstructureStatus.aimer().aimingForInnerPort()) {
436 this.innerPort.innerHTML = 'true';
437 } else {
438 this.innerPort.innerHTML = 'false';
439 }
440 if (!this.superstructureStatus.hood().zeroed()) {
441 this.setZeroing(this.hood);
442 } else if (this.superstructureStatus.hood().estopped()) {
443 this.setEstopped(this.hood);
444 } else {
445 this.setValue(
446 this.hood,
447 this.superstructureStatus.hood().estimatorState().position());
448 }
449 if (!this.superstructureStatus.turret().zeroed()) {
450 this.setZeroing(this.turret);
451 } else if (this.superstructureStatus.turret().estopped()) {
452 this.setEstopped(this.turret);
453 } else {
454 this.setValue(
455 this.turret,
456 this.superstructureStatus.turret().estimatorState().position());
457 }
458 if (!this.superstructureStatus.intake().zeroed()) {
459 this.setZeroing(this.intake);
460 } else if (this.superstructureStatus.intake().estopped()) {
461 this.setEstopped(this.intake);
462 } else {
463 this.setValue(
464 this.intake,
465 this.superstructureStatus.intake().estimatorState().position());
466 }
Austin Schuh840132b2021-10-17 17:40:14 -0700467 }
Alex Perry2124ae82020-03-07 14:19:06 -0800468 this.drawRobot(
469 this.drivetrainStatus.x(), this.drivetrainStatus.y(),
James Kuszmaul5e6aa252021-08-28 22:19:29 -0700470 this.drivetrainStatus.theta(),
471 this.superstructureStatus ?
472 this.superstructureStatus.turret().position() :
473 null);
Alex Perry2124ae82020-03-07 14:19:06 -0800474 }
475
Alex Perryb49a3fb2020-02-29 15:26:54 -0800476 window.requestAnimationFrame(() => this.draw());
Alex Perry5427c9a2020-02-15 17:43:45 -0800477 }
478
479 reset(): void {
480 const ctx = this.canvas.getContext('2d');
481 ctx.setTransform(1, 0, 0, 1, 0, 0);
482 const size = window.innerHeight * 0.9;
483 ctx.canvas.height = size;
Alex Perryb49a3fb2020-02-29 15:26:54 -0800484 const width = size / 2 + 20;
485 ctx.canvas.width = width;
486 ctx.clearRect(0, 0, size, width);
Alex Perry5427c9a2020-02-15 17:43:45 -0800487
Alex Perryb49a3fb2020-02-29 15:26:54 -0800488 // Translate to center of display.
489 ctx.translate(width / 2, size / 2);
Alex Perry5427c9a2020-02-15 17:43:45 -0800490 // Coordinate system is:
491 // x -> forward.
492 // y -> to the left.
493 ctx.rotate(-Math.PI / 2);
494 ctx.scale(1, -1);
Alex Perry5427c9a2020-02-15 17:43:45 -0800495
496 const M_TO_PX = (size - 10) / FIELD_LENGTH;
497 ctx.scale(M_TO_PX, M_TO_PX);
498 ctx.lineWidth = 1 / M_TO_PX;
499 }
500}