James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 1 | // This file provides an index of all standard plot configs. |
| 2 | // In the future, this may be split into more pieces (e.g., to have |
| 3 | // year-specific indices). For now, the pattern for creating a new plot |
| 4 | // is that you provide an exported function (a la the plot*() functions imported |
| 5 | // below) which, when called, will generate the plot in the provided div. |
| 6 | // This file handles providing a master list of all known plots so that |
| 7 | // the user can just open a single web-page and select the plot that they want |
| 8 | // from a drop-down. A given plot will not be loaded until it has been selected |
| 9 | // once, at which point it will stay loaded. This means that if the user wants |
| 10 | // to switch between several plot configs, they don't incur any performance |
| 11 | // penalty associated with swapping. |
| 12 | // By default, no plot is selected, but the plot= URL parameter may be used |
| 13 | // to specify a specific plot, so that people can create links to a specific |
| 14 | // plot. |
| 15 | // The plot*() functions are called *after* we have already received a valid |
| 16 | // config from the web server, so config handlers do not need to be used. |
| 17 | // |
| 18 | // The exact setup of this will be in flux as we add more configs and figure out |
| 19 | // what setups work best--we will likely end up with separate index files for |
| 20 | // each robot year, and may even end up allowing plots to be specified solely |
| 21 | // using JSON rather than requiring people to write a script just to create |
| 22 | // a plot. |
Philipp Schrader | 548aedf | 2023-02-17 20:09:13 -0800 | [diff] [blame] | 23 | import {Configuration} from '../../aos/configuration_generated'; |
Philipp Schrader | 548aedf | 2023-02-17 20:09:13 -0800 | [diff] [blame] | 24 | import {plotDemo} from '../../aos/network/www/demo_plot'; |
James Kuszmaul | 2e818b2 | 2024-02-24 09:02:42 -0800 | [diff] [blame] | 25 | import {Connection} from '../../aos/network/www/proxy'; |
| 26 | import {plotLocalizer as plot2020Localizer} from '../../y2020/control_loops/drivetrain/localizer_plotter' |
| 27 | import {plotAccelerator as plot2020Accelerator} from '../../y2020/control_loops/superstructure/accelerator_plotter' |
| 28 | import {plotFinisher as plot2020Finisher} from '../../y2020/control_loops/superstructure/finisher_plotter' |
| 29 | import {plotHood as plot2020Hood} from '../../y2020/control_loops/superstructure/hood_plotter' |
| 30 | import {plotTurret as plot2020Turret} from '../../y2020/control_loops/superstructure/turret_plotter' |
| 31 | import {plotSuperstructure as plot2021Superstructure} from '../../y2021_bot3/control_loops/superstructure/superstructure_plotter'; |
| 32 | import {plotCatapult as plot2022Catapult} from '../../y2022/control_loops/superstructure/catapult_plotter' |
| 33 | import {plotClimber as plot2022Climber} from '../../y2022/control_loops/superstructure/climber_plotter' |
| 34 | import {plotIntakeBack as plot2022IntakeBack, plotIntakeFront as plot2022IntakeFront} from '../../y2022/control_loops/superstructure/intake_plotter' |
| 35 | import {plotSuperstructure as plot2022Superstructure} from '../../y2022/control_loops/superstructure/superstructure_plotter' |
| 36 | import {plotTurret as plot2022Turret} from '../../y2022/control_loops/superstructure/turret_plotter' |
| 37 | import {plotLocalizer as plot2022Localizer} from '../../y2022/localizer/localizer_plotter' |
| 38 | import {plotVision as plot2022Vision} from '../../y2022/vision/vision_plotter' |
| 39 | import {plotSuperstructure as plot2023Superstructure} from '../../y2023/control_loops/superstructure/superstructure_plotter' |
| 40 | import {plotVision as plot2023Corrections} from '../../y2023/localizer/corrections_plotter' |
| 41 | import {plotLocalizer as plot2023Localizer} from '../../y2023/localizer/localizer_plotter' |
| 42 | import {plotClimber as plot2024Climber, plotIntake as plot2024Intake, plotSuperstructure as plot2024Superstructure} from '../../y2024/control_loops/superstructure/superstructure_plotter' |
| 43 | import {plotVision as plot2024Corrections} from '../../y2024/localizer/corrections_plotter' |
| 44 | import {plotLocalizer as plot2024Localizer} from '../../y2024/localizer/localizer_plotter' |
| 45 | import {plotDownEstimator} from '../control_loops/drivetrain/down_estimator_plotter'; |
| 46 | import {plotDrivetrain} from '../control_loops/drivetrain/drivetrain_plotter'; |
| 47 | import {plotRobotState} from '../control_loops/drivetrain/robot_state_plotter' |
| 48 | import {plotSpline} from '../control_loops/drivetrain/spline_plotter'; |
| 49 | import {plotImu} from '../wpilib/imu_plotter'; |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 50 | |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 51 | const rootDiv = document.createElement('div'); |
Austin Schuh | 5ec17ef | 2022-07-15 14:37:16 -0700 | [diff] [blame] | 52 | rootDiv.style.width = '100%'; |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 53 | document.body.appendChild(rootDiv); |
| 54 | |
| 55 | const helpDiv = document.createElement('div'); |
| 56 | rootDiv.appendChild(helpDiv); |
| 57 | helpDiv.innerHTML = |
James Kuszmaul | 461b068 | 2020-12-22 22:20:21 -0800 | [diff] [blame] | 58 | 'Help: click + drag to pan, double click to reset, scroll to zoom, ' + |
| 59 | 'right-click + drag to zoom to rectangle, Esc to cancel. ' + |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 60 | 'Hold the x/y keys to only pan/zoom along the x/y axes.'; |
| 61 | |
| 62 | class PlotState { |
| 63 | public readonly div: HTMLElement; |
| 64 | private initialized = false; |
| 65 | constructor( |
| 66 | parentDiv: HTMLElement, |
| 67 | private readonly initializer: |
| 68 | (conn: Connection, element: Element) => void) { |
| 69 | this.div = document.createElement('div'); |
| 70 | parentDiv.appendChild(this.div); |
| 71 | this.hide(); |
| 72 | } |
| 73 | initialize(conn: Connection): void { |
| 74 | if (!this.initialized) { |
| 75 | this.initializer(conn, this.div); |
| 76 | this.initialized = true; |
| 77 | } |
| 78 | } |
| 79 | hide(): void { |
| 80 | this.div.style.display = "none"; |
| 81 | } |
| 82 | show(): void { |
| 83 | this.div.style.display = "block"; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | const plotSelect = document.createElement('select'); |
| 88 | rootDiv.appendChild(plotSelect); |
| 89 | |
| 90 | const plotDiv = document.createElement('div'); |
Austin Schuh | 975c594 | 2022-07-15 14:38:10 -0700 | [diff] [blame] | 91 | plotDiv.style.marginTop = '10px'; |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 92 | plotDiv.style.left = '0'; |
| 93 | plotDiv.style.position = 'absolute'; |
Austin Schuh | 5ec17ef | 2022-07-15 14:37:16 -0700 | [diff] [blame] | 94 | plotDiv.style.width = '100%'; |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 95 | rootDiv.appendChild(plotDiv); |
| 96 | |
| 97 | // The master list of all the plots that we provide. For a given config, it |
| 98 | // is possible that not all of these plots will be usable depending on the |
| 99 | // presence of certain channels. |
| 100 | const plotIndex = new Map<string, PlotState>([ |
| 101 | ['Demo', new PlotState(plotDiv, plotDemo)], |
James Kuszmaul | 4867136 | 2020-12-24 13:54:16 -0800 | [diff] [blame] | 102 | ['IMU', new PlotState(plotDiv, plotImu)], |
James Kuszmaul | c4ae11c | 2020-12-26 16:26:58 -0800 | [diff] [blame] | 103 | ['Drivetrain', new PlotState(plotDiv, plotDrivetrain)], |
James Kuszmaul | 73fc135 | 2021-04-09 22:31:25 -0700 | [diff] [blame] | 104 | ['Spline Debug', new PlotState(plotDiv, plotSpline)], |
James Kuszmaul | ac2b6b4 | 2021-03-07 22:38:06 -0800 | [diff] [blame] | 105 | ['Down Estimator', new PlotState(plotDiv, plotDownEstimator)], |
milind upadhyay | 9bd381d | 2021-01-23 13:44:13 -0800 | [diff] [blame] | 106 | ['Robot State', new PlotState(plotDiv, plotRobotState)], |
James Kuszmaul | 313e9ce | 2024-02-11 17:47:33 -0800 | [diff] [blame] | 107 | ['2024 Vision', new PlotState(plotDiv, plot2024Corrections)], |
| 108 | ['2024 Localizer', new PlotState(plotDiv, plot2024Localizer)], |
James Kuszmaul | 2e818b2 | 2024-02-24 09:02:42 -0800 | [diff] [blame] | 109 | ['2024 Superstructure', new PlotState(plotDiv, plot2024Superstructure)], |
| 110 | ['2024 Climber', new PlotState(plotDiv, plot2024Climber)], |
| 111 | ['2024 Intake', new PlotState(plotDiv, plot2024Intake)], |
James Kuszmaul | a8e0d6e | 2023-03-12 13:33:36 -0700 | [diff] [blame] | 112 | ['2023 Vision', new PlotState(plotDiv, plot2023Corrections)], |
James Kuszmaul | 827a6d6 | 2023-03-26 12:40:29 -0700 | [diff] [blame] | 113 | ['2023 Localizer', new PlotState(plotDiv, plot2023Localizer)], |
James Kuszmaul | 81e7184 | 2023-09-29 15:25:13 -0700 | [diff] [blame] | 114 | ['2023 Superstructure', new PlotState(plotDiv, plot2023Superstructure)], |
Milind Upadhyay | eb739bb | 2022-03-02 10:49:21 -0800 | [diff] [blame] | 115 | ['2020 Finisher', new PlotState(plotDiv, plot2020Finisher)], |
| 116 | ['2020 Accelerator', new PlotState(plotDiv, plot2020Accelerator)], |
| 117 | ['2020 Hood', new PlotState(plotDiv, plot2020Hood)], |
| 118 | ['2020 Turret', new PlotState(plotDiv, plot2020Turret)], |
James Kuszmaul | f7c8a09 | 2022-02-12 16:46:09 -0800 | [diff] [blame] | 119 | ['2020 Localizer', new PlotState(plotDiv, plot2020Localizer)], |
Milind Upadhyay | eb739bb | 2022-03-02 10:49:21 -0800 | [diff] [blame] | 120 | ['2022 Localizer', new PlotState(plotDiv, plot2022Localizer)], |
James Kuszmaul | b35e234 | 2022-03-06 15:44:00 -0800 | [diff] [blame] | 121 | ['2022 Vision', new PlotState(plotDiv, plot2022Vision)], |
Ravago Jones | b64988c | 2022-03-06 15:05:01 -0800 | [diff] [blame] | 122 | ['2022 Superstructure', new PlotState(plotDiv, plot2022Superstructure)], |
Austin Schuh | 76f227c | 2022-02-23 16:34:08 -0800 | [diff] [blame] | 123 | ['2022 Catapult', new PlotState(plotDiv, plot2022Catapult)], |
Ravago Jones | 8b00478 | 2022-03-05 17:23:08 -0800 | [diff] [blame] | 124 | ['2022 Intake Front', new PlotState(plotDiv, plot2022IntakeFront)], |
| 125 | ['2022 Intake Back', new PlotState(plotDiv, plot2022IntakeBack)], |
Milind Upadhyay | eb739bb | 2022-03-02 10:49:21 -0800 | [diff] [blame] | 126 | ['2022 Climber', new PlotState(plotDiv, plot2022Climber)], |
| 127 | ['2022 Turret', new PlotState(plotDiv, plot2022Turret)], |
Milind Upadhyay | eb739bb | 2022-03-02 10:49:21 -0800 | [diff] [blame] | 128 | ['Y2021 3rd Robot Superstructure', new PlotState(plotDiv, plot2021Superstructure)], |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 129 | ]); |
| 130 | |
| 131 | const invalidSelectValue = 'null'; |
| 132 | function getDefaultPlot(): string { |
| 133 | const urlParams = (new URL(document.URL)).searchParams; |
| 134 | const urlParamKey = 'plot'; |
| 135 | if (!urlParams.has(urlParamKey)) { |
| 136 | return invalidSelectValue; |
| 137 | } |
| 138 | const desiredPlot = urlParams.get(urlParamKey); |
| 139 | if (!plotIndex.has(desiredPlot)) { |
| 140 | return invalidSelectValue; |
| 141 | } |
| 142 | return desiredPlot; |
| 143 | } |
| 144 | |
| 145 | const conn = new Connection(); |
| 146 | |
James Kuszmaul | b8989f7 | 2021-03-07 20:00:02 -0800 | [diff] [blame] | 147 | let reloadOnChange = false; |
| 148 | |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 149 | conn.connect(); |
| 150 | |
| 151 | conn.addConfigHandler((config: Configuration) => { |
| 152 | plotSelect.add(new Option("Select Plot", invalidSelectValue)); |
| 153 | for (const name of plotIndex.keys()) { |
| 154 | plotSelect.add(new Option(name, name)); |
| 155 | } |
| 156 | plotSelect.addEventListener('input', () => { |
| 157 | for (const plot of plotIndex.values()) { |
| 158 | plot.hide(); |
| 159 | } |
| 160 | if (plotSelect.value == invalidSelectValue) { |
| 161 | return; |
| 162 | } |
| 163 | plotIndex.get(plotSelect.value).initialize(conn); |
| 164 | plotIndex.get(plotSelect.value).show(); |
James Kuszmaul | c4ae11c | 2020-12-26 16:26:58 -0800 | [diff] [blame] | 165 | // Set the URL so that if you reload you get back to this plot. |
| 166 | window.history.replaceState( |
| 167 | null, null, '?plot=' + encodeURIComponent(plotSelect.value)); |
James Kuszmaul | b8989f7 | 2021-03-07 20:00:02 -0800 | [diff] [blame] | 168 | if (reloadOnChange) { |
| 169 | window.location.reload(); |
| 170 | } |
| 171 | reloadOnChange = true; |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 172 | }); |
| 173 | plotSelect.value = getDefaultPlot(); |
| 174 | // Force the event to occur once at the start. |
| 175 | plotSelect.dispatchEvent(new Event('input')); |
James Kuszmaul | f7c8a09 | 2022-02-12 16:46:09 -0800 | [diff] [blame] | 176 | }); |