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. |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 23 | import {Configuration} from 'org_frc971/aos/configuration_generated'; |
| 24 | import {Connection} from 'org_frc971/aos/network/www/proxy'; |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 25 | import {plotImu} from 'org_frc971/frc971/wpilib/imu_plotter'; |
James Kuszmaul | c4ae11c | 2020-12-26 16:26:58 -0800 | [diff] [blame] | 26 | import {plotDrivetrain} from 'org_frc971/frc971/control_loops/drivetrain/drivetrain_plotter'; |
James Kuszmaul | 73fc135 | 2021-04-09 22:31:25 -0700 | [diff] [blame] | 27 | import {plotSpline} from 'org_frc971/frc971/control_loops/drivetrain/spline_plotter'; |
James Kuszmaul | ac2b6b4 | 2021-03-07 22:38:06 -0800 | [diff] [blame] | 28 | import {plotDownEstimator} from 'org_frc971/frc971/control_loops/drivetrain/down_estimator_plotter'; |
milind upadhyay | 9bd381d | 2021-01-23 13:44:13 -0800 | [diff] [blame] | 29 | import {plotRobotState} from |
| 30 | 'org_frc971/frc971/control_loops/drivetrain/robot_state_plotter' |
Milind Upadhyay | eb739bb | 2022-03-02 10:49:21 -0800 | [diff] [blame] | 31 | import {plotFinisher as plot2020Finisher} from |
Austin Schuh | 7d63eab | 2021-03-06 20:15:02 -0800 | [diff] [blame] | 32 | 'org_frc971/y2020/control_loops/superstructure/finisher_plotter' |
Milind Upadhyay | eb739bb | 2022-03-02 10:49:21 -0800 | [diff] [blame] | 33 | import {plotTurret as plot2020Turret} from |
James Kuszmaul | 7810140 | 2021-09-11 12:42:21 -0700 | [diff] [blame] | 34 | 'org_frc971/y2020/control_loops/superstructure/turret_plotter' |
James Kuszmaul | f7c8a09 | 2022-02-12 16:46:09 -0800 | [diff] [blame] | 35 | import {plotLocalizer as plot2020Localizer} from |
James Kuszmaul | 9c23d26 | 2021-09-25 21:50:02 -0700 | [diff] [blame] | 36 | 'org_frc971/y2020/control_loops/drivetrain/localizer_plotter' |
Milind Upadhyay | eb739bb | 2022-03-02 10:49:21 -0800 | [diff] [blame] | 37 | import {plotAccelerator as plot2020Accelerator} from |
| 38 | 'org_frc971/y2020/control_loops/superstructure/accelerator_plotter' |
| 39 | import {plotHood as plot2020Hood} from |
| 40 | 'org_frc971/y2020/control_loops/superstructure/hood_plotter' |
| 41 | import {plotSuperstructure as plot2021Superstructure} from |
| 42 | 'org_frc971/y2021_bot3/control_loops/superstructure/superstructure_plotter'; |
| 43 | import {plotTurret as plot2022Turret} from |
| 44 | 'org_frc971/y2022/control_loops/superstructure/turret_plotter' |
Ravago Jones | b64988c | 2022-03-06 15:05:01 -0800 | [diff] [blame] | 45 | import {plotSuperstructure as plot2022Superstructure} from |
| 46 | 'org_frc971/y2022/control_loops/superstructure/superstructure_plotter' |
Austin Schuh | 76f227c | 2022-02-23 16:34:08 -0800 | [diff] [blame] | 47 | import {plotCatapult as plot2022Catapult} from |
| 48 | 'org_frc971/y2022/control_loops/superstructure/catapult_plotter' |
Ravago Jones | 8b00478 | 2022-03-05 17:23:08 -0800 | [diff] [blame] | 49 | import {plotIntakeFront as plot2022IntakeFront, plotIntakeBack as plot2022IntakeBack} from |
Milind Upadhyay | eb739bb | 2022-03-02 10:49:21 -0800 | [diff] [blame] | 50 | 'org_frc971/y2022/control_loops/superstructure/intake_plotter' |
| 51 | import {plotClimber as plot2022Climber} from |
| 52 | 'org_frc971/y2022/control_loops/superstructure/climber_plotter' |
James Kuszmaul | f7c8a09 | 2022-02-12 16:46:09 -0800 | [diff] [blame] | 53 | import {plotLocalizer as plot2022Localizer} from |
James Kuszmaul | 51fa1ae | 2022-02-26 00:49:57 -0800 | [diff] [blame] | 54 | 'org_frc971/y2022/localizer/localizer_plotter' |
James Kuszmaul | b35e234 | 2022-03-06 15:44:00 -0800 | [diff] [blame] | 55 | import {plotVision as plot2022Vision} from |
| 56 | 'org_frc971/y2022/vision/vision_plotter' |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 57 | import {plotDemo} from 'org_frc971/aos/network/www/demo_plot'; |
James Kuszmaul | 4867136 | 2020-12-24 13:54:16 -0800 | [diff] [blame] | 58 | import {plotData} from 'org_frc971/frc971/analysis/plot_data_utils'; |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 59 | |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 60 | const rootDiv = document.createElement('div'); |
Austin Schuh | 5ec17ef | 2022-07-15 14:37:16 -0700 | [diff] [blame^] | 61 | rootDiv.style.width = '100%'; |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 62 | document.body.appendChild(rootDiv); |
| 63 | |
| 64 | const helpDiv = document.createElement('div'); |
| 65 | rootDiv.appendChild(helpDiv); |
| 66 | helpDiv.innerHTML = |
James Kuszmaul | 461b068 | 2020-12-22 22:20:21 -0800 | [diff] [blame] | 67 | 'Help: click + drag to pan, double click to reset, scroll to zoom, ' + |
| 68 | 'right-click + drag to zoom to rectangle, Esc to cancel. ' + |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 69 | 'Hold the x/y keys to only pan/zoom along the x/y axes.'; |
| 70 | |
| 71 | class PlotState { |
| 72 | public readonly div: HTMLElement; |
| 73 | private initialized = false; |
| 74 | constructor( |
| 75 | parentDiv: HTMLElement, |
| 76 | private readonly initializer: |
| 77 | (conn: Connection, element: Element) => void) { |
| 78 | this.div = document.createElement('div'); |
| 79 | parentDiv.appendChild(this.div); |
| 80 | this.hide(); |
| 81 | } |
| 82 | initialize(conn: Connection): void { |
| 83 | if (!this.initialized) { |
| 84 | this.initializer(conn, this.div); |
| 85 | this.initialized = true; |
| 86 | } |
| 87 | } |
| 88 | hide(): void { |
| 89 | this.div.style.display = "none"; |
| 90 | } |
| 91 | show(): void { |
| 92 | this.div.style.display = "block"; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | const plotSelect = document.createElement('select'); |
| 97 | rootDiv.appendChild(plotSelect); |
| 98 | |
| 99 | const plotDiv = document.createElement('div'); |
| 100 | plotDiv.style.top = (plotSelect.getBoundingClientRect().bottom + 10).toString(); |
| 101 | plotDiv.style.left = '0'; |
| 102 | plotDiv.style.position = 'absolute'; |
Austin Schuh | 5ec17ef | 2022-07-15 14:37:16 -0700 | [diff] [blame^] | 103 | plotDiv.style.width = '100%'; |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 104 | rootDiv.appendChild(plotDiv); |
| 105 | |
| 106 | // The master list of all the plots that we provide. For a given config, it |
| 107 | // is possible that not all of these plots will be usable depending on the |
| 108 | // presence of certain channels. |
| 109 | const plotIndex = new Map<string, PlotState>([ |
| 110 | ['Demo', new PlotState(plotDiv, plotDemo)], |
James Kuszmaul | 4867136 | 2020-12-24 13:54:16 -0800 | [diff] [blame] | 111 | ['IMU', new PlotState(plotDiv, plotImu)], |
James Kuszmaul | c4ae11c | 2020-12-26 16:26:58 -0800 | [diff] [blame] | 112 | ['Drivetrain', new PlotState(plotDiv, plotDrivetrain)], |
James Kuszmaul | 73fc135 | 2021-04-09 22:31:25 -0700 | [diff] [blame] | 113 | ['Spline Debug', new PlotState(plotDiv, plotSpline)], |
James Kuszmaul | ac2b6b4 | 2021-03-07 22:38:06 -0800 | [diff] [blame] | 114 | ['Down Estimator', new PlotState(plotDiv, plotDownEstimator)], |
milind upadhyay | 9bd381d | 2021-01-23 13:44:13 -0800 | [diff] [blame] | 115 | ['Robot State', new PlotState(plotDiv, plotRobotState)], |
Milind Upadhyay | eb739bb | 2022-03-02 10:49:21 -0800 | [diff] [blame] | 116 | ['2020 Finisher', new PlotState(plotDiv, plot2020Finisher)], |
| 117 | ['2020 Accelerator', new PlotState(plotDiv, plot2020Accelerator)], |
| 118 | ['2020 Hood', new PlotState(plotDiv, plot2020Hood)], |
| 119 | ['2020 Turret', new PlotState(plotDiv, plot2020Turret)], |
James Kuszmaul | f7c8a09 | 2022-02-12 16:46:09 -0800 | [diff] [blame] | 120 | ['2020 Localizer', new PlotState(plotDiv, plot2020Localizer)], |
Milind Upadhyay | eb739bb | 2022-03-02 10:49:21 -0800 | [diff] [blame] | 121 | ['2022 Localizer', new PlotState(plotDiv, plot2022Localizer)], |
James Kuszmaul | b35e234 | 2022-03-06 15:44:00 -0800 | [diff] [blame] | 122 | ['2022 Vision', new PlotState(plotDiv, plot2022Vision)], |
Ravago Jones | b64988c | 2022-03-06 15:05:01 -0800 | [diff] [blame] | 123 | ['2022 Superstructure', new PlotState(plotDiv, plot2022Superstructure)], |
Austin Schuh | 76f227c | 2022-02-23 16:34:08 -0800 | [diff] [blame] | 124 | ['2022 Catapult', new PlotState(plotDiv, plot2022Catapult)], |
Ravago Jones | 8b00478 | 2022-03-05 17:23:08 -0800 | [diff] [blame] | 125 | ['2022 Intake Front', new PlotState(plotDiv, plot2022IntakeFront)], |
| 126 | ['2022 Intake Back', new PlotState(plotDiv, plot2022IntakeBack)], |
Milind Upadhyay | eb739bb | 2022-03-02 10:49:21 -0800 | [diff] [blame] | 127 | ['2022 Climber', new PlotState(plotDiv, plot2022Climber)], |
| 128 | ['2022 Turret', new PlotState(plotDiv, plot2022Turret)], |
James Kuszmaul | 4867136 | 2020-12-24 13:54:16 -0800 | [diff] [blame] | 129 | ['C++ Plotter', new PlotState(plotDiv, plotData)], |
Milind Upadhyay | eb739bb | 2022-03-02 10:49:21 -0800 | [diff] [blame] | 130 | ['Y2021 3rd Robot Superstructure', new PlotState(plotDiv, plot2021Superstructure)], |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 131 | ]); |
| 132 | |
| 133 | const invalidSelectValue = 'null'; |
| 134 | function getDefaultPlot(): string { |
| 135 | const urlParams = (new URL(document.URL)).searchParams; |
| 136 | const urlParamKey = 'plot'; |
| 137 | if (!urlParams.has(urlParamKey)) { |
| 138 | return invalidSelectValue; |
| 139 | } |
| 140 | const desiredPlot = urlParams.get(urlParamKey); |
| 141 | if (!plotIndex.has(desiredPlot)) { |
| 142 | return invalidSelectValue; |
| 143 | } |
| 144 | return desiredPlot; |
| 145 | } |
| 146 | |
| 147 | const conn = new Connection(); |
| 148 | |
James Kuszmaul | b8989f7 | 2021-03-07 20:00:02 -0800 | [diff] [blame] | 149 | let reloadOnChange = false; |
| 150 | |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 151 | conn.connect(); |
| 152 | |
| 153 | conn.addConfigHandler((config: Configuration) => { |
| 154 | plotSelect.add(new Option("Select Plot", invalidSelectValue)); |
| 155 | for (const name of plotIndex.keys()) { |
| 156 | plotSelect.add(new Option(name, name)); |
| 157 | } |
| 158 | plotSelect.addEventListener('input', () => { |
| 159 | for (const plot of plotIndex.values()) { |
| 160 | plot.hide(); |
| 161 | } |
| 162 | if (plotSelect.value == invalidSelectValue) { |
| 163 | return; |
| 164 | } |
| 165 | plotIndex.get(plotSelect.value).initialize(conn); |
| 166 | plotIndex.get(plotSelect.value).show(); |
James Kuszmaul | c4ae11c | 2020-12-26 16:26:58 -0800 | [diff] [blame] | 167 | // Set the URL so that if you reload you get back to this plot. |
| 168 | window.history.replaceState( |
| 169 | null, null, '?plot=' + encodeURIComponent(plotSelect.value)); |
James Kuszmaul | b8989f7 | 2021-03-07 20:00:02 -0800 | [diff] [blame] | 170 | if (reloadOnChange) { |
| 171 | window.location.reload(); |
| 172 | } |
| 173 | reloadOnChange = true; |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 174 | }); |
| 175 | plotSelect.value = getDefaultPlot(); |
| 176 | // Force the event to occur once at the start. |
| 177 | plotSelect.dispatchEvent(new Event('input')); |
James Kuszmaul | f7c8a09 | 2022-02-12 16:46:09 -0800 | [diff] [blame] | 178 | }); |