Library for accelerometer and SysClean

Committer:
jotamo
Date:
Tue Oct 18 17:47:44 2016 +0000
Revision:
1:55d35606b477
Child:
2:f9ddabfe2eb6
Create cpp for library and it has Y angles value, still!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jotamo 1:55d35606b477 1 #include "BMI160.h"
jotamo 1:55d35606b477 2
jotamo 1:55d35606b477 3 /* defines the axis for acc */
jotamo 1:55d35606b477 4 #define ACC_NOOF_AXIS 3
jotamo 1:55d35606b477 5
jotamo 1:55d35606b477 6 /* bmi160 slave address */
jotamo 1:55d35606b477 7 #define BMI160_ADDR ((0x68)<<1)
jotamo 1:55d35606b477 8
jotamo 1:55d35606b477 9 /*Valor para Transformar de Radiano para Graus*/
jotamo 1:55d35606b477 10 #define RAD_DEG 57.29577951
jotamo 1:55d35606b477 11
jotamo 1:55d35606b477 12 /* buffer to store acc samples */
jotamo 1:55d35606b477 13 int16_t acc_sample_buffer[ACC_NOOF_AXIS] = {0x5555, 0x5555, 0x5555};
jotamo 1:55d35606b477 14
jotamo 1:55d35606b477 15 double acc_result_buffer[ACC_NOOF_AXIS] = {0x5555, 0x5555, 0x5555};
jotamo 1:55d35606b477 16
jotamo 1:55d35606b477 17 double accel_ang_x, accel_ang_y;
jotamo 1:55d35606b477 18
jotamo 1:55d35606b477 19 char i2c_reg_buffer[2] = {0};
jotamo 1:55d35606b477 20
jotamo 1:55d35606b477 21 //Instancia o LCD
jotamo 1:55d35606b477 22 ST7567 lcd(D11, D13, D12, D9, D10); // mosi, sclk, reset, A0, nCS
jotamo 1:55d35606b477 23
jotamo 1:55d35606b477 24 /*Comunicacao I2C*/
jotamo 1:55d35606b477 25 I2C i2c(P2_3, P2_4);
jotamo 1:55d35606b477 26
jotamo 1:55d35606b477 27
jotamo 1:55d35606b477 28 void BMI160::lcdClear(){
jotamo 1:55d35606b477 29 for(int i=0;i<5;i++)
jotamo 1:55d35606b477 30 lcd.printf("\n");
jotamo 1:55d35606b477 31 }
jotamo 1:55d35606b477 32 void BMI160::configureAccelerometer(){
jotamo 1:55d35606b477 33
jotamo 1:55d35606b477 34 /*Config Freq. I2C Bus*/
jotamo 1:55d35606b477 35 i2c.frequency(20000);
jotamo 1:55d35606b477 36
jotamo 1:55d35606b477 37 /*Reset BMI160*/
jotamo 1:55d35606b477 38 i2c_reg_buffer[0] = 0x7E;
jotamo 1:55d35606b477 39 i2c_reg_buffer[1] = 0xB6;
jotamo 1:55d35606b477 40 i2c.write(BMI160_ADDR, i2c_reg_buffer, sizeof(i2c_reg_buffer), false);
jotamo 1:55d35606b477 41 wait_ms(200);
jotamo 1:55d35606b477 42 //lcd.printf("BMI160 Resetado\n\r");
jotamo 1:55d35606b477 43 wait_ms(100);
jotamo 1:55d35606b477 44
jotamo 1:55d35606b477 45 /*Habilita o Acelerometro*/
jotamo 1:55d35606b477 46 i2c_reg_buffer[0] = 0x7E;
jotamo 1:55d35606b477 47 i2c_reg_buffer[1] = 0x11; //PMU Normal
jotamo 1:55d35606b477 48 i2c.write(BMI160_ADDR, i2c_reg_buffer, sizeof(i2c_reg_buffer), false);
jotamo 1:55d35606b477 49 //lcd.printf("Acc Habilitado\n\r");
jotamo 1:55d35606b477 50
jotamo 1:55d35606b477 51 /*Config o Data Rate ACC em 1600Hz*/
jotamo 1:55d35606b477 52 i2c_reg_buffer[0] = 0x40;
jotamo 1:55d35606b477 53 i2c_reg_buffer[1] = 0x2C;
jotamo 1:55d35606b477 54 i2c.write(BMI160_ADDR, i2c_reg_buffer, sizeof(i2c_reg_buffer), false);
jotamo 1:55d35606b477 55 //lcd.printf("Data Rate ACC Selecionado a 1600Hz\n\r");
jotamo 1:55d35606b477 56
jotamo 1:55d35606b477 57 }
jotamo 1:55d35606b477 58
jotamo 1:55d35606b477 59 void BMI160::calculateMovement(){
jotamo 1:55d35606b477 60
jotamo 1:55d35606b477 61 /*Ajusta dados brutos Acelerometro em unidades de g */
jotamo 1:55d35606b477 62 acc_result_buffer[0] = (acc_sample_buffer[0]/16384.0);
jotamo 1:55d35606b477 63 acc_result_buffer[1] = (acc_sample_buffer[1]/16384.0);
jotamo 1:55d35606b477 64 acc_result_buffer[2] = (acc_sample_buffer[2]/16384.0);
jotamo 1:55d35606b477 65
jotamo 1:55d35606b477 66 /*Calcula os Angulos de Inclinacao com valor do Acelerometro*/
jotamo 1:55d35606b477 67 accel_ang_x=atan(acc_result_buffer[0]/sqrt(pow(acc_result_buffer[1],2) + pow(acc_result_buffer[2],2)))*RAD_DEG;
jotamo 1:55d35606b477 68 // accel_ang_y=atan(acc_result_buffer[1]/sqrt(pow(acc_result_buffer[0],2) + pow(acc_result_buffer[2],2)))*RAD_DEG;
jotamo 1:55d35606b477 69
jotamo 1:55d35606b477 70
jotamo 1:55d35606b477 71 /*Imprime os dados ACC pre-formatados*/
jotamo 1:55d35606b477 72 // lcd.printf("Acc(G): %.3f,%.3f,%.3f\n\r",acc_result_buffer[0], acc_result_buffer[1], acc_result_buffer[2]);
jotamo 1:55d35606b477 73 lcd.printf("Angles(x,y): %.3f\n\r", accel_ang_x);
jotamo 1:55d35606b477 74 wait_ms(3000);
jotamo 1:55d35606b477 75 lcdClear();
jotamo 1:55d35606b477 76 }
jotamo 1:55d35606b477 77
jotamo 1:55d35606b477 78 void BMI160::readAccelerometer(){
jotamo 1:55d35606b477 79
jotamo 1:55d35606b477 80 while(1) {
jotamo 1:55d35606b477 81 /*Le os Registradores do Acelerometro*/
jotamo 1:55d35606b477 82 i2c_reg_buffer[0] = 0x12;
jotamo 1:55d35606b477 83 i2c.write(BMI160_ADDR, i2c_reg_buffer, 1, true);
jotamo 1:55d35606b477 84 i2c.read(BMI160_ADDR, (char *)&acc_sample_buffer, sizeof(acc_sample_buffer), false);
jotamo 1:55d35606b477 85 calculateMovement();
jotamo 1:55d35606b477 86 }
jotamo 1:55d35606b477 87 }
jotamo 1:55d35606b477 88
jotamo 1:55d35606b477 89 void BMI160::runTempCommands(){
jotamo 1:55d35606b477 90 configureAccelerometer();
jotamo 1:55d35606b477 91 readAccelerometer();
jotamo 1:55d35606b477 92
jotamo 1:55d35606b477 93 }