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