Skip to content

imu.h

Generated from imu.h in platform-idf. Edit the header’s doxygen comments to change this page.

BSP IMU (ST LSM6DS3TR-C) interface.

esp_err_t bsp_imu_init(void)

Initialize IMU.

This function initializes the LSM6DS3TR-C IMU sensor on the I2C bus. The I2C bus must be initialized first by calling bsp_i2c_init(). This function does NOT start the IMU task automatically.

Returns

  • ESP_OK on success
  • ESP_ERR_INVALID_STATE if I2C is not initialized
  • ESP_FAIL if IMU initialization failed

Declared in imu.h:95.

esp_err_t bsp_imu_start_task(uint32_t update_rate_ms, uint32_t task_priority, uint32_t task_stack_size)

Start IMU task.

Starts a FreeRTOS task that reads the IMU in the background: on every gyro data-ready pulse (52 Hz) where BSP_HAS_IMU_INT2, otherwise every update_rate_ms. The data buffer is read with bsp_imu_get_data().

Parameters

  • update_rate_ms — Poll period without a data-ready line (default: 20 ms)
  • task_priority — Task priority (default: 5)
  • task_stack_size — Task stack size in bytes (default: 4096)

Returns

  • ESP_OK on success
  • ESP_ERR_INVALID_STATE if IMU not initialized or task already running
  • ESP_FAIL on task creation failure

Declared in imu.h:112.

esp_err_t bsp_imu_stop_task(void)

Stop IMU task.

Returns

  • ESP_OK on success
  • ESP_ERR_INVALID_STATE if task is not running

Declared in imu.h:121.

esp_err_t bsp_imu_get_data(bsp_imu_data_t *data)

Get latest IMU data (thread-safe)

Returns the latest IMU data read by the background task. This function is thread-safe and can be called from any context.

Parameters

  • data — Pointer to structure to store IMU data

Returns

  • ESP_OK on success
  • ESP_ERR_INVALID_ARG if data is NULL
  • ESP_ERR_INVALID_STATE if IMU task is not running

Declared in imu.h:135.

esp_err_t bsp_imu_read(bsp_imu_data_t *data)

Read IMU data directly (blocking)

This function directly reads data from the IMU sensor (blocking call). Use bsp_imu_get_data() instead if you are using the IMU task.

Parameters

  • data — Pointer to structure to store IMU data

Returns

  • ESP_OK on success
  • ESP_ERR_INVALID_ARG if data is NULL
  • ESP_FAIL if read failed

Declared in imu.h:149.

esp_err_t bsp_imu_get_temperature(float *temp)

Get IMU temperature (thread-safe)

Parameters

  • temp — Pointer to store temperature value

Returns

  • ESP_OK on success
  • ESP_ERR_INVALID_ARG if temp is NULL
  • ESP_FAIL if read failed

Declared in imu.h:160.

bool bsp_imu_is_task_running(void)

Check if IMU task is running.

Returns

true if task is running, false otherwise

Declared in imu.h:167.

void bsp_imu_get_stats(bsp_imu_stats_t *stats)

Counters of the IMU task (lock-free snapshot)

Parameters

  • stats — Filled with the current counters

Declared in imu.h:174.

void bsp_imu_mute_taps_for(uint32_t ms)

Ignore knocks on the case for the next ms.

Pad hits also move the case. The pad input says when one happens; this drops the knocks before they are paired, so a real tap arriving afterwards never pairs with a pad hit.

Declared in imu.h:184.

void bsp_imu_get_events(bsp_imu_event_counts_t *counts)

Event counters since boot (lock-free snapshot)

Declared in imu.h:189.

void bsp_imu_set_event_callback(bsp_imu_event_cb_t cb, void *ctx)

Register the event callback (one; NULL removes it)

Runs in the IMU task: keep it short and never block on the I2C bus.

Declared in imu.h:196.

esp_err_t bsp_imu_read_registers(uint8_t first, uint8_t *out, size_t len)

Raw register read from the LSM6DS3TR-C, for diagnostics.

Reading a latched source register (0x1B..0x1D, 0x53) clears it.

Declared in imu.h:203.

esp_err_t bsp_imu_write_register(uint8_t reg_addr, uint8_t value)

Raw register write to the LSM6DS3TR-C, for diagnostics.

Not persisted: the next reconfiguration (boot, recovery) writes the driver’s values back.

