Skip to content
esp-idf-libPublic

About

No description, website, or topics provided.

Resources

Stars

4 stars

Watchers

1 watching

Forks

Repository files navigation

esp-idf-lib/bmi160

Build examples Build docs Validation

ESP-IDF driver for the Bosch BMI160, a 16-bit inertial measurement unit with a 3-axis accelerometer and a 3-axis gyroscope.

Installation

idf.py add-dependency esp-idf-lib/bmi160

Hardware

The chip answers on 0x68 when SDO is tied to ground and on 0x69 when it is tied to VDDIO (BMI160_I2C_ADDRESS_GND and BMI160_I2C_ADDRESS_VDD). The driver sets the port to 1 MHz, the fastest rate ESP-IDF supports; the chip itself would go up to 2.44 MHz.

The interrupt pins INT1 and INT2 are configured by the driver as outputs of the chip, but nothing is attached to them on the ESP side: which GPIO to use and whether to poll it or wire it to an interrupt is the application's decision. The default example does the second with gpio_isr_handler_add().

Usage

#include <bmi160.h>

static bmi160_t dev;

// 1. Create the descriptor; the chip is not touched yet
ESP_ERROR_CHECK(bmi160_init(&dev, BMI160_I2C_ADDRESS_GND, 0,
                            CONFIG_EXAMPLE_SDA_GPIO, CONFIG_EXAMPLE_SCL_GPIO));
ESP_ERROR_CHECK(i2cdev_init());

// 2. Optionally run the self test, it resets the chip when done
ESP_ERROR_CHECK(bmi160_self_test(&dev));

// 3. Describe the measurement and start the chip
bmi160_conf_t conf = {
    .accRange = BMI160_ACC_RANGE_2G,
    .accOdr = BMI160_ACC_ODR_200HZ,
    .accAvg = BMI160_ACC_LP_AVG_2,
    .accUs = BMI160_ACC_US_OFF,
    .accMode = BMI160_PMU_ACC_NORMAL,
    .gyrRange = BMI160_GYR_RANGE_125DPS,
    .gyrOdr = BMI160_GYR_ODR_200HZ,
    .gyrMode = BMI160_PMU_GYR_NORMAL,
};
ESP_ERROR_CHECK(bmi160_start(&dev, &conf));

// 4. Read both sensors in one burst
bmi160_result_t result;
ESP_ERROR_CHECK(bmi160_read_data(&dev, &result));
ESP_LOGI(TAG, "acc %+.3f %+.3f %+.3f g, gyro %+.3f %+.3f %+.3f dps",
         result.accX, result.accY, result.accZ,
         result.gyroX, result.gyroY, result.gyroZ);

bmi160_start() reads the chip ID, checks it against BMI160_CHIP_ID_DEFAULT_VALUE, soft-resets the chip and only then writes the configuration. A wrong ID returns ESP_FAIL. bmi160_init() itself never talks to the bus: i2cdev_init() must run before the first transfer, which happens inside bmi160_self_test() or bmi160_start().

Configuration

bmi160_conf_t is validated before anything is written, an impossible combination returns ESP_FAIL with the reason in the log:

  • accelerometer ranges are ±2, ±4, ±8 and ±16 g, gyroscope ranges ±125, ±250, ±500, ±1000 and ±2000 dps;
  • in normal mode the accelerometer ODR must be at least 12.5 Hz;
  • in low-power mode the averaging window bounds the ODR: 400 Hz allows 1 or 2 samples, 200 Hz up to 4, 100 Hz up to 8, 50 Hz up to 16, 25 Hz up to 32, 12.5 Hz up to 64, and 6.25 Hz and slower up to 128;
  • the gyroscope has no low-power mode, only suspend, normal and fast start-up, and its ODR starts at 25 Hz;
  • a sensor set to BMI160_PMU_*_SUSPEND is not started at all.

The ranges and ODRs can also be changed later with bmi160_set_acc_range(), bmi160_set_gyr_range(), bmi160_set_acc_conf() and bmi160_set_gyr_odr(); they return ESP_FAIL for a value the chip cannot take.

Reading

bmi160_read_data() reads 12 bytes in one transaction, starting at gyroscope X, so the accelerometer and the gyroscope always belong to the same sample. Each axis is a signed 16-bit value scaled by the resolution of the configured range:

acceleration = raw * range_g / 32768      gyroscope = raw * range_dps / 32768

At ±2 g one count is 61 µg, at ±125 dps one count is 0.0038 dps. From both values the bias stored in the descriptor is subtracted (see below).

Bias calibration

bmi160_calibrate() takes 64 readings about 20 ms apart (the delay is quantised to the FreeRTOS tick), averages them and keeps the average as the bias of both sensors. Every later reading has it subtracted. Calling it again is safe: the existing bias is dropped before the averaging, so a second run measures the sensor, not its own leftovers.

Note what this means in practice: the accelerometer average includes gravity, so a sensor lying still on a table has roughly 1 g on one axis, and after the calibration the driver reports zero on all three axes. Calibrate when you want the sensor to show "no movement and no gravity", not when you need the gravity vector for an orientation. The gyroscope part is a plain zero-rate offset.

Self test

bmi160_self_test() resets the chip, runs the accelerometer self test in both directions and compares the results: the datasheet requires more than 2 g of difference on every axis (2 g counts as a pass). Then it resets the chip again and checks the gyroscope self-test status bit. It returns ESP_OK only when both sensors pass, and it leaves the chip reset, so bmi160_start() must follow it.

Interrupts

bmi160_enable_int_new_data(), bmi160_enable_int_step() and bmi160_enable_int_tap() take a bmi160_int_out_conf_t: which pin (INT1 or INT2), push-pull or open drain, active high or low. They configure the chip side only; the pin still has to be read or attached to a GPIO interrupt by the application.

Step counter

bmi160_enable_step_counter() switches the chip's step counter on in normal, sensitive or robust mode, bmi160_read_step_counter() returns the count and bmi160_reset_step_counter() sends the reset command. bmi160_enable_int_step() adds an interrupt on every step. The counter works in low-power mode too, but the datasheet recommends switching the accelerometer to normal mode before reading to get the current value; the step_counter example does exactly that.

Tap detection

bmi160_enable_tap_detection() takes a bmi160_tap_conf_t: single or double tap, the threshold in g (0.032 g … 4 g), the duration of the tap pulse, the quiet time after it and the shock time. bmi160_read_tap_orient() returns the raw orientation byte the chip latches for the detected tap, so interpreting the bit field is up to the application.

Limitations

  • Only I2C is implemented; the chip's SPI mode and the magnetometer connected to its auxiliary interface are not supported.
  • There is no FIFO support, every reading is a separate transaction.
  • The bias is a single constant per axis and does not depend on temperature or on the range, so changing the range invalidates a calibration done at another one.
  • The mutex protects single transactions, not the sequences: bmi160_start() and bmi160_self_test() take it once for the whole sequence, bmi160_calibrate() also holds it for all 64 readings, but concurrent calls to those three from two tasks should still be avoided.
  • bmi160_free() releases the mutex and the descriptor only; the I2C bus stays until i2cdev_done().

Examples

  • default: reads both sensors, either in a loop or on the INT1 new-data interrupt (USE_NEW_DATA_INT at the top of main.c).
  • step_counter: counts steps and prints them.
  • tap_detection: prints taps and their orientation.

Support

For questions and discussions about the component, please use Discussions at esp-idf-lib/core.

Contributing

Please read CONTRIBUTING.md at esp-idf-lib/core.

About

No description, website, or topics provided.

Resources

Stars

4 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages