DepthAI Tutorials
DepthAI API References

ON THIS PAGE

  • IMU Accelerometer & Gyroscope
  • Demo
  • Setup
  • Source code

IMU Accelerometer & Gyroscope

This example shows accelerometer and gyroscope at a combined/synchronized 500 hz rate using the onboard IMU. Returns acceleration [m/s^2] and angular velocity [rad/s].

Demo

Example script output
Command Line
1~/depthai-python/examples$ python3 imu_gyroscope_accelerometer.py
2Accelerometer timestamp: 0.000 ms
3Accelerometer [m/s^2]: x: -0.162806 y: 6.445191 z: 3.189077
4Gyroscope timestamp: 1.642 ms
5Gyroscope [rad/s]: x: -0.040480 y: 0.088417 z: -0.168312
6Accelerometer timestamp: 2.073 ms
7Accelerometer [m/s^2]: x: -0.229843 y: 6.263232 z: 3.572149
8Gyroscope timestamp: 3.663 ms
9Gyroscope [rad/s]: x: -0.072438 y: 0.115049 z: -0.350472

Setup

Please run the install script to download all required dependencies. Please note that this script must be ran from git context, so you have to download the depthai-python repository first and then run the script
Command Line
1git clone https://github.com/luxonis/depthai-python.git
2cd depthai-python/examples
3python3 install_requirements.py
For additional information, please follow the installation guide.

Source code

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

Need assistance?

Head over to Discussion Forum for technical support or any other questions you might have.