IMU Accelerometer & Gyroscope
This example shows accelerometer and gyroscope at a combined/synchronized 400 Hz rate using the onboard IMU. Returns acceleration [m/s^2] and angular velocity [rad/s].Demo
Example script outputCommand 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
Setup
This example requires the DepthAI v3 API, see installation instructions.Pipeline
Source code
Python
C++
Python
PythonGitHub
1#!/usr/bin/env python3
2import depthai as dai
3
4# Create pipeline
5with dai.Pipeline() as pipeline:
6 # Define sources and outputs
7 imu = pipeline.create(dai.node.IMU)
8
9 # enable ACCELEROMETER_RAW at 500 hz rate
10 imu.enableIMUSensor(dai.IMUSensor.ACCELEROMETER_RAW, 480)
11 # enable GYROSCOPE_RAW at 400 hz rate
12 imu.enableIMUSensor(dai.IMUSensor.GYROSCOPE_RAW, 400)
13 # it's recommended to set both setBatchReportThreshold and setMaxBatchReports to 20 when integrating in a pipeline with a lot of input/output connections
14 # above this threshold packets will be sent in batch of X, if the host is not blocked and USB bandwidth is available
15 imu.setBatchReportThreshold(1)
16 # maximum number of IMU packets in a batch, if it's reached device will block sending until host can receive it
17 # if lower or equal to batchReportThreshold then the sending is always blocking on device
18 # useful to reduce device's CPU load and number of lost packets, if CPU load is high on device side due to multiple nodes
19 imu.setMaxBatchReports(10)
20
21 imuQueue = imu.out.createOutputQueue(maxSize=50, blocking=False)
22
23 pipeline.start()
24 baseTs = None
25 def timeDeltaToMilliS(delta) -> float:
26 return delta.total_seconds()*1000
27
28 while pipeline.isRunning():
29 imuData = imuQueue.get()
30 assert isinstance(imuData, dai.IMUData)
31 imuPackets = imuData.packets
32 for imuPacket in imuPackets:
33 acceleroValues = imuPacket.acceleroMeter
34 gyroValues = imuPacket.gyroscope
35
36 acceleroTs = acceleroValues.getTimestamp()
37 gyroTs = gyroValues.getTimestamp()
38
39 imuF = "{:.06f}"
40 tsF = "{:.03f}"
41
42 print(f"Accelerometer timestamp: {acceleroTs}")
43 print(f"Latency [ms]: {dai.Clock.now() - acceleroValues.getTimestamp()}")
44 print(f"Accelerometer [m/s^2]: x: {imuF.format(acceleroValues.x)} y: {imuF.format(acceleroValues.y)} z: {imuF.format(acceleroValues.z)}")
45 print(f"Gyroscope timestamp: {gyroTs}")
46 print(f"Gyroscope [rad/s]: x: {imuF.format(gyroValues.x)} y: {imuF.format(gyroValues.y)} z: {imuF.format(gyroValues.z)} ")
Need assistance?
Head over to Discussion Forum for technical support or any other questions you might have.