blob: 29ce056438b49716e98c5e1ef7e2a0c2cbe523c0 [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';
Philipp Schrader548aedf2023-02-17 20:09:13 -080024import {plotDemo} from '../../aos/network/www/demo_plot';
James Kuszmaul2e818b22024-02-24 09:02:42 -080025import {Connection} from '../../aos/network/www/proxy';
26import {plotLocalizer as plot2020Localizer} from '../../y2020/control_loops/drivetrain/localizer_plotter'
27import {plotAccelerator as plot2020Accelerator} from '../../y2020/control_loops/superstructure/accelerator_plotter'
28import {plotFinisher as plot2020Finisher} from '../../y2020/control_loops/superstructure/finisher_plotter'
29import {plotHood as plot2020Hood} from '../../y2020/control_loops/superstructure/hood_plotter'
30import {plotTurret as plot2020Turret} from '../../y2020/control_loops/superstructure/turret_plotter'
31import {plotSuperstructure as plot2021Superstructure} from '../../y2021_bot3/control_loops/superstructure/superstructure_plotter';
32import {plotCatapult as plot2022Catapult} from '../../y2022/control_loops/superstructure/catapult_plotter'
33import {plotClimber as plot2022Climber} from '../../y2022/control_loops/superstructure/climber_plotter'
34import {plotIntakeBack as plot2022IntakeBack, plotIntakeFront as plot2022IntakeFront} from '../../y2022/control_loops/superstructure/intake_plotter'
35import {plotSuperstructure as plot2022Superstructure} from '../../y2022/control_loops/superstructure/superstructure_plotter'
36import {plotTurret as plot2022Turret} from '../../y2022/control_loops/superstructure/turret_plotter'
37import {plotLocalizer as plot2022Localizer} from '../../y2022/localizer/localizer_plotter'
38import {plotVision as plot2022Vision} from '../../y2022/vision/vision_plotter'
39import {plotSuperstructure as plot2023Superstructure} from '../../y2023/control_loops/superstructure/superstructure_plotter'
40import {plotVision as plot2023Corrections} from '../../y2023/localizer/corrections_plotter'
41import {plotLocalizer as plot2023Localizer} from '../../y2023/localizer/localizer_plotter'
42import {plotClimber as plot2024Climber, plotIntake as plot2024Intake, plotSuperstructure as plot2024Superstructure} from '../../y2024/control_loops/superstructure/superstructure_plotter'
James Kuszmaul9be2e1c2024-07-06 13:43:24 -070043import {plotSwerve} from '../../y2024_swerve/control_loops/swerve_plotter'
James Kuszmaul2e818b22024-02-24 09:02:42 -080044import {plotVision as plot2024Corrections} from '../../y2024/localizer/corrections_plotter'
45import {plotLocalizer as plot2024Localizer} from '../../y2024/localizer/localizer_plotter'
46import {plotDownEstimator} from '../control_loops/drivetrain/down_estimator_plotter';
47import {plotDrivetrain} from '../control_loops/drivetrain/drivetrain_plotter';
48import {plotRobotState} from '../control_loops/drivetrain/robot_state_plotter'
49import {plotSpline} from '../control_loops/drivetrain/spline_plotter';
50import {plotImu} from '../wpilib/imu_plotter';
James Kuszmaul5f5e1232020-12-22 20:58:00 -080051
James Kuszmaul5f5e1232020-12-22 20:58:00 -080052const rootDiv = document.createElement('div');
Austin Schuh5ec17ef2022-07-15 14:37:16 -070053rootDiv.style.width = '100%';
James Kuszmaul5f5e1232020-12-22 20:58:00 -080054document.body.appendChild(rootDiv);
55
56const helpDiv = document.createElement('div');
57rootDiv.appendChild(helpDiv);
58helpDiv.innerHTML =
James Kuszmaul461b0682020-12-22 22:20:21 -080059 'Help: click + drag to pan, double click to reset, scroll to zoom, ' +
60 'right-click + drag to zoom to rectangle, Esc to cancel. ' +
James Kuszmaul5f5e1232020-12-22 20:58:00 -080061 'Hold the x/y keys to only pan/zoom along the x/y axes.';
62
63class PlotState {
64 public readonly div: HTMLElement;
65 private initialized = false;
66 constructor(
67 parentDiv: HTMLElement,
68 private readonly initializer:
69 (conn: Connection, element: Element) => void) {
70 this.div = document.createElement('div');
71 parentDiv.appendChild(this.div);
72 this.hide();
73 }
74 initialize(conn: Connection): void {
75 if (!this.initialized) {
76 this.initializer(conn, this.div);
77 this.initialized = true;
78 }
79 }
80 hide(): void {
81 this.div.style.display = "none";
82 }
83 show(): void {
84 this.div.style.display = "block";
85 }
86}
87
88const plotSelect = document.createElement('select');
89rootDiv.appendChild(plotSelect);
90
91const plotDiv = document.createElement('div');
Austin Schuh975c5942022-07-15 14:38:10 -070092plotDiv.style.marginTop = '10px';
James Kuszmaul5f5e1232020-12-22 20:58:00 -080093plotDiv.style.left = '0';
94plotDiv.style.position = 'absolute';
Austin Schuh5ec17ef2022-07-15 14:37:16 -070095plotDiv.style.width = '100%';
James Kuszmaul5f5e1232020-12-22 20:58:00 -080096rootDiv.appendChild(plotDiv);
97
98// The master list of all the plots that we provide. For a given config, it
99// is possible that not all of these plots will be usable depending on the
100// presence of certain channels.
101const plotIndex = new Map<string, PlotState>([
102 ['Demo', new PlotState(plotDiv, plotDemo)],
James Kuszmaul48671362020-12-24 13:54:16 -0800103 ['IMU', new PlotState(plotDiv, plotImu)],
James Kuszmaulc4ae11c2020-12-26 16:26:58 -0800104 ['Drivetrain', new PlotState(plotDiv, plotDrivetrain)],
James Kuszmaul73fc1352021-04-09 22:31:25 -0700105 ['Spline Debug', new PlotState(plotDiv, plotSpline)],
James Kuszmaulac2b6b42021-03-07 22:38:06 -0800106 ['Down Estimator', new PlotState(plotDiv, plotDownEstimator)],
milind upadhyay9bd381d2021-01-23 13:44:13 -0800107 ['Robot State', new PlotState(plotDiv, plotRobotState)],
James Kuszmaul313e9ce2024-02-11 17:47:33 -0800108 ['2024 Vision', new PlotState(plotDiv, plot2024Corrections)],
109 ['2024 Localizer', new PlotState(plotDiv, plot2024Localizer)],
James Kuszmaul2e818b22024-02-24 09:02:42 -0800110 ['2024 Superstructure', new PlotState(plotDiv, plot2024Superstructure)],
James Kuszmaul9be2e1c2024-07-06 13:43:24 -0700111 ['2024 Swerve', new PlotState(plotDiv, plotSwerve)],
James Kuszmaul2e818b22024-02-24 09:02:42 -0800112 ['2024 Climber', new PlotState(plotDiv, plot2024Climber)],
113 ['2024 Intake', new PlotState(plotDiv, plot2024Intake)],
James Kuszmaula8e0d6e2023-03-12 13:33:36 -0700114 ['2023 Vision', new PlotState(plotDiv, plot2023Corrections)],
James Kuszmaul827a6d62023-03-26 12:40:29 -0700115 ['2023 Localizer', new PlotState(plotDiv, plot2023Localizer)],
James Kuszmaul81e71842023-09-29 15:25:13 -0700116 ['2023 Superstructure', new PlotState(plotDiv, plot2023Superstructure)],
Milind Upadhyayeb739bb2022-03-02 10:49:21 -0800117 ['2020 Finisher', new PlotState(plotDiv, plot2020Finisher)],
118 ['2020 Accelerator', new PlotState(plotDiv, plot2020Accelerator)],
119 ['2020 Hood', new PlotState(plotDiv, plot2020Hood)],
120 ['2020 Turret', new PlotState(plotDiv, plot2020Turret)],
James Kuszmaulf7c8a092022-02-12 16:46:09 -0800121 ['2020 Localizer', new PlotState(plotDiv, plot2020Localizer)],
Milind Upadhyayeb739bb2022-03-02 10:49:21 -0800122 ['2022 Localizer', new PlotState(plotDiv, plot2022Localizer)],
James Kuszmaulb35e2342022-03-06 15:44:00 -0800123 ['2022 Vision', new PlotState(plotDiv, plot2022Vision)],
Ravago Jonesb64988c2022-03-06 15:05:01 -0800124 ['2022 Superstructure', new PlotState(plotDiv, plot2022Superstructure)],
Austin Schuh76f227c2022-02-23 16:34:08 -0800125 ['2022 Catapult', new PlotState(plotDiv, plot2022Catapult)],
Ravago Jones8b004782022-03-05 17:23:08 -0800126 ['2022 Intake Front', new PlotState(plotDiv, plot2022IntakeFront)],
127 ['2022 Intake Back', new PlotState(plotDiv, plot2022IntakeBack)],
Milind Upadhyayeb739bb2022-03-02 10:49:21 -0800128 ['2022 Climber', new PlotState(plotDiv, plot2022Climber)],
129 ['2022 Turret', new PlotState(plotDiv, plot2022Turret)],
Milind Upadhyayeb739bb2022-03-02 10:49:21 -0800130 ['Y2021 3rd Robot Superstructure', new PlotState(plotDiv, plot2021Superstructure)],
James Kuszmaul5f5e1232020-12-22 20:58:00 -0800131]);
132
133const invalidSelectValue = 'null';
134function 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
147const conn = new Connection();
148
James Kuszmaulb8989f72021-03-07 20:00:02 -0800149let reloadOnChange = false;
150
James Kuszmaul5f5e1232020-12-22 20:58:00 -0800151conn.connect();
152
153conn.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 Kuszmaulc4ae11c2020-12-26 16:26:58 -0800167 // 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 Kuszmaulb8989f72021-03-07 20:00:02 -0800170 if (reloadOnChange) {
171 window.location.reload();
172 }
173 reloadOnChange = true;
James Kuszmaul5f5e1232020-12-22 20:58:00 -0800174 });
175 plotSelect.value = getDefaultPlot();
176 // Force the event to occur once at the start.
177 plotSelect.dispatchEvent(new Event('input'));
James Kuszmaulf7c8a092022-02-12 16:46:09 -0800178});