James Kuszmaul | 92ba0e5 | 2019-03-29 17:19:30 -0700 | [diff] [blame] | 1 | import {CAMERA_POSES} from './camera_constants'; |
Alex Perry | 5b89562 | 2019-04-06 14:27:55 -0700 | [diff] [blame] | 2 | import {FT_TO_M, IN_TO_M} from './constants'; |
Alex Perry | d13750f | 2019-04-10 21:15:28 -0700 | [diff] [blame] | 3 | import {drawTarget} from './field'; |
Alex Perry | 554cec0 | 2019-03-23 20:15:12 -0700 | [diff] [blame] | 4 | |
| 5 | const ROBOT_WIDTH = 25 * IN_TO_M; |
| 6 | const ROBOT_LENGTH = 31 * IN_TO_M; |
Alex Perry | d13750f | 2019-04-10 21:15:28 -0700 | [diff] [blame] | 7 | const CAMERA_SCALE = 0.2; |
| 8 | |
| 9 | interface Pose { |
| 10 | x : number; |
| 11 | y : number; |
| 12 | theta: number; |
| 13 | } |
| 14 | |
| 15 | export interface Frame { |
| 16 | timeSinceLastTarget : number; |
| 17 | currentFrameAge : number; |
| 18 | targets : Pose[]; |
| 19 | } |
Alex Perry | 554cec0 | 2019-03-23 20:15:12 -0700 | [diff] [blame] | 20 | |
Alex Perry | 5b89562 | 2019-04-06 14:27:55 -0700 | [diff] [blame] | 21 | function drawCamera( |
Alex Perry | d13750f | 2019-04-10 21:15:28 -0700 | [diff] [blame] | 22 | ctx: CanvasRenderingContext2D, pose: Pose, frame: Frame): void { |
| 23 | ctx.save(); |
| 24 | ctx.translate(pose.x, pose.y); |
| 25 | ctx.rotate(pose.theta); |
| 26 | if (frame.timeSinceLastTarget > 0.25) { |
| 27 | ctx.strokeStyle = 'red'; |
| 28 | } else { |
| 29 | ctx.strokeStyle = 'green'; |
| 30 | } |
James Kuszmaul | 92ba0e5 | 2019-03-29 17:19:30 -0700 | [diff] [blame] | 31 | ctx.beginPath(); |
Alex Perry | d13750f | 2019-04-10 21:15:28 -0700 | [diff] [blame] | 32 | ctx.moveTo(0, 0); |
| 33 | ctx.lineTo(CAMERA_SCALE, CAMERA_SCALE); |
| 34 | ctx.lineTo(CAMERA_SCALE, -CAMERA_SCALE); |
James Kuszmaul | 92ba0e5 | 2019-03-29 17:19:30 -0700 | [diff] [blame] | 35 | ctx.closePath(); |
| 36 | ctx.stroke(); |
Alex Perry | d13750f | 2019-04-10 21:15:28 -0700 | [diff] [blame] | 37 | ctx.restore(); |
James Kuszmaul | 92ba0e5 | 2019-03-29 17:19:30 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Alex Perry | 5b89562 | 2019-04-06 14:27:55 -0700 | [diff] [blame] | 40 | export function drawRobot( |
| 41 | ctx: CanvasRenderingContext2D, x: number, y: number, theta: number, |
Alex Perry | d13750f | 2019-04-10 21:15:28 -0700 | [diff] [blame] | 42 | cameraFrames: Frame[]): void { |
Alex Perry | 554cec0 | 2019-03-23 20:15:12 -0700 | [diff] [blame] | 43 | ctx.save(); |
| 44 | ctx.translate(x, y); |
| 45 | ctx.rotate(theta); |
| 46 | |
| 47 | ctx.fillStyle = 'blue'; |
| 48 | ctx.fillRect(-ROBOT_LENGTH / 2, -ROBOT_WIDTH / 2, ROBOT_LENGTH, ROBOT_WIDTH); |
| 49 | |
James Kuszmaul | 92ba0e5 | 2019-03-29 17:19:30 -0700 | [diff] [blame] | 50 | ctx.beginPath(); |
| 51 | ctx.strokeStyle = 'black'; |
Alex Perry | 5b89562 | 2019-04-06 14:27:55 -0700 | [diff] [blame] | 52 | ctx.moveTo(ROBOT_LENGTH / 2, -ROBOT_WIDTH / 2); |
Alex Perry | 554cec0 | 2019-03-23 20:15:12 -0700 | [diff] [blame] | 53 | ctx.lineTo(ROBOT_LENGTH / 2 + 0.1, 0); |
Alex Perry | 5b89562 | 2019-04-06 14:27:55 -0700 | [diff] [blame] | 54 | ctx.lineTo(ROBOT_LENGTH / 2, ROBOT_WIDTH / 2); |
Alex Perry | 554cec0 | 2019-03-23 20:15:12 -0700 | [diff] [blame] | 55 | ctx.closePath(); |
| 56 | ctx.stroke(); |
James Kuszmaul | 92ba0e5 | 2019-03-29 17:19:30 -0700 | [diff] [blame] | 57 | ctx.lineWidth = 3.0 * ctx.lineWidth; |
| 58 | for (let ii of [0, 1, 2, 3, 4]) { |
Alex Perry | d13750f | 2019-04-10 21:15:28 -0700 | [diff] [blame] | 59 | if (ii < cameraFrames.length) { |
| 60 | drawCamera(ctx, CAMERA_POSES[ii], cameraFrames[ii]); |
| 61 | } |
James Kuszmaul | 92ba0e5 | 2019-03-29 17:19:30 -0700 | [diff] [blame] | 62 | } |
Alex Perry | 554cec0 | 2019-03-23 20:15:12 -0700 | [diff] [blame] | 63 | |
| 64 | ctx.restore(); |
Alex Perry | d13750f | 2019-04-10 21:15:28 -0700 | [diff] [blame] | 65 | |
James Kuszmaul | 08f519e | 2019-04-14 10:44:11 -0700 | [diff] [blame] | 66 | ctx.save(); |
| 67 | ctx.lineWidth = 3.0 * ctx.lineWidth; |
| 68 | ctx.strokeStyle = 'yellow'; |
Alex Perry | d13750f | 2019-04-10 21:15:28 -0700 | [diff] [blame] | 69 | for (let frame of cameraFrames) { |
| 70 | if (frame.targets) { |
| 71 | for (let target of frame.targets) { |
Alex Perry | d13750f | 2019-04-10 21:15:28 -0700 | [diff] [blame] | 72 | drawTarget(ctx, target.x, target.y, target.theta); |
| 73 | } |
| 74 | } |
| 75 | } |
James Kuszmaul | 08f519e | 2019-04-14 10:44:11 -0700 | [diff] [blame] | 76 | ctx.restore(); |
Alex Perry | 554cec0 | 2019-03-23 20:15:12 -0700 | [diff] [blame] | 77 | } |