blob: b2daea1889a8ad74115f58bf1957caf6f4c15885 [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';
6import * as sift from 'org_frc971/y2020/vision/sift/sift_generated';
James Kuszmaul71a81932020-12-15 21:08:01 -08007import * as web_proxy from 'org_frc971/aos/network/web_proxy_generated';
Philipp Schradere625ba22020-11-16 20:11:37 -08008
9import DrivetrainStatus = drivetrain.frc971.control_loops.drivetrain.Status;
10import ImageMatchResult = sift.frc971.vision.sift.ImageMatchResult;
Philipp Schradere625ba22020-11-16 20:11:37 -080011import Channel = configuration.aos.Channel;
James Kuszmaul71a81932020-12-15 21:08:01 -080012import SubscriberRequest = web_proxy.aos.web_proxy.SubscriberRequest;
13import ChannelRequest = web_proxy.aos.web_proxy.ChannelRequest;
14import TransferMethod = web_proxy.aos.web_proxy.TransferMethod;
Alex Perry5427c9a2020-02-15 17:43:45 -080015
Alex Perry2124ae82020-03-07 14:19:06 -080016import {FIELD_LENGTH, FIELD_WIDTH, FT_TO_M, IN_TO_M} from './constants';
17
Alex Perryb49a3fb2020-02-29 15:26:54 -080018// (0,0) is field center, +X is toward red DS
Alex Perry5427c9a2020-02-15 17:43:45 -080019const FIELD_SIDE_Y = FIELD_WIDTH / 2;
Alex Perryb49a3fb2020-02-29 15:26:54 -080020const FIELD_EDGE_X = FIELD_LENGTH / 2;
Alex Perry5427c9a2020-02-15 17:43:45 -080021
22const DS_WIDTH = 69 * IN_TO_M;
23const DS_ANGLE = 20 * Math.PI / 180;
Alex Perryb49a3fb2020-02-29 15:26:54 -080024const DS_END_X = FIELD_EDGE_X - DS_WIDTH * Math.sin(DS_ANGLE);
Alex Perry5427c9a2020-02-15 17:43:45 -080025const DS_INSIDE_Y = FIELD_SIDE_Y - DS_WIDTH * Math.cos(DS_ANGLE);
26
Alex Perryb49a3fb2020-02-29 15:26:54 -080027const TRENCH_X = 108 * IN_TO_M;
Alex Perry5427c9a2020-02-15 17:43:45 -080028const TRENCH_WIDTH = 55.5 * IN_TO_M;
29const TRENCH_INSIDE = FIELD_SIDE_Y - TRENCH_WIDTH;
30
31const SPINNER_LENGTH = 30 * IN_TO_M;
Alex Perryb49a3fb2020-02-29 15:26:54 -080032const SPINNER_TOP_X = 374.59 * IN_TO_M - FIELD_EDGE_X;
Alex Perry5427c9a2020-02-15 17:43:45 -080033const SPINNER_BOTTOM_X = SPINNER_TOP_X - SPINNER_LENGTH;
34
Alex Perryb49a3fb2020-02-29 15:26:54 -080035const SHIELD_BOTTOM_X = -116 * IN_TO_M;
Alex Perry5427c9a2020-02-15 17:43:45 -080036const SHIELD_BOTTOM_Y = 43.75 * IN_TO_M;
37
Alex Perryb49a3fb2020-02-29 15:26:54 -080038const SHIELD_TOP_X = 116 * IN_TO_M;
Alex Perry5427c9a2020-02-15 17:43:45 -080039const SHIELD_TOP_Y = -43.75 * IN_TO_M;
40
Alex Perryb49a3fb2020-02-29 15:26:54 -080041const SHIELD_RIGHT_X = -51.06 * IN_TO_M;
Alex Perry5427c9a2020-02-15 17:43:45 -080042const SHIELD_RIGHT_Y = -112.88 * IN_TO_M;
43
Alex Perryb49a3fb2020-02-29 15:26:54 -080044const SHIELD_LEFT_X = 51.06 * IN_TO_M;
Alex Perry5427c9a2020-02-15 17:43:45 -080045const SHIELD_LEFT_Y = 112.88 * IN_TO_M;
46
47const SHIELD_CENTER_TOP_X = (SHIELD_TOP_X + SHIELD_LEFT_X) / 2
48const SHIELD_CENTER_TOP_Y = (SHIELD_TOP_Y + SHIELD_LEFT_Y) / 2
49
50const SHIELD_CENTER_BOTTOM_X = (SHIELD_BOTTOM_X + SHIELD_RIGHT_X) / 2
51const SHIELD_CENTER_BOTTOM_Y = (SHIELD_BOTTOM_Y + SHIELD_RIGHT_Y) / 2
52
Alex Perryb49a3fb2020-02-29 15:26:54 -080053const INITIATION_X = FIELD_EDGE_X - 120 * IN_TO_M;
Alex Perry5427c9a2020-02-15 17:43:45 -080054
Alex Perryb49a3fb2020-02-29 15:26:54 -080055const TARGET_ZONE_TIP_X = FIELD_EDGE_X - 30 * IN_TO_M;
Alex Perry5427c9a2020-02-15 17:43:45 -080056const TARGET_ZONE_WIDTH = 48 * IN_TO_M;
57const LOADING_ZONE_WIDTH = 60 * IN_TO_M;
58
Alex Perry2124ae82020-03-07 14:19:06 -080059const ROBOT_WIDTH = 28 * IN_TO_M;
60const ROBOT_LENGTH = 30 * IN_TO_M;
61
Alex Perry5427c9a2020-02-15 17:43:45 -080062export class FieldHandler {
63 private canvas = document.createElement('canvas');
Philipp Schradere625ba22020-11-16 20:11:37 -080064 private imageMatchResult: ImageMatchResult|null = null;
Philipp Schradera227d042020-11-14 17:33:52 -080065 private drivetrainStatus: DrivetrainStatus|null = null;
Alex Perry5427c9a2020-02-15 17:43:45 -080066
Alex Perryb49a3fb2020-02-29 15:26:54 -080067 constructor(private readonly connection: Connection) {
Alex Perry5427c9a2020-02-15 17:43:45 -080068 document.body.appendChild(this.canvas);
Alex Perryb49a3fb2020-02-29 15:26:54 -080069
70 this.connection.addConfigHandler(() => {
James Kuszmaul527038a2020-12-21 23:40:44 -080071 this.connection.addHandler(
72 '/camera', ImageMatchResult.getFullyQualifiedName(), (res) => {
73 this.handleImageMatchResult(res);
74 });
75 this.connection.addHandler(
76 '/drivetrain', DrivetrainStatus.getFullyQualifiedName(), (data) => {
77 this.handleDrivetrainStatus(data);
78 });
Alex Perryb49a3fb2020-02-29 15:26:54 -080079 });
Alex Perryb49a3fb2020-02-29 15:26:54 -080080 }
81
82 private handleImageMatchResult(data: Uint8Array): void {
Philipp Schradere625ba22020-11-16 20:11:37 -080083 const fbBuffer = new ByteBuffer(data);
84 this.imageMatchResult = ImageMatchResult.getRootAsImageMatchResult(
85 fbBuffer as unknown as flatbuffers.ByteBuffer);
Alex Perryb49a3fb2020-02-29 15:26:54 -080086 }
87
Alex Perry2124ae82020-03-07 14:19:06 -080088 private handleDrivetrainStatus(data: Uint8Array): void {
Philipp Schradere625ba22020-11-16 20:11:37 -080089 const fbBuffer = new ByteBuffer(data);
90 this.drivetrainStatus = DrivetrainStatus.getRootAsStatus(
91 fbBuffer as unknown as flatbuffers.ByteBuffer);
Alex Perry2124ae82020-03-07 14:19:06 -080092 }
93
Alex Perry5427c9a2020-02-15 17:43:45 -080094 drawField(): void {
95 const MY_COLOR = 'red';
96 const OTHER_COLOR = 'blue';
97 const ctx = this.canvas.getContext('2d');
98 // draw perimiter
99 ctx.beginPath();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800100 ctx.moveTo(FIELD_EDGE_X, DS_INSIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800101 ctx.lineTo(DS_END_X, FIELD_SIDE_Y);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800102 ctx.lineTo(-DS_END_X, FIELD_SIDE_Y);
103 ctx.lineTo(-FIELD_EDGE_X, DS_INSIDE_Y);
104 ctx.lineTo(-FIELD_EDGE_X, -DS_INSIDE_Y);
105 ctx.lineTo(-DS_END_X, -FIELD_SIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800106 ctx.lineTo(DS_END_X, -FIELD_SIDE_Y);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800107 ctx.lineTo(FIELD_EDGE_X, -DS_INSIDE_Y);
108 ctx.lineTo(FIELD_EDGE_X, DS_INSIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800109 ctx.stroke();
110
111 // draw shield generator
112 ctx.beginPath();
113 ctx.moveTo(SHIELD_BOTTOM_X, SHIELD_BOTTOM_Y);
114 ctx.lineTo(SHIELD_RIGHT_X, SHIELD_RIGHT_Y);
115 ctx.lineTo(SHIELD_TOP_X, SHIELD_TOP_Y);
116 ctx.lineTo(SHIELD_LEFT_X, SHIELD_LEFT_Y);
117 ctx.lineTo(SHIELD_BOTTOM_X, SHIELD_BOTTOM_Y);
118 ctx.moveTo(SHIELD_CENTER_TOP_X, SHIELD_CENTER_TOP_Y);
119 ctx.lineTo(SHIELD_CENTER_BOTTOM_X, SHIELD_CENTER_BOTTOM_Y);
120 ctx.stroke();
121
Alex Perryb49a3fb2020-02-29 15:26:54 -0800122 this.drawHalfField(ctx, 'red');
123 ctx.rotate(Math.PI);
124 this.drawHalfField(ctx, 'blue');
125 ctx.rotate(Math.PI);
126 }
Alex Perry5427c9a2020-02-15 17:43:45 -0800127
Alex Perryb49a3fb2020-02-29 15:26:54 -0800128 drawHalfField(ctx, color: string): void {
129 // trenches
130 ctx.strokeStyle = color;
Alex Perry5427c9a2020-02-15 17:43:45 -0800131 ctx.beginPath();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800132 ctx.moveTo(TRENCH_X, FIELD_SIDE_Y);
133 ctx.lineTo(TRENCH_X, TRENCH_INSIDE);
134 ctx.lineTo(-TRENCH_X, TRENCH_INSIDE);
135 ctx.lineTo(-TRENCH_X, FIELD_SIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800136 ctx.stroke();
137
138 ctx.strokeStyle = 'black';
139 ctx.beginPath();
140 ctx.moveTo(SPINNER_TOP_X, FIELD_SIDE_Y);
141 ctx.lineTo(SPINNER_TOP_X, TRENCH_INSIDE);
142 ctx.lineTo(SPINNER_BOTTOM_X, TRENCH_INSIDE);
143 ctx.lineTo(SPINNER_BOTTOM_X, FIELD_SIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800144 ctx.stroke();
145
Alex Perry5427c9a2020-02-15 17:43:45 -0800146 ctx.beginPath();
147 ctx.moveTo(INITIATION_X, FIELD_SIDE_Y);
148 ctx.lineTo(INITIATION_X, -FIELD_SIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800149 ctx.stroke();
150
Alex Perryb49a3fb2020-02-29 15:26:54 -0800151 // target/loading
152 ctx.strokeStyle = color;
Alex Perry5427c9a2020-02-15 17:43:45 -0800153 ctx.beginPath();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800154 ctx.moveTo(FIELD_EDGE_X, DS_INSIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800155 ctx.lineTo(TARGET_ZONE_TIP_X, DS_INSIDE_Y - 0.5 * TARGET_ZONE_WIDTH);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800156 ctx.lineTo(FIELD_EDGE_X, DS_INSIDE_Y - TARGET_ZONE_WIDTH);
Alex Perry5427c9a2020-02-15 17:43:45 -0800157
Alex Perryb49a3fb2020-02-29 15:26:54 -0800158 ctx.moveTo(-FIELD_EDGE_X, DS_INSIDE_Y);
159 ctx.lineTo(-TARGET_ZONE_TIP_X, DS_INSIDE_Y - 0.5 * LOADING_ZONE_WIDTH);
160 ctx.lineTo(-FIELD_EDGE_X, DS_INSIDE_Y - LOADING_ZONE_WIDTH);
Alex Perry5427c9a2020-02-15 17:43:45 -0800161 ctx.stroke();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800162 }
Alex Perry5427c9a2020-02-15 17:43:45 -0800163
Alex Perryb49a3fb2020-02-29 15:26:54 -0800164 drawCamera(x: number, y: number, theta: number): void {
165 const ctx = this.canvas.getContext('2d');
166 ctx.save();
167 ctx.translate(x, y);
168 ctx.rotate(theta);
Alex Perry5427c9a2020-02-15 17:43:45 -0800169 ctx.beginPath();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800170 ctx.moveTo(0.5, 0.5);
171 ctx.lineTo(0, 0);
172 ctx.lineTo(0.5, -0.5);
Alex Perry5427c9a2020-02-15 17:43:45 -0800173 ctx.stroke();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800174 ctx.beginPath();
Philipp Schradere625ba22020-11-16 20:11:37 -0800175 ctx.arc(0, 0, 0.25, -Math.PI / 4, Math.PI / 4);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800176 ctx.stroke();
177 ctx.restore();
178 }
179
Alex Perry2124ae82020-03-07 14:19:06 -0800180 drawRobot(x: number, y: number, theta: number): void {
181 const ctx = this.canvas.getContext('2d');
182 ctx.save();
183 ctx.translate(x, y);
184 ctx.rotate(theta);
185 ctx.rect(-ROBOT_LENGTH / 2, -ROBOT_WIDTH / 2, ROBOT_LENGTH, ROBOT_WIDTH);
186 ctx.stroke();
187 ctx.beginPath();
188 ctx.moveTo(0, 0);
189 ctx.lineTo(ROBOT_LENGTH / 2, 0);
190 ctx.stroke();
191 ctx.restore();
192 }
193
Philipp Schradere625ba22020-11-16 20:11:37 -0800194 draw(): void {
Alex Perryb49a3fb2020-02-29 15:26:54 -0800195 this.reset();
196 this.drawField();
Philipp Schradere625ba22020-11-16 20:11:37 -0800197 // draw cameras
Alex Perryb49a3fb2020-02-29 15:26:54 -0800198 if (this.imageMatchResult) {
Philipp Schradera227d042020-11-14 17:33:52 -0800199 for (let i = 0; i < this.imageMatchResult.cameraPosesLength(); i++) {
Alex Perryb49a3fb2020-02-29 15:26:54 -0800200 const pose = this.imageMatchResult.cameraPoses(i);
201 const mat = pose.fieldToCamera();
202 const x = mat.data(3);
203 const y = mat.data(7);
Alex Perry2124ae82020-03-07 14:19:06 -0800204 const theta = Math.atan2(
205 -mat.data(8),
206 Math.sqrt(Math.pow(mat.data(9), 2) + Math.pow(mat.data(10), 2)));
207 this.drawCamera(x, y, theta);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800208 }
209 }
210
Alex Perry2124ae82020-03-07 14:19:06 -0800211 if (this.drivetrainStatus) {
212 this.drawRobot(
213 this.drivetrainStatus.x(), this.drivetrainStatus.y(),
214 this.drivetrainStatus.theta());
215 }
216
Alex Perryb49a3fb2020-02-29 15:26:54 -0800217 window.requestAnimationFrame(() => this.draw());
Alex Perry5427c9a2020-02-15 17:43:45 -0800218 }
219
220 reset(): void {
221 const ctx = this.canvas.getContext('2d');
222 ctx.setTransform(1, 0, 0, 1, 0, 0);
223 const size = window.innerHeight * 0.9;
224 ctx.canvas.height = size;
Alex Perryb49a3fb2020-02-29 15:26:54 -0800225 const width = size / 2 + 20;
226 ctx.canvas.width = width;
227 ctx.clearRect(0, 0, size, width);
Alex Perry5427c9a2020-02-15 17:43:45 -0800228
Alex Perryb49a3fb2020-02-29 15:26:54 -0800229 // Translate to center of display.
230 ctx.translate(width / 2, size / 2);
Alex Perry5427c9a2020-02-15 17:43:45 -0800231 // Coordinate system is:
232 // x -> forward.
233 // y -> to the left.
234 ctx.rotate(-Math.PI / 2);
235 ctx.scale(1, -1);
Alex Perry5427c9a2020-02-15 17:43:45 -0800236
237 const M_TO_PX = (size - 10) / FIELD_LENGTH;
238 ctx.scale(M_TO_PX, M_TO_PX);
239 ctx.lineWidth = 1 / M_TO_PX;
240 }
241}