Advertisement
Guest User

Untitled

a guest
Jul 28th, 2015
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.86 KB | None | 0 0
  1. #ifndef MPU6050_H
  2. #define MPU6050_H
  3.  
  4. #include "mbed.h"
  5. #include "softwarei2c.h"
  6. #include "millis.h"
  7. #include "math.h"
  8.  
  9. // Define registers per MPU6050, Register Map and Descriptions, Rev 4.2, 08/19/2013 6 DOF Motion sensor fusion device
  10. // Invensense Inc., www.invensense.com
  11. // See also MPU-6050 Register Map and Descriptions, Revision 4.0, RM-MPU-6050A-00, 9/12/2012 for registers not listed in
  12. // above document; the MPU6050 and MPU 9150 are virtually identical but the latter has an on-board magnetic sensor
  13. //
  14. #define XGOFFS_TC 0x00 // Bit 7 PWR_MODE, bits 6:1 XG_OFFS_TC, bit 0 OTP_BNK_VLD
  15. #define YGOFFS_TC 0x01
  16. #define ZGOFFS_TC 0x02
  17. #define X_FINE_GAIN 0x03 // [7:0] fine gain
  18. #define Y_FINE_GAIN 0x04
  19. #define Z_FINE_GAIN 0x05
  20. #define XA_OFFSET_H 0x06 // User-defined trim values for accelerometer
  21. #define XA_OFFSET_L_TC 0x07
  22. #define YA_OFFSET_H 0x08
  23. #define YA_OFFSET_L_TC 0x09
  24. #define ZA_OFFSET_H 0x0A
  25. #define ZA_OFFSET_L_TC 0x0B
  26. #define SELF_TEST_X 0x0D
  27. #define SELF_TEST_Y 0x0E
  28. #define SELF_TEST_Z 0x0F
  29. #define SELF_TEST_A 0x10
  30. #define XG_OFFS_USRH 0x13 // User-defined trim values for gyroscope; supported in MPU-6050?
  31. #define XG_OFFS_USRL 0x14
  32. #define YG_OFFS_USRH 0x15
  33. #define YG_OFFS_USRL 0x16
  34. #define ZG_OFFS_USRH 0x17
  35. #define ZG_OFFS_USRL 0x18
  36. #define SMPLRT_DIV 0x19
  37. #define CONFIG 0x1A
  38. #define GYRO_CONFIG 0x1B
  39. #define ACCEL_CONFIG 0x1C
  40. #define FF_THR 0x1D // Free-fall
  41. #define FF_DUR 0x1E // Free-fall
  42. #define MOT_THR 0x1F // Motion detection threshold bits [7:0]
  43. #define MOT_DUR 0x20 // Duration counter threshold for motion interrupt generation, 1 kHz rate, LSB = 1 ms
  44. #define ZMOT_THR 0x21 // Zero-motion detection threshold bits [7:0]
  45. #define ZRMOT_DUR 0x22 // Duration counter threshold for zero motion interrupt generation, 16 Hz rate, LSB = 64 ms
  46. #define FIFO_EN 0x23
  47. #define I2C_MST_CTRL 0x24
  48. #define I2C_SLV0_ADDR 0x25
  49. #define I2C_SLV0_REG 0x26
  50. #define I2C_SLV0_CTRL 0x27
  51. #define I2C_SLV1_ADDR 0x28
  52. #define I2C_SLV1_REG 0x29
  53. #define I2C_SLV1_CTRL 0x2A
  54. #define I2C_SLV2_ADDR 0x2B
  55. #define I2C_SLV2_REG 0x2C
  56. #define I2C_SLV2_CTRL 0x2D
  57. #define I2C_SLV3_ADDR 0x2E
  58. #define I2C_SLV3_REG 0x2F
  59. #define I2C_SLV3_CTRL 0x30
  60. #define I2C_SLV4_ADDR 0x31
  61. #define I2C_SLV4_REG 0x32
  62. #define I2C_SLV4_DO 0x33
  63. #define I2C_SLV4_CTRL 0x34
  64. #define I2C_SLV4_DI 0x35
  65. #define I2C_MST_STATUS 0x36
  66. #define INT_PIN_CFG 0x37
  67. #define INT_ENABLE 0x38
  68. #define DMP_INT_STATUS 0x39 // Check DMP interrupt
  69. #define INT_STATUS 0x3A
  70. #define ACCEL_XOUT_H 0x3B
  71. #define ACCEL_XOUT_L 0x3C
  72. #define ACCEL_YOUT_H 0x3D
  73. #define ACCEL_YOUT_L 0x3E
  74. #define ACCEL_ZOUT_H 0x3F
  75. #define ACCEL_ZOUT_L 0x40
  76. #define TEMP_OUT_H 0x41
  77. #define TEMP_OUT_L 0x42
  78. #define GYRO_XOUT_H 0x43
  79. #define GYRO_XOUT_L 0x44
  80. #define GYRO_YOUT_H 0x45
  81. #define GYRO_YOUT_L 0x46
  82. #define GYRO_ZOUT_H 0x47
  83. #define GYRO_ZOUT_L 0x48
  84. #define EXT_SENS_DATA_00 0x49
  85. #define EXT_SENS_DATA_01 0x4A
  86. #define EXT_SENS_DATA_02 0x4B
  87. #define EXT_SENS_DATA_03 0x4C
  88. #define EXT_SENS_DATA_04 0x4D
  89. #define EXT_SENS_DATA_05 0x4E
  90. #define EXT_SENS_DATA_06 0x4F
  91. #define EXT_SENS_DATA_07 0x50
  92. #define EXT_SENS_DATA_08 0x51
  93. #define EXT_SENS_DATA_09 0x52
  94. #define EXT_SENS_DATA_10 0x53
  95. #define EXT_SENS_DATA_11 0x54
  96. #define EXT_SENS_DATA_12 0x55
  97. #define EXT_SENS_DATA_13 0x56
  98. #define EXT_SENS_DATA_14 0x57
  99. #define EXT_SENS_DATA_15 0x58
  100. #define EXT_SENS_DATA_16 0x59
  101. #define EXT_SENS_DATA_17 0x5A
  102. #define EXT_SENS_DATA_18 0x5B
  103. #define EXT_SENS_DATA_19 0x5C
  104. #define EXT_SENS_DATA_20 0x5D
  105. #define EXT_SENS_DATA_21 0x5E
  106. #define EXT_SENS_DATA_22 0x5F
  107. #define EXT_SENS_DATA_23 0x60
  108. #define MOT_DETECT_STATUS 0x61
  109. #define I2C_SLV0_DO 0x63
  110. #define I2C_SLV1_DO 0x64
  111. #define I2C_SLV2_DO 0x65
  112. #define I2C_SLV3_DO 0x66
  113. #define I2C_MST_DELAY_CTRL 0x67
  114. #define SIGNAL_PATH_RESET 0x68
  115. #define MOT_DETECT_CTRL 0x69
  116. #define USER_CTRL 0x6A // Bit 7 enable DMP, bit 3 reset DMP
  117. #define PWR_MGMT_1 0x6B // Device defaults to the SLEEP mode
  118. #define PWR_MGMT_2 0x6C
  119. #define DMP_BANK 0x6D // Activates a specific bank in the DMP
  120. #define DMP_RW_PNT 0x6E // Set read/write pointer to a specific start address in specified DMP bank
  121. #define DMP_REG 0x6F // Register in DMP from which to read or to which to write
  122. #define DMP_REG_1 0x70
  123. #define DMP_REG_2 0x71
  124. #define FIFO_COUNTH 0x72
  125. #define FIFO_COUNTL 0x73
  126. #define FIFO_R_W 0x74
  127. #define WHO_AM_I_MPU6050 0x75 // Should return 0x68
  128.  
  129. // Using the GY-521 breakout board, I set ADO to 0 by grounding through a 4k7 resistor
  130. // Seven-bit device address is 110100 for ADO = 0 and 110101 for ADO = 1
  131. #define ADO 0
  132. #if ADO
  133. #define MPU6050_ADDRESS 0x69<<1 // Device address when ADO = 1
  134. #else
  135. #define MPU6050_ADDRESS 0x68<<1 // Device address when ADO = 0
  136. #endif
  137.  
  138. // Set initial input parameters
  139. enum Ascale {
  140. AFS_2G = 0,
  141. AFS_4G,
  142. AFS_8G,
  143. AFS_16G
  144. };
  145.  
  146. enum Gscale {
  147. GFS_250DPS = 0,
  148. GFS_500DPS,
  149. GFS_1000DPS,
  150. GFS_2000DPS
  151. };
  152.  
  153. class MPU6050
  154. {
  155. protected:
  156. float q[4]; // vector to hold quaternion
  157. SoftwareI2C *i2c;
  158. DigitalOut *ad0;
  159.  
  160. private:
  161. // Specify sensor full scale
  162. int gScale, aScale;
  163. float aRes, gRes; // scale resolutions per LSB for the sensors
  164.  
  165. int16_t accelCount[3]; // Stores the 16-bit signed accelerometer sensor output
  166. int16_t gyroCount[3]; // Stores the 16-bit signed gyro sensor output
  167.  
  168. float ax, ay, az; // Stores the real accel value in g's
  169. float gx, gy, gz; // Stores the real gyro value in degrees per seconds
  170.  
  171. float gyroBias[3];
  172. float accelBias[3];
  173.  
  174. int16_t tempCount; // Stores the real internal chip temperature in degrees Celsius
  175. float temperature;
  176.  
  177. float selfTestData[6];
  178. float pitch, yaw, roll;
  179.  
  180. float dt; // integration interval for both filter schemes
  181.  
  182. // parameters for 6 DoF sensor fusion calculations
  183. const static float PI;
  184. const static float GyroMeasError; // gyroscope measurement error in rads/s (start at 60 deg/s), then reduce after ~10 s to 3
  185. const static float beta; // compute beta
  186. const static float GyroMeasDrift; // gyroscope measurement drift in rad/s/s (start at 0.0 deg/s/s)
  187. const static float zeta; // compute zeta, the other free parameter in the Madgwick scheme usually set to a small or zero value
  188.  
  189. int lastUpdate, firstUpdate; // used to calculate integration interval
  190.  
  191. public:
  192. MPU6050(SoftwareI2C *iic, PinName ad0pin);
  193. MPU6050(){};
  194. ~MPU6050(){delete ad0;};
  195.  
  196. void create(SoftwareI2C *iic, PinName ad0pin);
  197.  
  198. public:
  199. void writeByte(uint8_t address, uint8_t subAddress, uint8_t data) ;
  200. uint8_t readByte(uint8_t address, uint8_t subAddress);
  201. void readBytes(uint8_t address, uint8_t subAddress, uint8_t count, uint8_t * dest);
  202.  
  203. public:
  204. void getGres();
  205. void getAres();
  206.  
  207. public:
  208. void readAccelData(int16_t * destination);
  209. void readGyroData(int16_t * destination);
  210. int16_t readTempData();
  211.  
  212. public:
  213. void init();
  214. void reset();
  215.  
  216. // Function which accumulates gyro and accelerometer data after device initialization. It calculates the average
  217. // of the at-rest readings and then loads the resulting offsets into accelerometer and gyro bias registers.
  218. void calibrate(float * dest1, float * dest2);
  219. void selfTest(float * destination); // Should return percent deviation from factory trim values, +/- 14 or less deviation is a pass
  220.  
  221.  
  222. void updateDt();
  223. void updateQuaternion(float ax, float ay, float az, float gx, float gy, float gz);
  224. float invSqrt(float x);
  225. };
  226. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement