blob: d3bafa237c3b529d73e66ddb394922fe7f0eb604 [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 Perryb49a3fb2020-02-29 15:26:54 -080062/**
63 * All the messages that are required to display camera information on the field.
64 * Messages not readable on the server node are ignored.
65 */
66const REQUIRED_CHANNELS = [
67 {
68 name: '/pi1/camera',
69 type: 'frc971.vision.sift.ImageMatchResult',
70 },
71 {
72 name: '/pi2/camera',
73 type: 'frc971.vision.sift.ImageMatchResult',
74 },
75 {
76 name: '/pi3/camera',
77 type: 'frc971.vision.sift.ImageMatchResult',
78 },
79 {
80 name: '/pi4/camera',
81 type: 'frc971.vision.sift.ImageMatchResult',
82 },
83 {
84 name: '/pi5/camera',
85 type: 'frc971.vision.sift.ImageMatchResult',
86 },
Alex Perry2124ae82020-03-07 14:19:06 -080087 {
88 name: '/drivetrain',
89 type: 'frc971.control_loops.drivetrain.Status',
90 },
Alex Perryb49a3fb2020-02-29 15:26:54 -080091];
92
Alex Perry5427c9a2020-02-15 17:43:45 -080093export class FieldHandler {
94 private canvas = document.createElement('canvas');
Philipp Schradere625ba22020-11-16 20:11:37 -080095 private imageMatchResult: ImageMatchResult|null = null;
Philipp Schradera227d042020-11-14 17:33:52 -080096 private drivetrainStatus: DrivetrainStatus|null = null;
Alex Perry5427c9a2020-02-15 17:43:45 -080097
Alex Perryb49a3fb2020-02-29 15:26:54 -080098 constructor(private readonly connection: Connection) {
Alex Perry5427c9a2020-02-15 17:43:45 -080099 document.body.appendChild(this.canvas);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800100
101 this.connection.addConfigHandler(() => {
102 this.sendConnect();
103 });
Austin Schuh7c75e582020-11-14 16:41:18 -0800104 this.connection.addHandler(
Philipp Schradere625ba22020-11-16 20:11:37 -0800105 ImageMatchResult.getFullyQualifiedName(), (res) => {
Austin Schuh7c75e582020-11-14 16:41:18 -0800106 this.handleImageMatchResult(res);
107 });
Philipp Schradere625ba22020-11-16 20:11:37 -0800108 this.connection.addHandler(
109 DrivetrainStatus.getFullyQualifiedName(), (data) => {
110 this.handleDrivetrainStatus(data);
111 });
Alex Perryb49a3fb2020-02-29 15:26:54 -0800112 }
113
114 private handleImageMatchResult(data: Uint8Array): void {
Philipp Schradere625ba22020-11-16 20:11:37 -0800115 const fbBuffer = new ByteBuffer(data);
116 this.imageMatchResult = ImageMatchResult.getRootAsImageMatchResult(
117 fbBuffer as unknown as flatbuffers.ByteBuffer);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800118 }
119
Alex Perry2124ae82020-03-07 14:19:06 -0800120 private handleDrivetrainStatus(data: Uint8Array): void {
Philipp Schradere625ba22020-11-16 20:11:37 -0800121 const fbBuffer = new ByteBuffer(data);
122 this.drivetrainStatus = DrivetrainStatus.getRootAsStatus(
123 fbBuffer as unknown as flatbuffers.ByteBuffer);
Alex Perry2124ae82020-03-07 14:19:06 -0800124 }
125
Alex Perryb49a3fb2020-02-29 15:26:54 -0800126 private sendConnect(): void {
Philipp Schradere625ba22020-11-16 20:11:37 -0800127 const builder =
128 new flatbuffers_builder.Builder(512) as unknown as flatbuffers.Builder;
Alex Perryb49a3fb2020-02-29 15:26:54 -0800129 const channels: flatbuffers.Offset[] = [];
130 for (const channel of REQUIRED_CHANNELS) {
131 const nameFb = builder.createString(channel.name);
132 const typeFb = builder.createString(channel.type);
Philipp Schradere625ba22020-11-16 20:11:37 -0800133 Channel.startChannel(builder);
134 Channel.addName(builder, nameFb);
135 Channel.addType(builder, typeFb);
136 const channelFb = Channel.endChannel(builder);
James Kuszmaul71a81932020-12-15 21:08:01 -0800137 ChannelRequest.startChannelRequest(builder);
138 ChannelRequest.addChannel(builder, channelFb);
139 ChannelRequest.addMethod(builder, TransferMethod.SUBSAMPLE);
140 channels.push(ChannelRequest.endChannelRequest(builder));
Alex Perryb49a3fb2020-02-29 15:26:54 -0800141 }
142
Philipp Schradere625ba22020-11-16 20:11:37 -0800143 const channelsFb =
James Kuszmaul71a81932020-12-15 21:08:01 -0800144 SubscriberRequest.createChannelsToTransferVector(builder, channels);
145 SubscriberRequest.startSubscriberRequest(builder);
146 SubscriberRequest.addChannelsToTransfer(builder, channelsFb);
147 const connect = SubscriberRequest.endSubscriberRequest(builder);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800148 builder.finish(connect);
149 this.connection.sendConnectMessage(builder);
Alex Perry5427c9a2020-02-15 17:43:45 -0800150 }
151
152 drawField(): void {
153 const MY_COLOR = 'red';
154 const OTHER_COLOR = 'blue';
155 const ctx = this.canvas.getContext('2d');
156 // draw perimiter
157 ctx.beginPath();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800158 ctx.moveTo(FIELD_EDGE_X, DS_INSIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800159 ctx.lineTo(DS_END_X, FIELD_SIDE_Y);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800160 ctx.lineTo(-DS_END_X, FIELD_SIDE_Y);
161 ctx.lineTo(-FIELD_EDGE_X, DS_INSIDE_Y);
162 ctx.lineTo(-FIELD_EDGE_X, -DS_INSIDE_Y);
163 ctx.lineTo(-DS_END_X, -FIELD_SIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800164 ctx.lineTo(DS_END_X, -FIELD_SIDE_Y);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800165 ctx.lineTo(FIELD_EDGE_X, -DS_INSIDE_Y);
166 ctx.lineTo(FIELD_EDGE_X, DS_INSIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800167 ctx.stroke();
168
169 // draw shield generator
170 ctx.beginPath();
171 ctx.moveTo(SHIELD_BOTTOM_X, SHIELD_BOTTOM_Y);
172 ctx.lineTo(SHIELD_RIGHT_X, SHIELD_RIGHT_Y);
173 ctx.lineTo(SHIELD_TOP_X, SHIELD_TOP_Y);
174 ctx.lineTo(SHIELD_LEFT_X, SHIELD_LEFT_Y);
175 ctx.lineTo(SHIELD_BOTTOM_X, SHIELD_BOTTOM_Y);
176 ctx.moveTo(SHIELD_CENTER_TOP_X, SHIELD_CENTER_TOP_Y);
177 ctx.lineTo(SHIELD_CENTER_BOTTOM_X, SHIELD_CENTER_BOTTOM_Y);
178 ctx.stroke();
179
Alex Perryb49a3fb2020-02-29 15:26:54 -0800180 this.drawHalfField(ctx, 'red');
181 ctx.rotate(Math.PI);
182 this.drawHalfField(ctx, 'blue');
183 ctx.rotate(Math.PI);
184 }
Alex Perry5427c9a2020-02-15 17:43:45 -0800185
Alex Perryb49a3fb2020-02-29 15:26:54 -0800186 drawHalfField(ctx, color: string): void {
187 // trenches
188 ctx.strokeStyle = color;
Alex Perry5427c9a2020-02-15 17:43:45 -0800189 ctx.beginPath();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800190 ctx.moveTo(TRENCH_X, FIELD_SIDE_Y);
191 ctx.lineTo(TRENCH_X, TRENCH_INSIDE);
192 ctx.lineTo(-TRENCH_X, TRENCH_INSIDE);
193 ctx.lineTo(-TRENCH_X, FIELD_SIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800194 ctx.stroke();
195
196 ctx.strokeStyle = 'black';
197 ctx.beginPath();
198 ctx.moveTo(SPINNER_TOP_X, FIELD_SIDE_Y);
199 ctx.lineTo(SPINNER_TOP_X, TRENCH_INSIDE);
200 ctx.lineTo(SPINNER_BOTTOM_X, TRENCH_INSIDE);
201 ctx.lineTo(SPINNER_BOTTOM_X, FIELD_SIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800202 ctx.stroke();
203
Alex Perry5427c9a2020-02-15 17:43:45 -0800204 ctx.beginPath();
205 ctx.moveTo(INITIATION_X, FIELD_SIDE_Y);
206 ctx.lineTo(INITIATION_X, -FIELD_SIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800207 ctx.stroke();
208
Alex Perryb49a3fb2020-02-29 15:26:54 -0800209 // target/loading
210 ctx.strokeStyle = color;
Alex Perry5427c9a2020-02-15 17:43:45 -0800211 ctx.beginPath();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800212 ctx.moveTo(FIELD_EDGE_X, DS_INSIDE_Y);
Alex Perry5427c9a2020-02-15 17:43:45 -0800213 ctx.lineTo(TARGET_ZONE_TIP_X, DS_INSIDE_Y - 0.5 * TARGET_ZONE_WIDTH);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800214 ctx.lineTo(FIELD_EDGE_X, DS_INSIDE_Y - TARGET_ZONE_WIDTH);
Alex Perry5427c9a2020-02-15 17:43:45 -0800215
Alex Perryb49a3fb2020-02-29 15:26:54 -0800216 ctx.moveTo(-FIELD_EDGE_X, DS_INSIDE_Y);
217 ctx.lineTo(-TARGET_ZONE_TIP_X, DS_INSIDE_Y - 0.5 * LOADING_ZONE_WIDTH);
218 ctx.lineTo(-FIELD_EDGE_X, DS_INSIDE_Y - LOADING_ZONE_WIDTH);
Alex Perry5427c9a2020-02-15 17:43:45 -0800219 ctx.stroke();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800220 }
Alex Perry5427c9a2020-02-15 17:43:45 -0800221
Alex Perryb49a3fb2020-02-29 15:26:54 -0800222 drawCamera(x: number, y: number, theta: number): void {
223 const ctx = this.canvas.getContext('2d');
224 ctx.save();
225 ctx.translate(x, y);
226 ctx.rotate(theta);
Alex Perry5427c9a2020-02-15 17:43:45 -0800227 ctx.beginPath();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800228 ctx.moveTo(0.5, 0.5);
229 ctx.lineTo(0, 0);
230 ctx.lineTo(0.5, -0.5);
Alex Perry5427c9a2020-02-15 17:43:45 -0800231 ctx.stroke();
Alex Perryb49a3fb2020-02-29 15:26:54 -0800232 ctx.beginPath();
Philipp Schradere625ba22020-11-16 20:11:37 -0800233 ctx.arc(0, 0, 0.25, -Math.PI / 4, Math.PI / 4);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800234 ctx.stroke();
235 ctx.restore();
236 }
237
Alex Perry2124ae82020-03-07 14:19:06 -0800238 drawRobot(x: number, y: number, theta: number): void {
239 const ctx = this.canvas.getContext('2d');
240 ctx.save();
241 ctx.translate(x, y);
242 ctx.rotate(theta);
243 ctx.rect(-ROBOT_LENGTH / 2, -ROBOT_WIDTH / 2, ROBOT_LENGTH, ROBOT_WIDTH);
244 ctx.stroke();
245 ctx.beginPath();
246 ctx.moveTo(0, 0);
247 ctx.lineTo(ROBOT_LENGTH / 2, 0);
248 ctx.stroke();
249 ctx.restore();
250 }
251
Philipp Schradere625ba22020-11-16 20:11:37 -0800252 draw(): void {
Alex Perryb49a3fb2020-02-29 15:26:54 -0800253 this.reset();
254 this.drawField();
Philipp Schradere625ba22020-11-16 20:11:37 -0800255 // draw cameras
Alex Perryb49a3fb2020-02-29 15:26:54 -0800256 if (this.imageMatchResult) {
Philipp Schradera227d042020-11-14 17:33:52 -0800257 for (let i = 0; i < this.imageMatchResult.cameraPosesLength(); i++) {
Alex Perryb49a3fb2020-02-29 15:26:54 -0800258 const pose = this.imageMatchResult.cameraPoses(i);
259 const mat = pose.fieldToCamera();
260 const x = mat.data(3);
261 const y = mat.data(7);
Alex Perry2124ae82020-03-07 14:19:06 -0800262 const theta = Math.atan2(
263 -mat.data(8),
264 Math.sqrt(Math.pow(mat.data(9), 2) + Math.pow(mat.data(10), 2)));
265 this.drawCamera(x, y, theta);
Alex Perryb49a3fb2020-02-29 15:26:54 -0800266 }
267 }
268
Alex Perry2124ae82020-03-07 14:19:06 -0800269 if (this.drivetrainStatus) {
270 this.drawRobot(
271 this.drivetrainStatus.x(), this.drivetrainStatus.y(),
272 this.drivetrainStatus.theta());
273 }
274
Alex Perryb49a3fb2020-02-29 15:26:54 -0800275 window.requestAnimationFrame(() => this.draw());
Alex Perry5427c9a2020-02-15 17:43:45 -0800276 }
277
278 reset(): void {
279 const ctx = this.canvas.getContext('2d');
280 ctx.setTransform(1, 0, 0, 1, 0, 0);
281 const size = window.innerHeight * 0.9;
282 ctx.canvas.height = size;
Alex Perryb49a3fb2020-02-29 15:26:54 -0800283 const width = size / 2 + 20;
284 ctx.canvas.width = width;
285 ctx.clearRect(0, 0, size, width);
Alex Perry5427c9a2020-02-15 17:43:45 -0800286
Alex Perryb49a3fb2020-02-29 15:26:54 -0800287 // Translate to center of display.
288 ctx.translate(width / 2, size / 2);
Alex Perry5427c9a2020-02-15 17:43:45 -0800289 // Coordinate system is:
290 // x -> forward.
291 // y -> to the left.
292 ctx.rotate(-Math.PI / 2);
293 ctx.scale(1, -1);
Alex Perry5427c9a2020-02-15 17:43:45 -0800294
295 const M_TO_PX = (size - 10) / FIELD_LENGTH;
296 ctx.scale(M_TO_PX, M_TO_PX);
297 ctx.lineWidth = 1 / M_TO_PX;
298 }
299}