Lidar and IMU Packets

The PacketSource (previously .PacketMultiSource) is the basic interface for sensor packets and returns Tuple[int, Packet]. It can be advantageous to work with packets directly when latency is a concern, or when you wish to examine the data packet by packet, e.g., if you wish to examine timestamps of packets.

Let’s make a PacketSource from our sample data using pcap.PcapPacketSource:

source = pcap.PacketSource(pcap_path)
for packet in source:
    ...  # a list of packets, with (sensor_idx, Packet)

Now we can read packets from source with the following code:

# open source
source = pcap.PcapPacketSource(source_file)
metadata = source.sensor_info[0]
packet_format = core.PacketFormat(metadata)
for idx, packet in source:
    if isinstance(packet, core.LidarPacket):
        # Now we can process the LidarPacket. In this case, we access
        # the measurement ids, timestamps, and ranges
        measurement_ids = packet_format.packet_header(core.ColHeader.MEASUREMENT_ID, packet.buf)
        timestamps = packet_format.packet_header(core.ColHeader.TIMESTAMP, packet.buf)
        ranges = packet_format.packet_field(core.ChanField.RANGE, packet.buf)
        print(f'  encoder counts = {measurement_ids.shape}')
        print(f'  timestamps = {timestamps.shape}')
        print(f'  ranges = {ranges.shape}')

    elif isinstance(packet, core.ImuPacket):
        # and access ImuPacket content
        ax = packet_format.imu_la_x(packet.buf)
        ay = packet_format.imu_la_y(packet.buf)
        az = packet_format.imu_la_z(packet.buf)

        wx = packet_format.imu_av_x(packet.buf)
        wy = packet_format.imu_av_y(packet.buf)
        wz = packet_format.imu_av_z(packet.buf)

        print(f'  acceleration = {ax}, {ay}, {az}')
        print(f'  angular_velocity = {wx}, {wy}, {wz}')

You can try the above code with the pcap_read_packets():

$ python3 -m ouster.sdk.examples.pcap $SAMPLE_DATA_PCAP_PATH read-packets