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 | |
Alex Perry | 554cec0 | 2019-03-23 20:15:12 -0700 | [diff] [blame] | 21 | constructor() { |
| 22 | const canvas = <HTMLCanvasElement>document.getElementById('field'); |
| 23 | const ctx = canvas.getContext('2d'); |
| 24 | |
| 25 | const server = location.host; |
Alex Perry | 5b89562 | 2019-04-06 14:27:55 -0700 | [diff] [blame] | 26 | this.initWebSocket(server); |
| 27 | window.requestAnimationFrame(() => this.draw(ctx)); |
| 28 | } |
| 29 | |
| 30 | initWebSocket(server: string): void { |
Alex Perry | 554cec0 | 2019-03-23 20:15:12 -0700 | [diff] [blame] | 31 | const socket = new WebSocket(`ws://${server}/ws`); |
Alex Perry | d13750f | 2019-04-10 21:15:28 -0700 | [diff] [blame] | 32 | this.cameraFrames = []; |
Austin Schuh | 89a9505 | 2019-04-14 14:51:14 -0700 | [diff] [blame^] | 33 | |
| 34 | socket.addEventListener('message', (event) => { |
| 35 | const j = JSON.parse(event.data); |
James Kuszmaul | d6d37d1 | 2019-03-30 13:04:54 -0700 | [diff] [blame] | 36 | this.x = j.robotPose.x; |
| 37 | this.y = j.robotPose.y; |
| 38 | this.theta = j.robotPose.theta; |
Alex Perry | d2df965 | 2019-03-23 22:53:04 -0700 | [diff] [blame] | 39 | |
Alex Perry | 5b89562 | 2019-04-06 14:27:55 -0700 | [diff] [blame] | 40 | if (j.lineFollowDebug) { |
| 41 | this.targetLocked = |
| 42 | j.lineFollowDebug.frozen && j.lineFollowDebug.haveTarget; |
James Kuszmaul | d6d37d1 | 2019-03-30 13:04:54 -0700 | [diff] [blame] | 43 | this.targetX = j.lineFollowDebug.goalTarget.x; |
| 44 | this.targetY = j.lineFollowDebug.goalTarget.y; |
| 45 | this.targetTheta = j.lineFollowDebug.goalTarget.theta; |
Alex Perry | d2df965 | 2019-03-23 22:53:04 -0700 | [diff] [blame] | 46 | } |
Alex Perry | d13750f | 2019-04-10 21:15:28 -0700 | [diff] [blame] | 47 | this.cameraFrames = j.cameraDebug; |
Alex Perry | 554cec0 | 2019-03-23 20:15:12 -0700 | [diff] [blame] | 48 | }); |
Alex Perry | 5b89562 | 2019-04-06 14:27:55 -0700 | [diff] [blame] | 49 | socket.addEventListener('close', (event) => { |
| 50 | setTimeout(() => { |
| 51 | this.initWebSocket(server); |
| 52 | }, 1000); |
| 53 | }); |
Alex Perry | 554cec0 | 2019-03-23 20:15:12 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Alex Perry | 5b89562 | 2019-04-06 14:27:55 -0700 | [diff] [blame] | 56 | reset(ctx: CanvasRenderingContext2D): void { |
| 57 | ctx.setTransform(1, 0, 0, 1, 0, 0); |
Alex Perry | 554cec0 | 2019-03-23 20:15:12 -0700 | [diff] [blame] | 58 | const size = Math.min(window.innerHeight, window.innerWidth) * 0.98; |
| 59 | ctx.canvas.height = size; |
| 60 | ctx.canvas.width = size; |
Alex Perry | 5b89562 | 2019-04-06 14:27:55 -0700 | [diff] [blame] | 61 | ctx.clearRect(0, 0, size, size); |
Alex Perry | 554cec0 | 2019-03-23 20:15:12 -0700 | [diff] [blame] | 62 | |
Alex Perry | 5b89562 | 2019-04-06 14:27:55 -0700 | [diff] [blame] | 63 | ctx.translate(size / 2, size); |
Alex Perry | 554cec0 | 2019-03-23 20:15:12 -0700 | [diff] [blame] | 64 | ctx.rotate(-Math.PI / 2); |
| 65 | ctx.scale(1, -1); |
| 66 | const M_TO_PX = size / FIELD_WIDTH |
| 67 | ctx.scale(M_TO_PX, M_TO_PX); |
| 68 | ctx.lineWidth = 1 / M_TO_PX; |
Alex Perry | 554cec0 | 2019-03-23 20:15:12 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Alex Perry | 5b89562 | 2019-04-06 14:27:55 -0700 | [diff] [blame] | 71 | draw(ctx: CanvasRenderingContext2D): void { |
Alex Perry | 554cec0 | 2019-03-23 20:15:12 -0700 | [diff] [blame] | 72 | this.reset(ctx); |
| 73 | |
| 74 | drawField(ctx); |
Alex Perry | d13750f | 2019-04-10 21:15:28 -0700 | [diff] [blame] | 75 | drawRobot(ctx, this.x, this.y, this.theta, this.cameraFrames); |
James Kuszmaul | 94b3c17 | 2019-03-24 14:31:48 -0700 | [diff] [blame] | 76 | ctx.save(); |
| 77 | ctx.lineWidth = 2.0 * ctx.lineWidth; |
| 78 | if (this.targetLocked) { |
| 79 | ctx.strokeStyle = 'blue'; |
| 80 | } else { |
Alex Perry | d2df965 | 2019-03-23 22:53:04 -0700 | [diff] [blame] | 81 | ctx.strokeStyle = 'red'; |
Alex Perry | d2df965 | 2019-03-23 22:53:04 -0700 | [diff] [blame] | 82 | } |
James Kuszmaul | 94b3c17 | 2019-03-24 14:31:48 -0700 | [diff] [blame] | 83 | drawTarget(ctx, this.targetX, this.targetY, this.targetTheta); |
| 84 | ctx.restore(); |
Alex Perry | 554cec0 | 2019-03-23 20:15:12 -0700 | [diff] [blame] | 85 | window.requestAnimationFrame(() => this.draw(ctx)); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | main(); |