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'; |
James Kuszmaul | f75ecd6 | 2021-10-23 14:33:46 -0700 | [diff] [blame] | 6 | import * as localizer from 'org_frc971/y2020/control_loops/drivetrain/localizer_debug_generated'; |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 7 | import * as sift from 'org_frc971/y2020/vision/sift/sift_generated'; |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame] | 8 | import * as web_proxy from 'org_frc971/aos/network/web_proxy_generated'; |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 9 | 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] | 10 | |
| 11 | import DrivetrainStatus = drivetrain.frc971.control_loops.drivetrain.Status; |
James Kuszmaul | f75ecd6 | 2021-10-23 14:33:46 -0700 | [diff] [blame] | 12 | import LocalizerDebug = localizer.y2020.control_loops.drivetrain.LocalizerDebug; |
| 13 | import RejectionReason = localizer.y2020.control_loops.drivetrain.RejectionReason; |
| 14 | import ImageMatchDebug = localizer.y2020.control_loops.drivetrain.ImageMatchDebug; |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 15 | import SuperstructureStatus = ss.y2020.control_loops.superstructure.Status; |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 16 | import ImageMatchResult = sift.frc971.vision.sift.ImageMatchResult; |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 17 | import Channel = configuration.aos.Channel; |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame] | 18 | import SubscriberRequest = web_proxy.aos.web_proxy.SubscriberRequest; |
| 19 | import ChannelRequest = web_proxy.aos.web_proxy.ChannelRequest; |
| 20 | import TransferMethod = web_proxy.aos.web_proxy.TransferMethod; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 21 | |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 22 | import {FIELD_LENGTH, FIELD_WIDTH, FT_TO_M, IN_TO_M} from './constants'; |
| 23 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 24 | // (0,0) is field center, +X is toward red DS |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 25 | const FIELD_SIDE_Y = FIELD_WIDTH / 2; |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 26 | const FIELD_EDGE_X = FIELD_LENGTH / 2; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 27 | |
| 28 | const DS_WIDTH = 69 * IN_TO_M; |
| 29 | const DS_ANGLE = 20 * Math.PI / 180; |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 30 | 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] | 31 | const DS_INSIDE_Y = FIELD_SIDE_Y - DS_WIDTH * Math.cos(DS_ANGLE); |
| 32 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 33 | const TRENCH_X = 108 * IN_TO_M; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 34 | const TRENCH_WIDTH = 55.5 * IN_TO_M; |
| 35 | const TRENCH_INSIDE = FIELD_SIDE_Y - TRENCH_WIDTH; |
| 36 | |
| 37 | const SPINNER_LENGTH = 30 * IN_TO_M; |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 38 | const SPINNER_TOP_X = 374.59 * IN_TO_M - FIELD_EDGE_X; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 39 | const SPINNER_BOTTOM_X = SPINNER_TOP_X - SPINNER_LENGTH; |
| 40 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 41 | const SHIELD_BOTTOM_X = -116 * IN_TO_M; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 42 | const SHIELD_BOTTOM_Y = 43.75 * IN_TO_M; |
| 43 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 44 | const SHIELD_TOP_X = 116 * IN_TO_M; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 45 | const SHIELD_TOP_Y = -43.75 * IN_TO_M; |
| 46 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 47 | const SHIELD_RIGHT_X = -51.06 * IN_TO_M; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 48 | const SHIELD_RIGHT_Y = -112.88 * IN_TO_M; |
| 49 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 50 | const SHIELD_LEFT_X = 51.06 * IN_TO_M; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 51 | const SHIELD_LEFT_Y = 112.88 * IN_TO_M; |
| 52 | |
| 53 | const SHIELD_CENTER_TOP_X = (SHIELD_TOP_X + SHIELD_LEFT_X) / 2 |
| 54 | const SHIELD_CENTER_TOP_Y = (SHIELD_TOP_Y + SHIELD_LEFT_Y) / 2 |
| 55 | |
| 56 | const SHIELD_CENTER_BOTTOM_X = (SHIELD_BOTTOM_X + SHIELD_RIGHT_X) / 2 |
| 57 | const SHIELD_CENTER_BOTTOM_Y = (SHIELD_BOTTOM_Y + SHIELD_RIGHT_Y) / 2 |
| 58 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 59 | const INITIATION_X = FIELD_EDGE_X - 120 * IN_TO_M; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 60 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 61 | const TARGET_ZONE_TIP_X = FIELD_EDGE_X - 30 * IN_TO_M; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 62 | const TARGET_ZONE_WIDTH = 48 * IN_TO_M; |
| 63 | const LOADING_ZONE_WIDTH = 60 * IN_TO_M; |
| 64 | |
Austin Schuh | b863f93 | 2021-10-23 23:29:09 -0700 | [diff] [blame^] | 65 | const ROBOT_WIDTH = 34 * IN_TO_M; |
| 66 | const ROBOT_LENGTH = 36 * IN_TO_M; |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 67 | |
James Kuszmaul | 286b80c | 2021-10-23 21:56:33 -0700 | [diff] [blame] | 68 | const PI_COLORS = ["#ff00ff", "#ffff00", "#000000", "#00ffff", "#ffa500"]; |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 69 | |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 70 | export class FieldHandler { |
| 71 | private canvas = document.createElement('canvas'); |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 72 | private imageMatchResult = new Map<string, ImageMatchResult>(); |
Philipp Schrader | a227d04 | 2020-11-14 17:33:52 -0800 | [diff] [blame] | 73 | private drivetrainStatus: DrivetrainStatus|null = null; |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 74 | private superstructureStatus: SuperstructureStatus|null = null; |
James Kuszmaul | f75ecd6 | 2021-10-23 14:33:46 -0700 | [diff] [blame] | 75 | // Image information indexed by timestamp (seconds since the epoch), so that |
| 76 | // we can stop displaying images after a certain amount of time. |
| 77 | private localizerImageMatches = new Map<number, LocalizerDebug>(); |
Austin Schuh | 840132b | 2021-10-17 17:40:14 -0700 | [diff] [blame] | 78 | private x: HTMLDivElement = (document.getElementById('x') as HTMLDivElement); |
| 79 | private y: HTMLDivElement = (document.getElementById('y') as HTMLDivElement); |
| 80 | private theta: HTMLDivElement = (document.getElementById('theta') as HTMLDivElement); |
| 81 | private shotDistance: HTMLDivElement = (document.getElementById('shot_distance') as HTMLDivElement); |
| 82 | private finisher: HTMLDivElement = (document.getElementById('finisher') as HTMLDivElement); |
| 83 | private leftAccelerator: HTMLDivElement = (document.getElementById('left_accelerator') as HTMLDivElement); |
| 84 | private rightAccelerator: HTMLDivElement = (document.getElementById('right_accelerator') as HTMLDivElement); |
| 85 | private innerPort: HTMLDivElement = (document.getElementById('inner_port') as HTMLDivElement); |
| 86 | private hood: HTMLDivElement = (document.getElementById('hood') as HTMLDivElement); |
| 87 | private turret: HTMLDivElement = (document.getElementById('turret') as HTMLDivElement); |
| 88 | private intake: HTMLDivElement = (document.getElementById('intake') as HTMLDivElement); |
James Kuszmaul | f75ecd6 | 2021-10-23 14:33:46 -0700 | [diff] [blame] | 89 | private imagesAcceptedCounter: HTMLDivElement = (document.getElementById('images_accepted') as HTMLDivElement); |
| 90 | private imagesRejectedCounter: HTMLDivElement = (document.getElementById('images_rejected') as HTMLDivElement); |
| 91 | private rejectionReasonCells: HTMLDivElement[] = []; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 92 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 93 | constructor(private readonly connection: Connection) { |
Austin Schuh | 840132b | 2021-10-17 17:40:14 -0700 | [diff] [blame] | 94 | (document.getElementById('field') as HTMLElement).appendChild(this.canvas); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 95 | |
James Kuszmaul | f75ecd6 | 2021-10-23 14:33:46 -0700 | [diff] [blame] | 96 | for (const value in RejectionReason) { |
| 97 | // Typescript generates an iterator that produces both numbers and |
| 98 | // strings... don't do anything on the string iterations. |
| 99 | if (isNaN(Number(value))) { |
| 100 | continue; |
| 101 | } |
| 102 | const row = document.createElement("div"); |
| 103 | const nameCell = document.createElement("div"); |
| 104 | nameCell.innerHTML = RejectionReason[value]; |
| 105 | row.appendChild(nameCell); |
| 106 | const valueCell = document.createElement("div"); |
| 107 | valueCell.innerHTML = "NA"; |
| 108 | this.rejectionReasonCells.push(valueCell); |
| 109 | row.appendChild(valueCell); |
| 110 | document.getElementById('readouts').appendChild(row); |
| 111 | } |
| 112 | |
James Kuszmaul | 286b80c | 2021-10-23 21:56:33 -0700 | [diff] [blame] | 113 | for (let ii = 0; ii < PI_COLORS.length; ++ii) { |
| 114 | const legendEntry = document.createElement("div"); |
| 115 | legendEntry.style.color = PI_COLORS[ii]; |
| 116 | legendEntry.innerHTML = "PI" + (ii + 1).toString() |
| 117 | document.getElementById("legend").appendChild(legendEntry); |
| 118 | } |
| 119 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 120 | this.connection.addConfigHandler(() => { |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 121 | // Go through and register handlers for both all the individual pis as |
| 122 | // well as the local pi. Depending on the node that we are running on, |
| 123 | // different subsets of these will be available. |
James Kuszmaul | 286b80c | 2021-10-23 21:56:33 -0700 | [diff] [blame] | 124 | for (const prefix of ['', '/pi1', '/pi2', '/pi3', '/pi4', '/pi5']) { |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 125 | this.connection.addHandler( |
James Kuszmaul | 286b80c | 2021-10-23 21:56:33 -0700 | [diff] [blame] | 126 | prefix + '/camera', ImageMatchResult.getFullyQualifiedName(), |
| 127 | (res) => { |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 128 | this.handleImageMatchResult(prefix, res); |
| 129 | }); |
| 130 | } |
James Kuszmaul | 527038a | 2020-12-21 23:40:44 -0800 | [diff] [blame] | 131 | this.connection.addHandler( |
James Kuszmaul | f75ecd6 | 2021-10-23 14:33:46 -0700 | [diff] [blame] | 132 | '/drivetrain', LocalizerDebug.getFullyQualifiedName(), (data) => { |
| 133 | this.handleLocalizerDebug(data); |
| 134 | }); |
| 135 | this.connection.addHandler( |
James Kuszmaul | 527038a | 2020-12-21 23:40:44 -0800 | [diff] [blame] | 136 | '/drivetrain', DrivetrainStatus.getFullyQualifiedName(), (data) => { |
| 137 | this.handleDrivetrainStatus(data); |
| 138 | }); |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 139 | this.connection.addHandler( |
| 140 | '/superstructure', SuperstructureStatus.getFullyQualifiedName(), |
| 141 | (data) => { |
| 142 | this.handleSuperstructureStatus(data); |
| 143 | }); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 144 | }); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 145 | } |
| 146 | |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 147 | private handleImageMatchResult(prefix: string, data: Uint8Array): void { |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 148 | const fbBuffer = new ByteBuffer(data); |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 149 | this.imageMatchResult.set( |
| 150 | prefix, |
| 151 | ImageMatchResult.getRootAsImageMatchResult( |
| 152 | fbBuffer as unknown as flatbuffers.ByteBuffer)); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 153 | } |
| 154 | |
James Kuszmaul | f75ecd6 | 2021-10-23 14:33:46 -0700 | [diff] [blame] | 155 | private handleLocalizerDebug(data: Uint8Array): void { |
| 156 | const now = Date.now() / 1000.0; |
| 157 | |
| 158 | const fbBuffer = new ByteBuffer(data); |
| 159 | this.localizerImageMatches.set( |
| 160 | now, |
| 161 | LocalizerDebug.getRootAsLocalizerDebug( |
| 162 | fbBuffer as unknown as flatbuffers.ByteBuffer)); |
| 163 | |
| 164 | const debug = this.localizerImageMatches.get(now); |
| 165 | |
| 166 | if (debug.statistics()) { |
| 167 | this.imagesAcceptedCounter.innerHTML = |
| 168 | debug.statistics().totalAccepted().toString(); |
| 169 | this.imagesRejectedCounter.innerHTML = |
| 170 | (debug.statistics().totalCandidates() - |
| 171 | debug.statistics().totalAccepted()) |
| 172 | .toString(); |
| 173 | if (debug.statistics().rejectionReasonCountLength() == |
| 174 | this.rejectionReasonCells.length) { |
| 175 | for (let ii = 0; ii < debug.statistics().rejectionReasonCountLength(); |
| 176 | ++ii) { |
| 177 | this.rejectionReasonCells[ii].innerHTML = |
| 178 | debug.statistics().rejectionReasonCount(ii).toString(); |
| 179 | } |
| 180 | } else { |
| 181 | console.error("Unexpected number of rejection reasons in counter."); |
| 182 | } |
| 183 | this.imagesRejectedCounter.innerHTML = |
| 184 | (debug.statistics().totalCandidates() - |
| 185 | debug.statistics().totalAccepted()) |
| 186 | .toString(); |
| 187 | } |
| 188 | } |
| 189 | |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 190 | private handleDrivetrainStatus(data: Uint8Array): void { |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 191 | const fbBuffer = new ByteBuffer(data); |
| 192 | this.drivetrainStatus = DrivetrainStatus.getRootAsStatus( |
| 193 | fbBuffer as unknown as flatbuffers.ByteBuffer); |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 194 | } |
| 195 | |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 196 | private handleSuperstructureStatus(data: Uint8Array): void { |
| 197 | const fbBuffer = new ByteBuffer(data); |
| 198 | this.superstructureStatus = SuperstructureStatus.getRootAsStatus( |
| 199 | fbBuffer as unknown as flatbuffers.ByteBuffer); |
| 200 | } |
| 201 | |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 202 | drawField(): void { |
| 203 | const MY_COLOR = 'red'; |
| 204 | const OTHER_COLOR = 'blue'; |
| 205 | const ctx = this.canvas.getContext('2d'); |
| 206 | // draw perimiter |
| 207 | ctx.beginPath(); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 208 | ctx.moveTo(FIELD_EDGE_X, DS_INSIDE_Y); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 209 | ctx.lineTo(DS_END_X, FIELD_SIDE_Y); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 210 | ctx.lineTo(-DS_END_X, FIELD_SIDE_Y); |
| 211 | ctx.lineTo(-FIELD_EDGE_X, DS_INSIDE_Y); |
| 212 | ctx.lineTo(-FIELD_EDGE_X, -DS_INSIDE_Y); |
| 213 | ctx.lineTo(-DS_END_X, -FIELD_SIDE_Y); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 214 | ctx.lineTo(DS_END_X, -FIELD_SIDE_Y); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 215 | ctx.lineTo(FIELD_EDGE_X, -DS_INSIDE_Y); |
| 216 | ctx.lineTo(FIELD_EDGE_X, DS_INSIDE_Y); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 217 | ctx.stroke(); |
| 218 | |
| 219 | // draw shield generator |
| 220 | ctx.beginPath(); |
| 221 | ctx.moveTo(SHIELD_BOTTOM_X, SHIELD_BOTTOM_Y); |
| 222 | ctx.lineTo(SHIELD_RIGHT_X, SHIELD_RIGHT_Y); |
| 223 | ctx.lineTo(SHIELD_TOP_X, SHIELD_TOP_Y); |
| 224 | ctx.lineTo(SHIELD_LEFT_X, SHIELD_LEFT_Y); |
| 225 | ctx.lineTo(SHIELD_BOTTOM_X, SHIELD_BOTTOM_Y); |
| 226 | ctx.moveTo(SHIELD_CENTER_TOP_X, SHIELD_CENTER_TOP_Y); |
| 227 | ctx.lineTo(SHIELD_CENTER_BOTTOM_X, SHIELD_CENTER_BOTTOM_Y); |
| 228 | ctx.stroke(); |
| 229 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 230 | this.drawHalfField(ctx, 'red'); |
| 231 | ctx.rotate(Math.PI); |
| 232 | this.drawHalfField(ctx, 'blue'); |
| 233 | ctx.rotate(Math.PI); |
| 234 | } |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 235 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 236 | drawHalfField(ctx, color: string): void { |
| 237 | // trenches |
| 238 | ctx.strokeStyle = color; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 239 | ctx.beginPath(); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 240 | ctx.moveTo(TRENCH_X, FIELD_SIDE_Y); |
| 241 | ctx.lineTo(TRENCH_X, TRENCH_INSIDE); |
| 242 | ctx.lineTo(-TRENCH_X, TRENCH_INSIDE); |
| 243 | ctx.lineTo(-TRENCH_X, FIELD_SIDE_Y); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 244 | ctx.stroke(); |
| 245 | |
| 246 | ctx.strokeStyle = 'black'; |
| 247 | ctx.beginPath(); |
| 248 | ctx.moveTo(SPINNER_TOP_X, FIELD_SIDE_Y); |
| 249 | ctx.lineTo(SPINNER_TOP_X, TRENCH_INSIDE); |
| 250 | ctx.lineTo(SPINNER_BOTTOM_X, TRENCH_INSIDE); |
| 251 | ctx.lineTo(SPINNER_BOTTOM_X, FIELD_SIDE_Y); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 252 | ctx.stroke(); |
| 253 | |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 254 | ctx.beginPath(); |
| 255 | ctx.moveTo(INITIATION_X, FIELD_SIDE_Y); |
| 256 | ctx.lineTo(INITIATION_X, -FIELD_SIDE_Y); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 257 | ctx.stroke(); |
| 258 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 259 | // target/loading |
| 260 | ctx.strokeStyle = color; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 261 | ctx.beginPath(); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 262 | ctx.moveTo(FIELD_EDGE_X, DS_INSIDE_Y); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 263 | 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] | 264 | ctx.lineTo(FIELD_EDGE_X, DS_INSIDE_Y - TARGET_ZONE_WIDTH); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 265 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 266 | ctx.moveTo(-FIELD_EDGE_X, DS_INSIDE_Y); |
| 267 | ctx.lineTo(-TARGET_ZONE_TIP_X, DS_INSIDE_Y - 0.5 * LOADING_ZONE_WIDTH); |
| 268 | ctx.lineTo(-FIELD_EDGE_X, DS_INSIDE_Y - LOADING_ZONE_WIDTH); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 269 | ctx.stroke(); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 270 | } |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 271 | |
James Kuszmaul | f75ecd6 | 2021-10-23 14:33:46 -0700 | [diff] [blame] | 272 | drawCamera( |
| 273 | x: number, y: number, theta: number, color: string = 'blue', |
| 274 | extendLines: boolean = true): void { |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 275 | const ctx = this.canvas.getContext('2d'); |
| 276 | ctx.save(); |
| 277 | ctx.translate(x, y); |
| 278 | ctx.rotate(theta); |
James Kuszmaul | f75ecd6 | 2021-10-23 14:33:46 -0700 | [diff] [blame] | 279 | ctx.strokeStyle = color; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 280 | ctx.beginPath(); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 281 | ctx.moveTo(0.5, 0.5); |
| 282 | ctx.lineTo(0, 0); |
James Kuszmaul | f75ecd6 | 2021-10-23 14:33:46 -0700 | [diff] [blame] | 283 | if (extendLines) { |
| 284 | ctx.lineTo(100.0, 0); |
| 285 | ctx.lineTo(0, 0); |
| 286 | } |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 287 | ctx.lineTo(0.5, -0.5); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 288 | ctx.stroke(); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 289 | ctx.beginPath(); |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 290 | ctx.arc(0, 0, 0.25, -Math.PI / 4, Math.PI / 4); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 291 | ctx.stroke(); |
| 292 | ctx.restore(); |
| 293 | } |
| 294 | |
James Kuszmaul | f75ecd6 | 2021-10-23 14:33:46 -0700 | [diff] [blame] | 295 | drawRobot( |
| 296 | x: number, y: number, theta: number, turret: number|null, |
| 297 | color: string = 'blue', dashed: boolean = false, |
| 298 | extendLines: boolean = true): void { |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 299 | const ctx = this.canvas.getContext('2d'); |
| 300 | ctx.save(); |
| 301 | ctx.translate(x, y); |
| 302 | ctx.rotate(theta); |
James Kuszmaul | f75ecd6 | 2021-10-23 14:33:46 -0700 | [diff] [blame] | 303 | ctx.strokeStyle = color; |
| 304 | if (dashed) { |
| 305 | ctx.setLineDash([0.05, 0.05]); |
| 306 | } else { |
| 307 | // Empty array = solid line. |
| 308 | ctx.setLineDash([]); |
| 309 | } |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 310 | ctx.rect(-ROBOT_LENGTH / 2, -ROBOT_WIDTH / 2, ROBOT_LENGTH, ROBOT_WIDTH); |
| 311 | ctx.stroke(); |
James Kuszmaul | f75ecd6 | 2021-10-23 14:33:46 -0700 | [diff] [blame] | 312 | |
| 313 | // Draw line indicating which direction is forwards on the robot. |
| 314 | ctx.beginPath(); |
| 315 | ctx.moveTo(0, 0); |
| 316 | if (extendLines) { |
| 317 | ctx.lineTo(1000.0, 0); |
| 318 | } else { |
| 319 | ctx.lineTo(ROBOT_LENGTH / 2.0, 0); |
| 320 | } |
| 321 | ctx.stroke(); |
| 322 | |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 323 | if (turret) { |
| 324 | ctx.save(); |
| 325 | ctx.rotate(turret + Math.PI); |
| 326 | const turretRadius = ROBOT_WIDTH / 4.0; |
| 327 | ctx.strokeStyle = "red"; |
| 328 | // Draw circle for turret. |
| 329 | ctx.beginPath(); |
| 330 | ctx.arc(0, 0, turretRadius, 0, 2.0 * Math.PI); |
| 331 | ctx.stroke(); |
| 332 | // Draw line in circle to show forwards. |
| 333 | ctx.beginPath(); |
| 334 | ctx.moveTo(0, 0); |
James Kuszmaul | f75ecd6 | 2021-10-23 14:33:46 -0700 | [diff] [blame] | 335 | if (extendLines) { |
| 336 | ctx.lineTo(1000.0, 0); |
| 337 | } else { |
| 338 | ctx.lineTo(turretRadius, 0); |
| 339 | } |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 340 | ctx.stroke(); |
| 341 | ctx.restore(); |
| 342 | } |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 343 | ctx.restore(); |
| 344 | } |
| 345 | |
Austin Schuh | 840132b | 2021-10-17 17:40:14 -0700 | [diff] [blame] | 346 | setZeroing(div: HTMLDivElement): void { |
| 347 | div.innerHTML = "zeroing"; |
| 348 | div.classList.remove("faulted"); |
| 349 | div.classList.add("zeroing"); |
| 350 | } |
| 351 | setEstopped(div: HTMLDivElement): void { |
| 352 | div.innerHTML = "estopped"; |
| 353 | div.classList.add("faulted"); |
| 354 | div.classList.remove("zeroing"); |
| 355 | } |
| 356 | setValue(div: HTMLDivElement, val: Number): void { |
| 357 | div.innerHTML = val.toFixed(4); |
| 358 | div.classList.remove("faulted"); |
| 359 | div.classList.remove("zeroing"); |
| 360 | } |
| 361 | |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 362 | draw(): void { |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 363 | this.reset(); |
| 364 | this.drawField(); |
James Kuszmaul | f75ecd6 | 2021-10-23 14:33:46 -0700 | [diff] [blame] | 365 | |
| 366 | // Draw the matches with debugging information from the localizer. |
| 367 | const now = Date.now() / 1000.0; |
| 368 | for (const [time, value] of this.localizerImageMatches) { |
| 369 | const age = now - time; |
| 370 | const kRemovalAge = 2.0; |
| 371 | if (age > kRemovalAge) { |
| 372 | this.localizerImageMatches.delete(time); |
| 373 | continue; |
| 374 | } |
| 375 | const ageAlpha = (kRemovalAge - age) / kRemovalAge |
| 376 | for (let i = 0; i < value.matchesLength(); i++) { |
| 377 | const imageDebug = value.matches(i); |
| 378 | const x = imageDebug.impliedRobotX(); |
| 379 | const y = imageDebug.impliedRobotY(); |
| 380 | const theta = imageDebug.impliedRobotTheta(); |
| 381 | const cameraX = imageDebug.cameraX(); |
| 382 | const cameraY = imageDebug.cameraY(); |
| 383 | const cameraTheta = imageDebug.cameraTheta(); |
| 384 | const accepted = imageDebug.accepted(); |
| 385 | // Make camera readings fade over time. |
| 386 | const alpha = Math.round(255 * ageAlpha).toString(16).padStart(2, '0'); |
| 387 | const dashed = false; |
James Kuszmaul | 286b80c | 2021-10-23 21:56:33 -0700 | [diff] [blame] | 388 | const acceptedRgb = accepted ? "#00FF00" : "#FF0000"; |
| 389 | const acceptedRgba = acceptedRgb + alpha; |
| 390 | const cameraRgb = PI_COLORS[imageDebug.camera()]; |
| 391 | const cameraRgba = cameraRgb + alpha; |
| 392 | this.drawRobot(x, y, theta, null, acceptedRgba, dashed, false); |
| 393 | this.drawCamera(cameraX, cameraY, cameraTheta, cameraRgba, false); |
James Kuszmaul | f75ecd6 | 2021-10-23 14:33:46 -0700 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | |
| 397 | // draw cameras from ImageMatchResults directly (helpful when viewing page |
| 398 | // on the pis individually). |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 399 | for (const keyPair of this.imageMatchResult) { |
| 400 | const value = keyPair[1]; |
| 401 | for (let i = 0; i < value.cameraPosesLength(); i++) { |
| 402 | const pose = value.cameraPoses(i); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 403 | const mat = pose.fieldToCamera(); |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 404 | // Matrix layout: |
| 405 | // [0, 1, 2, 3] |
| 406 | // [4, 5, 6, 7] |
| 407 | // [8, 9, 10, 11] |
| 408 | // [12, 13, 14, 15] |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 409 | const x = mat.data(3); |
| 410 | const y = mat.data(7); |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 411 | const theta = Math.atan2(mat.data(6), mat.data(2)); |
James Kuszmaul | 286b80c | 2021-10-23 21:56:33 -0700 | [diff] [blame] | 412 | const cameraColor = (keyPair[0].length > 0) ? |
| 413 | PI_COLORS[Number(keyPair[0][3]) - 1] : |
| 414 | 'blue'; |
| 415 | this.drawCamera(x, y, theta, cameraColor); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 419 | if (this.drivetrainStatus) { |
Austin Schuh | 840132b | 2021-10-17 17:40:14 -0700 | [diff] [blame] | 420 | if (!this.drivetrainStatus.zeroing().zeroed()) { |
| 421 | this.setZeroing(this.x); |
| 422 | this.setZeroing(this.y); |
| 423 | this.setZeroing(this.theta); |
| 424 | } else if (this.drivetrainStatus.zeroing().faulted()) { |
| 425 | this.setEstopped(this.x); |
| 426 | this.setEstopped(this.y); |
| 427 | this.setEstopped(this.theta); |
| 428 | } else { |
| 429 | this.setValue(this.x, this.drivetrainStatus.x()); |
| 430 | this.setValue(this.y, this.drivetrainStatus.y()); |
| 431 | this.setValue(this.theta, this.drivetrainStatus.theta()); |
| 432 | } |
| 433 | |
James Kuszmaul | f75ecd6 | 2021-10-23 14:33:46 -0700 | [diff] [blame] | 434 | if (this.superstructureStatus) { |
| 435 | this.shotDistance.innerHTML = |
| 436 | this.superstructureStatus.aimer().shotDistance().toFixed(2); |
| 437 | this.finisher.innerHTML = this.superstructureStatus.shooter() |
| 438 | .finisher() |
| 439 | .angularVelocity() |
| 440 | .toFixed(2); |
| 441 | this.leftAccelerator.innerHTML = this.superstructureStatus.shooter() |
| 442 | .acceleratorLeft() |
| 443 | .angularVelocity() |
| 444 | .toFixed(2); |
| 445 | this.rightAccelerator.innerHTML = this.superstructureStatus.shooter() |
| 446 | .acceleratorRight() |
| 447 | .angularVelocity() |
| 448 | .toFixed(2); |
| 449 | if (this.superstructureStatus.aimer().aimingForInnerPort()) { |
| 450 | this.innerPort.innerHTML = 'true'; |
| 451 | } else { |
| 452 | this.innerPort.innerHTML = 'false'; |
| 453 | } |
| 454 | if (!this.superstructureStatus.hood().zeroed()) { |
| 455 | this.setZeroing(this.hood); |
| 456 | } else if (this.superstructureStatus.hood().estopped()) { |
| 457 | this.setEstopped(this.hood); |
| 458 | } else { |
| 459 | this.setValue( |
| 460 | this.hood, |
| 461 | this.superstructureStatus.hood().estimatorState().position()); |
| 462 | } |
| 463 | if (!this.superstructureStatus.turret().zeroed()) { |
| 464 | this.setZeroing(this.turret); |
| 465 | } else if (this.superstructureStatus.turret().estopped()) { |
| 466 | this.setEstopped(this.turret); |
| 467 | } else { |
| 468 | this.setValue( |
| 469 | this.turret, |
| 470 | this.superstructureStatus.turret().estimatorState().position()); |
| 471 | } |
| 472 | if (!this.superstructureStatus.intake().zeroed()) { |
| 473 | this.setZeroing(this.intake); |
| 474 | } else if (this.superstructureStatus.intake().estopped()) { |
| 475 | this.setEstopped(this.intake); |
| 476 | } else { |
| 477 | this.setValue( |
| 478 | this.intake, |
| 479 | this.superstructureStatus.intake().estimatorState().position()); |
| 480 | } |
Austin Schuh | 840132b | 2021-10-17 17:40:14 -0700 | [diff] [blame] | 481 | } |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 482 | this.drawRobot( |
| 483 | this.drivetrainStatus.x(), this.drivetrainStatus.y(), |
James Kuszmaul | 5e6aa25 | 2021-08-28 22:19:29 -0700 | [diff] [blame] | 484 | this.drivetrainStatus.theta(), |
| 485 | this.superstructureStatus ? |
| 486 | this.superstructureStatus.turret().position() : |
| 487 | null); |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 488 | } |
| 489 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 490 | window.requestAnimationFrame(() => this.draw()); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | reset(): void { |
| 494 | const ctx = this.canvas.getContext('2d'); |
| 495 | ctx.setTransform(1, 0, 0, 1, 0, 0); |
| 496 | const size = window.innerHeight * 0.9; |
| 497 | ctx.canvas.height = size; |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 498 | const width = size / 2 + 20; |
| 499 | ctx.canvas.width = width; |
| 500 | ctx.clearRect(0, 0, size, width); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 501 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 502 | // Translate to center of display. |
| 503 | ctx.translate(width / 2, size / 2); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 504 | // Coordinate system is: |
| 505 | // x -> forward. |
| 506 | // y -> to the left. |
| 507 | ctx.rotate(-Math.PI / 2); |
| 508 | ctx.scale(1, -1); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 509 | |
| 510 | const M_TO_PX = (size - 10) / FIELD_LENGTH; |
| 511 | ctx.scale(M_TO_PX, M_TO_PX); |
| 512 | ctx.lineWidth = 1 / M_TO_PX; |
| 513 | } |
| 514 | } |