Lidar and IMU Packets

The PacketSource is the basic interface for sensor packets. 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.Pcap:

with open(metadata_path, 'r') as f:
    metadata = client.SensorInfo(f.read())

source = pcap.Pcap(pcap_path, metadata)

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

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

    elif isinstance(packet, client.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 $SAMPLE_DATA_JSON_PATH read-packets