blob: 1eba066b0af264bcac8ea6a38542961d0be9cc89 [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 Kuszmaulac2b6b42021-03-07 22:38:06 -080027import {plotDownEstimator} from 'org_frc971/frc971/control_loops/drivetrain/down_estimator_plotter';
milind upadhyay9bd381d2021-01-23 13:44:13 -080028import {plotRobotState} from
29 'org_frc971/frc971/control_loops/drivetrain/robot_state_plotter'
James Kuszmaul5f5e1232020-12-22 20:58:00 -080030import {plotDemo} from 'org_frc971/aos/network/www/demo_plot';
James Kuszmaul48671362020-12-24 13:54:16 -080031import {plotData} from 'org_frc971/frc971/analysis/plot_data_utils';
James Kuszmaul5f5e1232020-12-22 20:58:00 -080032
33import Connection = proxy.Connection;
34import Configuration = configuration.aos.Configuration;
35
36const rootDiv = document.createElement('div');
37document.body.appendChild(rootDiv);
38
39const helpDiv = document.createElement('div');
40rootDiv.appendChild(helpDiv);
41helpDiv.innerHTML =
James Kuszmaul461b0682020-12-22 22:20:21 -080042 'Help: click + drag to pan, double click to reset, scroll to zoom, ' +
43 'right-click + drag to zoom to rectangle, Esc to cancel. ' +
James Kuszmaul5f5e1232020-12-22 20:58:00 -080044 'Hold the x/y keys to only pan/zoom along the x/y axes.';
45
46class PlotState {
47 public readonly div: HTMLElement;
48 private initialized = false;
49 constructor(
50 parentDiv: HTMLElement,
51 private readonly initializer:
52 (conn: Connection, element: Element) => void) {
53 this.div = document.createElement('div');
54 parentDiv.appendChild(this.div);
55 this.hide();
56 }
57 initialize(conn: Connection): void {
58 if (!this.initialized) {
59 this.initializer(conn, this.div);
60 this.initialized = true;
61 }
62 }
63 hide(): void {
64 this.div.style.display = "none";
65 }
66 show(): void {
67 this.div.style.display = "block";
68 }
69}
70
71const plotSelect = document.createElement('select');
72rootDiv.appendChild(plotSelect);
73
74const plotDiv = document.createElement('div');
75plotDiv.style.top = (plotSelect.getBoundingClientRect().bottom + 10).toString();
76plotDiv.style.left = '0';
77plotDiv.style.position = 'absolute';
78rootDiv.appendChild(plotDiv);
79
80// The master list of all the plots that we provide. For a given config, it
81// is possible that not all of these plots will be usable depending on the
82// presence of certain channels.
83const plotIndex = new Map<string, PlotState>([
84 ['Demo', new PlotState(plotDiv, plotDemo)],
James Kuszmaul48671362020-12-24 13:54:16 -080085 ['IMU', new PlotState(plotDiv, plotImu)],
James Kuszmaulc4ae11c2020-12-26 16:26:58 -080086 ['Drivetrain', new PlotState(plotDiv, plotDrivetrain)],
James Kuszmaulac2b6b42021-03-07 22:38:06 -080087 ['Down Estimator', new PlotState(plotDiv, plotDownEstimator)],
milind upadhyay9bd381d2021-01-23 13:44:13 -080088 ['Robot State', new PlotState(plotDiv, plotRobotState)],
James Kuszmaul48671362020-12-24 13:54:16 -080089 ['C++ Plotter', new PlotState(plotDiv, plotData)],
James Kuszmaul5f5e1232020-12-22 20:58:00 -080090]);
91
92const invalidSelectValue = 'null';
93function getDefaultPlot(): string {
94 const urlParams = (new URL(document.URL)).searchParams;
95 const urlParamKey = 'plot';
96 if (!urlParams.has(urlParamKey)) {
97 return invalidSelectValue;
98 }
99 const desiredPlot = urlParams.get(urlParamKey);
100 if (!plotIndex.has(desiredPlot)) {
101 return invalidSelectValue;
102 }
103 return desiredPlot;
104}
105
106const conn = new Connection();
107
James Kuszmaulb8989f72021-03-07 20:00:02 -0800108let reloadOnChange = false;
109
James Kuszmaul5f5e1232020-12-22 20:58:00 -0800110conn.connect();
111
112conn.addConfigHandler((config: Configuration) => {
113 plotSelect.add(new Option("Select Plot", invalidSelectValue));
114 for (const name of plotIndex.keys()) {
115 plotSelect.add(new Option(name, name));
116 }
117 plotSelect.addEventListener('input', () => {
118 for (const plot of plotIndex.values()) {
119 plot.hide();
120 }
121 if (plotSelect.value == invalidSelectValue) {
122 return;
123 }
124 plotIndex.get(plotSelect.value).initialize(conn);
125 plotIndex.get(plotSelect.value).show();
James Kuszmaulc4ae11c2020-12-26 16:26:58 -0800126 // Set the URL so that if you reload you get back to this plot.
127 window.history.replaceState(
128 null, null, '?plot=' + encodeURIComponent(plotSelect.value));
James Kuszmaulb8989f72021-03-07 20:00:02 -0800129 if (reloadOnChange) {
130 window.location.reload();
131 }
132 reloadOnChange = true;
James Kuszmaul5f5e1232020-12-22 20:58:00 -0800133 });
134 plotSelect.value = getDefaultPlot();
135 // Force the event to occur once at the start.
136 plotSelect.dispatchEvent(new Event('input'));
137});