ITG3200.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*****************************************************************************/
  2. // Function: Cpp file for ITG3200
  3. // Hardware: Grove - 3-Axis Digital Gyro
  4. // Arduino IDE: Arduino-1.0
  5. // Author: Frankie.Chu
  6. // Date: Jan 11,2013
  7. // Version: v1.0
  8. // by www.seeedstudio.com
  9. //
  10. // This library is free software; you can redistribute it and/or
  11. // modify it under the terms of the GNU Lesser General Public
  12. // License as published by the Free Software Foundation; either
  13. // version 2.1 of the License, or (at your option) any later version.
  14. //
  15. // This library is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. // Lesser General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU Lesser General Public
  21. // License along with this library; if not, write to the Free Software
  22. // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  23. //
  24. /*******************************************************************************/
  25. #include <Arduino.h>
  26. #include <Wire.h>
  27. #include "ITG3200.h"
  28. /**********************************************************************/
  29. /*Function: Read a byte with the register address of ITG3200. */
  30. /*Parameter:-uint8_t _register,the register address of ITG3200 to read; */
  31. /*Return: -int8_t,the byte that is read from the register. */
  32. int8_t ITG3200::read(uint8_t _register) {
  33. int8_t data;
  34. Wire.beginTransmission(GYRO_ADDRESS);
  35. Wire.write(_register);
  36. Wire.endTransmission();
  37. Wire.requestFrom(GYRO_ADDRESS, 1);
  38. if (Wire.available() > 0) {
  39. data = Wire.read();
  40. }
  41. Wire.endTransmission();
  42. return data;
  43. }
  44. /*Function: Write a byte to the register of the MMA7660*/
  45. void ITG3200::write(uint8_t _register, uint8_t _data) {
  46. Wire.begin();
  47. Wire.beginTransmission(GYRO_ADDRESS);
  48. Wire.write(_register);
  49. Wire.write(_data);
  50. Wire.endTransmission();
  51. }
  52. /**********************************************************************/
  53. /*Function: Initialization for ITG3200. */
  54. void ITG3200::init() {
  55. Wire.begin();
  56. write(ITG3200_PWR_M, 0x80); //send a reset to the device
  57. write(ITG3200_SMPL, 0x00); //sample rate divider
  58. write(ITG3200_DLPF, 0x18); //+/-2000 degrees/s (default value)
  59. }
  60. int16_t ITG3200::read(uint8_t addressh, uint8_t addressl) {
  61. int data, t_data;
  62. Wire.beginTransmission(GYRO_ADDRESS);
  63. Wire.write(addressh);
  64. Wire.endTransmission();
  65. Wire.requestFrom(GYRO_ADDRESS, 1);
  66. if (Wire.available() > 0) {
  67. t_data = Wire.read();
  68. data = t_data << 8;
  69. }
  70. Wire.beginTransmission(GYRO_ADDRESS);
  71. Wire.write(addressl);
  72. Wire.endTransmission();
  73. Wire.requestFrom(GYRO_ADDRESS, 1);
  74. if (Wire.available() > 0) {
  75. data |= Wire.read();
  76. }
  77. return data;
  78. }
  79. /*Function: Get the temperature from ITG3200 that with a on-chip*/
  80. /* temperature sensor. */
  81. double ITG3200::getTemperature() {
  82. int temp;
  83. double temperature;
  84. temp = read(ITG3200_TMP_H, ITG3200_TMP_L);
  85. temperature = 35 + ((double)(temp + 13200)) / 280;
  86. return (temperature);
  87. }
  88. /*Function: Get the contents of the registers in the ITG3200*/
  89. /* so as to calculate the angular velocity. */
  90. void ITG3200::getXYZ(int16_t* x, int16_t* y, int16_t* z) {
  91. *x = read(ITG3200_GX_H, ITG3200_GX_L) + x_offset;
  92. *y = read(ITG3200_GY_H, ITG3200_GY_L) + y_offset;
  93. *z = read(ITG3200_GZ_H, ITG3200_GZ_L) + z_offset;
  94. }
  95. /*Function: Get the angular velocity and its unit is degree per second.*/
  96. void ITG3200::getAngularVelocity(float* ax, float* ay, float* az) {
  97. int16_t x, y, z;
  98. getXYZ(&x, &y, &z);
  99. *ax = x / 14.375;
  100. *ay = y / 14.375;
  101. *az = z / 14.375;
  102. }
  103. void ITG3200::zeroCalibrate(unsigned int samples, unsigned int sampleDelayMS) {
  104. long x_offset_temp = 0;
  105. long y_offset_temp = 0;
  106. long z_offset_temp = 0;
  107. int16_t x, y, z;
  108. x_offset = 0;
  109. y_offset = 0;
  110. z_offset = 0;
  111. getXYZ(&x, &y, &z); //
  112. for (int i = 0; i < samples; i++) {
  113. delay(sampleDelayMS);
  114. getXYZ(&x, &y, &z);
  115. x_offset_temp += x;
  116. y_offset_temp += y;
  117. z_offset_temp += z;
  118. }
  119. x_offset = abs(x_offset_temp) / samples;
  120. y_offset = abs(y_offset_temp) / samples;
  121. z_offset = abs(z_offset_temp) / samples;
  122. if (x_offset_temp > 0) {
  123. x_offset = -x_offset;
  124. }
  125. if (y_offset_temp > 0) {
  126. y_offset = -y_offset;
  127. }
  128. if (z_offset_temp > 0) {
  129. z_offset = -z_offset;
  130. }
  131. }