Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.22 KB | None | 0 0
  1. // I2C device class (I2Cdev) demonstration Arduino sketch for MPU6050 class
  2. // 10/7/2011 by Jeff Rowberg <jeff@rowberg.net>
  3. // Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
  4. //
  5. // Changelog:
  6. // 2013-05-08 - added multiple output formats
  7. // - added seamless Fastwire support
  8. // 2011-10-07 - initial release
  9.  
  10. /* ============================================
  11. I2Cdev device library code is placed under the MIT license
  12. Copyright (c) 2011 Jeff Rowberg
  13.  
  14. Permission is hereby granted, free of charge, to any person obtaining a copy
  15. of this software and associated documentation files (the "Software"), to deal
  16. in the Software without restriction, including without limitation the rights
  17. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  18. copies of the Software, and to permit persons to whom the Software is
  19. furnished to do so, subject to the following conditions:
  20.  
  21. The above copyright notice and this permission notice shall be included in
  22. all copies or substantial portions of the Software.
  23.  
  24. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  25. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  27. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  28. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  29. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  30. THE SOFTWARE.
  31. ===============================================
  32. */
  33.  
  34. // I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files
  35. // for both classes must be in the include path of your project
  36. #include "I2Cdev.h"
  37. #include "MPU6050.h"
  38.  
  39. // Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation
  40. // is used in I2Cdev.h
  41. #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
  42. #include "Wire.h"
  43. #endif
  44.  
  45. // class default I2C address is 0x68
  46. // specific I2C addresses may be passed as a parameter here
  47. // AD0 low = 0x68 (default for InvenSense evaluation board)
  48. // AD0 high = 0x69
  49. MPU6050 accelgyro;
  50. //MPU6050 accelgyro(0x69); // <-- use for AD0 high
  51.  
  52. int16_t ax, ay, az;
  53. int16_t gx, gy, gz;
  54.  
  55.  
  56.  
  57. // uncomment "OUTPUT_READABLE_ACCELGYRO" if you want to see a tab-separated
  58. // list of the accel X/Y/Z and then gyro X/Y/Z values in decimal. Easy to read,
  59. // not so easy to parse, and slow(er) over UART.
  60. #define OUTPUT_READABLE_ACCELGYRO
  61.  
  62. // uncomment "OUTPUT_BINARY_ACCELGYRO" to send all 6 axes of data as 16-bit
  63. // binary, one right after the other. This is very fast (as fast as possible
  64. // without compression or data loss), and easy to parse, but impossible to read
  65. // for a human.
  66. //#define OUTPUT_BINARY_ACCELGYRO
  67.  
  68.  
  69. #define LED_PIN 13
  70. bool blinkState = false;
  71.  
  72. void setup() {
  73. // join I2C bus (I2Cdev library doesn't do this automatically)
  74. #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
  75. Wire.begin();
  76. #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
  77. Fastwire::setup(400, true);
  78. #endif
  79.  
  80. // initialize serial communication
  81. // (38400 chosen because it works as well at 8MHz as it does at 16MHz, but
  82. // it's really up to you depending on your project)
  83. Serial.begin(38400);
  84.  
  85. // initialize device
  86. //Serial.println("Initializing I2C devices...");
  87. //accelgyro.initialize();
  88.  
  89. // verify connection
  90. //Serial.println("Testing device connections...");
  91. //Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
  92.  
  93. // use the code below to change accel/gyro offset values
  94. /*
  95. //Serial.println("Updating internal sensor offsets...");
  96. // -76 -2359 1688 0 0 0
  97. //Serial.print(accelgyro.getXAccelOffset()); Serial.print("\t"); // -76
  98. //Serial.print(accelgyro.getYAccelOffset()); Serial.print("\t"); // -2359
  99. //Serial.print(accelgyro.getZAccelOffset()); Serial.print("\t"); // 1688
  100. //Serial.print(accelgyro.getXGyroOffset()); Serial.print("\t"); // 0
  101. //Serial.print(accelgyro.getYGyroOffset()); Serial.print("\t"); // 0
  102. //Serial.print(accelgyro.getZGyroOffset()); Serial.print("\t"); // 0
  103. //Serial.print("\n");
  104. //accelgyro.setXGyroOffset(220);
  105. //accelgyro.setYGyroOffset(76);
  106. //accelgyro.setZGyroOffset(-85);
  107. //Serial.print(accelgyro.getXAccelOffset()); Serial.print("\t"); // -76
  108. //Serial.print(accelgyro.getYAccelOffset()); Serial.print("\t"); // -2359
  109. //Serial.print(accelgyro.getZAccelOffset()); Serial.print("\t"); // 1688
  110. //Serial.print(accelgyro.getXGyroOffset()); Serial.print("\t"); // 0
  111. //Serial.print(accelgyro.getYGyroOffset()); Serial.print("\t"); // 0
  112. //Serial.print(accelgyro.getZGyroOffset()); Serial.print("\t"); // 0
  113. //Serial.print("\n");
  114. */
  115.  
  116. // configure Arduino LED for
  117. pinMode(LED_PIN, OUTPUT);
  118. }
  119.  
  120. void loop() {
  121. // read raw accel/gyro measurements from device
  122. accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  123.  
  124. // these methods (and a few others) are also available
  125. //accelgyro.getAcceleration(&ax, &ay, &az);
  126. //accelgyro.getRotation(&gx, &gy, &gz);
  127.  
  128. #ifdef OUTPUT_READABLE_ACCELGYRO
  129. // display tab-separated accel/gyro x/y/z values
  130. //Serial.print("a/g:\t");
  131. //Serial.print(ax); Serial.print("\t");
  132. Serial.print(ay); Serial.print("\t");
  133. //Serial.print(az); Serial.print("\t");
  134. //Serial.print(gx); Serial.print("\t");
  135. //Serial.print(gy); Serial.print("\t");
  136. //Serial.println(gz);
  137. #endif
  138.  
  139. #ifdef OUTPUT_BINARY_ACCELGYRO
  140. Serial.write((uint8_t)(ax >> 8)); Serial.write((uint8_t)(ax & 0xFF));
  141. Serial.write((uint8_t)(ay >> 8)); Serial.write((uint8_t)(ay & 0xFF));
  142. Serial.write((uint8_t)(az >> 8)); Serial.write((uint8_t)(az & 0xFF));
  143. Serial.write((uint8_t)(gx >> 8)); Serial.write((uint8_t)(gx & 0xFF));
  144. Serial.write((uint8_t)(gy >> 8)); Serial.write((uint8_t)(gy & 0xFF));
  145. Serial.write((uint8_t)(gz >> 8)); Serial.write((uint8_t)(gz & 0xFF));
  146. #endif
  147.  
  148. // blink LED to indicate activity
  149. blinkState = !blinkState;
  150. digitalWrite(LED_PIN, blinkState);
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement