blob: ecd4da6b129d0be614c230a289b06d9090ecbffc [file] [log] [blame]
James Kuszmaul5f5e1232020-12-22 20:58:00 -08001// This file provides a basic demonstration of the plotting functionality.
2// The plotDemo() function provided here is called by
3// //frc971/analysis:plot_index.ts
4// To view the demo plot, run
5// bazel run -c opt //frc971/analysis:live_web_plotter_demo
6// And then navigate to
7// http://localhost:8080/?plot=Demo
8// The plot=Demo isn't structly necessary, but ensures that this plot is
9// selected by default--otherwise, you'll need to select "Demo" from the
10// drop-down menu.
11//
12// This example shows how to:
13// (a) Make use of the AosPlotter to plot a shmem message as a time-series.
14// (b) Define your own custom plot with whatever data you want.
15import {AosPlotter} from 'org_frc971/aos/network/www/aos_plotter';
James Kuszmaul0d7df892021-04-09 22:19:49 -070016import {Plot, Point} from 'org_frc971/aos/network/www/plotter';
James Kuszmaul5f5e1232020-12-22 20:58:00 -080017import * as proxy from 'org_frc971/aos/network/www/proxy';
18
19import Connection = proxy.Connection;
20
21export function plotDemo(conn: Connection, parentDiv: Element): void {
James Kuszmaul8073c0d2021-03-07 20:14:41 -080022 const width = AosPlotter.DEFAULT_WIDTH;
23 const height = AosPlotter.DEFAULT_HEIGHT;
James Kuszmaul5f5e1232020-12-22 20:58:00 -080024
25 const benchmarkDiv = document.createElement('div');
Austin Schuhc2e9c502021-11-25 21:23:24 -080026 benchmarkDiv.style.width = width.toString() + "px";
27 benchmarkDiv.style.height = height.toString() + "px";
28 benchmarkDiv.style.position = 'relative';
James Kuszmaul5f5e1232020-12-22 20:58:00 -080029 parentDiv.appendChild(benchmarkDiv);
30
Austin Schuhc2e9c502021-11-25 21:23:24 -080031 const benchmarkPlot = new Plot(benchmarkDiv);
James Kuszmaul5f5e1232020-12-22 20:58:00 -080032
33 const aosPlotter = new AosPlotter(conn);
34
35 {
James Kuszmaul8073c0d2021-03-07 20:14:41 -080036 // Setup a plot that shows some arbitrary PDP current values.
37 const pdpValues =
38 aosPlotter.addMessageSource('/aos', 'frc971.PDPValues');
39 const timingPlot = aosPlotter.addPlot(parentDiv);
40 timingPlot.plot.getAxisLabels().setTitle('Current Values');
41 timingPlot.plot.getAxisLabels().setYLabel('Current (Amps)');
42 timingPlot.plot.getAxisLabels().setXLabel('Monotonic Send Time (sec)');
43 // Displays points for every single current sample at each time-point.
44 const allValuesLine = timingPlot.addMessageLine(pdpValues, ['currents[]']);
45 allValuesLine.setDrawLine(false);
46 allValuesLine.setPointSize(5);
47 // Displays a line for the current along channel 1.
48 const singleValueLine = timingPlot.addMessageLine(pdpValues, ['currents[1]']);
49 singleValueLine.setDrawLine(true);
50 singleValueLine.setPointSize(0);
51 const voltageLine = timingPlot.addMessageLine(pdpValues, ['voltage']);
52 voltageLine.setPointSize(0);
53 }
54
55 {
James Kuszmaul5f5e1232020-12-22 20:58:00 -080056 const timingReport =
57 aosPlotter.addMessageSource('/aos', 'aos.timing.Report');
James Kuszmaul8073c0d2021-03-07 20:14:41 -080058 // Setup a plot that just shows some arbitrary timing data.
59 const timingPlot = aosPlotter.addPlot(parentDiv);
60 timingPlot.plot.getAxisLabels().setTitle('Timing Report Wakeups');
James Kuszmaul5f5e1232020-12-22 20:58:00 -080061 timingPlot.plot.getAxisLabels().setYLabel('PID');
62 timingPlot.plot.getAxisLabels().setXLabel('Monotonic Send Time (sec)');
James Kuszmaul8073c0d2021-03-07 20:14:41 -080063 // Show *all* the wakeup latencies for all timers.
64 const allValuesLine = timingPlot.addMessageLine(
65 timingReport, ['timers[]', 'wakeup_latency', 'average']);
66 allValuesLine.setDrawLine(false);
67 allValuesLine.setPointSize(5);
68 // Show *all* the wakeup latencies for the first timer in each timing report
69 // (this is not actually all that helpful unless you were to also filter by
70 // PID).
71 const singleValueLine = timingPlot.addMessageLine(
72 timingReport, ['timers[0]', 'wakeup_latency', 'average']);
73 singleValueLine.setDrawLine(true);
74 singleValueLine.setPointSize(0);
James Kuszmaul5f5e1232020-12-22 20:58:00 -080075 }
76
77 // Set up and draw the benchmarking plot.
78 benchmarkPlot.getAxisLabels().setTitle(
79 'Benchmarking plot (1M points per line)');
80 const line1 = benchmarkPlot.getDrawer().addLine();
81 // For demonstration purposes, make line1 only have points and line2 only have
82 // lines.
83 line1.setDrawLine(false);
84 line1.setLabel('LINE ONE');
85 const line2 = benchmarkPlot.getDrawer().addLine();
86 line2.setPointSize(0);
87 line2.setLabel('LINE TWO');
88 const NUM_POINTS = 1000000;
89 const points1 = [];
90 const points2 = [];
91 for (let ii = 0; ii < NUM_POINTS; ++ii) {
James Kuszmaul0d7df892021-04-09 22:19:49 -070092 points1.push(new Point(ii, Math.sin(ii * 10 / NUM_POINTS)));
93 points2.push(new Point(ii, Math.cos(ii * 10 / NUM_POINTS)));
James Kuszmaul5f5e1232020-12-22 20:58:00 -080094 }
James Kuszmaul0d7df892021-04-09 22:19:49 -070095 line1.setPoints(points1);
96 line2.setPoints(points2);
James Kuszmaul5f5e1232020-12-22 20:58:00 -080097}