Library for accelerometer and SysClean

Revision:
0:362698102bff
Child:
1:55d35606b477
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BMI160.h	Mon Oct 17 23:45:10 2016 +0000
@@ -0,0 +1,127 @@
+/* mbed library for the mbed BMI160 accelerometer
+    Author: Josias Marcos Orlando
+    Organization: SysClean Solutions
+ */
+
+#ifndef BMI160_H
+#define BMI160_H
+
+#include "mbed.h"
+#include "ST7567.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);
+
+class BMI160{
+    public:
+        void configureAccelerometer();
+        void calculateMovement();
+        void readAccelerometer();
+        void runTempCommands();
+        void lcdClear();
+};
+
+void BMI160::lcdClear(){
+    for(int i=0;i<5;i++)
+        lcd.printf("\n");
+}
+void BMI160::configureAccelerometer(){
+    //lcd.printf("Configurando BMI160...\n\r");
+   // wait_ms(250);
+    //lcdClear();
+    
+    /*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(1000);
+    //lcdClear();
+    
+    /*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");
+   //wait_ms(1000);
+    //lcdClear();
+    
+    /*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");
+    //lcdClear();
+    
+    //wait_ms(2000);
+   // lcd.printf("BMI160 Configurado\n\r");
+   // lcdClear();
+}
+
+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,%.3f\n\r", accel_ang_x, accel_ang_y);
+    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();
+    
+}
+
+
+
+
+
+#endif