Alex Perry | 5b89562 | 2019-04-06 14:27:55 -0700 | [diff] [blame] | 1 | import {FIELD_WIDTH, FT_TO_M} from './constants'; |
Alex Perry | d2df965 | 2019-03-23 22:53:04 -0700 | [diff] [blame] | 2 | import {drawField, drawTarget} from './field'; |
Alex Perry | d13750f | 2019-04-10 21:15:28 -0700 | [diff] [blame] | 3 | import {drawRobot, Frame} from './robot'; |
Alex Perry | 554cec0 | 2019-03-23 20:15:12 -0700 | [diff] [blame] | 4 | |
Alex Perry | 554cec0 | 2019-03-23 20:15:12 -0700 | [diff] [blame] | 5 | function main(): void { |
| 6 | const vis = new Visualiser(); |
| 7 | } |
| 8 | |
| 9 | class Visualiser { |
| 10 | private x = 3; |
| 11 | private y = 0; |
| 12 | private theta = 0; |
| 13 | |
Alex Perry | d2df965 | 2019-03-23 22:53:04 -0700 | [diff] [blame] | 14 | private drawLocked = false; |
Alex Perry | b243346 | 2019-03-26 21:45:26 -0700 | [diff] [blame] | 15 | private targetLocked = false; |
| 16 | private targetX = 0; |
| 17 | private targetY = 0; |
| 18 | private targetTheta = 0; |
Alex Perry | d13750f | 2019-04-10 21:15:28 -0700 | [diff] [blame] | 19 | private cameraFrames : Frame[]; |
Alex Perry | d2df965 | 2019-03-23 22:53:04 -0700 | [diff] [blame] | 20 | |
Austin Schuh | 71ae795 | 2019-04-14 15:12:52 -0700 | [diff] [blame] | 21 | private wrist: number = -1; |
| 22 | private elevator: number = -1; |
| 23 | private intake: number = -1; |
| 24 | private stilts: number = -1; |
Austin Schuh | 1a8fb57 | 2019-05-08 20:07:58 -0700 | [diff] [blame] | 25 | private has_piece: number = 0; |
Austin Schuh | 71ae795 | 2019-04-14 15:12:52 -0700 | [diff] [blame] | 26 | |
| 27 | private wrist_div: HTMLDivElement; |
| 28 | private elevator_div: HTMLDivElement; |
| 29 | private intake_div: HTMLDivElement; |
| 30 | private stilts_div: HTMLDivElement; |
Austin Schuh | 1a8fb57 | 2019-05-08 20:07:58 -0700 | [diff] [blame] | 31 | private has_piece_div: HTMLDivElement; |
Austin Schuh | 71ae795 | 2019-04-14 15:12:52 -0700 | [diff] [blame] | 32 | |
Alex Perry | 554cec0 | 2019-03-23 20:15:12 -0700 | [diff] [blame] | 33 | constructor() { |
| 34 | const canvas = <HTMLCanvasElement>document.getElementById('field'); |
Austin Schuh | 71ae795 | 2019-04-14 15:12:52 -0700 | [diff] [blame] | 35 | 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 Schuh | 1a8fb57 | 2019-05-08 20:07:58 -0700 | [diff] [blame] | 39 | this.has_piece_div = <HTMLDivElement>document.getElementById('has_piece'); |
Austin Schuh | 71ae795 | 2019-04-14 15:12:52 -0700 | [diff] [blame] | 40 | |
Alex Perry | 554cec0 | 2019-03-23 20:15:12 -0700 | [diff] [blame] | 41 | const ctx = canvas.getContext('2d'); |
| 42 | |
| 43 | const server = location.host; |
Alex Perry | 5b89562 | 2019-04-06 14:27:55 -0700 | [diff] [blame] | 44 | this.initWebSocket(server); |
| 45 | window.requestAnimationFrame(() => this.draw(ctx)); |
| 46 | } |
| 47 | |
| 48 | initWebSocket(server: string): void { |
Alex Perry | 554cec0 | 2019-03-23 20:15:12 -0700 | [diff] [blame] | 49 | const socket = new WebSocket(`ws://${server}/ws`); |
Alex Perry | d13750f | 2019-04-10 21:15:28 -0700 | [diff] [blame] | 50 | this.cameraFrames = []; |
Austin Schuh | 89a9505 | 2019-04-14 14:51:14 -0700 | [diff] [blame] | 51 | |
| 52 | socket.addEventListener('message', (event) => { |
| 53 | const j = JSON.parse(event.data); |
James Kuszmaul | d6d37d1 | 2019-03-30 13:04:54 -0700 | [diff] [blame] | 54 | this.x = j.robotPose.x; |
| 55 | this.y = j.robotPose.y; |
| 56 | this.theta = j.robotPose.theta; |
Alex Perry | d2df965 | 2019-03-23 22:53:04 -0700 | [diff] [blame] | 57 | |
Alex Perry | 5b89562 | 2019-04-06 14:27:55 -0700 | [diff] [blame] | 58 | if (j.lineFollowDebug) { |
| 59 | this.targetLocked = |
| 60 | j.lineFollowDebug.frozen && j.lineFollowDebug.haveTarget; |
James Kuszmaul | d6d37d1 | 2019-03-30 13:04:54 -0700 | [diff] [blame] | 61 | this.targetX = j.lineFollowDebug.goalTarget.x; |
| 62 | this.targetY = j.lineFollowDebug.goalTarget.y; |
| 63 | this.targetTheta = j.lineFollowDebug.goalTarget.theta; |
Alex Perry | d2df965 | 2019-03-23 22:53:04 -0700 | [diff] [blame] | 64 | } |
Alex Perry | d13750f | 2019-04-10 21:15:28 -0700 | [diff] [blame] | 65 | this.cameraFrames = j.cameraDebug; |
Austin Schuh | 71ae795 | 2019-04-14 15:12:52 -0700 | [diff] [blame] | 66 | |
| 67 | this.wrist = j.sensors.wrist; |
| 68 | this.elevator = j.sensors.elevator; |
| 69 | this.intake = j.sensors.intake; |
| 70 | this.stilts = j.sensors.stilts; |
Austin Schuh | 1a8fb57 | 2019-05-08 20:07:58 -0700 | [diff] [blame] | 71 | this.has_piece = j.sensors.hasPiece; |
Alex Perry | 554cec0 | 2019-03-23 20:15:12 -0700 | [diff] [blame] | 72 | }); |
Alex Perry | 5b89562 | 2019-04-06 14:27:55 -0700 | [diff] [blame] | 73 | socket.addEventListener('close', (event) => { |
| 74 | setTimeout(() => { |
| 75 | this.initWebSocket(server); |
| 76 | }, 1000); |
| 77 | }); |
Alex Perry | 554cec0 | 2019-03-23 20:15:12 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Alex Perry | 5b89562 | 2019-04-06 14:27:55 -0700 | [diff] [blame] | 80 | reset(ctx: CanvasRenderingContext2D): void { |
| 81 | ctx.setTransform(1, 0, 0, 1, 0, 0); |
Alex Perry | 554cec0 | 2019-03-23 20:15:12 -0700 | [diff] [blame] | 82 | const size = Math.min(window.innerHeight, window.innerWidth) * 0.98; |
| 83 | ctx.canvas.height = size; |
| 84 | ctx.canvas.width = size; |
Alex Perry | 5b89562 | 2019-04-06 14:27:55 -0700 | [diff] [blame] | 85 | ctx.clearRect(0, 0, size, size); |
Alex Perry | 554cec0 | 2019-03-23 20:15:12 -0700 | [diff] [blame] | 86 | |
Alex Perry | 5b89562 | 2019-04-06 14:27:55 -0700 | [diff] [blame] | 87 | ctx.translate(size / 2, size); |
Alex Perry | 554cec0 | 2019-03-23 20:15:12 -0700 | [diff] [blame] | 88 | ctx.rotate(-Math.PI / 2); |
| 89 | ctx.scale(1, -1); |
| 90 | const M_TO_PX = size / FIELD_WIDTH |
| 91 | ctx.scale(M_TO_PX, M_TO_PX); |
| 92 | ctx.lineWidth = 1 / M_TO_PX; |
Austin Schuh | 71ae795 | 2019-04-14 15:12:52 -0700 | [diff] [blame] | 93 | |
| 94 | this.wrist_div.textContent = ""; |
| 95 | this.elevator_div.textContent = ""; |
| 96 | this.intake_div.textContent = ""; |
| 97 | this.stilts_div.textContent = ""; |
Austin Schuh | 1a8fb57 | 2019-05-08 20:07:58 -0700 | [diff] [blame] | 98 | this.has_piece_div.textContent = ""; |
Alex Perry | 554cec0 | 2019-03-23 20:15:12 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Alex Perry | 5b89562 | 2019-04-06 14:27:55 -0700 | [diff] [blame] | 101 | draw(ctx: CanvasRenderingContext2D): void { |
Alex Perry | 554cec0 | 2019-03-23 20:15:12 -0700 | [diff] [blame] | 102 | this.reset(ctx); |
| 103 | |
| 104 | drawField(ctx); |
Alex Perry | d13750f | 2019-04-10 21:15:28 -0700 | [diff] [blame] | 105 | drawRobot(ctx, this.x, this.y, this.theta, this.cameraFrames); |
James Kuszmaul | 94b3c17 | 2019-03-24 14:31:48 -0700 | [diff] [blame] | 106 | ctx.save(); |
| 107 | ctx.lineWidth = 2.0 * ctx.lineWidth; |
| 108 | if (this.targetLocked) { |
| 109 | ctx.strokeStyle = 'blue'; |
| 110 | } else { |
Alex Perry | d2df965 | 2019-03-23 22:53:04 -0700 | [diff] [blame] | 111 | ctx.strokeStyle = 'red'; |
Alex Perry | d2df965 | 2019-03-23 22:53:04 -0700 | [diff] [blame] | 112 | } |
James Kuszmaul | 94b3c17 | 2019-03-24 14:31:48 -0700 | [diff] [blame] | 113 | drawTarget(ctx, this.targetX, this.targetY, this.targetTheta); |
| 114 | ctx.restore(); |
Austin Schuh | 71ae795 | 2019-04-14 15:12:52 -0700 | [diff] [blame] | 115 | |
| 116 | // Now update the superstructure positions. |
| 117 | this.wrist_div.textContent = this.wrist.toFixed(3); |
| 118 | this.elevator_div.textContent = this.elevator.toFixed(3); |
| 119 | this.intake_div.textContent = this.intake.toFixed(3); |
| 120 | this.stilts_div.textContent = this.stilts.toFixed(3); |
Austin Schuh | 1a8fb57 | 2019-05-08 20:07:58 -0700 | [diff] [blame] | 121 | if (this.has_piece) { |
| 122 | this.has_piece_div.textContent = "t"; |
| 123 | } else { |
| 124 | this.has_piece_div.textContent = "f"; |
| 125 | } |
Austin Schuh | 71ae795 | 2019-04-14 15:12:52 -0700 | [diff] [blame] | 126 | |
Alex Perry | 554cec0 | 2019-03-23 20:15:12 -0700 | [diff] [blame] | 127 | window.requestAnimationFrame(() => this.draw(ctx)); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | main(); |