How to read IMU data via on I2C pins?

yunus_aczyunus_acz New Member Posts: 10
edited February 14 in UP Board Linux

Hi all !
I am trying to read data from my BMI160 sensor. (Operating system is Ubuntu 16.04)
My wiring to UpBoard like this:
Sensor UpBoard
SCL I2C1_SCL(Pin5)
SDA I2C1_SDA(Pin3)
VCC 5V(Pin2)
GND Ground(Pin6)
Sensor Ground also connected to SAO pin of sensor.

After all doing this. I installed i2detect and rund the command i2cdetect -y -r 5 and here is the result image:

When I unplug the pins 68 in the table disappears so I think it detects my sensor. The problem is that I dont know how to read data from sensor. According to driver document I tried to use code but I couldnt read any data. Here is my code I tried. But i dont know which side I am doing wrong. The problem is about the code or about the upboard. How can I read I2C data from terminal.

#include "bmi160.h"
#include <iostream>

using namespace std;


int main(int argc, char *argv[])
{

    struct bmi160_dev sensor;

    sensor.id = BMI160_I2C_ADDR;
    sensor.interface = BMI160_I2C_INTF;

    int8_t rslt = BMI160_OK;
    rslt = bmi160_init(&sensor);
    /* After the above function call, accel and gyro parameters in the device structure
    are set with default values, found in the datasheet of the sensor */



    /* Select the Output data rate, range of accelerometer sensor */
    sensor.accel_cfg.odr = BMI160_ACCEL_ODR_1600HZ;
    sensor.accel_cfg.range = BMI160_ACCEL_RANGE_2G;
    sensor.accel_cfg.bw = BMI160_ACCEL_BW_NORMAL_AVG4;

    /* Select the power mode of accelerometer sensor */
    sensor.accel_cfg.power = BMI160_ACCEL_NORMAL_MODE;

    /* Select the Output data rate, range of Gyroscope sensor */
    sensor.gyro_cfg.odr = BMI160_GYRO_ODR_3200HZ;
    sensor.gyro_cfg.range = BMI160_GYRO_RANGE_2000_DPS;
    sensor.gyro_cfg.bw = BMI160_GYRO_BW_NORMAL_MODE;

    /* Select the power mode of Gyroscope sensor */
    sensor.gyro_cfg.power = BMI160_GYRO_NORMAL_MODE;

    /* Set the sensor configuration */
    rslt = bmi160_set_sens_conf(&sensor);
    struct bmi160_sensor_data accel;
    struct bmi160_sensor_data gyro;

    /* To read only Accel data */
    rslt = bmi160_get_sensor_data(BMI160_ACCEL_SEL, &accel, NULL, &sensor);

    cout<<accel.z<<endl;


    /* To read only Gyro data */
    rslt = bmi160_get_sensor_data(BMI160_GYRO_SEL, NULL, &gyro, &sensor);

    /* To read both Accel and Gyro data */
    bmi160_get_sensor_data((BMI160_ACCEL_SEL | BMI160_GYRO_SEL), &accel, &gyro, &sensor);

    /* To read Accel data along with time */
    rslt = bmi160_get_sensor_data((BMI160_ACCEL_SEL | BMI160_TIME_SEL) , &accel, NULL, &sensor);

    /* To read Gyro data along with time */
    rslt = bmi160_get_sensor_data((BMI160_GYRO_SEL | BMI160_TIME_SEL), NULL, &gyro, &sensor);

    /* To read both Accel and Gyro data along with time*/
    bmi160_get_sensor_data((BMI160_ACCEL_SEL | BMI160_GYRO_SEL | BMI160_TIME_SEL), &accel, &gyro, &sensor);
}
Sign In or Register to comment.