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