Ariv Diggi | 0af59c0 | 2023-10-07 13:15:39 -0700 | [diff] [blame] | 1 | import {ByteBuffer} from 'flatbuffers' |
| 2 | import {ClientStatistics} from '../../aos/network/message_bridge_client_generated' |
| 3 | import {ServerStatistics, State as ConnectionState} from '../../aos/network/message_bridge_server_generated' |
| 4 | import {Connection} from '../../aos/network/www/proxy' |
| 5 | import {ZeroingError} from '../../frc971/control_loops/control_loops_generated' |
| 6 | import {Status as DrivetrainStatus} from '../../frc971/control_loops/drivetrain/drivetrain_status_generated' |
| 7 | import {LocalizerOutput} from '../../frc971/control_loops/drivetrain/localization/localizer_output_generated' |
| 8 | |
| 9 | import {FIELD_LENGTH, FIELD_WIDTH, FT_TO_M, IN_TO_M} from './constants'; |
| 10 | |
| 11 | // (0,0) is field center, +X is toward red DS |
| 12 | const FIELD_SIDE_Y = FIELD_WIDTH / 2; |
| 13 | const FIELD_EDGE_X = FIELD_LENGTH / 2; |
| 14 | |
| 15 | const ROBOT_WIDTH = 25 * IN_TO_M; |
| 16 | const ROBOT_LENGTH = 32 * IN_TO_M; |
| 17 | |
| 18 | export class FieldHandler { |
| 19 | private canvas = document.createElement('canvas'); |
| 20 | private localizerOutput: LocalizerOutput|null = null; |
| 21 | private drivetrainStatus: DrivetrainStatus|null = null; |
| 22 | |
| 23 | private handleDrivetrainStatus(data: Uint8Array): void { |
| 24 | const fbBuffer = new ByteBuffer(data); |
| 25 | this.drivetrainStatus = DrivetrainStatus.getRootAsStatus(fbBuffer); |
| 26 | } |
| 27 | |
| 28 | private setCurrentNodeState(element: HTMLElement, state: ConnectionState): |
| 29 | void { |
| 30 | if (state === ConnectionState.CONNECTED) { |
| 31 | element.innerHTML = ConnectionState[state]; |
| 32 | element.classList.remove('faulted'); |
| 33 | element.classList.add('connected'); |
| 34 | } else { |
| 35 | element.innerHTML = ConnectionState[state]; |
| 36 | element.classList.remove('connected'); |
| 37 | element.classList.add('faulted'); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | private handleServerStatistics(data: Uint8Array): void { |
| 42 | const fbBuffer = new ByteBuffer(data); |
| 43 | const serverStatistics = |
| 44 | ServerStatistics.getRootAsServerStatistics(fbBuffer); |
| 45 | |
| 46 | for (let ii = 0; ii < serverStatistics.connectionsLength(); ++ii) { |
| 47 | const connection = serverStatistics.connections(ii); |
| 48 | const nodeName = connection.node().name(); |
| 49 | if (!this.serverStatuses.has(nodeName)) { |
| 50 | this.populateNodeConnections(nodeName); |
| 51 | } |
| 52 | this.setCurrentNodeState( |
| 53 | this.serverStatuses.get(nodeName), connection.state()); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | private handleClientStatistics(data: Uint8Array): void { |
| 58 | const fbBuffer = new ByteBuffer(data); |
| 59 | const clientStatistics = |
| 60 | ClientStatistics.getRootAsClientStatistics(fbBuffer); |
| 61 | |
| 62 | for (let ii = 0; ii < clientStatistics.connectionsLength(); ++ii) { |
| 63 | const connection = clientStatistics.connections(ii); |
| 64 | const nodeName = connection.node().name(); |
| 65 | if (!this.clientStatuses.has(nodeName)) { |
| 66 | this.populateNodeConnections(nodeName); |
| 67 | } |
| 68 | this.setCurrentNodeState( |
| 69 | this.clientStatuses.get(nodeName), connection.state()); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | drawField(): void { |
| 74 | const ctx = this.canvas.getContext('2d'); |
| 75 | ctx.save(); |
| 76 | ctx.scale(1.0, -1.0); |
| 77 | ctx.restore(); |
| 78 | } |
| 79 | |
| 80 | drawCamera(x: number, y: number, theta: number, color: string = 'blue'): |
| 81 | void { |
| 82 | const ctx = this.canvas.getContext('2d'); |
| 83 | ctx.save(); |
| 84 | ctx.translate(x, y); |
| 85 | ctx.rotate(theta); |
| 86 | ctx.strokeStyle = color; |
| 87 | ctx.beginPath(); |
| 88 | ctx.moveTo(0.5, 0.5); |
| 89 | ctx.lineTo(0, 0); |
| 90 | ctx.lineTo(0.5, -0.5); |
| 91 | ctx.stroke(); |
| 92 | ctx.beginPath(); |
| 93 | ctx.arc(0, 0, 0.25, -Math.PI / 4, Math.PI / 4); |
| 94 | ctx.stroke(); |
| 95 | ctx.restore(); |
| 96 | } |
| 97 | |
| 98 | drawRobot( |
| 99 | x: number, y: number, theta: number, color: string = 'blue', |
| 100 | dashed: boolean = false): void { |
| 101 | const ctx = this.canvas.getContext('2d'); |
| 102 | ctx.save(); |
| 103 | ctx.translate(x, y); |
| 104 | ctx.rotate(theta); |
| 105 | ctx.strokeStyle = color; |
| 106 | ctx.lineWidth = ROBOT_WIDTH / 10.0; |
| 107 | if (dashed) { |
| 108 | ctx.setLineDash([0.05, 0.05]); |
| 109 | } else { |
| 110 | // Empty array = solid line. |
| 111 | ctx.setLineDash([]); |
| 112 | } |
| 113 | ctx.rect(-ROBOT_LENGTH / 2, -ROBOT_WIDTH / 2, ROBOT_LENGTH, ROBOT_WIDTH); |
| 114 | ctx.stroke(); |
| 115 | |
| 116 | // Draw line indicating which direction is forwards on the robot. |
| 117 | ctx.beginPath(); |
| 118 | ctx.moveTo(0, 0); |
| 119 | ctx.lineTo(ROBOT_LENGTH / 2.0, 0); |
| 120 | ctx.stroke(); |
| 121 | |
| 122 | ctx.restore(); |
| 123 | } |
| 124 | |
| 125 | setZeroing(div: HTMLElement): void { |
| 126 | div.innerHTML = 'zeroing'; |
| 127 | div.classList.remove('faulted'); |
| 128 | div.classList.add('zeroing'); |
| 129 | div.classList.remove('near'); |
| 130 | } |
| 131 | |
| 132 | setEstopped(div: HTMLElement): void { |
| 133 | div.innerHTML = 'estopped'; |
| 134 | div.classList.add('faulted'); |
| 135 | div.classList.remove('zeroing'); |
| 136 | div.classList.remove('near'); |
| 137 | } |
| 138 | |
| 139 | setValue(div: HTMLElement, val: number): void { |
| 140 | div.innerHTML = val.toFixed(4); |
| 141 | div.classList.remove('faulted'); |
| 142 | div.classList.remove('zeroing'); |
| 143 | div.classList.remove('near'); |
| 144 | } |
| 145 | |
| 146 | draw(): void { |
| 147 | this.reset(); |
| 148 | this.drawField(); |
| 149 | |
| 150 | // Draw the matches with debugging information from the localizer. |
| 151 | const now = Date.now() / 1000.0; |
| 152 | |
| 153 | if (this.drivetrainStatus && this.drivetrainStatus.trajectoryLogging()) { |
| 154 | this.drawRobot( |
| 155 | this.drivetrainStatus.trajectoryLogging().x(), |
| 156 | this.drivetrainStatus.trajectoryLogging().y(), |
| 157 | this.drivetrainStatus.trajectoryLogging().theta(), '#000000FF', |
| 158 | false); |
| 159 | } |
| 160 | |
| 161 | window.requestAnimationFrame(() => this.draw()); |
| 162 | } |
| 163 | |
| 164 | reset(): void { |
| 165 | const ctx = this.canvas.getContext('2d'); |
| 166 | ctx.setTransform(1, 0, 0, 1, 0, 0); |
| 167 | const size = window.innerHeight * 0.9; |
| 168 | ctx.canvas.height = size; |
| 169 | const width = size / 2 + 20; |
| 170 | ctx.canvas.width = width; |
| 171 | ctx.clearRect(0, 0, size, width); |
| 172 | |
| 173 | // Translate to center of display. |
| 174 | ctx.translate(width / 2, size / 2); |
| 175 | // Coordinate system is: |
| 176 | // x -> forward. |
| 177 | // y -> to the left. |
| 178 | ctx.rotate(-Math.PI / 2); |
| 179 | ctx.scale(1, -1); |
| 180 | |
| 181 | const M_TO_PX = (size - 10) / FIELD_LENGTH; |
| 182 | ctx.scale(M_TO_PX, M_TO_PX); |
| 183 | ctx.lineWidth = 1 / M_TO_PX; |
| 184 | } |
| 185 | } |