blob: 0610f6cac1f8c984c971e426882dd52d45c80d2e [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 Kuszmaul5f5e1232020-12-22 20:58:00 -080027import {plotDemo} from 'org_frc971/aos/network/www/demo_plot';
James Kuszmaul48671362020-12-24 13:54:16 -080028import {plotData} from 'org_frc971/frc971/analysis/plot_data_utils';
James Kuszmaul5f5e1232020-12-22 20:58:00 -080029
30import Connection = proxy.Connection;
31import Configuration = configuration.aos.Configuration;
32
33const rootDiv = document.createElement('div');
34document.body.appendChild(rootDiv);
35
36const helpDiv = document.createElement('div');
37rootDiv.appendChild(helpDiv);
38helpDiv.innerHTML =
James Kuszmaul461b0682020-12-22 22:20:21 -080039 'Help: click + drag to pan, double click to reset, scroll to zoom, ' +
40 'right-click + drag to zoom to rectangle, Esc to cancel. ' +
James Kuszmaul5f5e1232020-12-22 20:58:00 -080041 'Hold the x/y keys to only pan/zoom along the x/y axes.';
42
43class PlotState {
44 public readonly div: HTMLElement;
45 private initialized = false;
46 constructor(
47 parentDiv: HTMLElement,
48 private readonly initializer:
49 (conn: Connection, element: Element) => void) {
50 this.div = document.createElement('div');
51 parentDiv.appendChild(this.div);
52 this.hide();
53 }
54 initialize(conn: Connection): void {
55 if (!this.initialized) {
56 this.initializer(conn, this.div);
57 this.initialized = true;
58 }
59 }
60 hide(): void {
61 this.div.style.display = "none";
62 }
63 show(): void {
64 this.div.style.display = "block";
65 }
66}
67
68const plotSelect = document.createElement('select');
69rootDiv.appendChild(plotSelect);
70
71const plotDiv = document.createElement('div');
72plotDiv.style.top = (plotSelect.getBoundingClientRect().bottom + 10).toString();
73plotDiv.style.left = '0';
74plotDiv.style.position = 'absolute';
75rootDiv.appendChild(plotDiv);
76
77// The master list of all the plots that we provide. For a given config, it
78// is possible that not all of these plots will be usable depending on the
79// presence of certain channels.
80const plotIndex = new Map<string, PlotState>([
81 ['Demo', new PlotState(plotDiv, plotDemo)],
James Kuszmaul48671362020-12-24 13:54:16 -080082 ['IMU', new PlotState(plotDiv, plotImu)],
James Kuszmaulc4ae11c2020-12-26 16:26:58 -080083 ['Drivetrain', new PlotState(plotDiv, plotDrivetrain)],
James Kuszmaul48671362020-12-24 13:54:16 -080084 ['C++ Plotter', new PlotState(plotDiv, plotData)],
James Kuszmaul5f5e1232020-12-22 20:58:00 -080085]);
86
87const invalidSelectValue = 'null';
88function getDefaultPlot(): string {
89 const urlParams = (new URL(document.URL)).searchParams;
90 const urlParamKey = 'plot';
91 if (!urlParams.has(urlParamKey)) {
92 return invalidSelectValue;
93 }
94 const desiredPlot = urlParams.get(urlParamKey);
95 if (!plotIndex.has(desiredPlot)) {
96 return invalidSelectValue;
97 }
98 return desiredPlot;
99}
100
101const conn = new Connection();
102
103conn.connect();
104
105conn.addConfigHandler((config: Configuration) => {
106 plotSelect.add(new Option("Select Plot", invalidSelectValue));
107 for (const name of plotIndex.keys()) {
108 plotSelect.add(new Option(name, name));
109 }
110 plotSelect.addEventListener('input', () => {
111 for (const plot of plotIndex.values()) {
112 plot.hide();
113 }
114 if (plotSelect.value == invalidSelectValue) {
115 return;
116 }
117 plotIndex.get(plotSelect.value).initialize(conn);
118 plotIndex.get(plotSelect.value).show();
James Kuszmaulc4ae11c2020-12-26 16:26:58 -0800119 // Set the URL so that if you reload you get back to this plot.
120 window.history.replaceState(
121 null, null, '?plot=' + encodeURIComponent(plotSelect.value));
James Kuszmaul5f5e1232020-12-22 20:58:00 -0800122 });
123 plotSelect.value = getDefaultPlot();
124 // Force the event to occur once at the start.
125 plotSelect.dispatchEvent(new Event('input'));
126});