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 | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 62 | export class FieldHandler { |
| 63 | private canvas = document.createElement('canvas'); |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 64 | private imageMatchResult: ImageMatchResult|null = null; |
Philipp Schrader | a227d04 | 2020-11-14 17:33:52 -0800 | [diff] [blame] | 65 | private drivetrainStatus: DrivetrainStatus|null = null; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 66 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 67 | constructor(private readonly connection: Connection) { |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 68 | document.body.appendChild(this.canvas); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 69 | |
| 70 | this.connection.addConfigHandler(() => { |
James Kuszmaul | 527038a | 2020-12-21 23:40:44 -0800 | [diff] [blame^] | 71 | this.connection.addHandler( |
| 72 | '/camera', ImageMatchResult.getFullyQualifiedName(), (res) => { |
| 73 | this.handleImageMatchResult(res); |
| 74 | }); |
| 75 | this.connection.addHandler( |
| 76 | '/drivetrain', DrivetrainStatus.getFullyQualifiedName(), (data) => { |
| 77 | this.handleDrivetrainStatus(data); |
| 78 | }); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 79 | }); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | private handleImageMatchResult(data: Uint8Array): void { |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 83 | const fbBuffer = new ByteBuffer(data); |
| 84 | this.imageMatchResult = ImageMatchResult.getRootAsImageMatchResult( |
| 85 | fbBuffer as unknown as flatbuffers.ByteBuffer); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 88 | private handleDrivetrainStatus(data: Uint8Array): void { |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 89 | const fbBuffer = new ByteBuffer(data); |
| 90 | this.drivetrainStatus = DrivetrainStatus.getRootAsStatus( |
| 91 | fbBuffer as unknown as flatbuffers.ByteBuffer); |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 94 | drawField(): void { |
| 95 | const MY_COLOR = 'red'; |
| 96 | const OTHER_COLOR = 'blue'; |
| 97 | const ctx = this.canvas.getContext('2d'); |
| 98 | // draw perimiter |
| 99 | ctx.beginPath(); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 100 | ctx.moveTo(FIELD_EDGE_X, DS_INSIDE_Y); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 101 | ctx.lineTo(DS_END_X, FIELD_SIDE_Y); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 102 | ctx.lineTo(-DS_END_X, FIELD_SIDE_Y); |
| 103 | ctx.lineTo(-FIELD_EDGE_X, DS_INSIDE_Y); |
| 104 | ctx.lineTo(-FIELD_EDGE_X, -DS_INSIDE_Y); |
| 105 | ctx.lineTo(-DS_END_X, -FIELD_SIDE_Y); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 106 | ctx.lineTo(DS_END_X, -FIELD_SIDE_Y); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 107 | ctx.lineTo(FIELD_EDGE_X, -DS_INSIDE_Y); |
| 108 | ctx.lineTo(FIELD_EDGE_X, DS_INSIDE_Y); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 109 | ctx.stroke(); |
| 110 | |
| 111 | // draw shield generator |
| 112 | ctx.beginPath(); |
| 113 | ctx.moveTo(SHIELD_BOTTOM_X, SHIELD_BOTTOM_Y); |
| 114 | ctx.lineTo(SHIELD_RIGHT_X, SHIELD_RIGHT_Y); |
| 115 | ctx.lineTo(SHIELD_TOP_X, SHIELD_TOP_Y); |
| 116 | ctx.lineTo(SHIELD_LEFT_X, SHIELD_LEFT_Y); |
| 117 | ctx.lineTo(SHIELD_BOTTOM_X, SHIELD_BOTTOM_Y); |
| 118 | ctx.moveTo(SHIELD_CENTER_TOP_X, SHIELD_CENTER_TOP_Y); |
| 119 | ctx.lineTo(SHIELD_CENTER_BOTTOM_X, SHIELD_CENTER_BOTTOM_Y); |
| 120 | ctx.stroke(); |
| 121 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 122 | this.drawHalfField(ctx, 'red'); |
| 123 | ctx.rotate(Math.PI); |
| 124 | this.drawHalfField(ctx, 'blue'); |
| 125 | ctx.rotate(Math.PI); |
| 126 | } |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 127 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 128 | drawHalfField(ctx, color: string): void { |
| 129 | // trenches |
| 130 | ctx.strokeStyle = color; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 131 | ctx.beginPath(); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 132 | ctx.moveTo(TRENCH_X, FIELD_SIDE_Y); |
| 133 | ctx.lineTo(TRENCH_X, TRENCH_INSIDE); |
| 134 | ctx.lineTo(-TRENCH_X, TRENCH_INSIDE); |
| 135 | ctx.lineTo(-TRENCH_X, FIELD_SIDE_Y); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 136 | ctx.stroke(); |
| 137 | |
| 138 | ctx.strokeStyle = 'black'; |
| 139 | ctx.beginPath(); |
| 140 | ctx.moveTo(SPINNER_TOP_X, FIELD_SIDE_Y); |
| 141 | ctx.lineTo(SPINNER_TOP_X, TRENCH_INSIDE); |
| 142 | ctx.lineTo(SPINNER_BOTTOM_X, TRENCH_INSIDE); |
| 143 | ctx.lineTo(SPINNER_BOTTOM_X, FIELD_SIDE_Y); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 144 | ctx.stroke(); |
| 145 | |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 146 | ctx.beginPath(); |
| 147 | ctx.moveTo(INITIATION_X, FIELD_SIDE_Y); |
| 148 | ctx.lineTo(INITIATION_X, -FIELD_SIDE_Y); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 149 | ctx.stroke(); |
| 150 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 151 | // target/loading |
| 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(FIELD_EDGE_X, DS_INSIDE_Y); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 155 | 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] | 156 | ctx.lineTo(FIELD_EDGE_X, DS_INSIDE_Y - TARGET_ZONE_WIDTH); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 157 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 158 | ctx.moveTo(-FIELD_EDGE_X, DS_INSIDE_Y); |
| 159 | ctx.lineTo(-TARGET_ZONE_TIP_X, DS_INSIDE_Y - 0.5 * LOADING_ZONE_WIDTH); |
| 160 | ctx.lineTo(-FIELD_EDGE_X, DS_INSIDE_Y - LOADING_ZONE_WIDTH); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 161 | ctx.stroke(); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 162 | } |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 163 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 164 | drawCamera(x: number, y: number, theta: number): void { |
| 165 | const ctx = this.canvas.getContext('2d'); |
| 166 | ctx.save(); |
| 167 | ctx.translate(x, y); |
| 168 | ctx.rotate(theta); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 169 | ctx.beginPath(); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 170 | ctx.moveTo(0.5, 0.5); |
| 171 | ctx.lineTo(0, 0); |
| 172 | ctx.lineTo(0.5, -0.5); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 173 | ctx.stroke(); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 174 | ctx.beginPath(); |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 175 | ctx.arc(0, 0, 0.25, -Math.PI / 4, Math.PI / 4); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 176 | ctx.stroke(); |
| 177 | ctx.restore(); |
| 178 | } |
| 179 | |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 180 | drawRobot(x: number, y: number, theta: number): void { |
| 181 | const ctx = this.canvas.getContext('2d'); |
| 182 | ctx.save(); |
| 183 | ctx.translate(x, y); |
| 184 | ctx.rotate(theta); |
| 185 | ctx.rect(-ROBOT_LENGTH / 2, -ROBOT_WIDTH / 2, ROBOT_LENGTH, ROBOT_WIDTH); |
| 186 | ctx.stroke(); |
| 187 | ctx.beginPath(); |
| 188 | ctx.moveTo(0, 0); |
| 189 | ctx.lineTo(ROBOT_LENGTH / 2, 0); |
| 190 | ctx.stroke(); |
| 191 | ctx.restore(); |
| 192 | } |
| 193 | |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 194 | draw(): void { |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 195 | this.reset(); |
| 196 | this.drawField(); |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 197 | // draw cameras |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 198 | if (this.imageMatchResult) { |
Philipp Schrader | a227d04 | 2020-11-14 17:33:52 -0800 | [diff] [blame] | 199 | for (let i = 0; i < this.imageMatchResult.cameraPosesLength(); i++) { |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 200 | const pose = this.imageMatchResult.cameraPoses(i); |
| 201 | const mat = pose.fieldToCamera(); |
| 202 | const x = mat.data(3); |
| 203 | const y = mat.data(7); |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 204 | const theta = Math.atan2( |
| 205 | -mat.data(8), |
| 206 | Math.sqrt(Math.pow(mat.data(9), 2) + Math.pow(mat.data(10), 2))); |
| 207 | this.drawCamera(x, y, theta); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 211 | if (this.drivetrainStatus) { |
| 212 | this.drawRobot( |
| 213 | this.drivetrainStatus.x(), this.drivetrainStatus.y(), |
| 214 | this.drivetrainStatus.theta()); |
| 215 | } |
| 216 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 217 | window.requestAnimationFrame(() => this.draw()); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | reset(): void { |
| 221 | const ctx = this.canvas.getContext('2d'); |
| 222 | ctx.setTransform(1, 0, 0, 1, 0, 0); |
| 223 | const size = window.innerHeight * 0.9; |
| 224 | ctx.canvas.height = size; |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 225 | const width = size / 2 + 20; |
| 226 | ctx.canvas.width = width; |
| 227 | ctx.clearRect(0, 0, size, width); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 228 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 229 | // Translate to center of display. |
| 230 | ctx.translate(width / 2, size / 2); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 231 | // Coordinate system is: |
| 232 | // x -> forward. |
| 233 | // y -> to the left. |
| 234 | ctx.rotate(-Math.PI / 2); |
| 235 | ctx.scale(1, -1); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 236 | |
| 237 | const M_TO_PX = (size - 10) / FIELD_LENGTH; |
| 238 | ctx.scale(M_TO_PX, M_TO_PX); |
| 239 | ctx.lineWidth = 1 / M_TO_PX; |
| 240 | } |
| 241 | } |