This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

RTOS: I2C MPU-6050/MSP432P401

Other Parts Discussed in Thread: ENERGIA, SYSBIOS, TMP006

Tool/software: TI-RTOS

Hi, 

Im a new using TI products and CCS. Im trying to adapt a arduino's code, which reads data from a IMU (MPU-6050), to a MSP432P401 but i have some problems. If i tried to use the exactly code to Energia or CCS when reading the data from the IMU, i just get wrong data. This is the code that i have use: 

//Prueba de lectura de la IMU
//Librerias que incluímos:
#include <Wire.h> //Librería para la comunicacion con la IMU por I2C

//Declaración de variables para almacenar los datos de la IMU
int gyro_x, gyro_y, gyro_z;
long acc_x, acc_y, acc_z;
int temperatura;

#define LED RED_LED

void setup(){
Serial.begin(57600); //Inicilaización del monitor serie
Wire.begin(); //Inicializamos el I2C
setup_IMU();
Lee_IMU();


pinMode(LED,OUTPUT);
digitalWrite(LED,HIGH);

}

void loop(){

}

void setup_IMU() {
//Función que se encarga de inicializar la IMU
Wire.beginTransmission(0x68);
Wire.write(0x6B);
Wire.write(0x00);
Wire.endTransmission();
//Configuración de la IMU
Wire.beginTransmission(0x68);
Wire.write(0x1C);
Wire.write(0x10);
Wire.endTransmission();
//Configuración del giroscopio
Wire.beginTransmission(0x68);
Wire.write(0x1B);
Wire.write(0x08);
Wire.endTransmission();
}

void Lee_IMU(){
//Subrutina que se utiliza para leer los datos de la IMU
Wire.beginTransmission(0x68);
Wire.write(0x3B);
Wire.endTransmission();
Wire.requestFrom(0x68,14);
while(Wire.available() <14); //Esperamos a haber leido los 14 datos
acc_x = Wire.read()<<8 |Wire.read();
acc_y = Wire.read()<<8 |Wire.read();
acc_z = Wire.read()<<8 |Wire.read();
temperatura = Wire.read()<<8 |Wire.read();
gyro_x = Wire.read()<<8 |Wire.read();
gyro_y = Wire.read()<<8 |Wire.read();
gyro_z = Wire.read()<<8 |Wire.read();

}

This is the code from Energia, i have tried something similar with Code Composer Studio using TI RTOS but i get the same wrong data. 


I'm using pins 6.5 and 6.4 to connect to SCL and SDA, respectively (using the convenient pull up resistors).

Can someone help me? 

Thanks you,

