James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame^] | 1 | // This script provides a basic utility for de-batching the IMUValues |
| 2 | // message. See imu_plotter.ts for usage. |
| 3 | import * as configuration from 'org_frc971/aos/configuration_generated'; |
| 4 | import * as imu from 'org_frc971/frc971/wpilib/imu_batch_generated'; |
| 5 | import {MessageHandler, TimestampedMessage} from 'org_frc971/aos/network/www/aos_plotter'; |
| 6 | import {Table} from 'org_frc971/aos/network/www/reflection'; |
| 7 | import {ByteBuffer} from 'org_frc971/external/com_github_google_flatbuffers/ts/byte-buffer'; |
| 8 | |
| 9 | import Schema = configuration.reflection.Schema; |
| 10 | import IMUValuesBatch = imu.frc971.IMUValuesBatch; |
| 11 | import IMUValues = imu.frc971.IMUValues; |
| 12 | |
| 13 | export 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 | } |