blob: 332db349a677412dd6e67539198546fe2d5a8ad4 [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.
23import * as configuration from 'org_frc971/aos/configuration_generated';
24import * as proxy from 'org_frc971/aos/network/www/proxy';
25import {plotImu} from 'org_frc971/frc971/wpilib/imu_plotter';
James Kuszmaulc4ae11c2020-12-26 16:26:58 -080026import {plotDrivetrain} from 'org_frc971/frc971/control_loops/drivetrain/drivetrain_plotter';
James Kuszmaul73fc1352021-04-09 22:31:25 -070027import {plotSpline} from 'org_frc971/frc971/control_loops/drivetrain/spline_plotter';
James Kuszmaulac2b6b42021-03-07 22:38:06 -080028import {plotDownEstimator} from 'org_frc971/frc971/control_loops/drivetrain/down_estimator_plotter';
milind upadhyay9bd381d2021-01-23 13:44:13 -080029import {plotRobotState} from
30 'org_frc971/frc971/control_loops/drivetrain/robot_state_plotter'
Austin Schuh7d63eab2021-03-06 20:15:02 -080031import {plotFinisher} from
32 'org_frc971/y2020/control_loops/superstructure/finisher_plotter'
James Kuszmaul78101402021-09-11 12:42:21 -070033import {plotTurret} from
34 'org_frc971/y2020/control_loops/superstructure/turret_plotter'
James Kuszmaulf7c8a092022-02-12 16:46:09 -080035import {plotLocalizer as plot2020Localizer} from
James Kuszmaul9c23d262021-09-25 21:50:02 -070036 'org_frc971/y2020/control_loops/drivetrain/localizer_plotter'
Austin Schuh76f227c2022-02-23 16:34:08 -080037import {plotCatapult as plot2022Catapult} from
38 'org_frc971/y2022/control_loops/superstructure/catapult_plotter'
James Kuszmaulf7c8a092022-02-12 16:46:09 -080039import {plotLocalizer as plot2022Localizer} from
James Kuszmaul51fa1ae2022-02-26 00:49:57 -080040 'org_frc971/y2022/localizer/localizer_plotter'
Austin Schuh7d63eab2021-03-06 20:15:02 -080041import {plotAccelerator} from
42 'org_frc971/y2020/control_loops/superstructure/accelerator_plotter'
Austin Schuh2efe1682021-03-06 22:47:15 -080043import {plotHood} from
44 'org_frc971/y2020/control_loops/superstructure/hood_plotter'
Sabina Leaver58f04b72021-10-06 20:52:09 -070045import {plotSuperstructure} from
46 'org_frc971/y2021_bot3/control_loops/superstructure/superstructure_plotter';
James Kuszmaul5f5e1232020-12-22 20:58:00 -080047import {plotDemo} from 'org_frc971/aos/network/www/demo_plot';
James Kuszmaul48671362020-12-24 13:54:16 -080048import {plotData} from 'org_frc971/frc971/analysis/plot_data_utils';
James Kuszmaul5f5e1232020-12-22 20:58:00 -080049
50import Connection = proxy.Connection;
51import Configuration = configuration.aos.Configuration;
52
53const rootDiv = document.createElement('div');
54document.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');
92plotDiv.style.top = (plotSelect.getBoundingClientRect().bottom + 10).toString();
93plotDiv.style.left = '0';
94plotDiv.style.position = 'absolute';
95rootDiv.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.
100const plotIndex = new Map<string, PlotState>([
101 ['Demo', new PlotState(plotDiv, plotDemo)],
James Kuszmaul48671362020-12-24 13:54:16 -0800102 ['IMU', new PlotState(plotDiv, plotImu)],
James Kuszmaulc4ae11c2020-12-26 16:26:58 -0800103 ['Drivetrain', new PlotState(plotDiv, plotDrivetrain)],
James Kuszmaul73fc1352021-04-09 22:31:25 -0700104 ['Spline Debug', new PlotState(plotDiv, plotSpline)],
James Kuszmaulac2b6b42021-03-07 22:38:06 -0800105 ['Down Estimator', new PlotState(plotDiv, plotDownEstimator)],
milind upadhyay9bd381d2021-01-23 13:44:13 -0800106 ['Robot State', new PlotState(plotDiv, plotRobotState)],
Austin Schuh7d63eab2021-03-06 20:15:02 -0800107 ['Finisher', new PlotState(plotDiv, plotFinisher)],
108 ['Accelerator', new PlotState(plotDiv, plotAccelerator)],
Austin Schuh2efe1682021-03-06 22:47:15 -0800109 ['Hood', new PlotState(plotDiv, plotHood)],
James Kuszmaul78101402021-09-11 12:42:21 -0700110 ['Turret', new PlotState(plotDiv, plotTurret)],
James Kuszmaulf7c8a092022-02-12 16:46:09 -0800111 ['2022 Localizer', new PlotState(plotDiv, plot2022Localizer)],
112 ['2020 Localizer', new PlotState(plotDiv, plot2020Localizer)],
Austin Schuh76f227c2022-02-23 16:34:08 -0800113 ['2022 Catapult', new PlotState(plotDiv, plot2022Catapult)],
James Kuszmaul48671362020-12-24 13:54:16 -0800114 ['C++ Plotter', new PlotState(plotDiv, plotData)],
Sabina Leaver58f04b72021-10-06 20:52:09 -0700115 ['Y2021 3rd Robot Superstructure', new PlotState(plotDiv, plotSuperstructure)],
James Kuszmaul5f5e1232020-12-22 20:58:00 -0800116]);
117
118const invalidSelectValue = 'null';
119function getDefaultPlot(): string {
120 const urlParams = (new URL(document.URL)).searchParams;
121 const urlParamKey = 'plot';
122 if (!urlParams.has(urlParamKey)) {
123 return invalidSelectValue;
124 }
125 const desiredPlot = urlParams.get(urlParamKey);
126 if (!plotIndex.has(desiredPlot)) {
127 return invalidSelectValue;
128 }
129 return desiredPlot;
130}
131
132const conn = new Connection();
133
James Kuszmaulb8989f72021-03-07 20:00:02 -0800134let reloadOnChange = false;
135
James Kuszmaul5f5e1232020-12-22 20:58:00 -0800136conn.connect();
137
138conn.addConfigHandler((config: Configuration) => {
139 plotSelect.add(new Option("Select Plot", invalidSelectValue));
140 for (const name of plotIndex.keys()) {
141 plotSelect.add(new Option(name, name));
142 }
143 plotSelect.addEventListener('input', () => {
144 for (const plot of plotIndex.values()) {
145 plot.hide();
146 }
147 if (plotSelect.value == invalidSelectValue) {
148 return;
149 }
150 plotIndex.get(plotSelect.value).initialize(conn);
151 plotIndex.get(plotSelect.value).show();
James Kuszmaulc4ae11c2020-12-26 16:26:58 -0800152 // Set the URL so that if you reload you get back to this plot.
153 window.history.replaceState(
154 null, null, '?plot=' + encodeURIComponent(plotSelect.value));
James Kuszmaulb8989f72021-03-07 20:00:02 -0800155 if (reloadOnChange) {
156 window.location.reload();
157 }
158 reloadOnChange = true;
James Kuszmaul5f5e1232020-12-22 20:58:00 -0800159 });
160 plotSelect.value = getDefaultPlot();
161 // Force the event to occur once at the start.
162 plotSelect.dispatchEvent(new Event('input'));
James Kuszmaulf7c8a092022-02-12 16:46:09 -0800163});