ulvis.paste.net

Paste Search Dynamic
Recent pastes
path of your project
  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.  
  74.   pinMode(2, OUTPUT);
  75.   pinMode(3, OUTPUT);
  76.   pinMode(4, OUTPUT);
  77.   pinMode(5, OUTPUT);
  78.  
  79.   digitalWrite(2, 0);
  80.   digitalWrite(3, 0);
  81.   digitalWrite(4, 0);
  82.   digitalWrite(5, 0);
  83.   // join I2C bus (I2Cdev library doesn't do this automatically)
  84. #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
  85.   Wire.begin();
  86. #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
  87.   Fastwire::setup(400, true);
  88. #endif
  89.  
  90.   // initialize serial communication
  91.   // (38400 chosen because it works as well at 8MHz as it does at 16MHz, but
  92.   // it's really up to you depending on your project)
  93.   Serial.begin(38400);
  94.  
  95.   // initialize device
  96.   Serial.println("Initializing I2C devices...");
  97.   accelgyro.initialize();
  98.  
  99.   // verify connection
  100.   Serial.println("Testing device connections...");
  101.   Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
  102.  
  103.   // use the code below to change accel/gyro offset values
  104.   /*
  105.     Serial.println("Updating internal sensor offsets...");
  106.     // -76  -2359   1688    0   0   0
  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.     accelgyro.setXGyroOffset(220);
  115.     accelgyro.setYGyroOffset(76);
  116.     accelgyro.setZGyroOffset(-85);
  117.     Serial.print(accelgyro.getXAccelOffset()); Serial.print("\t"); // -76
  118.     Serial.print(accelgyro.getYAccelOffset()); Serial.print("\t"); // -2359
  119.     Serial.print(accelgyro.getZAccelOffset()); Serial.print("\t"); // 1688
  120.     Serial.print(accelgyro.getXGyroOffset()); Serial.print("\t"); // 0
  121.     Serial.print(accelgyro.getYGyroOffset()); Serial.print("\t"); // 0
  122.     Serial.print(accelgyro.getZGyroOffset()); Serial.print("\t"); // 0
  123.     Serial.print("\n");
  124.   */
  125.  
  126.   // configure Arduino LED pin for output
  127.   pinMode(LED_PIN, OUTPUT);
  128. }
  129.  
  130. void loop() {
  131.   // read raw accel/gyro measurements from device
  132.   accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  133.  
  134.   // these methods (and a few others) are also available
  135.   //accelgyro.getAcceleration(&ax, &ay, &az);
  136.   //accelgyro.getRotation(&gx, &gy, &gz);
  137.  
  138. #ifdef OUTPUT_READABLE_ACCELGYRO
  139.   // display tab-separated accel/gyro x/y/z values
  140.   Serial.print("a/g:\t");
  141.   Serial.print(ax); Serial.print("\t");
  142.   Serial.print(ay); Serial.print("\t");
  143.   Serial.println(az); Serial.print("\t");
  144.   // Serial.print(gx); Serial.print("\t");
  145.   // Serial.print(gy); Serial.print("\t");
  146.   // Serial.println(gz);
  147. #endif
  148.  
  149. #ifdef OUTPUT_BINARY_ACCELGYRO
  150.   Serial.write((uint8_t)(ax >> 8)); Serial.write((uint8_t)(ax & 0xFF));
  151.   Serial.write((uint8_t)(ay >> 8)); Serial.write((uint8_t)(ay & 0xFF));
  152.   Serial.write((uint8_t)(az >> 8)); Serial.write((uint8_t)(az & 0xFF));
  153.   //Serial.write((uint8_t)(gx >> 8)); Serial.write((uint8_t)(gx & 0xFF));
  154.   //Serial.write((uint8_t)(gy >> 8)); Serial.write((uint8_t)(gy & 0xFF));
  155.   //Serial.write((uint8_t)(gz >> 8)); Serial.write((uint8_t)(gz & 0xFF));
  156. #endif
  157.  
  158.   // blink LED to indicate activity
  159.   blinkState = !blinkState;
  160.   digitalWrite(LED_PIN, blinkState);
  161.   if (ax < 1000 && ay < -8000) {
  162.     digitalWrite(2, 1);
  163.     digitalWrite(3, 0);
  164.     digitalWrite(4, 0);
  165.     digitalWrite(5, 0);
  166.   }
  167.   else if (ax < 1000 && ay > 8000) {
  168.     digitalWrite(2, 0);
  169.     digitalWrite(3, 1);
  170.     digitalWrite(4, 0);
  171.     digitalWrite(5, 0);
  172.   }
  173.  
  174.   else if (ax > 8000 && ay < 1000) {
  175.     digitalWrite(2, 0);
  176.     digitalWrite(3, 0);
  177.     digitalWrite(4, 1);
  178.     digitalWrite(5, 0);
  179.   }
  180.  
  181.   else if (ax < -8000 && ay  < 1000) {
  182.     digitalWrite(2, 0);
  183.     digitalWrite(3, 0);
  184.     digitalWrite(4, 0);
  185.     digitalWrite(5, 1);
  186.   }
  187.   else {
  188.     digitalWrite(2, 0);
  189.     digitalWrite(3, 0);
  190.     digitalWrite(4, 0);
  191.     digitalWrite(5, 0);
  192.   }
  193.  
  194.   //delay(100);
  195. }
Parsed in 0.015 seconds