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.

Read MPU 9250 Accelerometer Data

Other Parts Discussed in Thread: CC2650

Hi,

I have a CC2650 with Debugger Devpack, and I use CCS Version 6.1.2.00015. I am trying to use the mpu9250 (built-in accelerometer), with some files from the SensorTag example source code, where the accelerometer is used. I already managed to initialize the accelerometer (calling sensorMpu9250Init()) and configure a callback for the interrupts, and also I am able to execute the code of the callback when I enable the Wake on Motion feature, using sensorMpu9250WomEnable(WOM_THR). However, when I read the data from the accelerometer I don't get coherent values. 

I use the function sensorMpu9250AccRead((uint16_t*) &accData); defined in sensor_mpu9250.c, where accData is a uint8_t array of 6 length (this code has been extracted from the SensorTag example). Then, I set three int16_t variables (one for each axis) to convert the data of each axis, using sensorMpu9250AccConvert(accel_x); function. This is the portion of code I use to read the accelerometer data:

uint8_t accData[6];
int16_t accel_x, accel_y, accel_z;
uint8_t range;
float accelx, accely, accelz;

while(1){
	sensorMpu9250AccRead((uint16_t*) &accData);
	range = sensorMpu9250AccReadRange();
	System_printf("Accelerometer Range: %u\n", range);

	accel_x = (accData[0] << 8) + accData[1];
	accel_y = (accData[2] << 8) + accData[3];
	accel_z = (accData[4] << 8) + accData[5];

	accelx = sensorMpu9250AccConvert(accel_x);
	accely = sensorMpu9250AccConvert(accel_y);
	accelz = sensorMpu9250AccConvert(accel_z);

	System_printf("Accel data x: %1.2f\n", accelx);
	System_printf("Accel data y: %1.2f\n", accely);
	System_printf("Accel data z: %1.2f \n", accelz);
	System_flush();

	Task_sleep(5000 * (1000 / Clock_tickPeriod));
}

When I start the program, the SensorTag is static on my table, and the accelerometer is pointing upwards (with the TI logo pointing upwards), so I expect the output would be 0 for x and y axes, and 1 for z axis. However, the output of the console shows inaccurate and non-coherent data. This is the console output after some seconds of running the program:

Accelerometer Range: 2
Accel data x: -6.6894
Accel data y: 0.1237
Accel data z: -1.1220 
Accelerometer Range: 2
Accel data x: -7.1894
Accel data y: 0.6237
Accel data z: -1.1220 
Accelerometer Range: 2
Accel data x: -7.5019
Accel data y: 0.3737
Accel data z: -1.3720 
Accelerometer Range: 2
Accel data x: -7.1269
Accel data y: 0.4362
Accel data z: -1.4345 
Accelerometer Range: 2
Accel data x: -7.2519
Accel data y: 0.5612
Accel data z: -1.4970 
Accelerometer Range: 2
Accel data x: -6.7519
Accel data y: 0.4362
Accel data z: -1.1220

I have tried changing the range of the accelerometer, and the same happens for all ranges, the results never exceed the range, but they are near to the maximum although the SensorTag remains static.

I may be missing something but I don't know what it is. ¿Anyone can help me on this?

