James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 1 | // This library provides a wrapper around our WebGL plotter that makes it |
| 2 | // easy to plot AOS messages/channels as time series. |
| 3 | // |
| 4 | // This is works by subscribing to each channel that we want to plot, storing |
| 5 | // all the messages for that channel, and then periodically running through |
| 6 | // every message and extracting the fields to plot. |
| 7 | // It is also possible to insert code to make modifications to the messages |
| 8 | // as we read/process them, as is the case for the IMU processing code (see |
| 9 | // //frc971/wpilib:imu*.ts) where each message is actually a batch of several |
| 10 | // individual messages that need to be plotted as separate points. |
| 11 | // |
| 12 | // The basic flow for using the AosPlotter is: |
| 13 | // // 1) Construct the plotter |
| 14 | // const aosPlotter = new AosPlotter(connection); |
| 15 | // // 2) Add messages sources that we'll want to subscribe to. |
| 16 | // const source = aosPlotter.addMessageSource('/aos', 'aos.timing.Report'); |
| 17 | // // 3) Create figures at defined positions within a given HTML element.. |
| 18 | // const timingPlot = aosPlotter.addPlot(parentDiv, [0, 0], [width, height]); |
| 19 | // // 4) Add specific signals to each figure, using the message sources you |
| 20 | // defined at the start. |
| 21 | // timingPlot.addMessageLine(source, ['pid']); |
| 22 | // |
| 23 | // The demo_plot.ts script has a basic example of using this library, with all |
| 24 | // the required boilerplate, as well as some extra examples about how to |
| 25 | // add axis labels and the such. |
| 26 | import * as configuration from 'org_frc971/aos/configuration_generated'; |
James Kuszmaul | 0d7df89 | 2021-04-09 22:19:49 -0700 | [diff] [blame] | 27 | import {Line, Plot, Point} from 'org_frc971/aos/network/www/plotter'; |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 28 | import * as proxy from 'org_frc971/aos/network/www/proxy'; |
| 29 | import * as web_proxy from 'org_frc971/aos/network/web_proxy_generated'; |
| 30 | import * as reflection from 'org_frc971/aos/network/www/reflection' |
| 31 | import * as flatbuffers_builder from 'org_frc971/external/com_github_google_flatbuffers/ts/builder'; |
| 32 | import {ByteBuffer} from 'org_frc971/external/com_github_google_flatbuffers/ts/byte-buffer'; |
| 33 | |
| 34 | import Channel = configuration.aos.Channel; |
| 35 | import Connection = proxy.Connection; |
| 36 | import Configuration = configuration.aos.Configuration; |
| 37 | import Schema = configuration.reflection.Schema; |
| 38 | import Parser = reflection.Parser; |
| 39 | import Table = reflection.Table; |
| 40 | import SubscriberRequest = web_proxy.aos.web_proxy.SubscriberRequest; |
| 41 | import ChannelRequest = web_proxy.aos.web_proxy.ChannelRequest; |
| 42 | import TransferMethod = web_proxy.aos.web_proxy.TransferMethod; |
| 43 | |
| 44 | export class TimestampedMessage { |
| 45 | constructor( |
| 46 | public readonly message: Table, public readonly time: number) {} |
| 47 | } |
| 48 | |
| 49 | // The MessageHandler stores an array of every single message on a given channel |
| 50 | // and then supplies individual fields as arrays when requested. Currently this |
| 51 | // is very much unoptimized and re-processes the entire array of messages on |
| 52 | // every call to getField(). |
| 53 | export class MessageHandler { |
| 54 | protected parser: Parser; |
| 55 | protected messages: TimestampedMessage[] = []; |
| 56 | constructor(schema: Schema) { |
| 57 | this.parser = new Parser(schema); |
| 58 | } |
| 59 | addMessage(data: Uint8Array, time: number): void { |
| 60 | this.messages.push( |
| 61 | new TimestampedMessage(Table.getRootTable(new ByteBuffer(data)), time)); |
| 62 | } |
James Kuszmaul | f8355ff | 2021-12-19 22:08:45 -0800 | [diff] [blame^] | 63 | private parseFieldName(rawName: string): [string, boolean, number|null] { |
| 64 | // If the fieldName includes an array index at the end, attempt to read a |
| 65 | // vector. |
| 66 | // The indices can either be in the form [X] for some index X, or be an |
| 67 | // empty [], which is a request to return all values. |
James Kuszmaul | 8073c0d | 2021-03-07 20:14:41 -0800 | [diff] [blame] | 68 | const regex = /(.*)\[([0-9]*)\]/; |
James Kuszmaul | f8355ff | 2021-12-19 22:08:45 -0800 | [diff] [blame^] | 69 | const match = rawName.match(regex); |
James Kuszmaul | 8073c0d | 2021-03-07 20:14:41 -0800 | [diff] [blame] | 70 | if (match) { |
| 71 | const name = match[1]; |
James Kuszmaul | f8355ff | 2021-12-19 22:08:45 -0800 | [diff] [blame^] | 72 | const requestFullVector = match[2] === ''; |
| 73 | const requestedIndex = requestFullVector ? null : parseInt(match[2]); |
| 74 | return [name, true, requestedIndex]; |
| 75 | } else { |
| 76 | return [rawName, false, null]; |
James Kuszmaul | 8073c0d | 2021-03-07 20:14:41 -0800 | [diff] [blame] | 77 | } |
James Kuszmaul | f8355ff | 2021-12-19 22:08:45 -0800 | [diff] [blame^] | 78 | } |
| 79 | private readField<T>( |
| 80 | typeIndex: number, fieldName: string, |
| 81 | normalReader: (typeIndex: number, name: string) => (t: Table) => T | null, |
| 82 | vectorReader: (typeIndex: number, name: string) => (t: Table) => T[] | |
| 83 | null): (t: Table) => T[] | null { |
| 84 | const [name, isVector, vectorIndex] = this.parseFieldName(fieldName); |
| 85 | if (isVector) { |
| 86 | const vectorLambda = vectorReader(typeIndex, name); |
| 87 | const requestFullVector = vectorIndex === null; |
| 88 | return (message: Table) => { |
| 89 | const vector = vectorLambda(message); |
| 90 | if (vector === null) { |
| 91 | return null; |
| 92 | } |
| 93 | if (requestFullVector) { |
| 94 | return vector; |
| 95 | } else { |
| 96 | return (vectorIndex < vector.length) ? [vector[vectorIndex]] : null; |
| 97 | } |
| 98 | }; |
| 99 | } |
| 100 | const singleLambda = normalReader(typeIndex, fieldName); |
| 101 | return (message: Table) => { |
| 102 | // For single values, return as a 1-vector so that the type is |
| 103 | // consistent with that used for vectors. |
| 104 | const singleValue = singleLambda(message); |
| 105 | return (singleValue === null) ? null : [singleValue]; |
| 106 | }; |
James Kuszmaul | 8073c0d | 2021-03-07 20:14:41 -0800 | [diff] [blame] | 107 | } |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 108 | // Returns a time-series of every single instance of the given field. Format |
| 109 | // of the return value is [time0, value0, time1, value1,... timeN, valueN], |
| 110 | // to match with the Line.setPoint() interface. |
| 111 | // By convention, NaN is used to indicate that a message existed at a given |
| 112 | // timestamp but the requested field was not populated. |
James Kuszmaul | f3727e8 | 2021-03-06 16:52:51 -0800 | [diff] [blame] | 113 | // If you want to retrieve a single signal from a vector, you can specify it |
| 114 | // as "field_name[index]". |
James Kuszmaul | 0d7df89 | 2021-04-09 22:19:49 -0700 | [diff] [blame] | 115 | getField(field: string[]): Point[] { |
James Kuszmaul | f8355ff | 2021-12-19 22:08:45 -0800 | [diff] [blame^] | 116 | if (this.messages.length == 0) { |
| 117 | return []; |
| 118 | } |
| 119 | const rootType = this.messages[0].message.typeIndex; |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 120 | const fieldName = field[field.length - 1]; |
| 121 | const subMessage = field.slice(0, field.length - 1); |
James Kuszmaul | f8355ff | 2021-12-19 22:08:45 -0800 | [diff] [blame^] | 122 | |
| 123 | const lambdas = []; |
| 124 | let currentType = rootType; |
| 125 | for (const subMessageName of subMessage) { |
| 126 | lambdas.push(this.readField( |
| 127 | currentType, subMessageName, |
| 128 | (typeIndex: number, name: string) => |
| 129 | this.parser.readTableLambda(typeIndex, name), |
| 130 | (typeIndex: number, name: string) => |
| 131 | this.parser.readVectorOfTablesLambda(typeIndex, name))); |
| 132 | const [name, isVector, vectorIndex] = this.parseFieldName(subMessageName); |
| 133 | currentType = |
| 134 | this.parser.getField(name, currentType).type().index(); |
| 135 | } |
| 136 | const fieldLambda = this.readField( |
| 137 | currentType, fieldName, |
| 138 | (typeIndex: number, name: string) => |
| 139 | this.parser.readScalarLambda(typeIndex, name), |
| 140 | (typeIndex: number, name: string) => |
| 141 | this.parser.readVectorOfScalarsLambda(typeIndex, name)); |
| 142 | |
James Kuszmaul | 8073c0d | 2021-03-07 20:14:41 -0800 | [diff] [blame] | 143 | const results = []; |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 144 | for (let ii = 0; ii < this.messages.length; ++ii) { |
James Kuszmaul | 8073c0d | 2021-03-07 20:14:41 -0800 | [diff] [blame] | 145 | let tables = [this.messages[ii].message]; |
James Kuszmaul | f8355ff | 2021-12-19 22:08:45 -0800 | [diff] [blame^] | 146 | for (const lambda of lambdas) { |
James Kuszmaul | 8073c0d | 2021-03-07 20:14:41 -0800 | [diff] [blame] | 147 | let nextTables = []; |
| 148 | for (const table of tables) { |
James Kuszmaul | f8355ff | 2021-12-19 22:08:45 -0800 | [diff] [blame^] | 149 | const nextTable = lambda(table); |
James Kuszmaul | 8073c0d | 2021-03-07 20:14:41 -0800 | [diff] [blame] | 150 | if (nextTable === null) { |
| 151 | continue; |
| 152 | } |
| 153 | nextTables = nextTables.concat(nextTable); |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 154 | } |
James Kuszmaul | 8073c0d | 2021-03-07 20:14:41 -0800 | [diff] [blame] | 155 | tables = nextTables; |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 156 | } |
James Kuszmaul | f8355ff | 2021-12-19 22:08:45 -0800 | [diff] [blame^] | 157 | |
| 158 | const values = []; |
| 159 | for (const table of tables) { |
| 160 | const value = fieldLambda(table); |
| 161 | if (value !== null) { |
| 162 | values.push(value); |
| 163 | } |
| 164 | } |
James Kuszmaul | 8073c0d | 2021-03-07 20:14:41 -0800 | [diff] [blame] | 165 | const time = this.messages[ii].time; |
| 166 | if (tables.length === 0) { |
James Kuszmaul | 0d7df89 | 2021-04-09 22:19:49 -0700 | [diff] [blame] | 167 | results.push(new Point(time, NaN)); |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 168 | } else { |
James Kuszmaul | f8355ff | 2021-12-19 22:08:45 -0800 | [diff] [blame^] | 169 | for (const valueVector of values) { |
| 170 | for (const value of valueVector) { |
| 171 | results.push(new Point(time, (value === null) ? NaN : value)); |
James Kuszmaul | f3727e8 | 2021-03-06 16:52:51 -0800 | [diff] [blame] | 172 | } |
| 173 | } |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 174 | } |
| 175 | } |
James Kuszmaul | 0d7df89 | 2021-04-09 22:19:49 -0700 | [diff] [blame] | 176 | return results; |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 177 | } |
| 178 | numMessages(): number { |
| 179 | return this.messages.length; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | class MessageLine { |
James Kuszmaul | 3e382c0 | 2021-04-09 22:34:36 -0700 | [diff] [blame] | 184 | private _lastNumMessages: number = 0; |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 185 | constructor( |
| 186 | public readonly messages: MessageHandler, public readonly line: Line, |
| 187 | public readonly field: string[]) {} |
James Kuszmaul | 3e382c0 | 2021-04-09 22:34:36 -0700 | [diff] [blame] | 188 | hasUpdate(): boolean { |
| 189 | const updated = this._lastNumMessages != this.messages.numMessages(); |
| 190 | this._lastNumMessages = this.messages.numMessages(); |
| 191 | return updated; |
| 192 | } |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | class AosPlot { |
| 196 | private lines: MessageLine[] = []; |
| 197 | constructor( |
| 198 | private readonly plotter: AosPlotter, public readonly plot: Plot) {} |
| 199 | |
| 200 | // Adds a line to the figure. |
| 201 | // message specifies what channel/data source to pull from, and field |
| 202 | // specifies the field within that channel. field is an array specifying |
| 203 | // the full path to the field within the message. For instance, to |
| 204 | // plot whether the drivetrain is currently zeroed based on the drivetrain |
| 205 | // status message, you would specify the ['zeroing', 'zeroed'] field to |
| 206 | // get the DrivetrainStatus.zeroing().zeroed() member. |
| 207 | // Currently, this interface does not provide any support for non-numeric |
| 208 | // fields or for repeated fields (or sub-messages) of any sort. |
| 209 | addMessageLine(message: MessageHandler, field: string[]): Line { |
| 210 | const line = this.plot.getDrawer().addLine(); |
| 211 | line.setLabel(field.join('.')); |
| 212 | this.lines.push(new MessageLine(message, line, field)); |
| 213 | return line; |
| 214 | } |
| 215 | |
| 216 | draw(): void { |
| 217 | // Only redraw lines if the number of points has changed--because getField() |
| 218 | // is a relatively expensive call, we don't want to do it any more than |
| 219 | // necessary. |
| 220 | for (const line of this.lines) { |
James Kuszmaul | 3e382c0 | 2021-04-09 22:34:36 -0700 | [diff] [blame] | 221 | if (line.hasUpdate()) { |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 222 | line.line.setPoints(line.messages.getField(line.field)); |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | export class AosPlotter { |
milind upadhyay | 9bd381d | 2021-01-23 13:44:13 -0800 | [diff] [blame] | 229 | public static readonly TIME: string = "Monotonic Time (sec)"; |
| 230 | |
| 231 | public static readonly DEFAULT_WIDTH: number = 900; |
| 232 | public static readonly DEFAULT_HEIGHT: number = 400; |
| 233 | |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 234 | private plots: AosPlot[] = []; |
| 235 | private messages = new Set<MessageHandler>(); |
| 236 | constructor(private readonly connection: Connection) { |
| 237 | // Set up to redraw at some regular interval. The exact rate is unimportant. |
| 238 | setInterval(() => { |
| 239 | this.draw(); |
| 240 | }, 100); |
| 241 | } |
| 242 | |
| 243 | // Sets up an AOS channel as a message source. Returns a handler that can |
| 244 | // be passed to addMessageLine(). |
| 245 | addMessageSource(name: string, type: string): MessageHandler { |
| 246 | return this.addRawMessageSource( |
| 247 | name, type, new MessageHandler(this.connection.getSchema(type))); |
| 248 | } |
| 249 | |
| 250 | // Same as addMessageSource, but allows you to specify a custom MessageHandler |
| 251 | // that does some processing on the requested message. This allows you to |
| 252 | // create post-processed versions of individual channels. |
| 253 | addRawMessageSource( |
| 254 | name: string, type: string, |
| 255 | messageHandler: MessageHandler): MessageHandler { |
| 256 | this.messages.add(messageHandler); |
| 257 | // Use a "reliable" handler so that we get *all* the data when we are |
| 258 | // plotting from a logfile. |
| 259 | this.connection.addReliableHandler( |
| 260 | name, type, (data: Uint8Array, time: number) => { |
| 261 | messageHandler.addMessage(data, time); |
| 262 | }); |
| 263 | return messageHandler; |
| 264 | } |
| 265 | // Add a new figure at the provided position with the provided size within |
| 266 | // parentElement. |
James Kuszmaul | 9dbb99a | 2021-03-07 20:01:27 -0800 | [diff] [blame] | 267 | addPlot( |
Austin Schuh | c2e9c50 | 2021-11-25 21:23:24 -0800 | [diff] [blame] | 268 | parentElement: Element, |
James Kuszmaul | 9dbb99a | 2021-03-07 20:01:27 -0800 | [diff] [blame] | 269 | size: number[] = [AosPlotter.DEFAULT_WIDTH, AosPlotter.DEFAULT_HEIGHT]): |
| 270 | AosPlot { |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 271 | const div = document.createElement("div"); |
Austin Schuh | c2e9c50 | 2021-11-25 21:23:24 -0800 | [diff] [blame] | 272 | div.style.position = 'relative'; |
| 273 | div.style.width = size[0].toString() + "px"; |
| 274 | div.style.height = size[1].toString() + "px"; |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 275 | parentElement.appendChild(div); |
Austin Schuh | c2e9c50 | 2021-11-25 21:23:24 -0800 | [diff] [blame] | 276 | const newPlot = new Plot(div); |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 277 | for (let plot of this.plots.values()) { |
| 278 | newPlot.linkXAxis(plot.plot); |
| 279 | } |
| 280 | this.plots.push(new AosPlot(this, newPlot)); |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 281 | return this.plots[this.plots.length - 1]; |
| 282 | } |
| 283 | private draw(): void { |
| 284 | for (const plot of this.plots) { |
| 285 | plot.draw(); |
| 286 | } |
| 287 | } |
| 288 | } |