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. |
| 23 | import * as configuration from 'org_frc971/aos/configuration_generated'; |
| 24 | import * as proxy from 'org_frc971/aos/network/www/proxy'; |
| 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' |
Austin Schuh | 7d63eab | 2021-03-06 20:15:02 -0800 | [diff] [blame] | 31 | import {plotFinisher} from |
| 32 | 'org_frc971/y2020/control_loops/superstructure/finisher_plotter' |
James Kuszmaul | 7810140 | 2021-09-11 12:42:21 -0700 | [diff] [blame] | 33 | import {plotTurret} from |
| 34 | 'org_frc971/y2020/control_loops/superstructure/turret_plotter' |
James Kuszmaul | 9c23d26 | 2021-09-25 21:50:02 -0700 | [diff] [blame^] | 35 | import {plotLocalizer} from |
| 36 | 'org_frc971/y2020/control_loops/drivetrain/localizer_plotter' |
Austin Schuh | 7d63eab | 2021-03-06 20:15:02 -0800 | [diff] [blame] | 37 | import {plotAccelerator} from |
| 38 | 'org_frc971/y2020/control_loops/superstructure/accelerator_plotter' |
Austin Schuh | 2efe168 | 2021-03-06 22:47:15 -0800 | [diff] [blame] | 39 | import {plotHood} from |
| 40 | 'org_frc971/y2020/control_loops/superstructure/hood_plotter' |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 41 | import {plotDemo} from 'org_frc971/aos/network/www/demo_plot'; |
James Kuszmaul | 4867136 | 2020-12-24 13:54:16 -0800 | [diff] [blame] | 42 | import {plotData} from 'org_frc971/frc971/analysis/plot_data_utils'; |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 43 | |
| 44 | import Connection = proxy.Connection; |
| 45 | import Configuration = configuration.aos.Configuration; |
| 46 | |
| 47 | const rootDiv = document.createElement('div'); |
| 48 | document.body.appendChild(rootDiv); |
| 49 | |
| 50 | const helpDiv = document.createElement('div'); |
| 51 | rootDiv.appendChild(helpDiv); |
| 52 | helpDiv.innerHTML = |
James Kuszmaul | 461b068 | 2020-12-22 22:20:21 -0800 | [diff] [blame] | 53 | 'Help: click + drag to pan, double click to reset, scroll to zoom, ' + |
| 54 | 'right-click + drag to zoom to rectangle, Esc to cancel. ' + |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 55 | 'Hold the x/y keys to only pan/zoom along the x/y axes.'; |
| 56 | |
| 57 | class PlotState { |
| 58 | public readonly div: HTMLElement; |
| 59 | private initialized = false; |
| 60 | constructor( |
| 61 | parentDiv: HTMLElement, |
| 62 | private readonly initializer: |
| 63 | (conn: Connection, element: Element) => void) { |
| 64 | this.div = document.createElement('div'); |
| 65 | parentDiv.appendChild(this.div); |
| 66 | this.hide(); |
| 67 | } |
| 68 | initialize(conn: Connection): void { |
| 69 | if (!this.initialized) { |
| 70 | this.initializer(conn, this.div); |
| 71 | this.initialized = true; |
| 72 | } |
| 73 | } |
| 74 | hide(): void { |
| 75 | this.div.style.display = "none"; |
| 76 | } |
| 77 | show(): void { |
| 78 | this.div.style.display = "block"; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | const plotSelect = document.createElement('select'); |
| 83 | rootDiv.appendChild(plotSelect); |
| 84 | |
| 85 | const plotDiv = document.createElement('div'); |
| 86 | plotDiv.style.top = (plotSelect.getBoundingClientRect().bottom + 10).toString(); |
| 87 | plotDiv.style.left = '0'; |
| 88 | plotDiv.style.position = 'absolute'; |
| 89 | rootDiv.appendChild(plotDiv); |
| 90 | |
| 91 | // The master list of all the plots that we provide. For a given config, it |
| 92 | // is possible that not all of these plots will be usable depending on the |
| 93 | // presence of certain channels. |
| 94 | const plotIndex = new Map<string, PlotState>([ |
| 95 | ['Demo', new PlotState(plotDiv, plotDemo)], |
James Kuszmaul | 4867136 | 2020-12-24 13:54:16 -0800 | [diff] [blame] | 96 | ['IMU', new PlotState(plotDiv, plotImu)], |
James Kuszmaul | c4ae11c | 2020-12-26 16:26:58 -0800 | [diff] [blame] | 97 | ['Drivetrain', new PlotState(plotDiv, plotDrivetrain)], |
James Kuszmaul | 73fc135 | 2021-04-09 22:31:25 -0700 | [diff] [blame] | 98 | ['Spline Debug', new PlotState(plotDiv, plotSpline)], |
James Kuszmaul | ac2b6b4 | 2021-03-07 22:38:06 -0800 | [diff] [blame] | 99 | ['Down Estimator', new PlotState(plotDiv, plotDownEstimator)], |
milind upadhyay | 9bd381d | 2021-01-23 13:44:13 -0800 | [diff] [blame] | 100 | ['Robot State', new PlotState(plotDiv, plotRobotState)], |
Austin Schuh | 7d63eab | 2021-03-06 20:15:02 -0800 | [diff] [blame] | 101 | ['Finisher', new PlotState(plotDiv, plotFinisher)], |
| 102 | ['Accelerator', new PlotState(plotDiv, plotAccelerator)], |
Austin Schuh | 2efe168 | 2021-03-06 22:47:15 -0800 | [diff] [blame] | 103 | ['Hood', new PlotState(plotDiv, plotHood)], |
James Kuszmaul | 7810140 | 2021-09-11 12:42:21 -0700 | [diff] [blame] | 104 | ['Turret', new PlotState(plotDiv, plotTurret)], |
James Kuszmaul | 9c23d26 | 2021-09-25 21:50:02 -0700 | [diff] [blame^] | 105 | ['2020 Localizer', new PlotState(plotDiv, plotLocalizer)], |
James Kuszmaul | 4867136 | 2020-12-24 13:54:16 -0800 | [diff] [blame] | 106 | ['C++ Plotter', new PlotState(plotDiv, plotData)], |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 107 | ]); |
| 108 | |
| 109 | const invalidSelectValue = 'null'; |
| 110 | function getDefaultPlot(): string { |
| 111 | const urlParams = (new URL(document.URL)).searchParams; |
| 112 | const urlParamKey = 'plot'; |
| 113 | if (!urlParams.has(urlParamKey)) { |
| 114 | return invalidSelectValue; |
| 115 | } |
| 116 | const desiredPlot = urlParams.get(urlParamKey); |
| 117 | if (!plotIndex.has(desiredPlot)) { |
| 118 | return invalidSelectValue; |
| 119 | } |
| 120 | return desiredPlot; |
| 121 | } |
| 122 | |
| 123 | const conn = new Connection(); |
| 124 | |
James Kuszmaul | b8989f7 | 2021-03-07 20:00:02 -0800 | [diff] [blame] | 125 | let reloadOnChange = false; |
| 126 | |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 127 | conn.connect(); |
| 128 | |
| 129 | conn.addConfigHandler((config: Configuration) => { |
| 130 | plotSelect.add(new Option("Select Plot", invalidSelectValue)); |
| 131 | for (const name of plotIndex.keys()) { |
| 132 | plotSelect.add(new Option(name, name)); |
| 133 | } |
| 134 | plotSelect.addEventListener('input', () => { |
| 135 | for (const plot of plotIndex.values()) { |
| 136 | plot.hide(); |
| 137 | } |
| 138 | if (plotSelect.value == invalidSelectValue) { |
| 139 | return; |
| 140 | } |
| 141 | plotIndex.get(plotSelect.value).initialize(conn); |
| 142 | plotIndex.get(plotSelect.value).show(); |
James Kuszmaul | c4ae11c | 2020-12-26 16:26:58 -0800 | [diff] [blame] | 143 | // Set the URL so that if you reload you get back to this plot. |
| 144 | window.history.replaceState( |
| 145 | null, null, '?plot=' + encodeURIComponent(plotSelect.value)); |
James Kuszmaul | b8989f7 | 2021-03-07 20:00:02 -0800 | [diff] [blame] | 146 | if (reloadOnChange) { |
| 147 | window.location.reload(); |
| 148 | } |
| 149 | reloadOnChange = true; |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 150 | }); |
| 151 | plotSelect.value = getDefaultPlot(); |
| 152 | // Force the event to occur once at the start. |
| 153 | plotSelect.dispatchEvent(new Event('input')); |
| 154 | }); |