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