|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import cv2 |
| 4 | +import depthai as dai |
| 5 | +import time |
| 6 | +import math |
| 7 | + |
| 8 | +# Create pipeline |
| 9 | +pipeline = dai.Pipeline() |
| 10 | + |
| 11 | +# Define sources and outputs |
| 12 | +imu = pipeline.createIMU() |
| 13 | +xlinkOut = pipeline.createXLinkOut() |
| 14 | + |
| 15 | +xlinkOut.setStreamName("imu") |
| 16 | + |
| 17 | +# enable ACCELEROMETER_RAW and GYROSCOPE_RAW at 500 hz rate |
| 18 | +imu.enableIMUSensor([dai.IMUSensor.ACCELEROMETER_RAW, dai.IMUSensor.GYROSCOPE_RAW], 500) |
| 19 | +# above this threshold packets will be sent in batch of X, if the host is not blocked and USB bandwidth is available |
| 20 | +imu.setBatchReportThreshold(1) |
| 21 | +# maximum number of IMU packets in a batch, if it's reached device will block sending until host can receive it |
| 22 | +# if lower or equal to batchReportThreshold then the sending is always blocking on device |
| 23 | +# useful to reduce device's CPU load and number of lost packets, if CPU load is high on device side due to multiple nodes |
| 24 | +imu.setMaxBatchReports(10) |
| 25 | + |
| 26 | +# Link plugins IMU -> XLINK |
| 27 | +imu.out.link(xlinkOut.input) |
| 28 | + |
| 29 | +# Pipeline is defined, now we can connect to the device |
| 30 | +with dai.Device(pipeline) as device: |
| 31 | + |
| 32 | + def timeDeltaToMilliS(delta) -> float: |
| 33 | + return delta.total_seconds()*1000 |
| 34 | + |
| 35 | + # Output queue for imu bulk packets |
| 36 | + imuQueue = device.getOutputQueue(name="imu", maxSize=50, blocking=False) |
| 37 | + baseTs = None |
| 38 | + while True: |
| 39 | + imuData = imuQueue.get() # blocking call, will wait until a new data has arrived |
| 40 | + |
| 41 | + imuPackets = imuData.packets |
| 42 | + for imuPacket in imuPackets: |
| 43 | + acceleroValues = imuPacket.acceleroMeter |
| 44 | + gyroValues = imuPacket.gyroscope |
| 45 | + |
| 46 | + acceleroTs = acceleroValues.timestamp.get() |
| 47 | + gyroTs = gyroValues.timestamp.get() |
| 48 | + if baseTs is None: |
| 49 | + baseTs = acceleroTs if acceleroTs < gyroTs else gyroTs |
| 50 | + acceleroTs = timeDeltaToMilliS(acceleroTs - baseTs) |
| 51 | + gyroTs = timeDeltaToMilliS(gyroTs - baseTs) |
| 52 | + |
| 53 | + imuF = "{:.06f}" |
| 54 | + tsF = "{:.03f}" |
| 55 | + |
| 56 | + print(f"Accelerometer timestamp: {tsF.format(acceleroTs)} ms") |
| 57 | + print(f"Accelerometer [m/s^2]: x: {imuF.format(acceleroValues.x)} y: {imuF.format(acceleroValues.y)} z: {imuF.format(acceleroValues.z)}") |
| 58 | + print(f"Gyroscope timestamp: {tsF.format(gyroTs)} ms") |
| 59 | + print(f"Gyroscope [rad/s]: x: {imuF.format(gyroValues.x)} y: {imuF.format(gyroValues.y)} z: {imuF.format(gyroValues.z)} ") |
| 60 | + |
| 61 | + if cv2.waitKey(1) == ord('q'): |
| 62 | + break |
0 commit comments