blob: 9e90563bb8f5817aa6b327f65a05e381987894e4 [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 Perry23cca9a2019-10-30 20:18:06 -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;
Austin Schuh1a8fb572019-05-08 20:07:58 -070025 private has_piece: number = 0;
Austin Schuh71ae7952019-04-14 15:12:52 -070026
27 private wrist_div: HTMLDivElement;
28 private elevator_div: HTMLDivElement;
29 private intake_div: HTMLDivElement;
30 private stilts_div: HTMLDivElement;
Austin Schuh1a8fb572019-05-08 20:07:58 -070031 private has_piece_div: HTMLDivElement;
Austin Schuh71ae7952019-04-14 15:12:52 -070032
Alex Perry554cec02019-03-23 20:15:12 -070033 constructor() {
34 const canvas = <HTMLCanvasElement>document.getElementById('field');
Austin Schuh71ae7952019-04-14 15:12:52 -070035 this.wrist_div = <HTMLDivElement>document.getElementById('wrist');
36 this.elevator_div = <HTMLDivElement>document.getElementById('elevator');
37 this.intake_div = <HTMLDivElement>document.getElementById('intake');
38 this.stilts_div = <HTMLDivElement>document.getElementById('stilts');
Austin Schuh1a8fb572019-05-08 20:07:58 -070039 this.has_piece_div = <HTMLDivElement>document.getElementById('has_piece');
Austin Schuh71ae7952019-04-14 15:12:52 -070040
Alex Perry554cec02019-03-23 20:15:12 -070041 const ctx = canvas.getContext('2d');
42
43 const server = location.host;
Alex Perry5b895622019-04-06 14:27:55 -070044 this.initWebSocket(server);
Alex Perry23cca9a2019-10-30 20:18:06 -070045 if (!!ctx) {
46 window.requestAnimationFrame(() => this.draw(ctx));
47 }
Alex Perry5b895622019-04-06 14:27:55 -070048 }
49
50 initWebSocket(server: string): void {
Alex Perry554cec02019-03-23 20:15:12 -070051 const socket = new WebSocket(`ws://${server}/ws`);
Austin Schuh89a95052019-04-14 14:51:14 -070052
53 socket.addEventListener('message', (event) => {
54 const j = JSON.parse(event.data);
James Kuszmauld6d37d12019-03-30 13:04:54 -070055 this.x = j.robotPose.x;
56 this.y = j.robotPose.y;
57 this.theta = j.robotPose.theta;
Alex Perryd2df9652019-03-23 22:53:04 -070058
Alex Perry5b895622019-04-06 14:27:55 -070059 if (j.lineFollowDebug) {
60 this.targetLocked =
61 j.lineFollowDebug.frozen && j.lineFollowDebug.haveTarget;
James Kuszmauld6d37d12019-03-30 13:04:54 -070062 this.targetX = j.lineFollowDebug.goalTarget.x;
63 this.targetY = j.lineFollowDebug.goalTarget.y;
64 this.targetTheta = j.lineFollowDebug.goalTarget.theta;
Alex Perryd2df9652019-03-23 22:53:04 -070065 }
Alex Perryd13750f2019-04-10 21:15:28 -070066 this.cameraFrames = j.cameraDebug;
Austin Schuh71ae7952019-04-14 15:12:52 -070067
68 this.wrist = j.sensors.wrist;
69 this.elevator = j.sensors.elevator;
70 this.intake = j.sensors.intake;
71 this.stilts = j.sensors.stilts;
Austin Schuh1a8fb572019-05-08 20:07:58 -070072 this.has_piece = j.sensors.hasPiece;
Alex Perry554cec02019-03-23 20:15:12 -070073 });
Alex Perry5b895622019-04-06 14:27:55 -070074 socket.addEventListener('close', (event) => {
75 setTimeout(() => {
76 this.initWebSocket(server);
77 }, 1000);
78 });
Alex Perry554cec02019-03-23 20:15:12 -070079 }
80
Alex Perry5b895622019-04-06 14:27:55 -070081 reset(ctx: CanvasRenderingContext2D): void {
82 ctx.setTransform(1, 0, 0, 1, 0, 0);
Alex Perry554cec02019-03-23 20:15:12 -070083 const size = Math.min(window.innerHeight, window.innerWidth) * 0.98;
84 ctx.canvas.height = size;
85 ctx.canvas.width = size;
Alex Perry5b895622019-04-06 14:27:55 -070086 ctx.clearRect(0, 0, size, size);
Alex Perry554cec02019-03-23 20:15:12 -070087
Alex Perry5b895622019-04-06 14:27:55 -070088 ctx.translate(size / 2, size);
Alex Perry554cec02019-03-23 20:15:12 -070089 ctx.rotate(-Math.PI / 2);
90 ctx.scale(1, -1);
91 const M_TO_PX = size / FIELD_WIDTH
92 ctx.scale(M_TO_PX, M_TO_PX);
93 ctx.lineWidth = 1 / M_TO_PX;
Austin Schuh71ae7952019-04-14 15:12:52 -070094
95 this.wrist_div.textContent = "";
96 this.elevator_div.textContent = "";
97 this.intake_div.textContent = "";
98 this.stilts_div.textContent = "";
Austin Schuh1a8fb572019-05-08 20:07:58 -070099 this.has_piece_div.textContent = "";
Alex Perry554cec02019-03-23 20:15:12 -0700100 }
101
Alex Perry5b895622019-04-06 14:27:55 -0700102 draw(ctx: CanvasRenderingContext2D): void {
Alex Perry554cec02019-03-23 20:15:12 -0700103 this.reset(ctx);
104
105 drawField(ctx);
Alex Perryd13750f2019-04-10 21:15:28 -0700106 drawRobot(ctx, this.x, this.y, this.theta, this.cameraFrames);
James Kuszmaul94b3c172019-03-24 14:31:48 -0700107 ctx.save();
108 ctx.lineWidth = 2.0 * ctx.lineWidth;
109 if (this.targetLocked) {
110 ctx.strokeStyle = 'blue';
111 } else {
Alex Perryd2df9652019-03-23 22:53:04 -0700112 ctx.strokeStyle = 'red';
Alex Perryd2df9652019-03-23 22:53:04 -0700113 }
James Kuszmaul94b3c172019-03-24 14:31:48 -0700114 drawTarget(ctx, this.targetX, this.targetY, this.targetTheta);
115 ctx.restore();
Austin Schuh71ae7952019-04-14 15:12:52 -0700116
117 // Now update the superstructure positions.
118 this.wrist_div.textContent = this.wrist.toFixed(3);
119 this.elevator_div.textContent = this.elevator.toFixed(3);
120 this.intake_div.textContent = this.intake.toFixed(3);
121 this.stilts_div.textContent = this.stilts.toFixed(3);
Austin Schuh1a8fb572019-05-08 20:07:58 -0700122 if (this.has_piece) {
123 this.has_piece_div.textContent = "t";
124 } else {
125 this.has_piece_div.textContent = "f";
126 }
Austin Schuh71ae7952019-04-14 15:12:52 -0700127
Alex Perry554cec02019-03-23 20:15:12 -0700128 window.requestAnimationFrame(() => this.draw(ctx));
129 }
130}
131
132main();