Library for accelerometer and SysClean

Committer:
jotamo
Date:
Mon Oct 17 23:45:10 2016 +0000
Revision:
0:362698102bff
Child:
1:55d35606b477
First Version

Who changed what in which revision?

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