Library for accelerometer and SysClean
BMI160.cpp
- Committer:
- jotamo
- Date:
- 2016-10-18
- Revision:
- 1:55d35606b477
- Child:
- 2:f9ddabfe2eb6
File content as of revision 1:55d35606b477:
#include "BMI160.h" /* defines the axis for acc */ #define ACC_NOOF_AXIS 3 /* bmi160 slave address */ #define BMI160_ADDR ((0x68)<<1) /*Valor para Transformar de Radiano para Graus*/ #define RAD_DEG 57.29577951 /* buffer to store acc samples */ int16_t acc_sample_buffer[ACC_NOOF_AXIS] = {0x5555, 0x5555, 0x5555}; double acc_result_buffer[ACC_NOOF_AXIS] = {0x5555, 0x5555, 0x5555}; double accel_ang_x, accel_ang_y; char i2c_reg_buffer[2] = {0}; //Instancia o LCD ST7567 lcd(D11, D13, D12, D9, D10); // mosi, sclk, reset, A0, nCS /*Comunicacao I2C*/ I2C i2c(P2_3, P2_4); void BMI160::lcdClear(){ for(int i=0;i<5;i++) lcd.printf("\n"); } void BMI160::configureAccelerometer(){ /*Config Freq. I2C Bus*/ i2c.frequency(20000); /*Reset BMI160*/ i2c_reg_buffer[0] = 0x7E; i2c_reg_buffer[1] = 0xB6; i2c.write(BMI160_ADDR, i2c_reg_buffer, sizeof(i2c_reg_buffer), false); wait_ms(200); //lcd.printf("BMI160 Resetado\n\r"); wait_ms(100); /*Habilita o Acelerometro*/ i2c_reg_buffer[0] = 0x7E; i2c_reg_buffer[1] = 0x11; //PMU Normal i2c.write(BMI160_ADDR, i2c_reg_buffer, sizeof(i2c_reg_buffer), false); //lcd.printf("Acc Habilitado\n\r"); /*Config o Data Rate ACC em 1600Hz*/ i2c_reg_buffer[0] = 0x40; i2c_reg_buffer[1] = 0x2C; i2c.write(BMI160_ADDR, i2c_reg_buffer, sizeof(i2c_reg_buffer), false); //lcd.printf("Data Rate ACC Selecionado a 1600Hz\n\r"); } void BMI160::calculateMovement(){ /*Ajusta dados brutos Acelerometro em unidades de g */ acc_result_buffer[0] = (acc_sample_buffer[0]/16384.0); acc_result_buffer[1] = (acc_sample_buffer[1]/16384.0); acc_result_buffer[2] = (acc_sample_buffer[2]/16384.0); /*Calcula os Angulos de Inclinacao com valor do Acelerometro*/ accel_ang_x=atan(acc_result_buffer[0]/sqrt(pow(acc_result_buffer[1],2) + pow(acc_result_buffer[2],2)))*RAD_DEG; // accel_ang_y=atan(acc_result_buffer[1]/sqrt(pow(acc_result_buffer[0],2) + pow(acc_result_buffer[2],2)))*RAD_DEG; /*Imprime os dados ACC pre-formatados*/ // lcd.printf("Acc(G): %.3f,%.3f,%.3f\n\r",acc_result_buffer[0], acc_result_buffer[1], acc_result_buffer[2]); lcd.printf("Angles(x,y): %.3f\n\r", accel_ang_x); wait_ms(3000); lcdClear(); } void BMI160::readAccelerometer(){ while(1) { /*Le os Registradores do Acelerometro*/ i2c_reg_buffer[0] = 0x12; i2c.write(BMI160_ADDR, i2c_reg_buffer, 1, true); i2c.read(BMI160_ADDR, (char *)&acc_sample_buffer, sizeof(acc_sample_buffer), false); calculateMovement(); } } void BMI160::runTempCommands(){ configureAccelerometer(); readAccelerometer(); }