hanyazou / BMI160-Arduino

76 stars 35 forks source link

ESP 32 BMI160 #16

Open nisemana opened 2 years ago

nisemana commented 2 years ago

Hey, i'm pretty new to the whole arduino world and coming from mechanical engineering, so propably a lot of my coding is not best practice.. I'm currently working on a project, where i want to measure accelerometer Data while brushing teeth. On a long way of getting used with arduinos and searching for a good low power solution (The toothbrush should last for at least 30 days in standby while measuring 2x3min per day) i came to use the ESP32 because of low deep sleep current.

For the IMU i started with the MPU6050, but the breakoutboard i had drew way to much current in deep sleep. Thats why i switched to the BMI160. Unfortunatly the only library which is working for me is the DFRobot one.. I can get the values, and print them in an csv file with ~40Hz. The problem is, for me it would be gould to get at least 100Hz as a sample rate. I managed to set the BMI to a Accelrate of 1600Hz but the code just still slows down. I feel, that the method i use within the DFRobot lib just is slow?

When i try to implement your BMI160.h it wont work.. I can #include it and create a class frome it "BMI160Class bmi160;" without error but as soon as i try to use one of the methods it gives me errors and "error while compiling for the Board ESP32 Dev Module"

I really don't know if it is my bad practice gere or if it just won't work for the ESP32? :(

here is the code for the DFRobot lib which works just fine for me:

#include <DFRobot_BMI160.h>
#include "FS.h"
#include "SD.h"
#include "SPI.h"

#define Threshold 80

DFRobot_BMI160 bmi160;
const int8_t i2c_addr = 0x69;

void callback(){
}

void setupSDCard(){
  if(!SD.begin()){
      return;
  }
  uint8_t cardType = SD.cardType();
  if(cardType == CARD_NONE){
      return;
  }
}

void setupBMI(){
  //init the hardware bmin160  
  if (bmi160.softReset() != BMI160_OK){
    while(1);
  }

  //set and init the bmi160 i2c address
  if (bmi160.I2cInit(i2c_addr) != BMI160_OK){
    while(1);
  }
}

void setupTouch(){
}

void setup(){
  Serial.begin(115200);

touch_pad_init();
touch_pad_deinit();

setupSDCard();
setupBMI();

  if(!SD.exists("/data.csv")){
    File file = SD.open("/data.txt" ,FILE_APPEND);
    file.print("Time in millis;Gx;Gy;Gz;X;Y;Z\n");
    file.close();
  }
}

void loop(){

  int i = 0;
  int rslt;
  int16_t accelGyro[6]={0}; 

  //get both accel and gyro data from bmi160
  //parameter accelGyro is the pointer to store the data

  unsigned long check = millis();
  File file = SD.open("/data.txt" ,FILE_APPEND);
  Serial.println("while");

  while(touchRead(T3) < Threshold){

    //get data, as long as touch pin T3 is touched

    //Gets the data
    rslt = bmi160.getAccelGyroData(accelGyro);
    if(rslt == 0){
      file.print(millis());file.print(";");
      file.print(accelGyro[0]*3.14/180.0);file.print(";");
      file.print(accelGyro[1]*3.14/180.0);file.print(";");
      file.print(accelGyro[2]*3.14/180.0);file.print(";");
      file.print(accelGyro[3]/16384.0);file.print(";");
      file.print(accelGyro[4]/16384.0);file.print(";");
      file.print(accelGyro[5]/16384.0);file.print(";");

      file.print("\n");
    }
  }

  file.print("logging end");
  file.close();

  Serial.println("Going to sleep");

  SD.end();

  touchAttachInterrupt(T3, callback, Threshold);
  esp_sleep_enable_touchpad_wakeup();
  esp_deep_sleep_start();
}`