imu_bmi160.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include "bmi160.h"
  2. #include <furi_hal.h>
  3. #include "imu.h"
  4. #define TAG "BMI160"
  5. #define BMI160_DEV_ADDR (0x69 << 1)
  6. static const double DEG_TO_RAD = 0.017453292519943295769236907684886;
  7. static const double G = 9.81;
  8. struct bmi160_dev bmi160dev;
  9. struct bmi160_sensor_data bmi160_accel;
  10. struct bmi160_sensor_data bmi160_gyro;
  11. int8_t bmi160_write_i2c(uint8_t dev_addr, uint8_t reg_addr, uint8_t* data, uint16_t len) {
  12. if(furi_hal_i2c_write_mem(&furi_hal_i2c_handle_external, dev_addr, reg_addr, data, len, 50))
  13. return BMI160_OK;
  14. return BMI160_E_COM_FAIL;
  15. }
  16. int8_t bmi160_read_i2c(uint8_t dev_addr, uint8_t reg_addr, uint8_t* read_data, uint16_t len) {
  17. if(furi_hal_i2c_read_mem(&furi_hal_i2c_handle_external, dev_addr, reg_addr, read_data, len, 50))
  18. return BMI160_OK;
  19. return BMI160_E_COM_FAIL;
  20. }
  21. bool bmi160_begin() {
  22. FURI_LOG_I(TAG, "Init BMI160");
  23. if(!furi_hal_i2c_is_device_ready(&furi_hal_i2c_handle_external, BMI160_DEV_ADDR, 50)) {
  24. FURI_LOG_E(TAG, "Device not ready!");
  25. return false;
  26. }
  27. FURI_LOG_I(TAG, "Device ready!");
  28. bmi160dev.id = BMI160_DEV_ADDR;
  29. bmi160dev.intf = BMI160_I2C_INTF;
  30. bmi160dev.read = bmi160_read_i2c;
  31. bmi160dev.write = bmi160_write_i2c;
  32. bmi160dev.delay_ms = furi_delay_ms;
  33. if(bmi160_init(&bmi160dev) != BMI160_OK) {
  34. FURI_LOG_E(TAG, "Initialization failure!");
  35. FURI_LOG_E(TAG, "Chip ID 0x%X", bmi160dev.chip_id);
  36. return false;
  37. }
  38. bmi160dev.accel_cfg.odr = BMI160_ACCEL_ODR_400HZ;
  39. bmi160dev.accel_cfg.range = BMI160_ACCEL_RANGE_4G;
  40. bmi160dev.accel_cfg.bw = BMI160_ACCEL_BW_NORMAL_AVG4;
  41. bmi160dev.accel_cfg.power = BMI160_ACCEL_NORMAL_MODE;
  42. bmi160dev.gyro_cfg.odr = BMI160_GYRO_ODR_400HZ;
  43. bmi160dev.gyro_cfg.range = BMI160_GYRO_RANGE_2000_DPS;
  44. bmi160dev.gyro_cfg.bw = BMI160_GYRO_BW_NORMAL_MODE;
  45. bmi160dev.gyro_cfg.power = BMI160_GYRO_NORMAL_MODE;
  46. if(bmi160_set_sens_conf(&bmi160dev) != BMI160_OK) {
  47. FURI_LOG_E(TAG, "Initialization failure!");
  48. FURI_LOG_E(TAG, "Chip ID 0x%X", bmi160dev.chip_id);
  49. return false;
  50. }
  51. FURI_LOG_I(TAG, "Initialization success!");
  52. FURI_LOG_I(TAG, "Chip ID 0x%X", bmi160dev.chip_id);
  53. return true;
  54. }
  55. int bmi160_read(double* vec) {
  56. if(bmi160_get_sensor_data(
  57. (BMI160_ACCEL_SEL | BMI160_GYRO_SEL), &bmi160_accel, &bmi160_gyro, &bmi160dev) !=
  58. BMI160_OK) {
  59. return 0;
  60. }
  61. vec[0] = ((double)bmi160_accel.x * 4 / 32768) * G;
  62. vec[1] = ((double)bmi160_accel.y * 4 / 32768) * G;
  63. vec[2] = ((double)bmi160_accel.z * 4 / 32768) * G;
  64. vec[3] = ((double)bmi160_gyro.x * 2000 / 32768) * DEG_TO_RAD;
  65. vec[4] = ((double)bmi160_gyro.y * 2000 / 32768) * DEG_TO_RAD;
  66. vec[5] = ((double)bmi160_gyro.z * 2000 / 32768) * DEG_TO_RAD;
  67. return ACC_DATA_READY | GYR_DATA_READY;
  68. }