演示
Command Line
1~/examples/IMU$ python3 imu_gyroscope_accelerometer.py
2Accelerometer timestamp: 27 days, 4:31:26.532170
3Latency [ms]: 0:00:00.004806
4Accelerometer [m/s^2]: x: -0.098162 y: -0.062249 z: -9.715671
5Gyroscope timestamp: 27 days, 4:31:26.532170
6Gyroscope [rad/s]: x: 0.002131 y: 0.019175 z: 0.001065
7Accelerometer timestamp: 27 days, 4:31:26.534664
8Latency [ms]: 0:00:00.006309
9Accelerometer [m/s^2]: x: -0.064643 y: -0.119710 z: -9.758766
10Gyroscope timestamp: 27 days, 4:31:26.534664
11Gyroscope [rad/s]: x: 0.002131 y: 0.019175 z: 0.002131管道
源代码
Python
PythonGitHub
1#!/usr/bin/env python3
2import time
3
4import depthai as dai
5
6
7def timeDeltaToMilliS(delta) -> float:
8 return delta.total_seconds()*1000
9
10
11# Create pipeline
12with dai.Pipeline() as pipeline:
13 # Define sources and outputs
14 imu = pipeline.create(dai.node.IMU)
15
16 # enable ACCELEROMETER_UNCALIBRATED at 500 hz rate
17 imu.enableIMUSensor(dai.IMUSensor.ACCELEROMETER_UNCALIBRATED, 480)
18 # enable GYROSCOPE_UNCALIBRATED at 400 hz rate
19 imu.enableIMUSensor(dai.IMUSensor.GYROSCOPE_UNCALIBRATED, 400)
20 # it's recommended to set both setBatchReportThreshold and setMaxBatchReports to 20 when integrating in a pipeline with a lot of input/output connections
21 # above this threshold packets will be sent in batch of X, if the host is not blocked and USB bandwidth is available
22 imu.setBatchReportThreshold(1)
23 # maximum number of IMU packets in a batch, if it's reached device will block sending until host can receive it
24 # if lower or equal to batchReportThreshold then the sending is always blocking on device
25 # useful to reduce device's CPU load and number of lost packets, if CPU load is high on device side due to multiple nodes
26 imu.setMaxBatchReports(10)
27
28 imuQueue = imu.out.createOutputQueue(maxSize=50, blocking=False)
29
30 pipeline.start()
31 baseTs = None
32 lastPrintTime = 0.0
33 while pipeline.isRunning():
34 try:
35 imuData = imuQueue.get()
36 except KeyboardInterrupt:
37 break
38 assert isinstance(imuData, dai.IMUData)
39 imuPackets = imuData.packets
40 if not imuPackets:
41 continue
42
43 now = time.monotonic()
44 if now - lastPrintTime < 1.0:
45 continue
46 lastPrintTime = now
47
48 imuPacket = imuPackets[-1]
49 acceleroValues = imuPacket.acceleroMeter
50 gyroValues = imuPacket.gyroscope
51
52 acceleroTs = acceleroValues.getTimestamp()
53 gyroTs = gyroValues.getTimestamp()
54
55 imuF = "{:.06f}"
56
57 print(f"Accelerometer timestamp: {acceleroTs}")
58 print(f"Latency [ms]: {dai.Clock.now() - acceleroValues.getTimestamp()}")
59 print(f"Accelerometer [m/s^2]: x: {imuF.format(acceleroValues.x)} y: {imuF.format(acceleroValues.y)} z: {imuF.format(acceleroValues.z)}")
60 print(f"Gyroscope timestamp: {gyroTs}")
61 print(f"Gyroscope [rad/s]: x: {imuF.format(gyroValues.x)} y: {imuF.format(gyroValues.y)} z: {imuF.format(gyroValues.z)} ")
62 print()需要帮助?
请前往 OAKChina 官网 获取技术支持或解答您的任何疑问。