Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 1 | import {Configuration, Channel} from 'aos/configuration_generated'; |
| 2 | import {Connection} from 'aos/network/www/proxy'; |
| 3 | import {Connect} from 'aos/network/connect_generated'; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 4 | import {FIELD_LENGTH, FIELD_WIDTH, FT_TO_M, IN_TO_M} from './constants'; |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 5 | import {ImageMatchResult} from 'y2020/vision/sift/sift_generated' |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 6 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 7 | // (0,0) is field center, +X is toward red DS |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 8 | const FIELD_SIDE_Y = FIELD_WIDTH / 2; |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 9 | const FIELD_EDGE_X = FIELD_LENGTH / 2; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 10 | |
| 11 | const DS_WIDTH = 69 * IN_TO_M; |
| 12 | const DS_ANGLE = 20 * Math.PI / 180; |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 13 | 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] | 14 | const DS_INSIDE_Y = FIELD_SIDE_Y - DS_WIDTH * Math.cos(DS_ANGLE); |
| 15 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 16 | const TRENCH_X = 108 * IN_TO_M; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 17 | const TRENCH_WIDTH = 55.5 * IN_TO_M; |
| 18 | const TRENCH_INSIDE = FIELD_SIDE_Y - TRENCH_WIDTH; |
| 19 | |
| 20 | const SPINNER_LENGTH = 30 * IN_TO_M; |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 21 | const SPINNER_TOP_X = 374.59 * IN_TO_M - FIELD_EDGE_X; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 22 | const SPINNER_BOTTOM_X = SPINNER_TOP_X - SPINNER_LENGTH; |
| 23 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 24 | const SHIELD_BOTTOM_X = -116 * IN_TO_M; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 25 | const SHIELD_BOTTOM_Y = 43.75 * IN_TO_M; |
| 26 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 27 | const SHIELD_TOP_X = 116 * IN_TO_M; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 28 | const SHIELD_TOP_Y = -43.75 * IN_TO_M; |
| 29 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 30 | const SHIELD_RIGHT_X = -51.06 * IN_TO_M; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 31 | const SHIELD_RIGHT_Y = -112.88 * IN_TO_M; |
| 32 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 33 | const SHIELD_LEFT_X = 51.06 * IN_TO_M; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 34 | const SHIELD_LEFT_Y = 112.88 * IN_TO_M; |
| 35 | |
| 36 | const SHIELD_CENTER_TOP_X = (SHIELD_TOP_X + SHIELD_LEFT_X) / 2 |
| 37 | const SHIELD_CENTER_TOP_Y = (SHIELD_TOP_Y + SHIELD_LEFT_Y) / 2 |
| 38 | |
| 39 | const SHIELD_CENTER_BOTTOM_X = (SHIELD_BOTTOM_X + SHIELD_RIGHT_X) / 2 |
| 40 | const SHIELD_CENTER_BOTTOM_Y = (SHIELD_BOTTOM_Y + SHIELD_RIGHT_Y) / 2 |
| 41 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 42 | const INITIATION_X = FIELD_EDGE_X - 120 * IN_TO_M; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 43 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 44 | const TARGET_ZONE_TIP_X = FIELD_EDGE_X - 30 * IN_TO_M; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 45 | const TARGET_ZONE_WIDTH = 48 * IN_TO_M; |
| 46 | const LOADING_ZONE_WIDTH = 60 * IN_TO_M; |
| 47 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 48 | /** |
| 49 | * All the messages that are required to display camera information on the field. |
| 50 | * Messages not readable on the server node are ignored. |
| 51 | */ |
| 52 | const REQUIRED_CHANNELS = [ |
| 53 | { |
| 54 | name: '/pi1/camera', |
| 55 | type: 'frc971.vision.sift.ImageMatchResult', |
| 56 | }, |
| 57 | { |
| 58 | name: '/pi2/camera', |
| 59 | type: 'frc971.vision.sift.ImageMatchResult', |
| 60 | }, |
| 61 | { |
| 62 | name: '/pi3/camera', |
| 63 | type: 'frc971.vision.sift.ImageMatchResult', |
| 64 | }, |
| 65 | { |
| 66 | name: '/pi4/camera', |
| 67 | type: 'frc971.vision.sift.ImageMatchResult', |
| 68 | }, |
| 69 | { |
| 70 | name: '/pi5/camera', |
| 71 | type: 'frc971.vision.sift.ImageMatchResult', |
| 72 | }, |
| 73 | ]; |
| 74 | |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 75 | export class FieldHandler { |
| 76 | private canvas = document.createElement('canvas'); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 77 | private imageMatchResult :ImageMatchResult|null = null |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 78 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 79 | constructor(private readonly connection: Connection) { |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 80 | document.body.appendChild(this.canvas); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 81 | |
| 82 | this.connection.addConfigHandler(() => { |
| 83 | this.sendConnect(); |
| 84 | }); |
| 85 | this.connection.addHandler(ImageMatchResult.getFullyQualifiedName(), (res) => { |
| 86 | this.handleImageMatchResult(res); |
| 87 | }); |
| 88 | } |
| 89 | |
| 90 | private handleImageMatchResult(data: Uint8Array): void { |
| 91 | const fbBuffer = new flatbuffers.ByteBuffer(data); |
| 92 | this.imageMatchResult = ImageMatchResult.getRootAsImageMatchResult(fbBuffer); |
| 93 | } |
| 94 | |
| 95 | private sendConnect(): void { |
| 96 | const builder = new flatbuffers.Builder(512); |
| 97 | const channels: flatbuffers.Offset[] = []; |
| 98 | for (const channel of REQUIRED_CHANNELS) { |
| 99 | const nameFb = builder.createString(channel.name); |
| 100 | const typeFb = builder.createString(channel.type); |
| 101 | Channel.startChannel(builder); |
| 102 | Channel.addName(builder, nameFb); |
| 103 | Channel.addType(builder, typeFb); |
| 104 | const channelFb = Channel.endChannel(builder); |
| 105 | channels.push(channelFb); |
| 106 | } |
| 107 | |
| 108 | const channelsFb = Connect.createChannelsToTransferVector(builder, channels); |
| 109 | Connect.startConnect(builder); |
| 110 | Connect.addChannelsToTransfer(builder, channelsFb); |
| 111 | const connect = Connect.endConnect(builder); |
| 112 | builder.finish(connect); |
| 113 | this.connection.sendConnectMessage(builder); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 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); |
| 194 | ctx.lineTo(0.5, -0.5); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 195 | ctx.stroke(); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 196 | ctx.beginPath(); |
| 197 | ctx.arc(0, 0, 0.25, -Math.PI/4, Math.PI/4); |
| 198 | ctx.stroke(); |
| 199 | ctx.restore(); |
| 200 | } |
| 201 | |
| 202 | draw(): void { |
| 203 | this.reset(); |
| 204 | this.drawField(); |
| 205 | //draw cameras |
| 206 | if (this.imageMatchResult) { |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 207 | for (const i = 0; i < this.imageMatchResult.cameraPosesLength(); i++) { |
| 208 | const pose = this.imageMatchResult.cameraPoses(i); |
| 209 | const mat = pose.fieldToCamera(); |
| 210 | const x = mat.data(3); |
| 211 | const y = mat.data(7); |
| 212 | this.drawCamera(x, y, 0); |
| 213 | console.log(x, y); |
| 214 | } |
| 215 | } |
| 216 | |
| 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 | } |