Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.97 KB | None | 0 0
  1. //Interface MPU6050 Accelerometer and Gyroscope to Arduino Uno and display
  2. //gyroscope data on the Serial Monitor. This example is intended to show
  3. //the drift caused by the approximated integration of the angular velocity.
  4.  
  5. /*Copyright (c) 2019, ProteShea LLC
  6. All rights reserved.
  7.  
  8. Redistribution and use in source and binary forms, with or without
  9. modification, are permitted provided that the following conditions are met:
  10. 1. Redistributions of source code must retain the above copyright
  11. notice, this list of conditions and the following disclaimer.
  12. 2. Redistributions in binary form must reproduce the above copyright
  13. notice, this list of conditions and the following disclaimer in the
  14. documentation and/or other materials provided with the distribution.
  15. 3. Neither the name of the copyright holders nor the
  16. names of its contributors may be used to endorse or promote products
  17. derived from this software without specific prior written permission.
  18.  
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
  20. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
  23. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30.  
  31. #include <Wire.h>
  32.  
  33. #define MPUaddr0 0x68 //addr used if pin AD0 is set to 0 (left unconnected)
  34. #define MPUaddr1 0x69 //addr used if pin AD0 is set to 1 (wired to VDD)
  35.  
  36. float GyroX, GyroY, GyroZ = 0;
  37. float gyroXangle, gyroYangle, gyroZangle = 0;
  38. float yaw, pitch, roll = 0;
  39. float lastTime, realTime, Ts = 0;
  40.  
  41. void setup() {
  42. Serial.begin(115200); //set baud rate to 19200 for serial transmission
  43. Wire.begin();
  44. resetMPU(); //reset MPU to default settings
  45. setGyroSensitivity(0x00); //programmable range of +/-250, +/-500, +/-1000, +/-2000 DPS
  46. delay(10000); //wait for the device to stabilize
  47. }
  48.  
  49. void loop() {
  50. readGyro(131.0); //read XYZ Gyro data from registers 0x43 to 0x48
  51. displaySerial(); //display yaw, pitch, and roll on Serial Monitor
  52. delay(10);
  53.  
  54. }
  55.  
  56.  
  57. void displaySerial(void){
  58. Serial.print("Yaw: ");
  59. Serial.print(gyroZangle);
  60. Serial.print(" Pitch: ");
  61. Serial.print(gyroYangle);
  62. Serial.print(" Roll: ");
  63. Serial.println(gyroXangle);
  64.  
  65. }
  66.  
  67.  
  68. void readGyro(float gyroDivisor){
  69. // +/-250 dps, use divisor of 131
  70. // +/-500 dps, use divisor of 65.5
  71. // +/- 1000 dps, use divisor of 32.8
  72. // +/- 2000 dps, use divisor of 16.4
  73.  
  74. //Time calculations required since Gyro data returned with units of degrees/sec and we just need degrees
  75. //Have to calculate time elapsed between successive reads of Gyro data
  76. lastTime = realTime;
  77. realTime = millis();
  78. Ts = (realTime - lastTime) / 1000;
  79.  
  80. Wire.beginTransmission(MPUaddr0);
  81. Wire.write(0x43);
  82. Wire.endTransmission();
  83. Wire.requestFrom(MPUaddr0, 6); //read 6 consecutive registers starting at 0x43
  84. if (Wire.available() >= 6){
  85.  
  86. int16_t temp0 = Wire.read() << 8; //read upper byte of X
  87. int16_t temp1 = Wire.read(); //read lower byte of X
  88. GyroX = (float) (temp0 | temp1);
  89. GyroX = GyroX / gyroDivisor;
  90.  
  91. temp0 = Wire.read() << 8; //read upper byte of Y
  92. temp1 = Wire.read(); //read lower byte of Y
  93. GyroY = (float) (temp0 | temp1);
  94. GyroY = GyroY / gyroDivisor;
  95.  
  96. temp0 = Wire.read() << 8; //read upper byte of Z
  97. temp1 = Wire.read(); //read lower byte of Z
  98. GyroZ = (float) (temp0 | temp1);
  99. GyroZ = GyroZ / gyroDivisor;
  100.  
  101. }
  102. //Gyro data is given as angular velocity (degrees/sec) so to get position (degrees)
  103. //we have to integrate the angular velocity which is approximated by taking the sum
  104. //of a finite number of samples (2 in this case) taken at an interval Ts
  105. gyroXangle += GyroX * Ts;
  106. gyroYangle += GyroY * Ts;
  107. gyroZangle += GyroZ * Ts;
  108.  
  109. }
  110.  
  111.  
  112. void setGyroSensitivity(uint8_t dps){
  113. //Config FS_SEL[1:0] bits 4 and 3 in register 0x1B
  114. //0x00: +/-250 dps (default)
  115. //0x08: +/-500 dps
  116. //0x10: +/-1000 dps
  117. //0x18: +/-2000 dps
  118.  
  119. Wire.beginTransmission(MPUaddr0); //initialize comm with MPU @ 0x68
  120. Wire.write(0x1B); //write to register 0x1B
  121. Wire.write(dps); //setting bit 7 to 1 resets all internal registers to default values
  122. Wire.endTransmission(); //end comm
  123. }
  124.  
  125. void resetMPU(void){
  126. Wire.beginTransmission(MPUaddr0); //initialize comm with MPU @ 0x68
  127. Wire.write(0x6B); //write to register 0x6B
  128. Wire.write(0x00); //reset all internal registers to default values
  129. Wire.endTransmission(); //end comm
  130. delay(100);
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement