Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 1 | import * as configuration from 'org_frc971/aos/configuration_generated'; |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 2 | import {Connection} from 'org_frc971/aos/network/www/proxy'; |
| 3 | import * as flatbuffers_builder from 'org_frc971/external/com_github_google_flatbuffers/ts/builder'; |
| 4 | import {ByteBuffer} from 'org_frc971/external/com_github_google_flatbuffers/ts/byte-buffer'; |
| 5 | import * as drivetrain from 'org_frc971/frc971/control_loops/drivetrain/drivetrain_status_generated'; |
| 6 | import * as sift from 'org_frc971/y2020/vision/sift/sift_generated'; |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame] | 7 | import * as web_proxy from 'org_frc971/aos/network/web_proxy_generated'; |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 8 | import * as ss from 'org_frc971/y2020/control_loops/superstructure/superstructure_status_generated' |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 9 | |
| 10 | import DrivetrainStatus = drivetrain.frc971.control_loops.drivetrain.Status; |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 11 | import SuperstructureStatus = ss.y2020.control_loops.superstructure.Status; |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 12 | import ImageMatchResult = sift.frc971.vision.sift.ImageMatchResult; |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 13 | import Channel = configuration.aos.Channel; |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame] | 14 | import SubscriberRequest = web_proxy.aos.web_proxy.SubscriberRequest; |
| 15 | import ChannelRequest = web_proxy.aos.web_proxy.ChannelRequest; |
| 16 | import TransferMethod = web_proxy.aos.web_proxy.TransferMethod; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 17 | |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 18 | import {FIELD_LENGTH, FIELD_WIDTH, FT_TO_M, IN_TO_M} from './constants'; |
| 19 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 20 | // (0,0) is field center, +X is toward red DS |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 21 | const FIELD_SIDE_Y = FIELD_WIDTH / 2; |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 22 | const FIELD_EDGE_X = FIELD_LENGTH / 2; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 23 | |
| 24 | const DS_WIDTH = 69 * IN_TO_M; |
| 25 | const DS_ANGLE = 20 * Math.PI / 180; |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 26 | const DS_END_X = FIELD_EDGE_X - DS_WIDTH * Math.sin(DS_ANGLE); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 27 | const DS_INSIDE_Y = FIELD_SIDE_Y - DS_WIDTH * Math.cos(DS_ANGLE); |
| 28 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 29 | const TRENCH_X = 108 * IN_TO_M; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 30 | const TRENCH_WIDTH = 55.5 * IN_TO_M; |
| 31 | const TRENCH_INSIDE = FIELD_SIDE_Y - TRENCH_WIDTH; |
| 32 | |
| 33 | const SPINNER_LENGTH = 30 * IN_TO_M; |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 34 | const SPINNER_TOP_X = 374.59 * IN_TO_M - FIELD_EDGE_X; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 35 | const SPINNER_BOTTOM_X = SPINNER_TOP_X - SPINNER_LENGTH; |
| 36 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 37 | const SHIELD_BOTTOM_X = -116 * IN_TO_M; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 38 | const SHIELD_BOTTOM_Y = 43.75 * IN_TO_M; |
| 39 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 40 | const SHIELD_TOP_X = 116 * IN_TO_M; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 41 | const SHIELD_TOP_Y = -43.75 * IN_TO_M; |
| 42 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 43 | const SHIELD_RIGHT_X = -51.06 * IN_TO_M; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 44 | const SHIELD_RIGHT_Y = -112.88 * IN_TO_M; |
| 45 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 46 | const SHIELD_LEFT_X = 51.06 * IN_TO_M; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 47 | const SHIELD_LEFT_Y = 112.88 * IN_TO_M; |
| 48 | |
| 49 | const SHIELD_CENTER_TOP_X = (SHIELD_TOP_X + SHIELD_LEFT_X) / 2 |
| 50 | const SHIELD_CENTER_TOP_Y = (SHIELD_TOP_Y + SHIELD_LEFT_Y) / 2 |
| 51 | |
| 52 | const SHIELD_CENTER_BOTTOM_X = (SHIELD_BOTTOM_X + SHIELD_RIGHT_X) / 2 |
| 53 | const SHIELD_CENTER_BOTTOM_Y = (SHIELD_BOTTOM_Y + SHIELD_RIGHT_Y) / 2 |
| 54 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 55 | const INITIATION_X = FIELD_EDGE_X - 120 * IN_TO_M; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 56 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 57 | const TARGET_ZONE_TIP_X = FIELD_EDGE_X - 30 * IN_TO_M; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 58 | const TARGET_ZONE_WIDTH = 48 * IN_TO_M; |
| 59 | const LOADING_ZONE_WIDTH = 60 * IN_TO_M; |
| 60 | |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 61 | const ROBOT_WIDTH = 28 * IN_TO_M; |
| 62 | const ROBOT_LENGTH = 30 * IN_TO_M; |
| 63 | |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 64 | |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 65 | export class FieldHandler { |
| 66 | private canvas = document.createElement('canvas'); |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 67 | private imageMatchResult = new Map<string, ImageMatchResult>(); |
Philipp Schrader | a227d04 | 2020-11-14 17:33:52 -0800 | [diff] [blame] | 68 | private drivetrainStatus: DrivetrainStatus|null = null; |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 69 | private superstructureStatus: SuperstructureStatus|null = null; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 70 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 71 | constructor(private readonly connection: Connection) { |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 72 | document.body.appendChild(this.canvas); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 73 | |
| 74 | this.connection.addConfigHandler(() => { |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 75 | // Go through and register handlers for both all the individual pis as |
| 76 | // well as the local pi. Depending on the node that we are running on, |
| 77 | // different subsets of these will be available. |
| 78 | for (const prefix of ['', '/pi1', '/pi2', '/pi3', '/pi4']) { |
| 79 | this.connection.addHandler( |
| 80 | prefix + '/camera', ImageMatchResult.getFullyQualifiedName(), (res) => { |
| 81 | this.handleImageMatchResult(prefix, res); |
| 82 | }); |
| 83 | } |
James Kuszmaul | 527038a | 2020-12-21 23:40:44 -0800 | [diff] [blame] | 84 | this.connection.addHandler( |
| 85 | '/drivetrain', DrivetrainStatus.getFullyQualifiedName(), (data) => { |
| 86 | this.handleDrivetrainStatus(data); |
| 87 | }); |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 88 | this.connection.addHandler( |
| 89 | '/superstructure', SuperstructureStatus.getFullyQualifiedName(), |
| 90 | (data) => { |
| 91 | this.handleSuperstructureStatus(data); |
| 92 | }); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 93 | }); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 94 | } |
| 95 | |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 96 | private handleImageMatchResult(prefix: string, data: Uint8Array): void { |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 97 | const fbBuffer = new ByteBuffer(data); |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 98 | this.imageMatchResult.set( |
| 99 | prefix, |
| 100 | ImageMatchResult.getRootAsImageMatchResult( |
| 101 | fbBuffer as unknown as flatbuffers.ByteBuffer)); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 104 | private handleDrivetrainStatus(data: Uint8Array): void { |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 105 | const fbBuffer = new ByteBuffer(data); |
| 106 | this.drivetrainStatus = DrivetrainStatus.getRootAsStatus( |
| 107 | fbBuffer as unknown as flatbuffers.ByteBuffer); |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 108 | } |
| 109 | |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 110 | private handleSuperstructureStatus(data: Uint8Array): void { |
| 111 | const fbBuffer = new ByteBuffer(data); |
| 112 | this.superstructureStatus = SuperstructureStatus.getRootAsStatus( |
| 113 | fbBuffer as unknown as flatbuffers.ByteBuffer); |
| 114 | } |
| 115 | |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 116 | drawField(): void { |
| 117 | const MY_COLOR = 'red'; |
| 118 | const OTHER_COLOR = 'blue'; |
| 119 | const ctx = this.canvas.getContext('2d'); |
| 120 | // draw perimiter |
| 121 | ctx.beginPath(); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 122 | ctx.moveTo(FIELD_EDGE_X, DS_INSIDE_Y); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 123 | ctx.lineTo(DS_END_X, FIELD_SIDE_Y); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 124 | ctx.lineTo(-DS_END_X, FIELD_SIDE_Y); |
| 125 | ctx.lineTo(-FIELD_EDGE_X, DS_INSIDE_Y); |
| 126 | ctx.lineTo(-FIELD_EDGE_X, -DS_INSIDE_Y); |
| 127 | ctx.lineTo(-DS_END_X, -FIELD_SIDE_Y); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 128 | ctx.lineTo(DS_END_X, -FIELD_SIDE_Y); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 129 | ctx.lineTo(FIELD_EDGE_X, -DS_INSIDE_Y); |
| 130 | ctx.lineTo(FIELD_EDGE_X, DS_INSIDE_Y); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 131 | ctx.stroke(); |
| 132 | |
| 133 | // draw shield generator |
| 134 | ctx.beginPath(); |
| 135 | ctx.moveTo(SHIELD_BOTTOM_X, SHIELD_BOTTOM_Y); |
| 136 | ctx.lineTo(SHIELD_RIGHT_X, SHIELD_RIGHT_Y); |
| 137 | ctx.lineTo(SHIELD_TOP_X, SHIELD_TOP_Y); |
| 138 | ctx.lineTo(SHIELD_LEFT_X, SHIELD_LEFT_Y); |
| 139 | ctx.lineTo(SHIELD_BOTTOM_X, SHIELD_BOTTOM_Y); |
| 140 | ctx.moveTo(SHIELD_CENTER_TOP_X, SHIELD_CENTER_TOP_Y); |
| 141 | ctx.lineTo(SHIELD_CENTER_BOTTOM_X, SHIELD_CENTER_BOTTOM_Y); |
| 142 | ctx.stroke(); |
| 143 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 144 | this.drawHalfField(ctx, 'red'); |
| 145 | ctx.rotate(Math.PI); |
| 146 | this.drawHalfField(ctx, 'blue'); |
| 147 | ctx.rotate(Math.PI); |
| 148 | } |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 149 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 150 | drawHalfField(ctx, color: string): void { |
| 151 | // trenches |
| 152 | ctx.strokeStyle = color; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 153 | ctx.beginPath(); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 154 | ctx.moveTo(TRENCH_X, FIELD_SIDE_Y); |
| 155 | ctx.lineTo(TRENCH_X, TRENCH_INSIDE); |
| 156 | ctx.lineTo(-TRENCH_X, TRENCH_INSIDE); |
| 157 | ctx.lineTo(-TRENCH_X, FIELD_SIDE_Y); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 158 | ctx.stroke(); |
| 159 | |
| 160 | ctx.strokeStyle = 'black'; |
| 161 | ctx.beginPath(); |
| 162 | ctx.moveTo(SPINNER_TOP_X, FIELD_SIDE_Y); |
| 163 | ctx.lineTo(SPINNER_TOP_X, TRENCH_INSIDE); |
| 164 | ctx.lineTo(SPINNER_BOTTOM_X, TRENCH_INSIDE); |
| 165 | ctx.lineTo(SPINNER_BOTTOM_X, FIELD_SIDE_Y); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 166 | ctx.stroke(); |
| 167 | |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 168 | ctx.beginPath(); |
| 169 | ctx.moveTo(INITIATION_X, FIELD_SIDE_Y); |
| 170 | ctx.lineTo(INITIATION_X, -FIELD_SIDE_Y); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 171 | ctx.stroke(); |
| 172 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 173 | // target/loading |
| 174 | ctx.strokeStyle = color; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 175 | ctx.beginPath(); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 176 | ctx.moveTo(FIELD_EDGE_X, DS_INSIDE_Y); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 177 | ctx.lineTo(TARGET_ZONE_TIP_X, DS_INSIDE_Y - 0.5 * TARGET_ZONE_WIDTH); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 178 | ctx.lineTo(FIELD_EDGE_X, DS_INSIDE_Y - TARGET_ZONE_WIDTH); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 179 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 180 | ctx.moveTo(-FIELD_EDGE_X, DS_INSIDE_Y); |
| 181 | ctx.lineTo(-TARGET_ZONE_TIP_X, DS_INSIDE_Y - 0.5 * LOADING_ZONE_WIDTH); |
| 182 | ctx.lineTo(-FIELD_EDGE_X, DS_INSIDE_Y - LOADING_ZONE_WIDTH); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 183 | ctx.stroke(); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 184 | } |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 185 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 186 | drawCamera(x: number, y: number, theta: number): void { |
| 187 | const ctx = this.canvas.getContext('2d'); |
| 188 | ctx.save(); |
| 189 | ctx.translate(x, y); |
| 190 | ctx.rotate(theta); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 191 | ctx.beginPath(); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 192 | ctx.moveTo(0.5, 0.5); |
| 193 | ctx.lineTo(0, 0); |
James Kuszmaul | 71f9121 | 2021-09-25 17:35:48 -0700 | [diff] [blame^] | 194 | ctx.lineTo(100.0, 0); |
| 195 | ctx.lineTo(0, 0); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 196 | ctx.lineTo(0.5, -0.5); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 197 | ctx.stroke(); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 198 | ctx.beginPath(); |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 199 | ctx.arc(0, 0, 0.25, -Math.PI / 4, Math.PI / 4); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 200 | ctx.stroke(); |
| 201 | ctx.restore(); |
| 202 | } |
| 203 | |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 204 | drawRobot(x: number, y: number, theta: number, turret: number|null): void { |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 205 | const ctx = this.canvas.getContext('2d'); |
| 206 | ctx.save(); |
| 207 | ctx.translate(x, y); |
| 208 | ctx.rotate(theta); |
| 209 | ctx.rect(-ROBOT_LENGTH / 2, -ROBOT_WIDTH / 2, ROBOT_LENGTH, ROBOT_WIDTH); |
| 210 | ctx.stroke(); |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 211 | if (turret) { |
| 212 | ctx.save(); |
| 213 | ctx.rotate(turret + Math.PI); |
| 214 | const turretRadius = ROBOT_WIDTH / 4.0; |
| 215 | ctx.strokeStyle = "red"; |
| 216 | // Draw circle for turret. |
| 217 | ctx.beginPath(); |
| 218 | ctx.arc(0, 0, turretRadius, 0, 2.0 * Math.PI); |
| 219 | ctx.stroke(); |
| 220 | // Draw line in circle to show forwards. |
| 221 | ctx.beginPath(); |
| 222 | ctx.moveTo(0, 0); |
James Kuszmaul | 71f9121 | 2021-09-25 17:35:48 -0700 | [diff] [blame^] | 223 | ctx.lineTo(1000.0 * turretRadius, 0); |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 224 | ctx.stroke(); |
| 225 | ctx.restore(); |
| 226 | } |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 227 | ctx.beginPath(); |
| 228 | ctx.moveTo(0, 0); |
James Kuszmaul | 71f9121 | 2021-09-25 17:35:48 -0700 | [diff] [blame^] | 229 | ctx.lineTo(100.0 * ROBOT_LENGTH / 2, 0); |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 230 | ctx.stroke(); |
| 231 | ctx.restore(); |
| 232 | } |
| 233 | |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 234 | draw(): void { |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 235 | this.reset(); |
| 236 | this.drawField(); |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 237 | // draw cameras |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 238 | for (const keyPair of this.imageMatchResult) { |
| 239 | const value = keyPair[1]; |
| 240 | for (let i = 0; i < value.cameraPosesLength(); i++) { |
| 241 | const pose = value.cameraPoses(i); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 242 | const mat = pose.fieldToCamera(); |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 243 | // Matrix layout: |
| 244 | // [0, 1, 2, 3] |
| 245 | // [4, 5, 6, 7] |
| 246 | // [8, 9, 10, 11] |
| 247 | // [12, 13, 14, 15] |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 248 | const x = mat.data(3); |
| 249 | const y = mat.data(7); |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 250 | const theta = Math.atan2(mat.data(6), mat.data(2)); |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 251 | this.drawCamera(x, y, theta); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 255 | if (this.drivetrainStatus) { |
| 256 | this.drawRobot( |
| 257 | this.drivetrainStatus.x(), this.drivetrainStatus.y(), |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 258 | this.drivetrainStatus.theta(), |
| 259 | this.superstructureStatus ? |
| 260 | this.superstructureStatus.turret().position() : |
| 261 | null); |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 264 | window.requestAnimationFrame(() => this.draw()); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | reset(): void { |
| 268 | const ctx = this.canvas.getContext('2d'); |
| 269 | ctx.setTransform(1, 0, 0, 1, 0, 0); |
| 270 | const size = window.innerHeight * 0.9; |
| 271 | ctx.canvas.height = size; |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 272 | const width = size / 2 + 20; |
| 273 | ctx.canvas.width = width; |
| 274 | ctx.clearRect(0, 0, size, width); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 275 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 276 | // Translate to center of display. |
| 277 | ctx.translate(width / 2, size / 2); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 278 | // Coordinate system is: |
| 279 | // x -> forward. |
| 280 | // y -> to the left. |
| 281 | ctx.rotate(-Math.PI / 2); |
| 282 | ctx.scale(1, -1); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 283 | |
| 284 | const M_TO_PX = (size - 10) / FIELD_LENGTH; |
| 285 | ctx.scale(M_TO_PX, M_TO_PX); |
| 286 | ctx.lineWidth = 1 / M_TO_PX; |
| 287 | } |
| 288 | } |