Declared in imu.h:211.

esp_err_t bsp_imu_set_inactivity(bool enable)

Let the part sleep its gyro after ~2.5 s without motion (default off)

While still the accel drops to 12.5 Hz, where taps are not recognised, and pitch/roll/gyro stop updating until motion over the wake-up threshold. The platform turns it on when the screen goes dark (LIGHT sleep) and off again on wake, so a board in use keeps every tap.

Declared in imu.h:221.

esp_err_t bsp_imu_set_thresholds(uint8_t tap_ths, uint8_t wake_ths)

Set the tap and wake-up thresholds (at 2 g full scale)

The wake-up threshold has to stay above the tap threshold or the part stops reporting taps (they share the slope comparator). While the part is still it uses a fixed low wake-up threshold instead, so motion wakes it.

Parameters

  • tap_ths — 0..31, 1 LSB = 62.5 mg (default 3 = 187 mg)
  • wake_ths — 0..63, 1 LSB = 31.25 mg (default 8 = 250 mg)

Declared in imu.h:233.

esp_err_t bsp_imu_deinit(void)

Deinitialize IMU.

Stops the IMU task if running and deinitializes the sensor.

Returns

  • ESP_OK on success
  • ESP_FAIL on error

Declared in imu.h:244.

esp_err_t bsp_imu_set_kalman_params(float q_angle, float q_bias, float r_measure)

Tune Kalman filter parameters (angle + bias) for responsiveness.

Parameters

  • q_angle — Process noise variance for the angle state
  • q_bias — Process noise variance for the gyro bias state
  • r_measure — Measurement noise variance for accelerometer angle

Returns

ESP_OK on success

Declared in imu.h:254.

esp_err_t bsp_imu_set_update_rate(uint32_t update_rate_ms)

Change the IMU task’s poll period (used only without a data-ready line)

Parameters

  • update_rate_ms — New poll period in milliseconds (min 1ms)

Returns

ESP_OK on success

Declared in imu.h:262.

IMU data structure.

FieldTypeDescription
accel_xfloatAcceleration X-axis (g)
accel_yfloatAcceleration Y-axis (g)
accel_zfloatAcceleration Z-axis (g)
gyro_xfloatGyroscope X-axis (dps)
gyro_yfloatGyroscope Y-axis (dps)
gyro_zfloatGyroscope Z-axis (dps)
tempfloatTemperature (°C)
fused_pitchfloatFused pitch angle (degrees) - Kalman output.
fused_rollfloatFused roll angle (degrees) - Kalman output.

Declared in imu.h:24.

How the IMU task is being driven, for diagnostics.

FieldTypeDescription
drdy_lineboolreads are triggered by the gyro data-ready pulse on INT2
drdy_pulsesuint32_tdata-ready pulses seen by the ISR
samplesuint32_tnew gyro samples fused
read_failuresuint32_tI2C reads that failed.
tap_mutesuint32_ttimes something asked for taps to be ignored
taps_muteduint32_tknocks dropped because they fell in a muted window
tap_mute_ms_leftint32_twhat is left of the current mute, 0 when listening

Declared in imu.h:39.

Events the LSM6DS3TR-C recognises itself (bitmask)

ValueValueDescription
BSP_IMU_EVENT_SINGLE_TAP1u << 0
BSP_IMU_EVENT_DOUBLE_TAP1u << 1
BSP_IMU_EVENT_FREE_FALL1u << 2
BSP_IMU_EVENT_WAKE_UP1u << 3motion over the wake-up threshold
BSP_IMU_EVENT_ORIENTATION1u << 46D orientation changed
BSP_IMU_EVENT_STILL1u << 5no motion for ~10 s: gyro asleep until a wake-up

Declared in imu.h:52.

Counters of recognised events since boot.

FieldTypeDescription
single_tapuint32_t
double_tapuint32_t
free_falluint32_t
wake_upuint32_t
orientationuint32_t
still_periodsuint32_ttimes the part went still and slept its gyro
stillboolthe part is in its still (gyro asleep) state now
orientation_6duint8_tD6D_SRC axes: bit5 ZH, 4 ZL, 3 YH, 2 YL, 1 XH, 0 XL.

Declared in imu.h:64.

typedef void(* bsp_imu_event_cb_t)(uint32_t events, uint8_t orientation_6d, void *ctx)

Called from the IMU task when the part reports events.

Declared in imu.h:81.