blob: 5b7ad96892e5ce9a7ef31c00f785f4d12d69d1be [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';
milind upadhyay9bd381d2021-01-23 13:44:13 -080027import {plotRobotState} from
28 'org_frc971/frc971/control_loops/drivetrain/robot_state_plotter'
Austin Schuh7d63eab2021-03-06 20:15:02 -080029import {plotFinisher} from
30 'org_frc971/y2020/control_loops/superstructure/finisher_plotter'
31import {plotAccelerator} from
32 'org_frc971/y2020/control_loops/superstructure/accelerator_plotter'
Austin Schuh2efe1682021-03-06 22:47:15 -080033import {plotHood} from
34 'org_frc971/y2020/control_loops/superstructure/hood_plotter'
James Kuszmaul5f5e1232020-12-22 20:58:00 -080035import {plotDemo} from 'org_frc971/aos/network/www/demo_plot';
James Kuszmaul48671362020-12-24 13:54:16 -080036import {plotData} from 'org_frc971/frc971/analysis/plot_data_utils';
James Kuszmaul5f5e1232020-12-22 20:58:00 -080037
38import Connection = proxy.Connection;
39import Configuration = configuration.aos.Configuration;
40
41const rootDiv = document.createElement('div');
42document.body.appendChild(rootDiv);
43
44const helpDiv = document.createElement('div');
45rootDiv.appendChild(helpDiv);
46helpDiv.innerHTML =
James Kuszmaul461b0682020-12-22 22:20:21 -080047 'Help: click + drag to pan, double click to reset, scroll to zoom, ' +
48 'right-click + drag to zoom to rectangle, Esc to cancel. ' +
James Kuszmaul5f5e1232020-12-22 20:58:00 -080049 'Hold the x/y keys to only pan/zoom along the x/y axes.';
50
51class PlotState {
52 public readonly div: HTMLElement;
53 private initialized = false;
54 constructor(
55 parentDiv: HTMLElement,
56 private readonly initializer:
57 (conn: Connection, element: Element) => void) {
58 this.div = document.createElement('div');
59 parentDiv.appendChild(this.div);
60 this.hide();
61 }
62 initialize(conn: Connection): void {
63 if (!this.initialized) {
64 this.initializer(conn, this.div);
65 this.initialized = true;
66 }
67 }
68 hide(): void {
69 this.div.style.display = "none";
70 }
71 show(): void {
72 this.div.style.display = "block";
73 }
74}
75
76const plotSelect = document.createElement('select');
77rootDiv.appendChild(plotSelect);
78
79const plotDiv = document.createElement('div');
80plotDiv.style.top = (plotSelect.getBoundingClientRect().bottom + 10).toString();
81plotDiv.style.left = '0';
82plotDiv.style.position = 'absolute';
83rootDiv.appendChild(plotDiv);
84
85// The master list of all the plots that we provide. For a given config, it
86// is possible that not all of these plots will be usable depending on the
87// presence of certain channels.
88const plotIndex = new Map<string, PlotState>([
89 ['Demo', new PlotState(plotDiv, plotDemo)],
James Kuszmaul48671362020-12-24 13:54:16 -080090 ['IMU', new PlotState(plotDiv, plotImu)],
James Kuszmaulc4ae11c2020-12-26 16:26:58 -080091 ['Drivetrain', new PlotState(plotDiv, plotDrivetrain)],
milind upadhyay9bd381d2021-01-23 13:44:13 -080092 ['Robot State', new PlotState(plotDiv, plotRobotState)],
Austin Schuh7d63eab2021-03-06 20:15:02 -080093 ['Finisher', new PlotState(plotDiv, plotFinisher)],
94 ['Accelerator', new PlotState(plotDiv, plotAccelerator)],
Austin Schuh2efe1682021-03-06 22:47:15 -080095 ['Hood', new PlotState(plotDiv, plotHood)],
James Kuszmaul48671362020-12-24 13:54:16 -080096 ['C++ Plotter', new PlotState(plotDiv, plotData)],
James Kuszmaul5f5e1232020-12-22 20:58:00 -080097]);
98
99const invalidSelectValue = 'null';
100function getDefaultPlot(): string {
101 const urlParams = (new URL(document.URL)).searchParams;
102 const urlParamKey = 'plot';
103 if (!urlParams.has(urlParamKey)) {
104 return invalidSelectValue;
105 }
106 const desiredPlot = urlParams.get(urlParamKey);
107 if (!plotIndex.has(desiredPlot)) {
108 return invalidSelectValue;
109 }
110 return desiredPlot;
111}
112
113const conn = new Connection();
114
115conn.connect();
116
117conn.addConfigHandler((config: Configuration) => {
118 plotSelect.add(new Option("Select Plot", invalidSelectValue));
119 for (const name of plotIndex.keys()) {
120 plotSelect.add(new Option(name, name));
121 }
122 plotSelect.addEventListener('input', () => {
123 for (const plot of plotIndex.values()) {
124 plot.hide();
125 }
126 if (plotSelect.value == invalidSelectValue) {
127 return;
128 }
129 plotIndex.get(plotSelect.value).initialize(conn);
130 plotIndex.get(plotSelect.value).show();
James Kuszmaulc4ae11c2020-12-26 16:26:58 -0800131 // Set the URL so that if you reload you get back to this plot.
132 window.history.replaceState(
133 null, null, '?plot=' + encodeURIComponent(plotSelect.value));
James Kuszmaul5f5e1232020-12-22 20:58:00 -0800134 });
135 plotSelect.value = getDefaultPlot();
136 // Force the event to occur once at the start.
137 plotSelect.dispatchEvent(new Event('input'));
138});