Maker Pro
Maker Pro

Weird problem with Intel Edison and IMU MPU-9150

Frederic Philips

Nov 16, 2015
1
Joined
Nov 16, 2015
Messages
1
Hi guys,

So I am trying to get measurements from the MPU-9150 sensor from Invensense using the Intel Edison. I am able to communicate with the sensor and the values seem sensible.

But after a specific number of cycles, I measure only a zero from all data registers. This error is repeatable: When I try to read the Accelerometer and Gyro data(6 data registers), the data stream stops exactly at 84 cycles. If I try to read jus the Accelerometer values, it stops at double the no. of cycles i.e. 168. I sense some data is overflowing, but couldn't figure out yet.

Looking for your reply and Thanks!

Cheers,

Frederic

Code:

MPU9150.h

  1. //Self Test registers R/W
  2. #define SELF_TEST_X 0x0D
  3. #define SELF_TEST_Y 0x0E
  4. #define SELF_TEST_Z 0x0F
  5. #define SELF_TEST_A 0x0A
  6. //Configuration Registers
  7. #define SMPRT_DIV 0x19 //Sample Rate Divider
  8. #define CONFIG 0x1A //FSYNC & DLPF config
  9. #define GYRO_CONFIG 0x1B //Self-Test & Scale select
  10. #define ACCEL_CONFIG 0x1C //Self-Test & Scale select
  11. //Interrupt Registers
  12. #define INT_ENABLE 0x38
  13. #define INT_STATUS 0x3A
  14. //Accelerometer Measurement Registers
  15. #define ACCEL_XOUT_H 0x3B
  16. #define ACCEL_XOUT_L 0x3C
  17. #define ACCEL_YOUT_H 0x3D
  18. #define ACCEL_YOUT_L 0x3E
  19. #define ACCEL_ZOUT_H 0x3F
  20. #define ACCEL_ZOUT_L 0x40
  21. //Temperature Measurement Registers
  22. //Temperature in degrees C = (TEMP_OUT Register Value as a signed quantity)/340 + 35
  23. #define TEMP_OUT_H 0x41
  24. #define TEMP_OUT_L 0x42
  25. //Gyroscope Measurement Registers
  26. #define GYRO_XOUT_H 0x43
  27. #define GYRO_XOUT_L 0x44
  28. #define GYRO_YOUT_H 0x45
  29. #define GYRO_YOUT_L 0x46
  30. #define GYRO_ZOUT_H 0x47
  31. #define GYRO_ZOUT_L 0x48
  32. //Power Management Registers
  33. #define PWR_MGMT_1 0x6B
  34. #define PWR_MGMT_2 0x6C
  35. //Device i.d. Register
  36. #define WHO_AM_I 0x75
  37. /*
  38. R1 - 0x69
  39. R2 - 0x68
  40. */
  41. #define MPU_ADDR 0x68
  42. //Reset the Registers and Power
  43. #define PWR_RESET 0x80
  44. #define DEVICE_ON 0x00
  45. //Accelerometer Scale
  46. #define ACCEL_2G 0x00
  47. #define ACCEL_4G 0x08
  48. #define ACCEL_8G 0x10
  49. #define ACCEL_16G 0x18
  50. //Gyroscope Scale
  51. #define GYRO_250 0x00
  52. #define GYRO_500 0x08
  53. #define GYRO_1000 0x10
  54. #define GYRO_2000 0x18
  55. #define SAMPLE_RATE 0x00
  56. #define DLPF_CFG 0x01


