How to Get Lidar and IMU Data with Ouster Python SDK

How to Get Lidar and IMU Data with Ouster Python SDK

This document provides a simplified guide to using the Ouster Python SDK to acquire and display live lidar and IMU data.

 

Python Code Implementation

import ouster.sdk.client as client SIDX = 0 # show casing the example for a single sensor for simplicity class ScanSourceWithIMU: def __init__(self, source_url): self._source = client.SensorPacketSource(source_url) # or use a PcapPacketSource self._metadata = self._source._metadata def __iter__(self): packet_format = client.PacketFormat(self._metadata[SIDX]) field_types = client.get_field_types(self._metadata[SIDX]) columns_per_packet = self._metadata[SIDX].format.columns_per_packet W = self._metadata[SIDX].format.columns_per_frame H = self._metadata[SIDX].format.pixels_per_column batcher = client.ScanBatcher(self._metadata[SIDX]) ls_write = LidarScan(H, W, field_types, columns_per_packet) for _, packet in self._source: if isinstance(packet, client.LidarPacket): if batcher(packet, ls_write): yield [copy.deepcopy(ls_write)] if isinstance(packet, core.ImuPacket): STANDARD_G = 9.80665 ts1 = packet_format.imu_accel_ts(packet.buf) ax = STANDARD_G * packet_format.imu_la_x(packet.buf) ay = STANDARD_G * packet_format.imu_la_y(packet.buf) az = STANDARD_G * packet_format.imu_la_z(packet.buf) ts2 = packet_format.imu_gyro_ts(packet.buf) wx = np.deg2rad(packet_format.imu_av_x(packet.buf)) wy = np.deg2rad(packet_format.imu_av_y(packet.buf)) wz = np.deg2rad(packet_format.imu_av_z(packet.buf)) print(ts1, ax, ay, az, ts2, wx, wy, wz) def close() self._source.close() if __name__ == "__main__": scan_imu_source = ScanSourceWithIMU(SOURCE_URL) try: for data in scan_imu_source: if "lidar_scan" in data: lidar_scan = data["lidar_scan"] print(f"Received Lidar Scan with {lidar_scan.h} rows and {lidar_scan.w} columns.") # Add your lidar data processing and visualization here except KeyboardInterrupt: print("Stopping data stream.") finally: scan_imu_source.close()

Setup Instructions

  1. Install Ouster Python SDK:

pip install ouster-sdk
  1. Replace Placeholder:

    • Replace "your_sensor_hostname_or_ip" with your Ouster sensor's address.

  2. Run the Code:

python3 your_script_name.py
  • This will print IMU data to the console.

  • To display lidar data, add visualization code (e.g., using Open3D) in the if "lidar_scan" in data: block.