blob: 488aed9727e62308ec6b88a2e2af7e1138b71811 [file] [log] [blame]
James Kuszmaul78101402021-09-11 12:42:21 -07001// Provides a plot for debugging robot state-related issues.
2import {AosPlotter} from 'org_frc971/aos/network/www/aos_plotter';
3import * as proxy from 'org_frc971/aos/network/www/proxy';
James Kuszmaulda52ce62021-09-25 21:51:42 -07004import * as configuration from 'org_frc971/aos/configuration_generated';
James Kuszmaul78101402021-09-11 12:42:21 -07005import {BLUE, BROWN, CYAN, GREEN, PINK, RED, WHITE} from 'org_frc971/aos/network/www/colors';
James Kuszmaulda52ce62021-09-25 21:51:42 -07006import {MessageHandler, TimestampedMessage} from 'org_frc971/aos/network/www/aos_plotter';
7import {Point} from 'org_frc971/aos/network/www/plotter';
8import {Table} from 'org_frc971/aos/network/www/reflection';
9import {ByteBuffer} from 'org_frc971/external/com_github_google_flatbuffers/ts/byte-buffer';
James Kuszmaulf8355ff2021-12-19 22:08:45 -080010import {Long} from 'org_frc971/external/com_github_google_flatbuffers/ts/long';
James Kuszmaul78101402021-09-11 12:42:21 -070011
12import Connection = proxy.Connection;
James Kuszmaulda52ce62021-09-25 21:51:42 -070013import Schema = configuration.reflection.Schema;
James Kuszmaul78101402021-09-11 12:42:21 -070014
15const TIME = AosPlotter.TIME;
James Kuszmaul78101402021-09-11 12:42:21 -070016
James Kuszmaulda52ce62021-09-25 21:51:42 -070017class DerivativeMessageHandler extends MessageHandler {
18 // Calculated magnitude of the measured acceleration from the IMU.
19 private acceleration_magnitudes: Point[] = [];
20 constructor(private readonly schema: Schema) {
21 super(schema);
22 }
James Kuszmaulf8355ff2021-12-19 22:08:45 -080023 private readScalar(table: Table, fieldName: string): number|Long|null {
James Kuszmaulda52ce62021-09-25 21:51:42 -070024 return this.parser.readScalar(table, fieldName);
25 }
26
27 // Computes a numerical derivative for a given input.
28 private derivative(input: Point[]): Point[] {
29 const num_measurements = input.length;
30 const results = [];
31 for (let ii = 0; ii < num_measurements - 1; ++ii) {
32 const x0 = input[ii].x;
33 const x1 = input[ii + 1].x;
34 const y0 = input[ii].y;
35 const y1 = input[ii + 1].y;
36 results.push(new Point((x0 + x1) / 2.0, (y1 - y0) / (x1 - x0)));
37 }
38 return results;
39 }
40
41 getField(field: string[]): Point[] {
42 // Any requested input that ends with "_derivative" will get a derivative
43 // calculated for the provided field.
44 const derivative_suffix = "_derivative";
45 const num_fields = field.length;
46 const end_field = field[num_fields - 1];
47 if (end_field.endsWith(derivative_suffix)) {
48 const field_copy = [];
49 for (let ii = 0; ii < num_fields - 1; ++ii) {
50 field_copy.push(field[ii]);
51 }
52 field_copy.push(end_field.slice(0, end_field.length - derivative_suffix.length));
53 return this.derivative(this.getField(field_copy));
54 } else {
55 return super.getField(field);
56 }
57 }
58}
59
James Kuszmaul78101402021-09-11 12:42:21 -070060export function plotTurret(conn: Connection, element: Element) : void {
61 const aosPlotter = new AosPlotter(conn);
62 const goal = aosPlotter.addMessageSource('/superstructure', 'y2020.control_loops.superstructure.Goal');
63 const output = aosPlotter.addMessageSource('/superstructure', 'y2020.control_loops.superstructure.Output');
James Kuszmaulda52ce62021-09-25 21:51:42 -070064 const status = aosPlotter.addRawMessageSource(
65 '/superstructure', 'y2020.control_loops.superstructure.Status',
66 new DerivativeMessageHandler(conn.getSchema('y2020.control_loops.superstructure.Status'))
67 );
James Kuszmaul78101402021-09-11 12:42:21 -070068 const pdpValues =
69 aosPlotter.addMessageSource('/roborio/aos', 'frc971.PDPValues');
James Kuszmaulda52ce62021-09-25 21:51:42 -070070 const localizerDebug =
71 aosPlotter.addMessageSource('/drivetrain', 'y2020.control_loops.drivetrain.LocalizerDebug');
James Kuszmaul78101402021-09-11 12:42:21 -070072
Austin Schuhc2e9c502021-11-25 21:23:24 -080073 const turretPosPlot = aosPlotter.addPlot(element);
James Kuszmaulda52ce62021-09-25 21:51:42 -070074 turretPosPlot.plot.getAxisLabels().setTitle('Turret Position');
James Kuszmaul78101402021-09-11 12:42:21 -070075 turretPosPlot.plot.getAxisLabels().setXLabel(TIME);
76 turretPosPlot.plot.getAxisLabels().setYLabel('rad');
77
78 turretPosPlot.addMessageLine(status, ['aimer', 'turret_position'])
79 .setColor(RED)
80 .setPointSize(0.0);
81 turretPosPlot.addMessageLine(status, ['turret', 'position'])
82 .setColor(GREEN)
83 .setPointSize(0.0);
James Kuszmaulda52ce62021-09-25 21:51:42 -070084 turretPosPlot.addMessageLine(localizerDebug, ['matches[]', 'implied_turret_goal'])
85 .setColor(GREEN)
86 .setDrawLine(false);
James Kuszmaul78101402021-09-11 12:42:21 -070087 turretPosPlot.addMessageLine(status, ['turret', 'unprofiled_goal_position'])
88 .setColor(BLUE)
James Kuszmaulda52ce62021-09-25 21:51:42 -070089 .setDrawLine(false);
90
Austin Schuhc2e9c502021-11-25 21:23:24 -080091 const turretVelPlot = aosPlotter.addPlot(element);
James Kuszmaulda52ce62021-09-25 21:51:42 -070092 turretVelPlot.plot.getAxisLabels().setTitle('Turret Velocity');
93 turretVelPlot.plot.getAxisLabels().setXLabel(TIME);
94 turretVelPlot.plot.getAxisLabels().setYLabel('rad / sec');
95
96 turretVelPlot.addMessageLine(status, ['aimer', 'turret_velocity'])
97 .setColor(RED)
98 .setPointSize(0.0);
99 turretVelPlot.addMessageLine(status, ['turret', 'velocity'])
100 .setColor(GREEN)
101 .setPointSize(0.0);
102 turretVelPlot.addMessageLine(status, ['turret', 'unprofiled_goal_velocity'])
103 .setColor(BLUE)
104 .setDrawLine(false);
105
Austin Schuhc2e9c502021-11-25 21:23:24 -0800106 const turretAccelPlot = aosPlotter.addPlot(element);
James Kuszmaulda52ce62021-09-25 21:51:42 -0700107 turretAccelPlot.plot.getAxisLabels().setTitle('Turret Acceleration');
108 turretAccelPlot.plot.getAxisLabels().setXLabel(TIME);
109 turretAccelPlot.plot.getAxisLabels().setYLabel('rad / sec / sec');
110
111 turretAccelPlot.addMessageLine(status, ['aimer', 'turret_velocity_derivative'])
112 .setColor(RED)
James Kuszmaul78101402021-09-11 12:42:21 -0700113 .setPointSize(0.0);
114
Austin Schuhc2e9c502021-11-25 21:23:24 -0800115 const turretVoltagePlot = aosPlotter.addPlot(element);
James Kuszmaul78101402021-09-11 12:42:21 -0700116 turretVoltagePlot.plot.getAxisLabels().setTitle('Turret Voltage');
117 turretVoltagePlot.plot.getAxisLabels().setXLabel(TIME);
118 turretVoltagePlot.plot.getAxisLabels().setYLabel('V');
119
James Kuszmaulda52ce62021-09-25 21:51:42 -0700120 turretVoltagePlot.addMessageLine(status, ['turret', 'voltage_error'])
121 .setColor(GREEN)
122 .setPointSize(0.0);
123 turretVoltagePlot.addMessageLine(status, ['turret', 'position_power'])
124 .setColor(BLUE)
125 .setPointSize(0.0);
126 turretVoltagePlot.addMessageLine(status, ['turret', 'velocity_power'])
127 .setColor(CYAN)
128 .setPointSize(0.0);
James Kuszmaul78101402021-09-11 12:42:21 -0700129 turretVoltagePlot.addMessageLine(output, ['turret_voltage'])
130 .setColor(RED)
131 .setPointSize(0.0);
132
Austin Schuhc2e9c502021-11-25 21:23:24 -0800133 const currentPlot = aosPlotter.addPlot(element);
James Kuszmaul78101402021-09-11 12:42:21 -0700134 currentPlot.plot.getAxisLabels().setTitle('Current');
135 currentPlot.plot.getAxisLabels().setXLabel(TIME);
136 currentPlot.plot.getAxisLabels().setYLabel('Amps');
James Kuszmaulda52ce62021-09-25 21:51:42 -0700137 currentPlot.plot.setDefaultYRange([0.0, 40.0]);
James Kuszmaul78101402021-09-11 12:42:21 -0700138
139 currentPlot.addMessageLine(pdpValues, ['currents[6]'])
140 .setColor(GREEN)
141 .setPointSize(0.0);
142
143
Austin Schuhc2e9c502021-11-25 21:23:24 -0800144 const targetDistancePlot = aosPlotter.addPlot(element);
James Kuszmaul78101402021-09-11 12:42:21 -0700145 targetDistancePlot.plot.getAxisLabels().setTitle('Target distance');
146 targetDistancePlot.plot.getAxisLabels().setXLabel(TIME);
147 targetDistancePlot.plot.getAxisLabels().setYLabel('m');
148
149 targetDistancePlot.addMessageLine(status, ['aimer', 'target_distance'])
150 .setColor(RED)
151 .setPointSize(0.0);
152
Austin Schuhc2e9c502021-11-25 21:23:24 -0800153 const targetChoicePlot = aosPlotter.addPlot(element);
James Kuszmaul78101402021-09-11 12:42:21 -0700154 targetChoicePlot.plot.getAxisLabels().setTitle('Target choice');
155 targetChoicePlot.plot.getAxisLabels().setXLabel(TIME);
156 targetChoicePlot.plot.getAxisLabels().setYLabel('[bool]');
157 targetChoicePlot.plot.setDefaultYRange([-0.05, 1.05]);
158
159 targetChoicePlot.addMessageLine(status, ['aimer', 'aiming_for_inner_port'])
160 .setColor(RED)
161 .setPointSize(0.0);
James Kuszmaulda52ce62021-09-25 21:51:42 -0700162
Austin Schuhc2e9c502021-11-25 21:23:24 -0800163 const imageAcceptedPlot = aosPlotter.addPlot(element);
James Kuszmaulda52ce62021-09-25 21:51:42 -0700164 imageAcceptedPlot.plot.getAxisLabels().setTitle('Image Acceptance');
165 imageAcceptedPlot.plot.getAxisLabels().setXLabel(TIME);
166 imageAcceptedPlot.plot.getAxisLabels().setYLabel('[bool]');
167 imageAcceptedPlot.plot.setDefaultYRange([-0.05, 1.05]);
168
169 imageAcceptedPlot.addMessageLine(localizerDebug, ['matches[]', 'accepted'])
170 .setColor(RED)
171 .setDrawLine(false);
James Kuszmaul78101402021-09-11 12:42:21 -0700172}