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