|
| 1 | +// I2C device class (I2Cdev) demonstration Arduino sketch for MPU9150 |
| 2 | +// 1/4/2013 original by Jeff Rowberg <[email protected]> at https://github.com/jrowberg/i2cdevlib |
| 3 | +// modified by Aaron Weiss <[email protected]> |
| 4 | +// |
| 5 | +// Changelog: |
| 6 | +// 2011-10-07 - initial release |
| 7 | +// 2013-1-4 - added raw magnetometer output |
| 8 | + |
| 9 | +/* ============================================ |
| 10 | +I2Cdev device library code is placed under the MIT license |
| 11 | +
|
| 12 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 13 | +of this software and associated documentation files (the "Software"), to deal |
| 14 | +in the Software without restriction, including without limitation the rights |
| 15 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 16 | +copies of the Software, and to permit persons to whom the Software is |
| 17 | +furnished to do so, subject to the following conditions: |
| 18 | +
|
| 19 | +The above copyright notice and this permission notice shall be included in |
| 20 | +all copies or substantial portions of the Software. |
| 21 | +
|
| 22 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 23 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 24 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 25 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 26 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 27 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 28 | +THE SOFTWARE. |
| 29 | +=============================================== |
| 30 | +*/ |
| 31 | + |
| 32 | +// Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation |
| 33 | +// is used in I2Cdev.h |
| 34 | +#include "Wire.h" |
| 35 | + |
| 36 | +// I2Cdev and MPU9150 must be installed as libraries, or else the .cpp/.h files |
| 37 | +// for both classes must be in the include path of your project |
| 38 | +#include "I2Cdev.h" |
| 39 | +#include "MPU9150.h" |
| 40 | +#include "helper_3dmath.h" |
| 41 | + |
| 42 | +// class default I2C address is 0x68 |
| 43 | +// specific I2C addresses may be passed as a parameter here |
| 44 | +// AD0 low = 0x68 (default for InvenSense evaluation board) |
| 45 | +// AD0 high = 0x69 |
| 46 | +MPU9150 accelGyroMag; |
| 47 | + |
| 48 | +int16_t ax, ay, az; |
| 49 | +int16_t gx, gy, gz; |
| 50 | +int16_t mx, my, mz; |
| 51 | + |
| 52 | +#define LED_PIN 13 |
| 53 | +bool blinkState = false; |
| 54 | + |
| 55 | +void setup() { |
| 56 | + // join I2C bus (I2Cdev library doesn't do this automatically) |
| 57 | + Wire.begin(); |
| 58 | + |
| 59 | + // initialize serial communication |
| 60 | + // (38400 chosen because it works as well at 8MHz as it does at 16MHz, but |
| 61 | + // it's really up to you depending on your project) |
| 62 | + Serial.begin(115200); |
| 63 | + |
| 64 | + // initialize device |
| 65 | + Serial.println("Initializing I2C devices..."); |
| 66 | + accelGyroMag.initialize(); |
| 67 | + |
| 68 | + // verify connection |
| 69 | + Serial.println("Testing device connections..."); |
| 70 | + Serial.println(accelGyroMag.testConnection() ? "MPU9150 connection successful" : "MPU9150 connection failed"); |
| 71 | + |
| 72 | + // configure Arduino LED for |
| 73 | + pinMode(LED_PIN, OUTPUT); |
| 74 | +} |
| 75 | + |
| 76 | +void loop() { |
| 77 | + // read raw accel/gyro/mag measurements from device |
| 78 | + accelGyroMag.getMotion9(&ax, &ay, &az, &gx, &gy, &gz, &mx, &my, &mz); |
| 79 | + |
| 80 | + // these methods (and a few others) are also available |
| 81 | + //accelGyroMag.getAcceleration(&ax, &ay, &az); |
| 82 | + //accelGyroMag.getRotation(&gx, &gy, &gz); |
| 83 | + |
| 84 | + // display tab-separated accel/gyro/mag x/y/z values |
| 85 | +// Serial.print("a/g/m:\t"); |
| 86 | +// Serial.print(ax); Serial.print("\t"); |
| 87 | +// Serial.print(ay); Serial.print("\t"); |
| 88 | +// Serial.print(az); Serial.print("\t"); |
| 89 | +// Serial.print(gx); Serial.print("\t"); |
| 90 | +// Serial.print(gy); Serial.print("\t"); |
| 91 | +// Serial.print(gz); Serial.print("\t"); |
| 92 | + Serial.print(int(mx)*int(mx)); Serial.print("\t"); |
| 93 | + Serial.print(int(my)*int(my)); Serial.print("\t"); |
| 94 | + Serial.print(int(mz)*int(mz)); Serial.print("\t | "); |
| 95 | + |
| 96 | + const float N = 256; |
| 97 | + float mag = mx*mx/N + my*my/N + mz*mz/N; |
| 98 | + |
| 99 | + Serial.print(mag); Serial.print("\t"); |
| 100 | + for (int i=0; i<mag; i++) |
| 101 | + Serial.print("*"); |
| 102 | + Serial.print("\n"); |
| 103 | + |
| 104 | + // blink LED to indicate activity |
| 105 | + blinkState = !blinkState; |
| 106 | + digitalWrite(LED_PIN, blinkState); |
| 107 | + delay(50); |
| 108 | +} |
0 commit comments