blob: 67566eacb6046367faa0a8a3c5b001355768ae5f [file] [log] [blame]
Maxwell Hendersonad312342023-01-10 12:07:47 -08001import {ByteBuffer} from 'flatbuffers';
2import {Connection} from '../../aos/network/www/proxy';
3import {Status as SuperstructureStatus} from '../control_loops/superstructure/superstructure_status_generated'
4import {Status as DrivetrainStatus} from '../../frc971/control_loops/drivetrain/drivetrain_status_generated';
5
6import {FIELD_LENGTH, FIELD_WIDTH, FT_TO_M, IN_TO_M} from './constants';
7
8// (0,0) is field center, +X is toward red DS
9const FIELD_SIDE_Y = FIELD_WIDTH / 2;
10const FIELD_EDGE_X = FIELD_LENGTH / 2;
11
12const ROBOT_WIDTH = 34 * IN_TO_M;
13const ROBOT_LENGTH = 36 * IN_TO_M;
14
15const PI_COLORS = ['#ff00ff', '#ffff00', '#00ffff', '#ffa500'];
16
17export class FieldHandler {
18 private canvas = document.createElement('canvas');
19 private drivetrainStatus: DrivetrainStatus|null = null;
20 private superstructureStatus: SuperstructureStatus|null = null;
21
22 // Image information indexed by timestamp (seconds since the epoch), so that
23 // we can stop displaying images after a certain amount of time.
24 private x: HTMLElement = (document.getElementById('x') as HTMLElement);
25 private y: HTMLElement = (document.getElementById('y') as HTMLElement);
26 private theta: HTMLElement =
27 (document.getElementById('theta') as HTMLElement);
28 private superstructureState: HTMLElement =
29 (document.getElementById('superstructure_state') as HTMLElement);
30 private imagesAcceptedCounter: HTMLElement =
31 (document.getElementById('images_accepted') as HTMLElement);
32 private imagesRejectedCounter: HTMLElement =
33 (document.getElementById('images_rejected') as HTMLElement);
34 private fieldImage: HTMLImageElement = new Image();
35
36 constructor(private readonly connection: Connection) {
37 (document.getElementById('field') as HTMLElement).appendChild(this.canvas);
38
39 this.fieldImage.src = "2022.png";
40
41 for (let ii = 0; ii < PI_COLORS.length; ++ii) {
42 const legendEntry = document.createElement('div');
43 legendEntry.style.color = PI_COLORS[ii];
44 legendEntry.innerHTML = 'PI' + (ii + 1).toString()
45 document.getElementById('legend').appendChild(legendEntry);
46 }
47
48 this.connection.addConfigHandler(() => {
49 // Visualization message is reliable so that we can see *all* the vision
50 // matches.
51 this.connection.addHandler(
52 '/drivetrain', "frc971.control_loops.drivetrain.Status", (data) => {
53 this.handleDrivetrainStatus(data);
54 });
55 this.connection.addHandler(
56 '/superstructure', "y2023.control_loops.superstructure.Status",
57 (data) => {
58 this.handleSuperstructureStatus(data);
59 });
60 });
61 }
62
63 private handleDrivetrainStatus(data: Uint8Array): void {
64 const fbBuffer = new ByteBuffer(data);
65 this.drivetrainStatus = DrivetrainStatus.getRootAsStatus(fbBuffer);
66 }
67
68 private handleSuperstructureStatus(data: Uint8Array): void {
69 const fbBuffer = new ByteBuffer(data);
70 this.superstructureStatus = SuperstructureStatus.getRootAsStatus(fbBuffer);
71 }
72
73 drawField(): void {
74 const ctx = this.canvas.getContext('2d');
75 ctx.save();
76 ctx.scale(-1.0, 1.0);
77 ctx.drawImage(
78 this.fieldImage, 0, 0, this.fieldImage.width, this.fieldImage.height,
79 -FIELD_EDGE_X, -FIELD_SIDE_Y, FIELD_LENGTH, FIELD_WIDTH);
80 ctx.restore();
81 }
82
83 drawCamera(
84 x: number, y: number, theta: number, color: string = 'blue',
85 extendLines: boolean = true): void {
86 const ctx = this.canvas.getContext('2d');
87 ctx.save();
88 ctx.translate(x, y);
89 ctx.rotate(theta);
90 ctx.strokeStyle = color;
91 ctx.beginPath();
92 ctx.moveTo(0.5, 0.5);
93 ctx.lineTo(0, 0);
94 if (extendLines) {
95 ctx.lineTo(100.0, 0);
96 ctx.lineTo(0, 0);
97 }
98 ctx.lineTo(0.5, -0.5);
99 ctx.stroke();
100 ctx.beginPath();
101 ctx.arc(0, 0, 0.25, -Math.PI / 4, Math.PI / 4);
102 ctx.stroke();
103 ctx.restore();
104 }
105
106 drawRobot(
107 x: number, y: number, theta: number, turret: number|null,
108 color: string = 'blue', dashed: boolean = false,
109 extendLines: boolean = true): void {
110 const ctx = this.canvas.getContext('2d');
111 ctx.save();
112 ctx.translate(x, y);
113 ctx.rotate(theta);
114 ctx.strokeStyle = color;
115 ctx.lineWidth = ROBOT_WIDTH / 10.0;
116 if (dashed) {
117 ctx.setLineDash([0.05, 0.05]);
118 } else {
119 // Empty array = solid line.
120 ctx.setLineDash([]);
121 }
122 ctx.rect(-ROBOT_LENGTH / 2, -ROBOT_WIDTH / 2, ROBOT_LENGTH, ROBOT_WIDTH);
123 ctx.stroke();
124
125 // Draw line indicating which direction is forwards on the robot.
126 ctx.beginPath();
127 ctx.moveTo(0, 0);
128 if (extendLines) {
129 ctx.lineTo(1000.0, 0);
130 } else {
131 ctx.lineTo(ROBOT_LENGTH / 2.0, 0);
132 }
133 ctx.stroke();
134
135 ctx.restore();
136 }
137
138 setZeroing(div: HTMLElement): void {
139 div.innerHTML = 'zeroing';
140 div.classList.remove('faulted');
141 div.classList.add('zeroing');
142 div.classList.remove('near');
143 }
144
145 setEstopped(div: HTMLElement): void {
146 div.innerHTML = 'estopped';
147 div.classList.add('faulted');
148 div.classList.remove('zeroing');
149 div.classList.remove('near');
150 }
151
152 setTargetValue(
153 div: HTMLElement, target: number, val: number, tolerance: number): void {
154 div.innerHTML = val.toFixed(4);
155 div.classList.remove('faulted');
156 div.classList.remove('zeroing');
157 if (Math.abs(target - val) < tolerance) {
158 div.classList.add('near');
159 } else {
160 div.classList.remove('near');
161 }
162 }
163
164 setValue(div: HTMLElement, val: number): void {
165 div.innerHTML = val.toFixed(4);
166 div.classList.remove('faulted');
167 div.classList.remove('zeroing');
168 div.classList.remove('near');
169 }
170
171 draw(): void {
172 this.reset();
173 this.drawField();
174
175 // Draw the matches with debugging information from the localizer.
176 const now = Date.now() / 1000.0;
177
178 if (this.drivetrainStatus && this.drivetrainStatus.trajectoryLogging()) {
179 this.drawRobot(
180 this.drivetrainStatus.trajectoryLogging().x(),
181 this.drivetrainStatus.trajectoryLogging().y(),
182 this.drivetrainStatus.trajectoryLogging().theta(), null, "#000000FF",
183 false);
184 }
185
186 window.requestAnimationFrame(() => this.draw());
187 }
188
189 reset(): void {
190 const ctx = this.canvas.getContext('2d');
191 ctx.setTransform(1, 0, 0, 1, 0, 0);
192 const size = window.innerHeight * 0.9;
193 ctx.canvas.height = size;
194 const width = size / 2 + 20;
195 ctx.canvas.width = width;
196 ctx.clearRect(0, 0, size, width);
197
198 // Translate to center of display.
199 ctx.translate(width / 2, size / 2);
200 // Coordinate system is:
201 // x -> forward.
202 // y -> to the left.
203 ctx.rotate(-Math.PI / 2);
204 ctx.scale(1, -1);
205
206 const M_TO_PX = (size - 10) / FIELD_LENGTH;
207 ctx.scale(M_TO_PX, M_TO_PX);
208 ctx.lineWidth = 1 / M_TO_PX;
209 }
210}