BrickUp API Service for Docker version.

ITG3200.cpp 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. {
  34. int8_t data;
  35. Wire.beginTransmission(GYRO_ADDRESS);
  36. Wire.write(_register);
  37. Wire.endTransmission();
  38. Wire.requestFrom(GYRO_ADDRESS, 1);
  39. if(Wire.available() > 0)
  40. {
  41. data = Wire.read();
  42. }
  43. Wire.endTransmission();
  44. return data;
  45. }
  46. /*Function: Write a byte to the register of the MMA7660*/
  47. void ITG3200::write(uint8_t _register, uint8_t _data)
  48. {
  49. Wire.begin();
  50. Wire.beginTransmission(GYRO_ADDRESS);
  51. Wire.write(_register);
  52. Wire.write(_data);
  53. Wire.endTransmission();
  54. }
  55. /**********************************************************************/
  56. /*Function: Initialization for ITG3200. */
  57. void ITG3200::init()
  58. {
  59. Wire.begin();
  60. write(ITG3200_PWR_M,0x80);//send a reset to the device
  61. write(ITG3200_SMPL,0x00);//sample rate divider
  62. write(ITG3200_DLPF,0x18);//+/-2000 degrees/s (default value)
  63. }
  64. int16_t ITG3200::read(uint8_t addressh, uint8_t addressl)
  65. {
  66. int data, t_data;
  67. Wire.beginTransmission(GYRO_ADDRESS);
  68. Wire.write(addressh);
  69. Wire.endTransmission();
  70. Wire.requestFrom(GYRO_ADDRESS, 1);
  71. if(Wire.available() > 0)
  72. {
  73. t_data = Wire.read();
  74. data = t_data << 8;
  75. }
  76. Wire.beginTransmission(GYRO_ADDRESS);
  77. Wire.write(addressl);
  78. Wire.endTransmission();
  79. Wire.requestFrom(GYRO_ADDRESS, 1);
  80. if(Wire.available() > 0)
  81. {
  82. data |= Wire.read();
  83. }
  84. return data;
  85. }
  86. /*Function: Get the temperature from ITG3200 that with a on-chip*/
  87. /* temperature sensor. */
  88. double ITG3200::getTemperature()
  89. {
  90. int temp;
  91. double temperature;
  92. temp = read(ITG3200_TMP_H, ITG3200_TMP_L);
  93. temperature = 35+ ((double) (temp + 13200)) / 280;
  94. return(temperature);
  95. }
  96. /*Function: Get the contents of the registers in the ITG3200*/
  97. /* so as to calculate the angular velocity. */
  98. void ITG3200::getXYZ(int16_t *x,int16_t *y,int16_t *z)
  99. {
  100. *x = read(ITG3200_GX_H,ITG3200_GX_L)+x_offset;
  101. *y = read(ITG3200_GY_H,ITG3200_GY_L)+y_offset;
  102. *z = read(ITG3200_GZ_H,ITG3200_GZ_L)+z_offset;
  103. }
  104. /*Function: Get the angular velocity and its unit is degree per second.*/
  105. void ITG3200::getAngularVelocity(float *ax,float *ay,float *az)
  106. {
  107. int16_t x,y,z;
  108. getXYZ(&x,&y,&z);
  109. *ax = x/14.375;
  110. *ay = y/14.375;
  111. *az = z/14.375;
  112. }
  113. void ITG3200::zeroCalibrate(unsigned int samples, unsigned int sampleDelayMS)
  114. {
  115. int16_t x_offset_temp = 0;
  116. int16_t y_offset_temp = 0;
  117. int16_t z_offset_temp = 0;
  118. int16_t x,y,z;
  119. x_offset = 0;
  120. y_offset = 0;
  121. z_offset = 0;
  122. getXYZ(&x,&y,&z);//
  123. for (int i = 0;i < samples;i++){
  124. delay(sampleDelayMS);
  125. getXYZ(&x,&y,&z);
  126. x_offset_temp += x;
  127. y_offset_temp += y;
  128. z_offset_temp += z;
  129. }
  130. x_offset = abs(x_offset_temp)/samples;
  131. y_offset = abs(y_offset_temp)/samples;
  132. z_offset = abs(z_offset_temp)/samples;
  133. if(x_offset_temp > 0)x_offset = -x_offset;
  134. if(y_offset_temp > 0)y_offset = -y_offset;
  135. if(z_offset_temp > 0)z_offset = -z_offset;
  136. }