Inertial Measurement Unit (IMU)

[中文]

The Inertial Measurement Unit (IMU) can be used as a gyroscope sensor, an acceleration sensor, a sensor with multiple functions or etc. It is mainly used to measure the acceleration and angular velocity of an object, and then calculate the motion attitude of the object.

Adapted Products

Name

Function

Bus

Vendor

Datasheet

HAL

LIS2DH12

3-axis acceler

I2C

ST

LIS2DH12 Datasheet

MPU6050

3-axis acceler + 3-axis gyro

I2C

InvenSense

MPU6050 Datasheet

API Reference

The following APIs have implemented hardware abstraction on the IMU. Users can call the code from this layer directly to write a sensor application, or use the sensor interface in sensor_hub for easier development.

Header File

Functions

sensor_imu_handle_t imu_create(bus_handle_t bus, int imu_id)

Create a Inertial Measurement Unit sensor instance. Same series’ sensor or sensor with same address can only be created once.

Parameters
  • bus – i2c bus handle the sensor attached to

  • imu_id – id declared in imu_id_t

Returns

sensor_imu_handle_t return imu sensor handle if succeed, NULL is failed.

esp_err_t imu_delete(sensor_imu_handle_t *sensor)

Delete and release the sensor resource.

Parameters

sensor – point to imu sensor handle, will set to NULL if delete succeed.

Returns

esp_err_t

  • ESP_OK Success

  • ESP_FAIL Fail

esp_err_t imu_test(sensor_imu_handle_t sensor)

Test if sensor is active.

Parameters

sensor – imu sensor handle to operate

Returns

esp_err_t

  • ESP_OK Success

  • ESP_FAIL Fail

esp_err_t imu_acquire_acce(sensor_imu_handle_t sensor, axis3_t *acce)

Acquire imu sensor accelerometer result one time.

Parameters
  • sensor – imu sensor handle to operate

  • acce – result data (unit:g)

Returns

esp_err_t

  • ESP_OK Success

  • ESP_FAIL Fail

  • ESP_ERR_NOT_SUPPORTED Function not supported on this sensor

esp_err_t imu_acquire_gyro(sensor_imu_handle_t sensor, axis3_t *gyro)

Acquire imu sensor gyroscope result one time.

Parameters
  • sensor – imu sensor handle to operate

  • gyro – result data (unit:dps)

Returns

esp_err_t

  • ESP_OK Success

  • ESP_FAIL Fail

  • ESP_ERR_NOT_SUPPORTED Function not supported on this sensor

esp_err_t imu_sleep(sensor_imu_handle_t sensor)

Set sensor to sleep mode.

Parameters

sensor – imu sensor handle to operate

Returns

esp_err_t

  • ESP_OK Success

  • ESP_FAIL Fail

  • ESP_ERR_NOT_SUPPORTED Function not supported on this sensor

esp_err_t imu_wakeup(sensor_imu_handle_t sensor)

Wakeup sensor from sleep mode.

Parameters

sensor – imu sensor handle to operate

Returns

esp_err_t

  • ESP_OK Success

  • ESP_FAIL Fail

  • ESP_ERR_NOT_SUPPORTED Function not supported on this sensor

esp_err_t imu_acquire(sensor_imu_handle_t sensor, sensor_data_group_t *data_group)

acquire a group of sensor data

Parameters
  • sensor – imu sensor handle to operate

  • data_group – acquired data

Returns

esp_err_t

  • ESP_OK Success

  • ESP_FAIL Fail

esp_err_t imu_control(sensor_imu_handle_t sensor, sensor_command_t cmd, void *args)

control sensor mode with control commands and args

Parameters
  • sensor – imu sensor handle to operate

  • cmd – control commands detailed in sensor_command_t

  • args – control commands args

    • ESP_OK Success

    • ESP_FAIL Fail

    • ESP_ERR_NOT_SUPPORTED Function not supported on this sensor

Type Definitions

typedef void *sensor_imu_handle_t

imu sensor handle

Enumerations

enum imu_id_t

imu sensor id, used for imu_create

Values:

enumerator MPU6050_ID

MPU6050 imu sensor id

enumerator LIS2DH12_ID

LIS2DH12 imu sensor id

enumerator IMU_MAX_ID

max imu sensor id