blob: 4e6c371bbd1a5ab023f4dab3d02d8e54e166a821 [file] [log] [blame]
James Kuszmaul5f5e1232020-12-22 20:58:00 -08001// This script provides a basic utility for de-batching the IMUValues
2// message. See imu_plotter.ts for usage.
3import * as configuration from 'org_frc971/aos/configuration_generated';
4import * as imu from 'org_frc971/frc971/wpilib/imu_batch_generated';
5import {MessageHandler, TimestampedMessage} from 'org_frc971/aos/network/www/aos_plotter';
6import {Table} from 'org_frc971/aos/network/www/reflection';
7import {ByteBuffer} from 'org_frc971/external/com_github_google_flatbuffers/ts/byte-buffer';
8
9import Schema = configuration.reflection.Schema;
10import IMUValuesBatch = imu.frc971.IMUValuesBatch;
11import IMUValues = imu.frc971.IMUValues;
12
13export class ImuMessageHandler extends MessageHandler {
14 constructor(private readonly schema: Schema) {
15 super(schema);
16 }
17 addMessage(data: Uint8Array, time: number): void {
18 const batch = IMUValuesBatch.getRootAsIMUValuesBatch(
19 new ByteBuffer(data) as unknown as flatbuffers.ByteBuffer);
20 for (let ii = 0; ii < batch.readingsLength(); ++ii) {
21 const message = batch.readings(ii);
22 const table = Table.getNamedTable(
23 message.bb as unknown as ByteBuffer, this.schema, 'frc971.IMUValues',
24 message.bb_pos);
25 if (this.parser.readScalar(table, "monotonic_timestamp_ns") == null) {
26 console.log('Ignoring unpopulated IMU values: ');
27 console.log(this.parser.toObject(table));
28 continue;
29 }
30 this.messages.push(new TimestampedMessage(
31 table, message.monotonicTimestampNs().toFloat64() * 1e-9));
32 }
33 }
34}