@foxglove/velodyne-cloud
Version:
TypeScript library for converting Velodyne LIDAR packet data to point clouds
96 lines • 4.4 kB
JavaScript
;
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/
Object.defineProperty(exports, "__esModule", { value: true });
exports.RawPacket = void 0;
const RawBlock_1 = require("./RawBlock");
const VelodyneTypes_1 = require("./VelodyneTypes");
/**
* Parses a raw Velodyne UDP packet. The packet must be exactly 1206 bytes.
*/
class RawPacket {
constructor(data) {
this.data = data;
if (data.length !== 1206) {
throw new Error(`data has invalid length ${data.length}, expected 1206`);
}
this.blocks = [];
for (let i = 0; i < RawPacket.BLOCKS_PER_PACKET; i++) {
const blockSize = RawPacket.BLOCK_SIZE;
const blockData = new Uint8Array(data.buffer, data.byteOffset + blockSize * i, blockSize);
this.blocks.push(new RawBlock_1.RawBlock(blockData));
}
const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
this.gpsTimestamp = view.getUint32(1200, true);
this.factoryField1 = data[1204];
this.factoryField2 = data[1205];
this.returnMode = this.factoryField1 in VelodyneTypes_1.ReturnMode ? this.factoryField1 : undefined;
this.factoryId = this.factoryField2 in VelodyneTypes_1.FactoryId ? this.factoryField2 : undefined;
}
inferModel() {
return RawPacket.InferModel(this.data);
}
/**
* Converts the gpsTimestamp field to an absolute number of fractional seconds
* since the UNIX epoch. Since gpsTimestamp is relative to the top of the
* hour, the top of the hour can be specified. Otherwise, the most recent hour
* will be used
* @param topOfHour Optional Date representing the top of the hour the
* gpsTimestamp is relative to. If unspecified, the most recent top of the
* hour (relative to now) will be used
*/
timestamp(topOfHour) {
return RawPacket.GpsTimestampToTimestamp(this.gpsTimestamp, topOfHour);
}
static InferModel(packet) {
const factoryId = packet[1205];
switch (factoryId) {
case VelodyneTypes_1.FactoryId.HDL32E:
return VelodyneTypes_1.Model.HDL32E;
case VelodyneTypes_1.FactoryId.VLP16:
return VelodyneTypes_1.Model.VLP16;
case VelodyneTypes_1.FactoryId.VLP32AB:
return undefined;
case VelodyneTypes_1.FactoryId.VLP16HiRes:
return VelodyneTypes_1.Model.VLP16HiRes;
case VelodyneTypes_1.FactoryId.VLP32C:
return VelodyneTypes_1.Model.VLP32C;
case VelodyneTypes_1.FactoryId.Velarray:
return undefined;
case VelodyneTypes_1.FactoryId.HDL64:
// Is it possible to distinguish HDL64E / HDL64E_S21 / HDL64E_S3?
return VelodyneTypes_1.Model.HDL64E;
case VelodyneTypes_1.FactoryId.VLS128Old:
case VelodyneTypes_1.FactoryId.VLS128:
return VelodyneTypes_1.Model.VLS128;
default:
return undefined;
}
}
/**
* Convert a gpsTimestamp field representing the number of microseconds since
* the top of the hour to an absolute timestamp as fractional seconds since
* the UNIX epoch
* @param gpsTimestamp Number of microseconds since the top of the hour. This
* field is a member of the RawPacket class
* @param topOfHour Optional Date representing the top of the hour the
* gpsTimestamp is relative to. If unspecified, the most recent top of the
* hour (relative to now) will be used
*/
static GpsTimestampToTimestamp(gpsTimestamp, topOfHour) {
if (topOfHour == undefined) {
topOfHour = new Date();
topOfHour.setMinutes(0, 0, 0);
}
return +topOfHour / 1000 + gpsTimestamp / 1e-6;
}
}
exports.RawPacket = RawPacket;
RawPacket.RAW_SCAN_SIZE = 3;
RawPacket.SCANS_PER_BLOCK = 32;
RawPacket.BLOCK_DATA_SIZE = RawPacket.SCANS_PER_BLOCK * RawPacket.RAW_SCAN_SIZE;
RawPacket.BLOCK_SIZE = RawPacket.BLOCK_DATA_SIZE + 4;
RawPacket.BLOCKS_PER_PACKET = 12;
RawPacket.MAX_POINTS_PER_PACKET = RawPacket.BLOCKS_PER_PACKET * RawPacket.SCANS_PER_BLOCK;
//# sourceMappingURL=RawPacket.js.map