|
| 1 | +// I2C device class (I2Cdev) demonstration Arduino sketch for MPU6050 class |
| 2 | +// 10/7/2011 by Jeff Rowberg <[email protected]> |
| 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 | +#include "MPU6050.h" |
| 35 | +#include "I2Cdev.h" |
| 36 | + |
| 37 | +// class default I2C address is 0x68 |
| 38 | +// specific I2C addresses may be passed as a parameter here |
| 39 | +// AD0 low = 0x68 (default for InvenSense evaluation board) |
| 40 | +// AD0 high = 0x69 |
| 41 | +MPU6050 accelgyro; |
| 42 | +//MPU6050 accelgyro(0x69); // <-- use for AD0 high |
| 43 | + |
| 44 | +int16_t ax, ay, az; |
| 45 | +int16_t gx, gy, gz; |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | +// uncomment "OUTPUT_READABLE_ACCELGYRO" if you want to see a tab-separated |
| 50 | +// list of the accel X/Y/Z and then gyro X/Y/Z values in decimal. Easy to read, |
| 51 | +// not so easy to parse, and slow(er) over UART. |
| 52 | +#define OUTPUT_READABLE_ACCELGYRO |
| 53 | + |
| 54 | +// uncomment "OUTPUT_BINARY_ACCELGYRO" to send all 6 axes of data as 16-bit |
| 55 | +// binary, one right after the other. This is very fast (as fast as possible |
| 56 | +// without compression or data loss), and easy to parse, but impossible to read |
| 57 | +// for a human. |
| 58 | +//#define OUTPUT_BINARY_ACCELGYRO |
| 59 | + |
| 60 | + |
| 61 | +#define LED_PIN D7 |
| 62 | +bool blinkState = false; |
| 63 | + |
| 64 | +// I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files |
| 65 | +// for both classes must be in the include path of your project |
| 66 | + |
| 67 | +void mpu_raw_setup() { |
| 68 | + // join I2C bus (I2Cdev library doesn't do this automatically) |
| 69 | + Wire.begin(); |
| 70 | + |
| 71 | + |
| 72 | + // initialize serial communication |
| 73 | + // (38400 chosen because it works as well at 8MHz as it does at 16MHz, but |
| 74 | + // it's really up to you depending on your project) |
| 75 | + Serial.begin(38400); |
| 76 | + |
| 77 | + // initialize device |
| 78 | + Serial.println("Initializing I2C devices..."); |
| 79 | + accelgyro.initialize(); |
| 80 | + |
| 81 | + // verify connection |
| 82 | + Serial.println("Testing device connections..."); |
| 83 | + Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed"); |
| 84 | + |
| 85 | + // use the code below to change accel/gyro offset values |
| 86 | + /* |
| 87 | + Serial.println("Updating internal sensor offsets..."); |
| 88 | + // -76 -2359 1688 0 0 0 |
| 89 | + Serial.print(accelgyro.getXAccelOffset()); Serial.print("\t"); // -76 |
| 90 | + Serial.print(accelgyro.getYAccelOffset()); Serial.print("\t"); // -2359 |
| 91 | + Serial.print(accelgyro.getZAccelOffset()); Serial.print("\t"); // 1688 |
| 92 | + Serial.print(accelgyro.getXGyroOffset()); Serial.print("\t"); // 0 |
| 93 | + Serial.print(accelgyro.getYGyroOffset()); Serial.print("\t"); // 0 |
| 94 | + Serial.print(accelgyro.getZGyroOffset()); Serial.print("\t"); // 0 |
| 95 | + Serial.print("\n"); |
| 96 | + accelgyro.setXGyroOffset(220); |
| 97 | + accelgyro.setYGyroOffset(76); |
| 98 | + accelgyro.setZGyroOffset(-85); |
| 99 | + Serial.print(accelgyro.getXAccelOffset()); Serial.print("\t"); // -76 |
| 100 | + Serial.print(accelgyro.getYAccelOffset()); Serial.print("\t"); // -2359 |
| 101 | + Serial.print(accelgyro.getZAccelOffset()); Serial.print("\t"); // 1688 |
| 102 | + Serial.print(accelgyro.getXGyroOffset()); Serial.print("\t"); // 0 |
| 103 | + Serial.print(accelgyro.getYGyroOffset()); Serial.print("\t"); // 0 |
| 104 | + Serial.print(accelgyro.getZGyroOffset()); Serial.print("\t"); // 0 |
| 105 | + Serial.print("\n"); |
| 106 | + */ |
| 107 | + |
| 108 | + // configure Arduino LED for |
| 109 | + pinMode(LED_PIN, OUTPUT); |
| 110 | +} |
| 111 | + |
| 112 | +void mpu_raw_loop() { |
| 113 | + // read raw accel/gyro measurements from device |
| 114 | + accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); |
| 115 | + |
| 116 | + // these methods (and a few others) are also available |
| 117 | + //accelgyro.getAcceleration(&ax, &ay, &az); |
| 118 | + //accelgyro.getRotation(&gx, &gy, &gz); |
| 119 | + |
| 120 | + #ifdef OUTPUT_READABLE_ACCELGYRO |
| 121 | + // display tab-separated accel/gyro x/y/z values |
| 122 | + Serial.print("a/g:\t"); |
| 123 | + Serial.print(ax); Serial.print("\t"); |
| 124 | + Serial.print(ay); Serial.print("\t"); |
| 125 | + Serial.print(az); Serial.print("\t"); |
| 126 | + Serial.print(gx); Serial.print("\t"); |
| 127 | + Serial.print(gy); Serial.print("\t"); |
| 128 | + Serial.println(gz); |
| 129 | + #endif |
| 130 | + |
| 131 | + #ifdef OUTPUT_BINARY_ACCELGYRO |
| 132 | + Serial.write((uint8_t)(ax >> 8)); Serial.write((uint8_t)(ax & 0xFF)); |
| 133 | + Serial.write((uint8_t)(ay >> 8)); Serial.write((uint8_t)(ay & 0xFF)); |
| 134 | + Serial.write((uint8_t)(az >> 8)); Serial.write((uint8_t)(az & 0xFF)); |
| 135 | + Serial.write((uint8_t)(gx >> 8)); Serial.write((uint8_t)(gx & 0xFF)); |
| 136 | + Serial.write((uint8_t)(gy >> 8)); Serial.write((uint8_t)(gy & 0xFF)); |
| 137 | + Serial.write((uint8_t)(gz >> 8)); Serial.write((uint8_t)(gz & 0xFF)); |
| 138 | + #endif |
| 139 | + |
| 140 | + // blink LED to indicate activity |
| 141 | + blinkState = !blinkState; |
| 142 | + digitalWrite(LED_PIN, blinkState); |
| 143 | +} |
0 commit comments