Can I have some help using the I2C library in librobotcontrol

Hi, I’m a new programmer and i’m having trouble using the I2C library for librobotcontrol. I’m not sure if i’m using it correctly. I am testing out a an accelerometer mpu6050.

So far I have these functions that should open and start communication with the chip however I keep receiving no bytes.

ERROR: in rc_i2c_read_bytes, received -1 bytes from device, expected 6

I left out the other parts from the template and just left the stuff using i2c.
So far In C I have:

#define I2C_BUS 1
#define I2C_ADDRESS 0x68

void setupMPU(){
    // Init I2C Bus
    rc_i2c_init	(I2C_BUS,I2C_ADDRESS);
    // Wake up sensor
    rc_i2c_write_byte(I2C_BUS, 0x6B, 0x00);
    rc_i2c_close(I2C_BUS);

    // Accel Config
    rc_i2c_init	(I2C_BUS,I2C_ADDRESS);
    rc_i2c_write_byte(I2C_BUS, 0x1C, 0x00);
    rc_i2c_close(I2C_BUS);
}

float get_accel_values(){
    float ag;
    float ax = 0, ay = 0, az = 0;
    unsigned char data;
    rc_i2c_init	(I2C_BUS,I2C_ADDRESS);
    rc_i2c_send_byte(I2C_BUS, 0x3B);
    rc_i2c_close(I2C_BUS);

    rc_i2c_init(I2C_BUS,I2C_ADDRESS);
    rc_i2c_read_bytes(I2C_BUS, I2C_ADDRESS, 6, data);
    rc_i2c_close(I2C_BUS);
    return ax;
}

In my main I first call setupMPU and in a while loop call get_accel_values. Do I need to only initialize the bus once and keep writing to it?

I was wondering if I can get some help on implementing the i2c library correctly
:smiley:

Check out the example applications.

https://beagleboard.org/librobotcontrol/rc_test_mpu_8c-example.html

Using these examples as a template and removing/adding your code will be the easiest way for someone new to get started.

I don’t recall where they get installed on the image, so, looking…

From https://github.com/beagleboard/librobotcontrol/blob/master/Makefile#L16, I would infer that this is likely installed at /usr/share/robotcontrol/examples.

Thank you for your reply. I was actually just reading in an external sensor and was practicing on using the external I2C BUS. I don’t think I understand how the read and write functions work. I’m assuming when I use the read bytes function. It requests data from the first register and the next one and the next one ect… when using “rc_i2c_read_bytes(I2C_BUS, 0x3B,6, data);” For instance it will go from 0x3B to 0x3C to 0x3D up till 0x40

image

float recordAccelRegisters(float *Xa , float *Ya, float *Za){
    uint8_t data[I2C_BUFFER_SIZE];

    //printf("Recording Accel Data\n");
    rc_i2c_init	(I2C_BUS,I2C_ADDRESS);
    rc_i2c_read_bytes(I2C_BUS, 0x3B,6, data);
    rc_i2c_close(I2C_BUS);

    *Xa = data[1] << 8 | data[0];
    *Ya = data[3] << 8 | data[2];
    *Za = data[5] << 8 | data[4];    
    
    
    *Xa = *Xa/16384.0;
    *Ya = *Ya/16384.0;
    *Za = *Za/16384.0;
    return 0;
}

void setupMPU(){
    printf("Initalizing Mpu\n");

    // init I2C Bus
    rc_i2c_init	(I2C_BUS,I2C_ADDRESS);

    //Wake up setup
    rc_i2c_write_byte(I2C_BUS, 0x6B, 0x00);
    rc_i2c_close(I2C_BUS);

    //Accel Config
    printf("Configuring Accelerometer\n");
    printf("Initalizing Mpu\n");
    
    rc_i2c_init	(I2C_BUS,I2C_ADDRESS);
    rc_i2c_write_byte(I2C_BUS, 0x1C, 0x00);
    printf("Done with Accel Config\n");
    rc_i2c_close(I2C_BUS);
}

The way I have it now. I am getting data back but it is garbage. I am expecting something around 0,0,9.81 for x,y,z respectively but i’m getting around 1,2,3 for x,y,z. When using rc_i2c_read_bytes do I need to talk and read from each register directly?