blob: a106d5fc530f40128706bcc85c5dc117e7003d88 [file] [log] [blame]
James Kuszmaul92ba0e52019-03-29 17:19:30 -07001import {CAMERA_POSES} from './camera_constants';
Alex Perry5b895622019-04-06 14:27:55 -07002import {FT_TO_M, IN_TO_M} from './constants';
Alex Perryd13750f2019-04-10 21:15:28 -07003import {drawTarget} from './field';
Alex Perry554cec02019-03-23 20:15:12 -07004
5const ROBOT_WIDTH = 25 * IN_TO_M;
6const ROBOT_LENGTH = 31 * IN_TO_M;
Alex Perryd13750f2019-04-10 21:15:28 -07007const CAMERA_SCALE = 0.2;
8
9interface Pose {
10 x : number;
11 y : number;
12 theta: number;
13}
14
15export interface Frame {
16 timeSinceLastTarget : number;
17 currentFrameAge : number;
18 targets : Pose[];
19}
Alex Perry554cec02019-03-23 20:15:12 -070020
Alex Perry5b895622019-04-06 14:27:55 -070021function drawCamera(
Alex Perryd13750f2019-04-10 21:15:28 -070022 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 Kuszmaul92ba0e52019-03-29 17:19:30 -070031 ctx.beginPath();
Alex Perryd13750f2019-04-10 21:15:28 -070032 ctx.moveTo(0, 0);
33 ctx.lineTo(CAMERA_SCALE, CAMERA_SCALE);
34 ctx.lineTo(CAMERA_SCALE, -CAMERA_SCALE);
James Kuszmaul92ba0e52019-03-29 17:19:30 -070035 ctx.closePath();
36 ctx.stroke();
Alex Perryd13750f2019-04-10 21:15:28 -070037 ctx.restore();
James Kuszmaul92ba0e52019-03-29 17:19:30 -070038}
39
Alex Perry5b895622019-04-06 14:27:55 -070040export function drawRobot(
41 ctx: CanvasRenderingContext2D, x: number, y: number, theta: number,
Alex Perryd13750f2019-04-10 21:15:28 -070042 cameraFrames: Frame[]): void {
Alex Perry554cec02019-03-23 20:15:12 -070043 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 Kuszmaul92ba0e52019-03-29 17:19:30 -070050 ctx.beginPath();
51 ctx.strokeStyle = 'black';
Alex Perry5b895622019-04-06 14:27:55 -070052 ctx.moveTo(ROBOT_LENGTH / 2, -ROBOT_WIDTH / 2);
Alex Perry554cec02019-03-23 20:15:12 -070053 ctx.lineTo(ROBOT_LENGTH / 2 + 0.1, 0);
Alex Perry5b895622019-04-06 14:27:55 -070054 ctx.lineTo(ROBOT_LENGTH / 2, ROBOT_WIDTH / 2);
Alex Perry554cec02019-03-23 20:15:12 -070055 ctx.closePath();
56 ctx.stroke();
James Kuszmaul92ba0e52019-03-29 17:19:30 -070057 ctx.lineWidth = 3.0 * ctx.lineWidth;
58 for (let ii of [0, 1, 2, 3, 4]) {
Alex Perryd13750f2019-04-10 21:15:28 -070059 if (ii < cameraFrames.length) {
60 drawCamera(ctx, CAMERA_POSES[ii], cameraFrames[ii]);
61 }
James Kuszmaul92ba0e52019-03-29 17:19:30 -070062 }
Alex Perry554cec02019-03-23 20:15:12 -070063
64 ctx.restore();
Alex Perryd13750f2019-04-10 21:15:28 -070065
James Kuszmaul08f519e2019-04-14 10:44:11 -070066 ctx.save();
67 ctx.lineWidth = 3.0 * ctx.lineWidth;
68 ctx.strokeStyle = 'yellow';
Alex Perryd13750f2019-04-10 21:15:28 -070069 for (let frame of cameraFrames) {
70 if (frame.targets) {
71 for (let target of frame.targets) {
Alex Perryd13750f2019-04-10 21:15:28 -070072 drawTarget(ctx, target.x, target.y, target.theta);
73 }
74 }
75 }
James Kuszmaul08f519e2019-04-14 10:44:11 -070076 ctx.restore();
Alex Perry554cec02019-03-23 20:15:12 -070077}