MPU6050.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. MPU6050.h - Header file for the MPU6050 Triple Axis Gyroscope & Accelerometer Arduino Library.
  3. Version: 1.0.3
  4. (c) 2014-2015 Korneliusz Jarzebski
  5. www.jarzebski.pl
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the version 3 GNU General Public License as
  8. published by the Free Software Foundation.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #ifndef MPU6050_h
  17. #define MPU6050_h
  18. #if ARDUINO >= 100
  19. #include "Arduino.h"
  20. #else
  21. #include "WProgram.h"
  22. #endif
  23. #define MPU6050_ADDRESS (0x68) // 0x69 when AD0 pin to Vcc
  24. #define MPU6050_REG_ACCEL_XOFFS_H (0x06)
  25. #define MPU6050_REG_ACCEL_XOFFS_L (0x07)
  26. #define MPU6050_REG_ACCEL_YOFFS_H (0x08)
  27. #define MPU6050_REG_ACCEL_YOFFS_L (0x09)
  28. #define MPU6050_REG_ACCEL_ZOFFS_H (0x0A)
  29. #define MPU6050_REG_ACCEL_ZOFFS_L (0x0B)
  30. #define MPU6050_REG_GYRO_XOFFS_H (0x13)
  31. #define MPU6050_REG_GYRO_XOFFS_L (0x14)
  32. #define MPU6050_REG_GYRO_YOFFS_H (0x15)
  33. #define MPU6050_REG_GYRO_YOFFS_L (0x16)
  34. #define MPU6050_REG_GYRO_ZOFFS_H (0x17)
  35. #define MPU6050_REG_GYRO_ZOFFS_L (0x18)
  36. #define MPU6050_REG_CONFIG (0x1A)
  37. #define MPU6050_REG_GYRO_CONFIG (0x1B) // Gyroscope Configuration
  38. #define MPU6050_REG_ACCEL_CONFIG (0x1C) // Accelerometer Configuration
  39. #define MPU6050_REG_FF_THRESHOLD (0x1D)
  40. #define MPU6050_REG_FF_DURATION (0x1E)
  41. #define MPU6050_REG_MOT_THRESHOLD (0x1F)
  42. #define MPU6050_REG_MOT_DURATION (0x20)
  43. #define MPU6050_REG_ZMOT_THRESHOLD (0x21)
  44. #define MPU6050_REG_ZMOT_DURATION (0x22)
  45. #define MPU6050_REG_INT_PIN_CFG (0x37) // INT Pin. Bypass Enable Configuration
  46. #define MPU6050_REG_INT_ENABLE (0x38) // INT Enable
  47. #define MPU6050_REG_INT_STATUS (0x3A)
  48. #define MPU6050_REG_ACCEL_XOUT_H (0x3B)
  49. #define MPU6050_REG_ACCEL_XOUT_L (0x3C)
  50. #define MPU6050_REG_ACCEL_YOUT_H (0x3D)
  51. #define MPU6050_REG_ACCEL_YOUT_L (0x3E)
  52. #define MPU6050_REG_ACCEL_ZOUT_H (0x3F)
  53. #define MPU6050_REG_ACCEL_ZOUT_L (0x40)
  54. #define MPU6050_REG_TEMP_OUT_H (0x41)
  55. #define MPU6050_REG_TEMP_OUT_L (0x42)
  56. #define MPU6050_REG_GYRO_XOUT_H (0x43)
  57. #define MPU6050_REG_GYRO_XOUT_L (0x44)
  58. #define MPU6050_REG_GYRO_YOUT_H (0x45)
  59. #define MPU6050_REG_GYRO_YOUT_L (0x46)
  60. #define MPU6050_REG_GYRO_ZOUT_H (0x47)
  61. #define MPU6050_REG_GYRO_ZOUT_L (0x48)
  62. #define MPU6050_REG_MOT_DETECT_STATUS (0x61)
  63. #define MPU6050_REG_MOT_DETECT_CTRL (0x69)
  64. #define MPU6050_REG_USER_CTRL (0x6A) // User Control
  65. #define MPU6050_REG_PWR_MGMT_1 (0x6B) // Power Management 1
  66. #define MPU6050_REG_WHO_AM_I (0x75) // Who Am I
  67. #ifndef VECTOR_STRUCT_H
  68. #define VECTOR_STRUCT_H
  69. struct Vector
  70. {
  71. float XAxis;
  72. float YAxis;
  73. float ZAxis;
  74. };
  75. #endif
  76. struct Activites
  77. {
  78. bool isOverflow;
  79. bool isFreeFall;
  80. bool isInactivity;
  81. bool isActivity;
  82. bool isPosActivityOnX;
  83. bool isPosActivityOnY;
  84. bool isPosActivityOnZ;
  85. bool isNegActivityOnX;
  86. bool isNegActivityOnY;
  87. bool isNegActivityOnZ;
  88. bool isDataReady;
  89. };
  90. typedef enum
  91. {
  92. MPU6050_CLOCK_KEEP_RESET = 0b111,
  93. MPU6050_CLOCK_EXTERNAL_19MHZ = 0b101,
  94. MPU6050_CLOCK_EXTERNAL_32KHZ = 0b100,
  95. MPU6050_CLOCK_PLL_ZGYRO = 0b011,
  96. MPU6050_CLOCK_PLL_YGYRO = 0b010,
  97. MPU6050_CLOCK_PLL_XGYRO = 0b001,
  98. MPU6050_CLOCK_INTERNAL_8MHZ = 0b000
  99. } mpu6050_clockSource_t;
  100. typedef enum
  101. {
  102. MPU6050_SCALE_2000DPS = 0b11,
  103. MPU6050_SCALE_1000DPS = 0b10,
  104. MPU6050_SCALE_500DPS = 0b01,
  105. MPU6050_SCALE_250DPS = 0b00
  106. } mpu6050_dps_t;
  107. typedef enum
  108. {
  109. MPU6050_RANGE_16G = 0b11,
  110. MPU6050_RANGE_8G = 0b10,
  111. MPU6050_RANGE_4G = 0b01,
  112. MPU6050_RANGE_2G = 0b00,
  113. } mpu6050_range_t;
  114. typedef enum
  115. {
  116. MPU6050_DELAY_3MS = 0b11,
  117. MPU6050_DELAY_2MS = 0b10,
  118. MPU6050_DELAY_1MS = 0b01,
  119. MPU6050_NO_DELAY = 0b00,
  120. } mpu6050_onDelay_t;
  121. typedef enum
  122. {
  123. MPU6050_DHPF_HOLD = 0b111,
  124. MPU6050_DHPF_0_63HZ = 0b100,
  125. MPU6050_DHPF_1_25HZ = 0b011,
  126. MPU6050_DHPF_2_5HZ = 0b010,
  127. MPU6050_DHPF_5HZ = 0b001,
  128. MPU6050_DHPF_RESET = 0b000,
  129. } mpu6050_dhpf_t;
  130. typedef enum
  131. {
  132. MPU6050_DLPF_6 = 0b110,
  133. MPU6050_DLPF_5 = 0b101,
  134. MPU6050_DLPF_4 = 0b100,
  135. MPU6050_DLPF_3 = 0b011,
  136. MPU6050_DLPF_2 = 0b010,
  137. MPU6050_DLPF_1 = 0b001,
  138. MPU6050_DLPF_0 = 0b000,
  139. } mpu6050_dlpf_t;
  140. class MPU6050
  141. {
  142. public:
  143. bool begin(mpu6050_dps_t scale = MPU6050_SCALE_2000DPS, mpu6050_range_t range = MPU6050_RANGE_2G, int mpua = MPU6050_ADDRESS);
  144. void setClockSource(mpu6050_clockSource_t source);
  145. void setScale(mpu6050_dps_t scale);
  146. void setRange(mpu6050_range_t range);
  147. mpu6050_clockSource_t getClockSource(void);
  148. mpu6050_dps_t getScale(void);
  149. mpu6050_range_t getRange(void);
  150. void setDHPFMode(mpu6050_dhpf_t dhpf);
  151. void setDLPFMode(mpu6050_dlpf_t dlpf);
  152. mpu6050_onDelay_t getAccelPowerOnDelay();
  153. void setAccelPowerOnDelay(mpu6050_onDelay_t delay);
  154. uint8_t getIntStatus(void);
  155. bool getIntZeroMotionEnabled(void);
  156. void setIntZeroMotionEnabled(bool state);
  157. bool getIntMotionEnabled(void);
  158. void setIntMotionEnabled(bool state);
  159. bool getIntFreeFallEnabled(void);
  160. void setIntFreeFallEnabled(bool state);
  161. uint8_t getMotionDetectionThreshold(void);
  162. void setMotionDetectionThreshold(uint8_t threshold);
  163. uint8_t getMotionDetectionDuration(void);
  164. void setMotionDetectionDuration(uint8_t duration);
  165. uint8_t getZeroMotionDetectionThreshold(void);
  166. void setZeroMotionDetectionThreshold(uint8_t threshold);
  167. uint8_t getZeroMotionDetectionDuration(void);
  168. void setZeroMotionDetectionDuration(uint8_t duration);
  169. uint8_t getFreeFallDetectionThreshold(void);
  170. void setFreeFallDetectionThreshold(uint8_t threshold);
  171. uint8_t getFreeFallDetectionDuration(void);
  172. void setFreeFallDetectionDuration(uint8_t duration);
  173. bool getSleepEnabled(void);
  174. void setSleepEnabled(bool state);
  175. bool getI2CMasterModeEnabled(void);
  176. void setI2CMasterModeEnabled(bool state);
  177. bool getI2CBypassEnabled(void);
  178. void setI2CBypassEnabled(bool state);
  179. float readTemperature(void);
  180. Activites readActivites(void);
  181. int16_t getGyroOffsetX(void);
  182. void setGyroOffsetX(int16_t offset);
  183. int16_t getGyroOffsetY(void);
  184. void setGyroOffsetY(int16_t offset);
  185. int16_t getGyroOffsetZ(void);
  186. void setGyroOffsetZ(int16_t offset);
  187. int16_t getAccelOffsetX(void);
  188. void setAccelOffsetX(int16_t offset);
  189. int16_t getAccelOffsetY(void);
  190. void setAccelOffsetY(int16_t offset);
  191. int16_t getAccelOffsetZ(void);
  192. void setAccelOffsetZ(int16_t offset);
  193. void calibrateGyro(uint8_t samples = 50);
  194. void setThreshold(uint8_t multiple = 1);
  195. uint8_t getThreshold(void);
  196. Vector readRawGyro(void);
  197. Vector readNormalizeGyro(void);
  198. Vector readRawAccel(void);
  199. Vector readNormalizeAccel(void);
  200. Vector readScaledAccel(void);
  201. private:
  202. Vector ra, rg; // Raw vectors
  203. Vector na, ng; // Normalized vectors
  204. Vector tg, dg; // Threshold and Delta for Gyro
  205. Vector th; // Threshold
  206. Activites a; // Activities
  207. float dpsPerDigit, rangePerDigit;
  208. float actualThreshold;
  209. bool useCalibrate;
  210. int mpuAddress;
  211. uint8_t fastRegister8(uint8_t reg);
  212. uint8_t readRegister8(uint8_t reg);
  213. void writeRegister8(uint8_t reg, uint8_t value);
  214. int16_t readRegister16(uint8_t reg);
  215. void writeRegister16(uint8_t reg, int16_t value);
  216. bool readRegisterBit(uint8_t reg, uint8_t pos);
  217. void writeRegisterBit(uint8_t reg, uint8_t pos, bool state);
  218. };
  219. #endif