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; |
Austin Schuh | 840132b | 2021-10-17 17:40:14 -0700 | [diff] [blame^] | 70 | private x: HTMLDivElement = (document.getElementById('x') as HTMLDivElement); |
| 71 | private y: HTMLDivElement = (document.getElementById('y') as HTMLDivElement); |
| 72 | private theta: HTMLDivElement = (document.getElementById('theta') as HTMLDivElement); |
| 73 | private shotDistance: HTMLDivElement = (document.getElementById('shot_distance') as HTMLDivElement); |
| 74 | private finisher: HTMLDivElement = (document.getElementById('finisher') as HTMLDivElement); |
| 75 | private leftAccelerator: HTMLDivElement = (document.getElementById('left_accelerator') as HTMLDivElement); |
| 76 | private rightAccelerator: HTMLDivElement = (document.getElementById('right_accelerator') as HTMLDivElement); |
| 77 | private innerPort: HTMLDivElement = (document.getElementById('inner_port') as HTMLDivElement); |
| 78 | private hood: HTMLDivElement = (document.getElementById('hood') as HTMLDivElement); |
| 79 | private turret: HTMLDivElement = (document.getElementById('turret') as HTMLDivElement); |
| 80 | private intake: HTMLDivElement = (document.getElementById('intake') as HTMLDivElement); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 81 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 82 | constructor(private readonly connection: Connection) { |
Austin Schuh | 840132b | 2021-10-17 17:40:14 -0700 | [diff] [blame^] | 83 | (document.getElementById('field') as HTMLElement).appendChild(this.canvas); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 84 | |
| 85 | this.connection.addConfigHandler(() => { |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 86 | // Go through and register handlers for both all the individual pis as |
| 87 | // well as the local pi. Depending on the node that we are running on, |
| 88 | // different subsets of these will be available. |
| 89 | for (const prefix of ['', '/pi1', '/pi2', '/pi3', '/pi4']) { |
| 90 | this.connection.addHandler( |
| 91 | prefix + '/camera', ImageMatchResult.getFullyQualifiedName(), (res) => { |
| 92 | this.handleImageMatchResult(prefix, res); |
| 93 | }); |
| 94 | } |
James Kuszmaul | 527038a | 2020-12-21 23:40:44 -0800 | [diff] [blame] | 95 | this.connection.addHandler( |
| 96 | '/drivetrain', DrivetrainStatus.getFullyQualifiedName(), (data) => { |
| 97 | this.handleDrivetrainStatus(data); |
| 98 | }); |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 99 | this.connection.addHandler( |
| 100 | '/superstructure', SuperstructureStatus.getFullyQualifiedName(), |
| 101 | (data) => { |
| 102 | this.handleSuperstructureStatus(data); |
| 103 | }); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 104 | }); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 105 | } |
| 106 | |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 107 | private handleImageMatchResult(prefix: string, data: Uint8Array): void { |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 108 | const fbBuffer = new ByteBuffer(data); |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 109 | this.imageMatchResult.set( |
| 110 | prefix, |
| 111 | ImageMatchResult.getRootAsImageMatchResult( |
| 112 | fbBuffer as unknown as flatbuffers.ByteBuffer)); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 115 | private handleDrivetrainStatus(data: Uint8Array): void { |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 116 | const fbBuffer = new ByteBuffer(data); |
| 117 | this.drivetrainStatus = DrivetrainStatus.getRootAsStatus( |
| 118 | fbBuffer as unknown as flatbuffers.ByteBuffer); |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 119 | } |
| 120 | |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 121 | private handleSuperstructureStatus(data: Uint8Array): void { |
| 122 | const fbBuffer = new ByteBuffer(data); |
| 123 | this.superstructureStatus = SuperstructureStatus.getRootAsStatus( |
| 124 | fbBuffer as unknown as flatbuffers.ByteBuffer); |
| 125 | } |
| 126 | |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 127 | drawField(): void { |
| 128 | const MY_COLOR = 'red'; |
| 129 | const OTHER_COLOR = 'blue'; |
| 130 | const ctx = this.canvas.getContext('2d'); |
| 131 | // draw perimiter |
| 132 | ctx.beginPath(); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 133 | ctx.moveTo(FIELD_EDGE_X, DS_INSIDE_Y); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 134 | ctx.lineTo(DS_END_X, FIELD_SIDE_Y); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 135 | ctx.lineTo(-DS_END_X, FIELD_SIDE_Y); |
| 136 | ctx.lineTo(-FIELD_EDGE_X, DS_INSIDE_Y); |
| 137 | ctx.lineTo(-FIELD_EDGE_X, -DS_INSIDE_Y); |
| 138 | ctx.lineTo(-DS_END_X, -FIELD_SIDE_Y); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 139 | ctx.lineTo(DS_END_X, -FIELD_SIDE_Y); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 140 | ctx.lineTo(FIELD_EDGE_X, -DS_INSIDE_Y); |
| 141 | ctx.lineTo(FIELD_EDGE_X, DS_INSIDE_Y); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 142 | ctx.stroke(); |
| 143 | |
| 144 | // draw shield generator |
| 145 | ctx.beginPath(); |
| 146 | ctx.moveTo(SHIELD_BOTTOM_X, SHIELD_BOTTOM_Y); |
| 147 | ctx.lineTo(SHIELD_RIGHT_X, SHIELD_RIGHT_Y); |
| 148 | ctx.lineTo(SHIELD_TOP_X, SHIELD_TOP_Y); |
| 149 | ctx.lineTo(SHIELD_LEFT_X, SHIELD_LEFT_Y); |
| 150 | ctx.lineTo(SHIELD_BOTTOM_X, SHIELD_BOTTOM_Y); |
| 151 | ctx.moveTo(SHIELD_CENTER_TOP_X, SHIELD_CENTER_TOP_Y); |
| 152 | ctx.lineTo(SHIELD_CENTER_BOTTOM_X, SHIELD_CENTER_BOTTOM_Y); |
| 153 | ctx.stroke(); |
| 154 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 155 | this.drawHalfField(ctx, 'red'); |
| 156 | ctx.rotate(Math.PI); |
| 157 | this.drawHalfField(ctx, 'blue'); |
| 158 | ctx.rotate(Math.PI); |
| 159 | } |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 160 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 161 | drawHalfField(ctx, color: string): void { |
| 162 | // trenches |
| 163 | ctx.strokeStyle = color; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 164 | ctx.beginPath(); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 165 | ctx.moveTo(TRENCH_X, FIELD_SIDE_Y); |
| 166 | ctx.lineTo(TRENCH_X, TRENCH_INSIDE); |
| 167 | ctx.lineTo(-TRENCH_X, TRENCH_INSIDE); |
| 168 | ctx.lineTo(-TRENCH_X, FIELD_SIDE_Y); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 169 | ctx.stroke(); |
| 170 | |
| 171 | ctx.strokeStyle = 'black'; |
| 172 | ctx.beginPath(); |
| 173 | ctx.moveTo(SPINNER_TOP_X, FIELD_SIDE_Y); |
| 174 | ctx.lineTo(SPINNER_TOP_X, TRENCH_INSIDE); |
| 175 | ctx.lineTo(SPINNER_BOTTOM_X, TRENCH_INSIDE); |
| 176 | ctx.lineTo(SPINNER_BOTTOM_X, FIELD_SIDE_Y); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 177 | ctx.stroke(); |
| 178 | |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 179 | ctx.beginPath(); |
| 180 | ctx.moveTo(INITIATION_X, FIELD_SIDE_Y); |
| 181 | ctx.lineTo(INITIATION_X, -FIELD_SIDE_Y); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 182 | ctx.stroke(); |
| 183 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 184 | // target/loading |
| 185 | ctx.strokeStyle = color; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 186 | ctx.beginPath(); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 187 | ctx.moveTo(FIELD_EDGE_X, DS_INSIDE_Y); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 188 | 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] | 189 | ctx.lineTo(FIELD_EDGE_X, DS_INSIDE_Y - TARGET_ZONE_WIDTH); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 190 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 191 | ctx.moveTo(-FIELD_EDGE_X, DS_INSIDE_Y); |
| 192 | ctx.lineTo(-TARGET_ZONE_TIP_X, DS_INSIDE_Y - 0.5 * LOADING_ZONE_WIDTH); |
| 193 | ctx.lineTo(-FIELD_EDGE_X, DS_INSIDE_Y - LOADING_ZONE_WIDTH); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 194 | ctx.stroke(); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 195 | } |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 196 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 197 | drawCamera(x: number, y: number, theta: number): void { |
| 198 | const ctx = this.canvas.getContext('2d'); |
| 199 | ctx.save(); |
| 200 | ctx.translate(x, y); |
| 201 | ctx.rotate(theta); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 202 | ctx.beginPath(); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 203 | ctx.moveTo(0.5, 0.5); |
| 204 | ctx.lineTo(0, 0); |
James Kuszmaul | 71f9121 | 2021-09-25 17:35:48 -0700 | [diff] [blame] | 205 | ctx.lineTo(100.0, 0); |
| 206 | ctx.lineTo(0, 0); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 207 | ctx.lineTo(0.5, -0.5); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 208 | ctx.stroke(); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 209 | ctx.beginPath(); |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 210 | ctx.arc(0, 0, 0.25, -Math.PI / 4, Math.PI / 4); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 211 | ctx.stroke(); |
| 212 | ctx.restore(); |
| 213 | } |
| 214 | |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 215 | drawRobot(x: number, y: number, theta: number, turret: number|null): void { |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 216 | const ctx = this.canvas.getContext('2d'); |
| 217 | ctx.save(); |
| 218 | ctx.translate(x, y); |
| 219 | ctx.rotate(theta); |
| 220 | ctx.rect(-ROBOT_LENGTH / 2, -ROBOT_WIDTH / 2, ROBOT_LENGTH, ROBOT_WIDTH); |
| 221 | ctx.stroke(); |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 222 | if (turret) { |
| 223 | ctx.save(); |
| 224 | ctx.rotate(turret + Math.PI); |
| 225 | const turretRadius = ROBOT_WIDTH / 4.0; |
| 226 | ctx.strokeStyle = "red"; |
| 227 | // Draw circle for turret. |
| 228 | ctx.beginPath(); |
| 229 | ctx.arc(0, 0, turretRadius, 0, 2.0 * Math.PI); |
| 230 | ctx.stroke(); |
| 231 | // Draw line in circle to show forwards. |
| 232 | ctx.beginPath(); |
| 233 | ctx.moveTo(0, 0); |
James Kuszmaul | 71f9121 | 2021-09-25 17:35:48 -0700 | [diff] [blame] | 234 | ctx.lineTo(1000.0 * turretRadius, 0); |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 235 | ctx.stroke(); |
| 236 | ctx.restore(); |
| 237 | } |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 238 | ctx.beginPath(); |
| 239 | ctx.moveTo(0, 0); |
James Kuszmaul | 71f9121 | 2021-09-25 17:35:48 -0700 | [diff] [blame] | 240 | ctx.lineTo(100.0 * ROBOT_LENGTH / 2, 0); |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 241 | ctx.stroke(); |
| 242 | ctx.restore(); |
| 243 | } |
| 244 | |
Austin Schuh | 840132b | 2021-10-17 17:40:14 -0700 | [diff] [blame^] | 245 | setZeroing(div: HTMLDivElement): void { |
| 246 | div.innerHTML = "zeroing"; |
| 247 | div.classList.remove("faulted"); |
| 248 | div.classList.add("zeroing"); |
| 249 | } |
| 250 | setEstopped(div: HTMLDivElement): void { |
| 251 | div.innerHTML = "estopped"; |
| 252 | div.classList.add("faulted"); |
| 253 | div.classList.remove("zeroing"); |
| 254 | } |
| 255 | setValue(div: HTMLDivElement, val: Number): void { |
| 256 | div.innerHTML = val.toFixed(4); |
| 257 | div.classList.remove("faulted"); |
| 258 | div.classList.remove("zeroing"); |
| 259 | } |
| 260 | |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 261 | draw(): void { |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 262 | this.reset(); |
| 263 | this.drawField(); |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 264 | // draw cameras |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 265 | for (const keyPair of this.imageMatchResult) { |
| 266 | const value = keyPair[1]; |
| 267 | for (let i = 0; i < value.cameraPosesLength(); i++) { |
| 268 | const pose = value.cameraPoses(i); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 269 | const mat = pose.fieldToCamera(); |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 270 | // Matrix layout: |
| 271 | // [0, 1, 2, 3] |
| 272 | // [4, 5, 6, 7] |
| 273 | // [8, 9, 10, 11] |
| 274 | // [12, 13, 14, 15] |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 275 | const x = mat.data(3); |
| 276 | const y = mat.data(7); |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 277 | const theta = Math.atan2(mat.data(6), mat.data(2)); |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 278 | this.drawCamera(x, y, theta); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 282 | if (this.drivetrainStatus) { |
Austin Schuh | 840132b | 2021-10-17 17:40:14 -0700 | [diff] [blame^] | 283 | if (!this.drivetrainStatus.zeroing().zeroed()) { |
| 284 | this.setZeroing(this.x); |
| 285 | this.setZeroing(this.y); |
| 286 | this.setZeroing(this.theta); |
| 287 | } else if (this.drivetrainStatus.zeroing().faulted()) { |
| 288 | this.setEstopped(this.x); |
| 289 | this.setEstopped(this.y); |
| 290 | this.setEstopped(this.theta); |
| 291 | } else { |
| 292 | this.setValue(this.x, this.drivetrainStatus.x()); |
| 293 | this.setValue(this.y, this.drivetrainStatus.y()); |
| 294 | this.setValue(this.theta, this.drivetrainStatus.theta()); |
| 295 | } |
| 296 | |
| 297 | this.shotDistance.innerHTML = this.superstructureStatus.aimer().shotDistance().toFixed(2); |
| 298 | this.finisher.innerHTML = this.superstructureStatus.shooter().finisher().angularVelocity().toFixed(2); |
| 299 | this.leftAccelerator.innerHTML = this.superstructureStatus.shooter().acceleratorLeft().angularVelocity().toFixed(2); |
| 300 | this.rightAccelerator.innerHTML = this.superstructureStatus.shooter().acceleratorRight().angularVelocity().toFixed(2); |
| 301 | if (this.superstructureStatus.aimer().aimingForInnerPort()) { |
| 302 | this.innerPort.innerHTML = "true"; |
| 303 | } else { |
| 304 | this.innerPort.innerHTML = "false"; |
| 305 | } |
| 306 | if (!this.superstructureStatus.hood().zeroed()) { |
| 307 | this.setZeroing(this.hood); |
| 308 | } else if (this.superstructureStatus.hood().estopped()) { |
| 309 | this.setEstopped(this.hood); |
| 310 | } else { |
| 311 | this.setValue(this.hood, this.superstructureStatus.hood().estimatorState().position()); |
| 312 | } |
| 313 | if (!this.superstructureStatus.turret().zeroed()) { |
| 314 | this.setZeroing(this.turret); |
| 315 | } else if (this.superstructureStatus.turret().estopped()) { |
| 316 | this.setEstopped(this.turret); |
| 317 | } else { |
| 318 | this.setValue(this.turret, this.superstructureStatus.turret().estimatorState().position()); |
| 319 | } |
| 320 | if (!this.superstructureStatus.intake().zeroed()) { |
| 321 | this.setZeroing(this.intake); |
| 322 | } else if (this.superstructureStatus.intake().estopped()) { |
| 323 | this.setEstopped(this.intake); |
| 324 | } else { |
| 325 | this.setValue(this.intake, this.superstructureStatus.intake().estimatorState().position()); |
| 326 | } |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 327 | this.drawRobot( |
| 328 | this.drivetrainStatus.x(), this.drivetrainStatus.y(), |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 329 | this.drivetrainStatus.theta(), |
| 330 | this.superstructureStatus ? |
| 331 | this.superstructureStatus.turret().position() : |
| 332 | null); |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 333 | } |
| 334 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 335 | window.requestAnimationFrame(() => this.draw()); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | reset(): void { |
| 339 | const ctx = this.canvas.getContext('2d'); |
| 340 | ctx.setTransform(1, 0, 0, 1, 0, 0); |
| 341 | const size = window.innerHeight * 0.9; |
| 342 | ctx.canvas.height = size; |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 343 | const width = size / 2 + 20; |
| 344 | ctx.canvas.width = width; |
| 345 | ctx.clearRect(0, 0, size, width); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 346 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 347 | // Translate to center of display. |
| 348 | ctx.translate(width / 2, size / 2); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 349 | // Coordinate system is: |
| 350 | // x -> forward. |
| 351 | // y -> to the left. |
| 352 | ctx.rotate(-Math.PI / 2); |
| 353 | ctx.scale(1, -1); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 354 | |
| 355 | const M_TO_PX = (size - 10) / FIELD_LENGTH; |
| 356 | ctx.scale(M_TO_PX, M_TO_PX); |
| 357 | ctx.lineWidth = 1 / M_TO_PX; |
| 358 | } |
| 359 | } |