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'; |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 8 | |
| 9 | import DrivetrainStatus = drivetrain.frc971.control_loops.drivetrain.Status; |
| 10 | import ImageMatchResult = sift.frc971.vision.sift.ImageMatchResult; |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 11 | import Channel = configuration.aos.Channel; |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame^] | 12 | import SubscriberRequest = web_proxy.aos.web_proxy.SubscriberRequest; |
| 13 | import ChannelRequest = web_proxy.aos.web_proxy.ChannelRequest; |
| 14 | import TransferMethod = web_proxy.aos.web_proxy.TransferMethod; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 15 | |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 16 | import {FIELD_LENGTH, FIELD_WIDTH, FT_TO_M, IN_TO_M} from './constants'; |
| 17 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 18 | // (0,0) is field center, +X is toward red DS |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 19 | const FIELD_SIDE_Y = FIELD_WIDTH / 2; |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 20 | const FIELD_EDGE_X = FIELD_LENGTH / 2; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 21 | |
| 22 | const DS_WIDTH = 69 * IN_TO_M; |
| 23 | const DS_ANGLE = 20 * Math.PI / 180; |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 24 | 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] | 25 | const DS_INSIDE_Y = FIELD_SIDE_Y - DS_WIDTH * Math.cos(DS_ANGLE); |
| 26 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 27 | const TRENCH_X = 108 * IN_TO_M; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 28 | const TRENCH_WIDTH = 55.5 * IN_TO_M; |
| 29 | const TRENCH_INSIDE = FIELD_SIDE_Y - TRENCH_WIDTH; |
| 30 | |
| 31 | const SPINNER_LENGTH = 30 * IN_TO_M; |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 32 | const SPINNER_TOP_X = 374.59 * IN_TO_M - FIELD_EDGE_X; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 33 | const SPINNER_BOTTOM_X = SPINNER_TOP_X - SPINNER_LENGTH; |
| 34 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 35 | const SHIELD_BOTTOM_X = -116 * IN_TO_M; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 36 | const SHIELD_BOTTOM_Y = 43.75 * IN_TO_M; |
| 37 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 38 | const SHIELD_TOP_X = 116 * IN_TO_M; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 39 | const SHIELD_TOP_Y = -43.75 * IN_TO_M; |
| 40 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 41 | const SHIELD_RIGHT_X = -51.06 * IN_TO_M; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 42 | const SHIELD_RIGHT_Y = -112.88 * IN_TO_M; |
| 43 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 44 | const SHIELD_LEFT_X = 51.06 * IN_TO_M; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 45 | const SHIELD_LEFT_Y = 112.88 * IN_TO_M; |
| 46 | |
| 47 | const SHIELD_CENTER_TOP_X = (SHIELD_TOP_X + SHIELD_LEFT_X) / 2 |
| 48 | const SHIELD_CENTER_TOP_Y = (SHIELD_TOP_Y + SHIELD_LEFT_Y) / 2 |
| 49 | |
| 50 | const SHIELD_CENTER_BOTTOM_X = (SHIELD_BOTTOM_X + SHIELD_RIGHT_X) / 2 |
| 51 | const SHIELD_CENTER_BOTTOM_Y = (SHIELD_BOTTOM_Y + SHIELD_RIGHT_Y) / 2 |
| 52 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 53 | const INITIATION_X = FIELD_EDGE_X - 120 * IN_TO_M; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 54 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 55 | const TARGET_ZONE_TIP_X = FIELD_EDGE_X - 30 * IN_TO_M; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 56 | const TARGET_ZONE_WIDTH = 48 * IN_TO_M; |
| 57 | const LOADING_ZONE_WIDTH = 60 * IN_TO_M; |
| 58 | |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 59 | const ROBOT_WIDTH = 28 * IN_TO_M; |
| 60 | const ROBOT_LENGTH = 30 * IN_TO_M; |
| 61 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 62 | /** |
| 63 | * All the messages that are required to display camera information on the field. |
| 64 | * Messages not readable on the server node are ignored. |
| 65 | */ |
| 66 | const REQUIRED_CHANNELS = [ |
| 67 | { |
| 68 | name: '/pi1/camera', |
| 69 | type: 'frc971.vision.sift.ImageMatchResult', |
| 70 | }, |
| 71 | { |
| 72 | name: '/pi2/camera', |
| 73 | type: 'frc971.vision.sift.ImageMatchResult', |
| 74 | }, |
| 75 | { |
| 76 | name: '/pi3/camera', |
| 77 | type: 'frc971.vision.sift.ImageMatchResult', |
| 78 | }, |
| 79 | { |
| 80 | name: '/pi4/camera', |
| 81 | type: 'frc971.vision.sift.ImageMatchResult', |
| 82 | }, |
| 83 | { |
| 84 | name: '/pi5/camera', |
| 85 | type: 'frc971.vision.sift.ImageMatchResult', |
| 86 | }, |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 87 | { |
| 88 | name: '/drivetrain', |
| 89 | type: 'frc971.control_loops.drivetrain.Status', |
| 90 | }, |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 91 | ]; |
| 92 | |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 93 | export class FieldHandler { |
| 94 | private canvas = document.createElement('canvas'); |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 95 | private imageMatchResult: ImageMatchResult|null = null; |
Philipp Schrader | a227d04 | 2020-11-14 17:33:52 -0800 | [diff] [blame] | 96 | private drivetrainStatus: DrivetrainStatus|null = null; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 97 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 98 | constructor(private readonly connection: Connection) { |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 99 | document.body.appendChild(this.canvas); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 100 | |
| 101 | this.connection.addConfigHandler(() => { |
| 102 | this.sendConnect(); |
| 103 | }); |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 104 | this.connection.addHandler( |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 105 | ImageMatchResult.getFullyQualifiedName(), (res) => { |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 106 | this.handleImageMatchResult(res); |
| 107 | }); |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 108 | this.connection.addHandler( |
| 109 | DrivetrainStatus.getFullyQualifiedName(), (data) => { |
| 110 | this.handleDrivetrainStatus(data); |
| 111 | }); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | private handleImageMatchResult(data: Uint8Array): void { |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 115 | const fbBuffer = new ByteBuffer(data); |
| 116 | this.imageMatchResult = ImageMatchResult.getRootAsImageMatchResult( |
| 117 | fbBuffer as unknown as flatbuffers.ByteBuffer); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 120 | private handleDrivetrainStatus(data: Uint8Array): void { |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 121 | const fbBuffer = new ByteBuffer(data); |
| 122 | this.drivetrainStatus = DrivetrainStatus.getRootAsStatus( |
| 123 | fbBuffer as unknown as flatbuffers.ByteBuffer); |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 126 | private sendConnect(): void { |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 127 | const builder = |
| 128 | new flatbuffers_builder.Builder(512) as unknown as flatbuffers.Builder; |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 129 | const channels: flatbuffers.Offset[] = []; |
| 130 | for (const channel of REQUIRED_CHANNELS) { |
| 131 | const nameFb = builder.createString(channel.name); |
| 132 | const typeFb = builder.createString(channel.type); |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 133 | Channel.startChannel(builder); |
| 134 | Channel.addName(builder, nameFb); |
| 135 | Channel.addType(builder, typeFb); |
| 136 | const channelFb = Channel.endChannel(builder); |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame^] | 137 | ChannelRequest.startChannelRequest(builder); |
| 138 | ChannelRequest.addChannel(builder, channelFb); |
| 139 | ChannelRequest.addMethod(builder, TransferMethod.SUBSAMPLE); |
| 140 | channels.push(ChannelRequest.endChannelRequest(builder)); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 143 | const channelsFb = |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame^] | 144 | SubscriberRequest.createChannelsToTransferVector(builder, channels); |
| 145 | SubscriberRequest.startSubscriberRequest(builder); |
| 146 | SubscriberRequest.addChannelsToTransfer(builder, channelsFb); |
| 147 | const connect = SubscriberRequest.endSubscriberRequest(builder); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 148 | builder.finish(connect); |
| 149 | this.connection.sendConnectMessage(builder); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | drawField(): void { |
| 153 | const MY_COLOR = 'red'; |
| 154 | const OTHER_COLOR = 'blue'; |
| 155 | const ctx = this.canvas.getContext('2d'); |
| 156 | // draw perimiter |
| 157 | ctx.beginPath(); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 158 | ctx.moveTo(FIELD_EDGE_X, DS_INSIDE_Y); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 159 | ctx.lineTo(DS_END_X, FIELD_SIDE_Y); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 160 | ctx.lineTo(-DS_END_X, FIELD_SIDE_Y); |
| 161 | ctx.lineTo(-FIELD_EDGE_X, DS_INSIDE_Y); |
| 162 | ctx.lineTo(-FIELD_EDGE_X, -DS_INSIDE_Y); |
| 163 | ctx.lineTo(-DS_END_X, -FIELD_SIDE_Y); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 164 | ctx.lineTo(DS_END_X, -FIELD_SIDE_Y); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 165 | ctx.lineTo(FIELD_EDGE_X, -DS_INSIDE_Y); |
| 166 | ctx.lineTo(FIELD_EDGE_X, DS_INSIDE_Y); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 167 | ctx.stroke(); |
| 168 | |
| 169 | // draw shield generator |
| 170 | ctx.beginPath(); |
| 171 | ctx.moveTo(SHIELD_BOTTOM_X, SHIELD_BOTTOM_Y); |
| 172 | ctx.lineTo(SHIELD_RIGHT_X, SHIELD_RIGHT_Y); |
| 173 | ctx.lineTo(SHIELD_TOP_X, SHIELD_TOP_Y); |
| 174 | ctx.lineTo(SHIELD_LEFT_X, SHIELD_LEFT_Y); |
| 175 | ctx.lineTo(SHIELD_BOTTOM_X, SHIELD_BOTTOM_Y); |
| 176 | ctx.moveTo(SHIELD_CENTER_TOP_X, SHIELD_CENTER_TOP_Y); |
| 177 | ctx.lineTo(SHIELD_CENTER_BOTTOM_X, SHIELD_CENTER_BOTTOM_Y); |
| 178 | ctx.stroke(); |
| 179 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 180 | this.drawHalfField(ctx, 'red'); |
| 181 | ctx.rotate(Math.PI); |
| 182 | this.drawHalfField(ctx, 'blue'); |
| 183 | ctx.rotate(Math.PI); |
| 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 | drawHalfField(ctx, color: string): void { |
| 187 | // trenches |
| 188 | ctx.strokeStyle = color; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 189 | ctx.beginPath(); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 190 | ctx.moveTo(TRENCH_X, FIELD_SIDE_Y); |
| 191 | ctx.lineTo(TRENCH_X, TRENCH_INSIDE); |
| 192 | ctx.lineTo(-TRENCH_X, TRENCH_INSIDE); |
| 193 | ctx.lineTo(-TRENCH_X, FIELD_SIDE_Y); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 194 | ctx.stroke(); |
| 195 | |
| 196 | ctx.strokeStyle = 'black'; |
| 197 | ctx.beginPath(); |
| 198 | ctx.moveTo(SPINNER_TOP_X, FIELD_SIDE_Y); |
| 199 | ctx.lineTo(SPINNER_TOP_X, TRENCH_INSIDE); |
| 200 | ctx.lineTo(SPINNER_BOTTOM_X, TRENCH_INSIDE); |
| 201 | ctx.lineTo(SPINNER_BOTTOM_X, FIELD_SIDE_Y); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 202 | ctx.stroke(); |
| 203 | |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 204 | ctx.beginPath(); |
| 205 | ctx.moveTo(INITIATION_X, FIELD_SIDE_Y); |
| 206 | ctx.lineTo(INITIATION_X, -FIELD_SIDE_Y); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 207 | ctx.stroke(); |
| 208 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 209 | // target/loading |
| 210 | ctx.strokeStyle = color; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 211 | ctx.beginPath(); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 212 | ctx.moveTo(FIELD_EDGE_X, DS_INSIDE_Y); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 213 | 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] | 214 | ctx.lineTo(FIELD_EDGE_X, DS_INSIDE_Y - TARGET_ZONE_WIDTH); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 215 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 216 | ctx.moveTo(-FIELD_EDGE_X, DS_INSIDE_Y); |
| 217 | ctx.lineTo(-TARGET_ZONE_TIP_X, DS_INSIDE_Y - 0.5 * LOADING_ZONE_WIDTH); |
| 218 | ctx.lineTo(-FIELD_EDGE_X, DS_INSIDE_Y - LOADING_ZONE_WIDTH); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 219 | ctx.stroke(); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 220 | } |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 221 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 222 | drawCamera(x: number, y: number, theta: number): void { |
| 223 | const ctx = this.canvas.getContext('2d'); |
| 224 | ctx.save(); |
| 225 | ctx.translate(x, y); |
| 226 | ctx.rotate(theta); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 227 | ctx.beginPath(); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 228 | ctx.moveTo(0.5, 0.5); |
| 229 | ctx.lineTo(0, 0); |
| 230 | ctx.lineTo(0.5, -0.5); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 231 | ctx.stroke(); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 232 | ctx.beginPath(); |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 233 | ctx.arc(0, 0, 0.25, -Math.PI / 4, Math.PI / 4); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 234 | ctx.stroke(); |
| 235 | ctx.restore(); |
| 236 | } |
| 237 | |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 238 | drawRobot(x: number, y: number, theta: number): void { |
| 239 | const ctx = this.canvas.getContext('2d'); |
| 240 | ctx.save(); |
| 241 | ctx.translate(x, y); |
| 242 | ctx.rotate(theta); |
| 243 | ctx.rect(-ROBOT_LENGTH / 2, -ROBOT_WIDTH / 2, ROBOT_LENGTH, ROBOT_WIDTH); |
| 244 | ctx.stroke(); |
| 245 | ctx.beginPath(); |
| 246 | ctx.moveTo(0, 0); |
| 247 | ctx.lineTo(ROBOT_LENGTH / 2, 0); |
| 248 | ctx.stroke(); |
| 249 | ctx.restore(); |
| 250 | } |
| 251 | |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 252 | draw(): void { |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 253 | this.reset(); |
| 254 | this.drawField(); |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 255 | // draw cameras |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 256 | if (this.imageMatchResult) { |
Philipp Schrader | a227d04 | 2020-11-14 17:33:52 -0800 | [diff] [blame] | 257 | for (let i = 0; i < this.imageMatchResult.cameraPosesLength(); i++) { |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 258 | const pose = this.imageMatchResult.cameraPoses(i); |
| 259 | const mat = pose.fieldToCamera(); |
| 260 | const x = mat.data(3); |
| 261 | const y = mat.data(7); |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 262 | const theta = Math.atan2( |
| 263 | -mat.data(8), |
| 264 | Math.sqrt(Math.pow(mat.data(9), 2) + Math.pow(mat.data(10), 2))); |
| 265 | this.drawCamera(x, y, theta); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 269 | if (this.drivetrainStatus) { |
| 270 | this.drawRobot( |
| 271 | this.drivetrainStatus.x(), this.drivetrainStatus.y(), |
| 272 | this.drivetrainStatus.theta()); |
| 273 | } |
| 274 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 275 | window.requestAnimationFrame(() => this.draw()); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | reset(): void { |
| 279 | const ctx = this.canvas.getContext('2d'); |
| 280 | ctx.setTransform(1, 0, 0, 1, 0, 0); |
| 281 | const size = window.innerHeight * 0.9; |
| 282 | ctx.canvas.height = size; |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 283 | const width = size / 2 + 20; |
| 284 | ctx.canvas.width = width; |
| 285 | ctx.clearRect(0, 0, size, width); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 286 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 287 | // Translate to center of display. |
| 288 | ctx.translate(width / 2, size / 2); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 289 | // Coordinate system is: |
| 290 | // x -> forward. |
| 291 | // y -> to the left. |
| 292 | ctx.rotate(-Math.PI / 2); |
| 293 | ctx.scale(1, -1); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 294 | |
| 295 | const M_TO_PX = (size - 10) / FIELD_LENGTH; |
| 296 | ctx.scale(M_TO_PX, M_TO_PX); |
| 297 | ctx.lineWidth = 1 / M_TO_PX; |
| 298 | } |
| 299 | } |