blob: 57d50417e121460b47d17cd392304d5c0dd3004b [file] [log] [blame]
James Kuszmaul5f5e1232020-12-22 20:58:00 -08001// 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 Schrader548aedf2023-02-17 20:09:13 -080023import {Configuration} from '../../aos/configuration_generated';
24import {Connection} from '../../aos/network/www/proxy';
25import {plotImu} from '../wpilib/imu_plotter';
26import {plotDrivetrain} from '../control_loops/drivetrain/drivetrain_plotter';
27import {plotSpline} from '../control_loops/drivetrain/spline_plotter';
28import {plotDownEstimator} from '../control_loops/drivetrain/down_estimator_plotter';
milind upadhyay9bd381d2021-01-23 13:44:13 -080029import {plotRobotState} from
Philipp Schrader548aedf2023-02-17 20:09:13 -080030 '../control_loops/drivetrain/robot_state_plotter'
Milind Upadhyayeb739bb2022-03-02 10:49:21 -080031import {plotFinisher as plot2020Finisher} from
Philipp Schrader548aedf2023-02-17 20:09:13 -080032 '../../y2020/control_loops/superstructure/finisher_plotter'
Milind Upadhyayeb739bb2022-03-02 10:49:21 -080033import {plotTurret as plot2020Turret} from
Philipp Schrader548aedf2023-02-17 20:09:13 -080034 '../../y2020/control_loops/superstructure/turret_plotter'
James Kuszmaulf7c8a092022-02-12 16:46:09 -080035import {plotLocalizer as plot2020Localizer} from
Philipp Schrader548aedf2023-02-17 20:09:13 -080036 '../../y2020/control_loops/drivetrain/localizer_plotter'
Milind Upadhyayeb739bb2022-03-02 10:49:21 -080037import {plotAccelerator as plot2020Accelerator} from
Philipp Schrader548aedf2023-02-17 20:09:13 -080038 '../../y2020/control_loops/superstructure/accelerator_plotter'
Milind Upadhyayeb739bb2022-03-02 10:49:21 -080039import {plotHood as plot2020Hood} from
Philipp Schrader548aedf2023-02-17 20:09:13 -080040 '../../y2020/control_loops/superstructure/hood_plotter'
Milind Upadhyayeb739bb2022-03-02 10:49:21 -080041import {plotSuperstructure as plot2021Superstructure} from
Philipp Schrader548aedf2023-02-17 20:09:13 -080042 '../../y2021_bot3/control_loops/superstructure/superstructure_plotter';
Milind Upadhyayeb739bb2022-03-02 10:49:21 -080043import {plotTurret as plot2022Turret} from
Philipp Schrader548aedf2023-02-17 20:09:13 -080044 '../../y2022/control_loops/superstructure/turret_plotter'
Ravago Jonesb64988c2022-03-06 15:05:01 -080045import {plotSuperstructure as plot2022Superstructure} from
Philipp Schrader548aedf2023-02-17 20:09:13 -080046 '../../y2022/control_loops/superstructure/superstructure_plotter'
Austin Schuh76f227c2022-02-23 16:34:08 -080047import {plotCatapult as plot2022Catapult} from
Philipp Schrader548aedf2023-02-17 20:09:13 -080048 '../../y2022/control_loops/superstructure/catapult_plotter'
Ravago Jones8b004782022-03-05 17:23:08 -080049import {plotIntakeFront as plot2022IntakeFront, plotIntakeBack as plot2022IntakeBack} from
Philipp Schrader548aedf2023-02-17 20:09:13 -080050 '../../y2022/control_loops/superstructure/intake_plotter'
Milind Upadhyayeb739bb2022-03-02 10:49:21 -080051import {plotClimber as plot2022Climber} from
Philipp Schrader548aedf2023-02-17 20:09:13 -080052 '../../y2022/control_loops/superstructure/climber_plotter'
James Kuszmaulf7c8a092022-02-12 16:46:09 -080053import {plotLocalizer as plot2022Localizer} from
Philipp Schrader548aedf2023-02-17 20:09:13 -080054 '../../y2022/localizer/localizer_plotter'
James Kuszmaul827a6d62023-03-26 12:40:29 -070055import {plotLocalizer as plot2023Localizer} from
56 '../../y2023/localizer/localizer_plotter'
James Kuszmaulb35e2342022-03-06 15:44:00 -080057import {plotVision as plot2022Vision} from
Philipp Schrader548aedf2023-02-17 20:09:13 -080058 '../../y2022/vision/vision_plotter'
James Kuszmaula8e0d6e2023-03-12 13:33:36 -070059import {plotVision as plot2023Corrections} from
60 '../../y2023/localizer/corrections_plotter'
Philipp Schrader548aedf2023-02-17 20:09:13 -080061import {plotDemo} from '../../aos/network/www/demo_plot';
James Kuszmaul5f5e1232020-12-22 20:58:00 -080062
James Kuszmaul5f5e1232020-12-22 20:58:00 -080063const rootDiv = document.createElement('div');
Austin Schuh5ec17ef2022-07-15 14:37:16 -070064rootDiv.style.width = '100%';
James Kuszmaul5f5e1232020-12-22 20:58:00 -080065document.body.appendChild(rootDiv);
66
67const helpDiv = document.createElement('div');
68rootDiv.appendChild(helpDiv);
69helpDiv.innerHTML =
James Kuszmaul461b0682020-12-22 22:20:21 -080070 'Help: click + drag to pan, double click to reset, scroll to zoom, ' +
71 'right-click + drag to zoom to rectangle, Esc to cancel. ' +
James Kuszmaul5f5e1232020-12-22 20:58:00 -080072 'Hold the x/y keys to only pan/zoom along the x/y axes.';
73
74class PlotState {
75 public readonly div: HTMLElement;
76 private initialized = false;
77 constructor(
78 parentDiv: HTMLElement,
79 private readonly initializer:
80 (conn: Connection, element: Element) => void) {
81 this.div = document.createElement('div');
82 parentDiv.appendChild(this.div);
83 this.hide();
84 }
85 initialize(conn: Connection): void {
86 if (!this.initialized) {
87 this.initializer(conn, this.div);
88 this.initialized = true;
89 }
90 }
91 hide(): void {
92 this.div.style.display = "none";
93 }
94 show(): void {
95 this.div.style.display = "block";
96 }
97}
98
99const plotSelect = document.createElement('select');
100rootDiv.appendChild(plotSelect);
101
102const plotDiv = document.createElement('div');
Austin Schuh975c5942022-07-15 14:38:10 -0700103plotDiv.style.marginTop = '10px';
James Kuszmaul5f5e1232020-12-22 20:58:00 -0800104plotDiv.style.left = '0';
105plotDiv.style.position = 'absolute';
Austin Schuh5ec17ef2022-07-15 14:37:16 -0700106plotDiv.style.width = '100%';
James Kuszmaul5f5e1232020-12-22 20:58:00 -0800107rootDiv.appendChild(plotDiv);
108
109// The master list of all the plots that we provide. For a given config, it
110// is possible that not all of these plots will be usable depending on the
111// presence of certain channels.
112const plotIndex = new Map<string, PlotState>([
113 ['Demo', new PlotState(plotDiv, plotDemo)],
James Kuszmaul48671362020-12-24 13:54:16 -0800114 ['IMU', new PlotState(plotDiv, plotImu)],
James Kuszmaulc4ae11c2020-12-26 16:26:58 -0800115 ['Drivetrain', new PlotState(plotDiv, plotDrivetrain)],
James Kuszmaul73fc1352021-04-09 22:31:25 -0700116 ['Spline Debug', new PlotState(plotDiv, plotSpline)],
James Kuszmaulac2b6b42021-03-07 22:38:06 -0800117 ['Down Estimator', new PlotState(plotDiv, plotDownEstimator)],
milind upadhyay9bd381d2021-01-23 13:44:13 -0800118 ['Robot State', new PlotState(plotDiv, plotRobotState)],
James Kuszmaula8e0d6e2023-03-12 13:33:36 -0700119 ['2023 Vision', new PlotState(plotDiv, plot2023Corrections)],
James Kuszmaul827a6d62023-03-26 12:40:29 -0700120 ['2023 Localizer', new PlotState(plotDiv, plot2023Localizer)],
Milind Upadhyayeb739bb2022-03-02 10:49:21 -0800121 ['2020 Finisher', new PlotState(plotDiv, plot2020Finisher)],
122 ['2020 Accelerator', new PlotState(plotDiv, plot2020Accelerator)],
123 ['2020 Hood', new PlotState(plotDiv, plot2020Hood)],
124 ['2020 Turret', new PlotState(plotDiv, plot2020Turret)],
James Kuszmaulf7c8a092022-02-12 16:46:09 -0800125 ['2020 Localizer', new PlotState(plotDiv, plot2020Localizer)],
Milind Upadhyayeb739bb2022-03-02 10:49:21 -0800126 ['2022 Localizer', new PlotState(plotDiv, plot2022Localizer)],
James Kuszmaulb35e2342022-03-06 15:44:00 -0800127 ['2022 Vision', new PlotState(plotDiv, plot2022Vision)],
Ravago Jonesb64988c2022-03-06 15:05:01 -0800128 ['2022 Superstructure', new PlotState(plotDiv, plot2022Superstructure)],
Austin Schuh76f227c2022-02-23 16:34:08 -0800129 ['2022 Catapult', new PlotState(plotDiv, plot2022Catapult)],
Ravago Jones8b004782022-03-05 17:23:08 -0800130 ['2022 Intake Front', new PlotState(plotDiv, plot2022IntakeFront)],
131 ['2022 Intake Back', new PlotState(plotDiv, plot2022IntakeBack)],
Milind Upadhyayeb739bb2022-03-02 10:49:21 -0800132 ['2022 Climber', new PlotState(plotDiv, plot2022Climber)],
133 ['2022 Turret', new PlotState(plotDiv, plot2022Turret)],
Milind Upadhyayeb739bb2022-03-02 10:49:21 -0800134 ['Y2021 3rd Robot Superstructure', new PlotState(plotDiv, plot2021Superstructure)],
James Kuszmaul5f5e1232020-12-22 20:58:00 -0800135]);
136
137const invalidSelectValue = 'null';
138function getDefaultPlot(): string {
139 const urlParams = (new URL(document.URL)).searchParams;
140 const urlParamKey = 'plot';
141 if (!urlParams.has(urlParamKey)) {
142 return invalidSelectValue;
143 }
144 const desiredPlot = urlParams.get(urlParamKey);
145 if (!plotIndex.has(desiredPlot)) {
146 return invalidSelectValue;
147 }
148 return desiredPlot;
149}
150
151const conn = new Connection();
152
James Kuszmaulb8989f72021-03-07 20:00:02 -0800153let reloadOnChange = false;
154
James Kuszmaul5f5e1232020-12-22 20:58:00 -0800155conn.connect();
156
157conn.addConfigHandler((config: Configuration) => {
158 plotSelect.add(new Option("Select Plot", invalidSelectValue));
159 for (const name of plotIndex.keys()) {
160 plotSelect.add(new Option(name, name));
161 }
162 plotSelect.addEventListener('input', () => {
163 for (const plot of plotIndex.values()) {
164 plot.hide();
165 }
166 if (plotSelect.value == invalidSelectValue) {
167 return;
168 }
169 plotIndex.get(plotSelect.value).initialize(conn);
170 plotIndex.get(plotSelect.value).show();
James Kuszmaulc4ae11c2020-12-26 16:26:58 -0800171 // Set the URL so that if you reload you get back to this plot.
172 window.history.replaceState(
173 null, null, '?plot=' + encodeURIComponent(plotSelect.value));
James Kuszmaulb8989f72021-03-07 20:00:02 -0800174 if (reloadOnChange) {
175 window.location.reload();
176 }
177 reloadOnChange = true;
James Kuszmaul5f5e1232020-12-22 20:58:00 -0800178 });
179 plotSelect.value = getDefaultPlot();
180 // Force the event to occur once at the start.
181 plotSelect.dispatchEvent(new Event('input'));
James Kuszmaulf7c8a092022-02-12 16:46:09 -0800182});