MPU9150.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <signal.h>
  4. #include "mraa.h"
  5. #include "MPU9150.h"
  6. void MPU9150_I2C_Init(void);
  7. void MPU9150_I2C_Config(void);
  8. void MPU9150_I2C_Write(uint8_t address, uint8_t value);
  9. void MPU9150_I2C_Read(uint8_t address, uint8_t *value);
  10. int16_t MPU9150_Get_Measurement(uint8_t addrL, uint8_t addrH);
  11. void sig_handler(int signum);
  12. sig_atomic_tvolatile isrunning = 1;
  13. //Acceleometer Measurement Storage Variables
  14. int16_t Accel_X = 0;
  15. int16_t Accel_Y = 0;
  16. int16_t Accel_Z = 0;
  17. //Gyroscope Measurement Storage Variables
  18. int16_t Gyro_X = 0;
  19. int16_t Gyro_Y = 0;
  20. int16_t Gyro_Z = 0;
  21. //Temperature Measurement Storage Variable
  22. float Temperature = 0;
  23. //Variable to Store LOW and HIGH Register values
  24. uint8_t Measurement_L = 0;
  25. uint8_t Measurement_H = 0;
  26. int main(int argc, char **argv)
  27. {
  28. printf("--------------------------------------------------\n");
  29. printf("------------------eGlove Project------------------\n");
  30. printf("--------------------------------------------------\n");
  31. sleep(1); //1s delay
  32. signal(SIGINT, &sig_handler);
  33. usleep(1000); //1ms delay
  34. MPU9150_I2C_Init();
  35. sleep(1); //1s delay
  36. MPU9150_I2C_Config();
  37. sleep(1); //1s delay
  38. while(isrunning)
  39. {
  40. //Get Accelerometer Measurements
  41. Accel_X = MPU9150_Get_Measurement(ACCEL_XOUT_L, ACCEL_XOUT_H);
  42. Accel_Y = MPU9150_Get_Measurement(ACCEL_YOUT_L, ACCEL_YOUT_H);
  43. Accel_Z = MPU9150_Get_Measurement(ACCEL_ZOUT_L, ACCEL_ZOUT_H);
  44. //Get Gyroscope Measurements
  45. Gyro_X = MPU9150_Get_Measurement(GYRO_XOUT_L, GYRO_XOUT_H);
  46. Gyro_Y = MPU9150_Get_Measurement(GYRO_YOUT_L, GYRO_YOUT_H);
  47. Gyro_Z = MPU9150_Get_Measurement(GYRO_ZOUT_L, GYRO_ZOUT_H);
  48. //Print Accelerometer Values
  49. printf("Accelerometer:\n X:%d\n Y:%d\n Z:%d\n ", Accel_X, Accel_Y, Accel_Z);
  50. //Print Gyroscope Values
  51. printf("Gyroscope:\n X:%d\n Y:%d\n Z:%d\n ", Gyro_X, Gyro_Y,Gyro_Z);
  52. //Sample Readings every second
  53. sleep(1); //1s delay
  54. }
  55. return MRAA_SUCCESS;
  56. }
  57. void MPU9150_I2C_Init(void)
  58. {
  59. mraa_init();
  60. mraa_i2c_context mpu9150_i2c;
  61. mpu9150_i2c = mraa_i2c_init(1);
  62. if (mpu9150_i2c == NULL)
  63. {
  64. printf("MPU9150 I2C initialization failed, exit...\n");
  65. exit(1);
  66. }
  67. printf("MPU9150 I2C initialized successfully\n");
  68. mraa_i2c_address(mpu9150_i2c, MPU_ADDR);
  69. printf("MPU9150 I2C Address set to 0x%x\n", MPU_ADDR);
  70. }
  71. void MPU9150_I2C_Config(void)
  72. {
  73. mraa_i2c_context mpu9150_i2c;
  74. mpu9150_i2c = mraa_i2c_init(1);
  75. //Reset all the Registers
  76. mraa_i2c_address(mpu9150_i2c, MPU_ADDR);
  77. MPU9150_I2C_Write(PWR_MGMT_1, PWR_RESET);
  78. printf("MPU9150 Reset\n");
  79. sleep(1); //1s delay
  80. mraa_i2c_address(mpu9150_i2c, MPU_ADDR);
  81. MPU9150_I2C_Write(PWR_MGMT_1, DEVICE_ON);
  82. printf("MPU9150 Switched ON\n");
  83. sleep(1); //1s delay
  84. mraa_i2c_address(mpu9150_i2c, MPU_ADDR);
  85. uint8_t data = mraa_i2c_read_byte_data(mpu9150_i2c, WHO_AM_I);
  86. printf("Who am I: 0x%x\n", data);
  87. sleep(1); //1s delay
  88. MPU9150_I2C_Write(SMPRT_DIV, SAMPLE_RATE);
  89. MPU9150_I2C_Write(CONFIG, DLPF_CFG);
  90. //Set the Gyroscope Scale to 250°/s
  91. MPU9150_I2C_Write(GYRO_CONFIG, GYRO_500);
  92. //Set the Accelerometer Scale to 2G
  93. MPU9150_I2C_Write(ACCEL_CONFIG, ACCEL_2G);
  94. printf("Initialization Complete: All Systems are GO!!!\n");
  95. }
  96. void MPU9150_I2C_Write(uint8_t address, uint8_t value)
  97. {
  98. mraa_i2c_context mpu9150_i2c;
  99. mpu9150_i2c = mraa_i2c_init(1);
  100. //Set MPU Device Address
  101. mraa_i2c_address(mpu9150_i2c, MPU_ADDR);
  102. //Write Command and Data
  103. mraa_i2c_write_byte_data(mpu9150_i2c, value, address);
  104. }
  105. void MPU9150_I2C_Read(uint8_t address, uint8_t *value)
  106. {
  107. mraa_i2c_context mpu9150_i2c;
  108. mpu9150_i2c = mraa_i2c_init(1);
  109. //Set ALS Device Address
  110. mraa_i2c_address(mpu9150_i2c, MPU_ADDR);
  111. //Write Command and Read Data
  112. *value = mraa_i2c_read_byte_data(mpu9150_i2c, address);
  113. }
  114. int16_t MPU9150_Get_Measurement(uint8_t addrL, uint8_t addrH)
  115. {
  116. //Read & Store the Lower Byte
  117. MPU9150_I2C_Read(addrL, &Measurement_L);
  118. //Read & Store the Higher Byte
  119. MPU9150_I2C_Read(addrH, &Measurement_H);
  120. return (int16_t)((Measurement_H << 8) + Measurement_L);
  121. }
  122. //Signal Handler
  123. void sig_handler(int signum)
  124. {
  125. if(signum == SIGINT)
  126. {
  127. isrunning = 0;
  128. }
  129. }


Console Screenshot:





Reference:

MPU-9150 Product Specification: http://43zrtwysvxb2gf29r5o0athu.wpengine.netdna-cdn.com/wp-content/uploads/2015/02/MPU-9150-Datasheet.pdf

MPU-9150 Register Map: http://43zrtwysvxb2gf29r5o0athu.wpe...ent/uploads/2015/02/MPU-9150-Register-Map.pdf
 
Top