Thank you in advance! 

  • Hello,

    Since this questions is in regards to values you read from the MPU9250, it may be best to follow up with Invensense directly as they are the manufacturer of this device.

    Best wishes
  • Hi,

    Thanks for your response, I have already posted in Ivensense forum, but since I use the code from the SensorTag example, I think it is more suitable that you or some TI expert answers to this question.

    When I flash the SensorTag example into my CC2650, the values shown in the iOS app are correct, so the accelerometer is working fine. However, examining the code of the SensorTag example I don't see where the raw data is converted to acceleration, so I think it is done in the iOS app, but I don't have the source code of the app to examine it.

    Any thoughts?

    Thanks! 

  • Note the STK is returning acc values in G units. Have you accounted for this?
  • Hi Sean,

    Yes, I accounted this, that's why I was expecting values of x=0g, y=0g and z=1g. I have ran again the program and this is a sample of the values of the variables in my code:

    I have noted that when the SensorTag is static, the unchanging elements are 1, 3 and 5 instead of 0, 2 and 4, so I have changed the order of the bits for each axis and it worked! (I have already tried this before but it din't work, so maybe I was doing something else wrong).

    This is the final code to read from the accelerometer and convert the data to G units, that worked fine for me:

    sensorMpu9250AccRead((uint16_t*) &accData);
    
    accel_x = (((int16_t)accData[1]) << 8) | accData[0];
    accel_y = (((int16_t)accData[3]) << 8) | accData[2];
    accel_z = (((int16_t)accData[5]) << 8) | accData[4];
    
    accelx = sensorMpu9250AccConvert(accel_x);
    accely = sensorMpu9250AccConvert(accel_y);
    accelz = sensorMpu9250AccConvert(accel_z);

    Thanks for your replies and your help!

  • Hi Cdman

    Im also working with the MPU9250 and getting the data from it in order to send it to my computer, I know that I can base the code from the SensorTag app and use sensor_mpu9250.c from it. and also from SensorTag_Mov.c , Im a new on this and sincerely, I dont know how to start, I read the documentation, but I'm getting troubles of how to make a copy of the SensorTag app. Could you please help or give me an advise of getting started?
    I would appreciate your reply!

    Thanks
    RH
  • Hi RH,

    I was in your situation past year. What I suggest to you is what I did, and it worked for me. First of all, try to start a project from scratch by following this LAB:

    software-dl.ti.com/.../tirtos_01_basic.html

    This is an introduction lab that will give you some insights about TI-RTOS, where you will learn the basic concepts related with tasks, semaphores and interrupts. Later, you can copy the sensor_mpu9250.c file, and try to use the functions defined there (what I did first was to change the button interrupt used in the previous lab, by the one thrown by the MPU when it detects some motion).

    The SensorTag app is a very complex application, and it can be quite confusing to start from it, so I suggest you start from scratch and copy only the part of the code you need.

    Hope it helps!
  • Thank you very much!

    so the compiler you used was scratch right? I will follow the instructions you gave me and try to performance the accelerometer. You just copied the file .c and pasted it on scratch file?

    Thanks for your reply bro.

    RH

  • Sorry Bro a Translation mistake I got about scratch, theres a program named scratch thats why I got confused, What you meant was from the beginning.
    The problem I have now is how to clone a project from the Ble stack to create the program with the sensor_mpu9250.c file, Do you know how?
  • I think the easiest way is to clone the sensortag project in BLE Stack v2.2.1 and revise on it.
  • The Problem is when I clone the sensortag program, I got and error on building it.
  • Can you specify your steps and what errors do you see when you build it?
  • Hi Cdman,

    Thank you for your code. I have tried doing something similar but didn't succeed till now. I still have some questions though : I have tried storing the data values from the mpu9250 into global variables in order to add them into watch expressions (since otherwise it can't detect them), but still didn't get nothing. The other thing that still confuses me is where do i need to implement that code ? In which file ? should i create another task and implement it in there ?

    Thank you
  • Hi,

    The place where you implement the code is up to your design, you must decide wether to use the same task for everything, or using several tasks one for each service. You can read a lot about TI-RTOS, and you'll see where is the most appropriate plate to implement it.

    What I did was just implement the code to see the acceleration values on the debug console and blink LEDs, so I cannot help you with the watch expressions.

    Finally, if anyone is wondering about the error I was getting when I created this post, it finally worked with something like this:

    sensorMpu9250AccRead((uint16_t*) &accData);
    
    accel_x = (((int16_t)accData[1]) << 8) | accData[0];
    
    accel_y = (((int16_t)accData[3]) << 8) | accData[2];
    
    accel_z = (((int16_t)accData[5]) << 8) | accData[4];