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