blob: 1f905274c3aab3fcf6d03978d769002869ad5eed [file] [log] [blame]
Alex Perry5b895622019-04-06 14:27:55 -07001import {FIELD_WIDTH, FT_TO_M} from './constants';
Alex Perryd2df9652019-03-23 22:53:04 -07002import {drawField, drawTarget} from './field';
Alex Perryd13750f2019-04-10 21:15:28 -07003import {drawRobot, Frame} from './robot';
Alex Perry554cec02019-03-23 20:15:12 -07004
Alex Perry554cec02019-03-23 20:15:12 -07005function main(): void {
6 const vis = new Visualiser();
7}
8
9class Visualiser {
10 private x = 3;
11 private y = 0;
12 private theta = 0;
13
Alex Perryd2df9652019-03-23 22:53:04 -070014 private drawLocked = false;
Alex Perryb2433462019-03-26 21:45:26 -070015 private targetLocked = false;
16 private targetX = 0;
17 private targetY = 0;
18 private targetTheta = 0;
Alex Perryd13750f2019-04-10 21:15:28 -070019 private cameraFrames : Frame[];
Alex Perryd2df9652019-03-23 22:53:04 -070020
Austin Schuh71ae7952019-04-14 15:12:52 -070021 private wrist: number = -1;
22 private elevator: number = -1;
23 private intake: number = -1;
24 private stilts: number = -1;
25
26 private wrist_div: HTMLDivElement;
27 private elevator_div: HTMLDivElement;
28 private intake_div: HTMLDivElement;
29 private stilts_div: HTMLDivElement;
30
Alex Perry554cec02019-03-23 20:15:12 -070031 constructor() {
32 const canvas = <HTMLCanvasElement>document.getElementById('field');
Austin Schuh71ae7952019-04-14 15:12:52 -070033 this.wrist_div = <HTMLDivElement>document.getElementById('wrist');
34 this.elevator_div = <HTMLDivElement>document.getElementById('elevator');
35 this.intake_div = <HTMLDivElement>document.getElementById('intake');
36 this.stilts_div = <HTMLDivElement>document.getElementById('stilts');
37
Alex Perry554cec02019-03-23 20:15:12 -070038 const ctx = canvas.getContext('2d');
39
40 const server = location.host;
Alex Perry5b895622019-04-06 14:27:55 -070041 this.initWebSocket(server);
42 window.requestAnimationFrame(() => this.draw(ctx));
43 }
44
45 initWebSocket(server: string): void {
Alex Perry554cec02019-03-23 20:15:12 -070046 const socket = new WebSocket(`ws://${server}/ws`);
Alex Perryd13750f2019-04-10 21:15:28 -070047 this.cameraFrames = [];
Austin Schuh89a95052019-04-14 14:51:14 -070048
49 socket.addEventListener('message', (event) => {
50 const j = JSON.parse(event.data);
James Kuszmauld6d37d12019-03-30 13:04:54 -070051 this.x = j.robotPose.x;
52 this.y = j.robotPose.y;
53 this.theta = j.robotPose.theta;
Alex Perryd2df9652019-03-23 22:53:04 -070054
Alex Perry5b895622019-04-06 14:27:55 -070055 if (j.lineFollowDebug) {
56 this.targetLocked =
57 j.lineFollowDebug.frozen && j.lineFollowDebug.haveTarget;
James Kuszmauld6d37d12019-03-30 13:04:54 -070058 this.targetX = j.lineFollowDebug.goalTarget.x;
59 this.targetY = j.lineFollowDebug.goalTarget.y;
60 this.targetTheta = j.lineFollowDebug.goalTarget.theta;
Alex Perryd2df9652019-03-23 22:53:04 -070061 }
Alex Perryd13750f2019-04-10 21:15:28 -070062 this.cameraFrames = j.cameraDebug;
Austin Schuh71ae7952019-04-14 15:12:52 -070063
64 this.wrist = j.sensors.wrist;
65 this.elevator = j.sensors.elevator;
66 this.intake = j.sensors.intake;
67 this.stilts = j.sensors.stilts;
Alex Perry554cec02019-03-23 20:15:12 -070068 });
Alex Perry5b895622019-04-06 14:27:55 -070069 socket.addEventListener('close', (event) => {
70 setTimeout(() => {
71 this.initWebSocket(server);
72 }, 1000);
73 });
Alex Perry554cec02019-03-23 20:15:12 -070074 }
75
Alex Perry5b895622019-04-06 14:27:55 -070076 reset(ctx: CanvasRenderingContext2D): void {
77 ctx.setTransform(1, 0, 0, 1, 0, 0);
Alex Perry554cec02019-03-23 20:15:12 -070078 const size = Math.min(window.innerHeight, window.innerWidth) * 0.98;
79 ctx.canvas.height = size;
80 ctx.canvas.width = size;
Alex Perry5b895622019-04-06 14:27:55 -070081 ctx.clearRect(0, 0, size, size);
Alex Perry554cec02019-03-23 20:15:12 -070082
Alex Perry5b895622019-04-06 14:27:55 -070083 ctx.translate(size / 2, size);
Alex Perry554cec02019-03-23 20:15:12 -070084 ctx.rotate(-Math.PI / 2);
85 ctx.scale(1, -1);
86 const M_TO_PX = size / FIELD_WIDTH
87 ctx.scale(M_TO_PX, M_TO_PX);
88 ctx.lineWidth = 1 / M_TO_PX;
Austin Schuh71ae7952019-04-14 15:12:52 -070089
90 this.wrist_div.textContent = "";
91 this.elevator_div.textContent = "";
92 this.intake_div.textContent = "";
93 this.stilts_div.textContent = "";
Alex Perry554cec02019-03-23 20:15:12 -070094 }
95
Alex Perry5b895622019-04-06 14:27:55 -070096 draw(ctx: CanvasRenderingContext2D): void {
Alex Perry554cec02019-03-23 20:15:12 -070097 this.reset(ctx);
98
99 drawField(ctx);
Alex Perryd13750f2019-04-10 21:15:28 -0700100 drawRobot(ctx, this.x, this.y, this.theta, this.cameraFrames);
James Kuszmaul94b3c172019-03-24 14:31:48 -0700101 ctx.save();
102 ctx.lineWidth = 2.0 * ctx.lineWidth;
103 if (this.targetLocked) {
104 ctx.strokeStyle = 'blue';
105 } else {
Alex Perryd2df9652019-03-23 22:53:04 -0700106 ctx.strokeStyle = 'red';
Alex Perryd2df9652019-03-23 22:53:04 -0700107 }
James Kuszmaul94b3c172019-03-24 14:31:48 -0700108 drawTarget(ctx, this.targetX, this.targetY, this.targetTheta);
109 ctx.restore();
Austin Schuh71ae7952019-04-14 15:12:52 -0700110
111 // Now update the superstructure positions.
112 this.wrist_div.textContent = this.wrist.toFixed(3);
113 this.elevator_div.textContent = this.elevator.toFixed(3);
114 this.intake_div.textContent = this.intake.toFixed(3);
115 this.stilts_div.textContent = this.stilts.toFixed(3);
116
Alex Perry554cec02019-03-23 20:15:12 -0700117 window.requestAnimationFrame(() => this.draw(ctx));
118 }
119}
120
121main();