Luis.

  • Luis,

    I don't see where you're checking the IMU values. You're reading them once during setup() but never reading them again nor doing anything with them in the loop() function. How do you know they are "the same wrong data"?

    Alan

  • Hi Alan,

    Thanks you for the reply.

    I did not explain everything, but I wanted to shorten it as much as possible.
    I know they are wrong values ​​because I imported the energy code in CCS and executed it with breakpoints in CCS, so I could read the values ​​I received from the IMU.

    I have done 2 codes, in both case i read the same wrong values. One using TI RTOS in CCS, following the examples in CCS. This first code is this:

    /* XDCtools Header files */
    #include <xdc/std.h>
    #include <xdc/runtime/System.h>

    /* BIOS Header files */
    #include <ti/sysbios/BIOS.h>
    #include <ti/sysbios/knl/Task.h>

    /* TI-RTOS Header files */
    #include <ti/drivers/GPIO.h>
    #include <ti/drivers/I2C.h>

    /* Example/Board Header files */
    #include "Board.h"

    //Libreria de funciones matematicas
    #include <math.h>

    //_______________________Variables de la IMU____________________________________
    //ACELERÓMETRO
    long acc_x;
    long acc_y;
    long acc_z;

    //GIROSCOPIO
    long gyro_x;
    long gyro_y;
    long gyro_z;

    //_______________Variables del I2C para lectura de datos de la IMU_______________

    uint8_t txBuffer[2]; //ENVÍO: las ordenes siempre se escriben de dos en dos, primero el registro que queremos escribir y luego lo que escribimos en el
    uint8_t rxBuffer[14]; //LECTURA: 14 bytes en el caso de la IMU
    I2C_Handle i2c;
    I2C_Params i2cParams;
    I2C_Transaction i2cTransaction;




    #define TASKSTACKSIZE 640
    #define PI 3.14159

    Task_Struct task0Struct;
    Char task0Stack[TASKSTACKSIZE];

    /*
    * ======== taskFxn ========
    * Task for this function is created statically. See the project's .cfg file.
    */
    Void taskFxn(UArg arg0, UArg arg1)
    {
    //Este hilo será el encargado de toda la gestion de comunicación y captura de datos con la IMU


    //Inicializacion de la IMU:
    System_printf("Inicializamos la IMU: \n");
    System_flush();
    InicializaIMU(); //Subrutina que inicializa la IMU
    }

    /*
    * ======== main ========
    */
    int main(void)
    {
    Task_Params taskParams;

    /* Call board init functions */
    Board_initGeneral();
    Board_initGPIO();
    Board_initI2C();

    /* Construct tmp006 Task thread */
    Task_Params_init(&taskParams);
    taskParams.stackSize = TASKSTACKSIZE;
    taskParams.stack = &task0Stack;
    Task_construct(&task0Struct, (Task_FuncPtr)taskFxn, &taskParams, NULL);
    //De esta manera ya tiene inicializado un hilo que se encargará de sacar las medidas por I2C

    /* Turn on user LED */
    GPIO_write(Board_LED0, Board_LED_ON);




    /* SysMin will only print to the console when you call flush or exit */


    /* Start BIOS */
    BIOS_start();

    return (0);
    }



    //=======================================================================================================
    //--------------------------- Calibración IMU ------------------------------------------------------
    //=======================================================================================================


    void Calibracion () {
    //Esta subrutina se encarga de calibrar los GIROSCOPIOS de la IMU, Para ver la medida en parado y
    //poder restarsela a las proximas medidas:

    int LecturaCorrecta; //Variable que nos dice si la lectura ha sido correcta o no
    //Si LecturaCorrecta = 1 => Lectura correcta, SI LecturaCorrecta = 0 => Fallo en la lectura

    //De momento no calibramos nada, simplemente tomamos 20 medidas:
    int i;
    for ( i = 0; i < 20; i++){
    LecturaCorrecta = LecturaDatosIMU();


    if (LecturaCorrecta ==1) {
    System_printf ("Lectura correcta número %u \n", i);
    }else {
    //En caso de que no haya sido una buena lectura:
    System_printf ("Fallo en la lectura \n");
    }
    System_flush();
    Task_sleep(1000);
    }




    }


    //========================================================================================================
    //------------------------------------ Lectura de datos de la IMU --------------------------------------
    //========================================================================================================

    int LecturaDatosIMU(){
    //Función que se encarga de leer los datos de la IMU y guardarlos en variables globales:

    int LecturaCorrecta;



    //===========================================================================
    //------------ Prueba SETUP de la IMU ---------------------------------------
    //===========================================================================

    //Probamos a relaizar 20 medidas consecutivas
    /* Point to the T ambient register and read its 2 bytes */

    i2c = I2C_open(Board_I2C_IMU, &i2cParams);
    txBuffer[0] = 0x3B;
    i2cTransaction.slaveAddress = Board_IMU_ADDR;
    i2cTransaction.writeBuf = txBuffer;
    i2cTransaction.writeCount = 1;
    i2cTransaction.readBuf = rxBuffer;
    i2cTransaction.readCount = 14;

    if (I2C_transfer(i2c, &i2cTransaction)) {
    //Leemos todos los datos de la IMU
    //Como todos los datos tienen 16 bits tenemos que desplazar la parte baja y la parte alta:

    acc_x = (rxBuffer[0] << 8) | (rxBuffer[1]);
    acc_y = (rxBuffer[2] << 8) | (rxBuffer[3]);
    acc_z = (rxBuffer[4] << 8) | (rxBuffer[5]);



    gyro_x = (rxBuffer[8] << 8) | (rxBuffer[9]);
    gyro_y = (rxBuffer[10] << 8) | (rxBuffer[11]);
    gyro_z = (rxBuffer[12] << 8) | (rxBuffer[13]);

    LecturaCorrecta = 1;

    }
    else {
    LecturaCorrecta = 0;
    }



    /* Deinitialized I2C */
    I2C_close(i2c);
    return LecturaCorrecta;
    }

    void InicializaIMU (UArg arg0, UArg arg1){
    //Función que se encarga de inicializar los registros de la IMU, para posteriormente solo tener que leer los datos de la misma:
    /* Create I2C for usage */

    //Creación del objeto I2C para la comunicación:
    I2C_Params_init(&i2cParams);
    i2cParams.bitRate = I2C_400kHz; //Misma frecuencia para la IMU
    i2c = I2C_open(Board_I2C_IMU, &i2cParams);
    if (i2c == NULL) {
    System_abort("Error inicializando el I2C \n");
    }
    else {
    System_printf("I2C inicializado \n");
    }
    I2C_close(i2c); //Cerramos el canal

    i2c = I2C_open(Board_I2C_IMU, &i2cParams);
    //Solicitud de Inicio:
    txBuffer[0] = 0x6B; //Registro de solicitud de Inicio
    txBuffer[1] = 0x00; //Lo inicializamo
    i2cTransaction.slaveAddress = Board_IMU_ADDR;
    i2cTransaction.writeBuf = txBuffer;
    i2cTransaction.writeCount = 2;
    i2cTransaction.readBuf = NULL;
    i2cTransaction.readCount = 0;

    //Probamos a escribir esto y ver si da algun fallo:
    if (I2C_transfer(i2c, &i2cTransaction)) {
    //En caso de que no haya habido fallo_
    System_printf("Se ha inicializado la IMU \n");
    }
    else {
    System_printf("I2C Bus fault\n");
    }

    System_flush();
    I2C_close(i2c); //Cerramos el canal


    i2c = I2C_open(Board_I2C_IMU, &i2cParams);
    // INICIALIZACIÓN DEL ACELERÓMETRO
    txBuffer[0] = 0x1C; //Registro de inicialización de los acelerometros
    txBuffer[1] = 0x10; //Los inicializamos

    //Probamos a escribir la configuración, comprobando si da fallo
    if (I2C_transfer(i2c, &i2cTransaction)) {
    //En caso de que no haya habido fallo
    System_printf("Inicialización acelerómetros \n");
    }
    else {
    System_printf("I2C Bus fault\n");
    }

    System_flush();
    I2C_close(i2c); //Cerramos el canal

    i2c = I2C_open(Board_I2C_IMU, &i2cParams);
    // INICIALIZACIÓN DEL GIROSCOPIO
    txBuffer[0] = 0x1B; //Registro de inicialización de los acelerometros
    txBuffer[1] = 0x08; //Los inicializamos

    //Probamos a darle la configuración, comprobando si da fallo
    if (I2C_transfer(i2c, &i2cTransaction)) {
    //En caso de que no haya habido fallo
    System_printf("Inicialización giroscopios \n");
    }
    else {
    System_printf("I2C Bus fault\n");
    }

    System_flush();

    //Una vez configurados todos los parametros cerramos el canal
    I2C_close(i2c);
    System_printf("I2C cerrado \n");

    //Llamamos a la función de calibración, para sacarla media del ruido de los giroscopios:
    Calibracion();
    }



    The second code I have done is the one of Energia that I showed in my first post.

    Thanks you for your help,
    Luis.

**Attention** This is a public forum