James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 1 | // 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. |
| 15 | import {AosPlotter} from 'org_frc971/aos/network/www/aos_plotter'; |
James Kuszmaul | 0d7df89 | 2021-04-09 22:19:49 -0700 | [diff] [blame] | 16 | import {Plot, Point} from 'org_frc971/aos/network/www/plotter'; |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 17 | import * as proxy from 'org_frc971/aos/network/www/proxy'; |
| 18 | |
| 19 | import Connection = proxy.Connection; |
| 20 | |
| 21 | export function plotDemo(conn: Connection, parentDiv: Element): void { |
James Kuszmaul | 8073c0d | 2021-03-07 20:14:41 -0800 | [diff] [blame] | 22 | const width = AosPlotter.DEFAULT_WIDTH; |
| 23 | const height = AosPlotter.DEFAULT_HEIGHT; |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 24 | |
| 25 | const benchmarkDiv = document.createElement('div'); |
Austin Schuh | c2e9c50 | 2021-11-25 21:23:24 -0800 | [diff] [blame^] | 26 | benchmarkDiv.style.width = width.toString() + "px"; |
| 27 | benchmarkDiv.style.height = height.toString() + "px"; |
| 28 | benchmarkDiv.style.position = 'relative'; |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 29 | parentDiv.appendChild(benchmarkDiv); |
| 30 | |
Austin Schuh | c2e9c50 | 2021-11-25 21:23:24 -0800 | [diff] [blame^] | 31 | const benchmarkPlot = new Plot(benchmarkDiv); |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 32 | |
| 33 | const aosPlotter = new AosPlotter(conn); |
| 34 | |
| 35 | { |
James Kuszmaul | 8073c0d | 2021-03-07 20:14:41 -0800 | [diff] [blame] | 36 | // 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 Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 56 | const timingReport = |
| 57 | aosPlotter.addMessageSource('/aos', 'aos.timing.Report'); |
James Kuszmaul | 8073c0d | 2021-03-07 20:14:41 -0800 | [diff] [blame] | 58 | // 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 Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 61 | timingPlot.plot.getAxisLabels().setYLabel('PID'); |
| 62 | timingPlot.plot.getAxisLabels().setXLabel('Monotonic Send Time (sec)'); |
James Kuszmaul | 8073c0d | 2021-03-07 20:14:41 -0800 | [diff] [blame] | 63 | // 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 Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 75 | } |
| 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 Kuszmaul | 0d7df89 | 2021-04-09 22:19:49 -0700 | [diff] [blame] | 92 | points1.push(new Point(ii, Math.sin(ii * 10 / NUM_POINTS))); |
| 93 | points2.push(new Point(ii, Math.cos(ii * 10 / NUM_POINTS))); |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 94 | } |
James Kuszmaul | 0d7df89 | 2021-04-09 22:19:49 -0700 | [diff] [blame] | 95 | line1.setPoints(points1); |
| 96 | line2.setPoints(points2); |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 97 | } |