blob: 0b2937cde30590a9dc05ffd9cfe5dad4a1b6502a [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) => {
Philipp Schrader87277f42022-01-01 07:45:12 -080054 // data is a proto which we don't currently have TS support for.
55 const j = JSON.parse(event.data) as any;
James Kuszmauld6d37d12019-03-30 13:04:54 -070056 this.x = j.robotPose.x;
57 this.y = j.robotPose.y;
58 this.theta = j.robotPose.theta;
Alex Perryd2df9652019-03-23 22:53:04 -070059
Alex Perry5b895622019-04-06 14:27:55 -070060 if (j.lineFollowDebug) {
61 this.targetLocked =
62 j.lineFollowDebug.frozen && j.lineFollowDebug.haveTarget;
James Kuszmauld6d37d12019-03-30 13:04:54 -070063 this.targetX = j.lineFollowDebug.goalTarget.x;
64 this.targetY = j.lineFollowDebug.goalTarget.y;
65 this.targetTheta = j.lineFollowDebug.goalTarget.theta;
Alex Perryd2df9652019-03-23 22:53:04 -070066 }
Alex Perryd13750f2019-04-10 21:15:28 -070067 this.cameraFrames = j.cameraDebug;
Austin Schuh71ae7952019-04-14 15:12:52 -070068
69 this.wrist = j.sensors.wrist;
70 this.elevator = j.sensors.elevator;
71 this.intake = j.sensors.intake;
72 this.stilts = j.sensors.stilts;
Austin Schuh1a8fb572019-05-08 20:07:58 -070073 this.has_piece = j.sensors.hasPiece;
Alex Perry554cec02019-03-23 20:15:12 -070074 });
Alex Perry5b895622019-04-06 14:27:55 -070075 socket.addEventListener('close', (event) => {
76 setTimeout(() => {
77 this.initWebSocket(server);
78 }, 1000);
79 });
Alex Perry554cec02019-03-23 20:15:12 -070080 }
81
Alex Perry5b895622019-04-06 14:27:55 -070082 reset(ctx: CanvasRenderingContext2D): void {
83 ctx.setTransform(1, 0, 0, 1, 0, 0);
Alex Perry554cec02019-03-23 20:15:12 -070084 const size = Math.min(window.innerHeight, window.innerWidth) * 0.98;
85 ctx.canvas.height = size;
86 ctx.canvas.width = size;
Alex Perry5b895622019-04-06 14:27:55 -070087 ctx.clearRect(0, 0, size, size);
Alex Perry554cec02019-03-23 20:15:12 -070088
Alex Perry5b895622019-04-06 14:27:55 -070089 ctx.translate(size / 2, size);
Alex Perry554cec02019-03-23 20:15:12 -070090 ctx.rotate(-Math.PI / 2);
91 ctx.scale(1, -1);
92 const M_TO_PX = size / FIELD_WIDTH
93 ctx.scale(M_TO_PX, M_TO_PX);
94 ctx.lineWidth = 1 / M_TO_PX;
Austin Schuh71ae7952019-04-14 15:12:52 -070095
96 this.wrist_div.textContent = "";
97 this.elevator_div.textContent = "";
98 this.intake_div.textContent = "";
99 this.stilts_div.textContent = "";
Austin Schuh1a8fb572019-05-08 20:07:58 -0700100 this.has_piece_div.textContent = "";
Alex Perry554cec02019-03-23 20:15:12 -0700101 }
102
Alex Perry5b895622019-04-06 14:27:55 -0700103 draw(ctx: CanvasRenderingContext2D): void {
Alex Perry554cec02019-03-23 20:15:12 -0700104 this.reset(ctx);
105
106 drawField(ctx);
Alex Perryd13750f2019-04-10 21:15:28 -0700107 drawRobot(ctx, this.x, this.y, this.theta, this.cameraFrames);
James Kuszmaul94b3c172019-03-24 14:31:48 -0700108 ctx.save();
109 ctx.lineWidth = 2.0 * ctx.lineWidth;
110 if (this.targetLocked) {
111 ctx.strokeStyle = 'blue';
112 } else {
Alex Perryd2df9652019-03-23 22:53:04 -0700113 ctx.strokeStyle = 'red';
Alex Perryd2df9652019-03-23 22:53:04 -0700114 }
James Kuszmaul94b3c172019-03-24 14:31:48 -0700115 drawTarget(ctx, this.targetX, this.targetY, this.targetTheta);
116 ctx.restore();
Austin Schuh71ae7952019-04-14 15:12:52 -0700117
118 // Now update the superstructure positions.
119 this.wrist_div.textContent = this.wrist.toFixed(3);
120 this.elevator_div.textContent = this.elevator.toFixed(3);
121 this.intake_div.textContent = this.intake.toFixed(3);
122 this.stilts_div.textContent = this.stilts.toFixed(3);
Austin Schuh1a8fb572019-05-08 20:07:58 -0700123 if (this.has_piece) {
124 this.has_piece_div.textContent = "t";
125 } else {
126 this.has_piece_div.textContent = "f";
127 }
Austin Schuh71ae7952019-04-14 15:12:52 -0700128
Alex Perry554cec02019-03-23 20:15:12 -0700129 window.requestAnimationFrame(() => this.draw(ctx));
130 }
131}
132
133main();