Advertisement
JaiParth

Untitled

Apr 11th, 2019
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.09 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.  
  6.  
  7. // I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files
  8. // for both classes must be in the include path of your project
  9. #include "I2Cdev.h"
  10. #include "MPU6050.h"
  11.  
  12. // Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation
  13. // is used in I2Cdev.h
  14. #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
  15.     #include "Wire.h"
  16. #endif
  17.  
  18. // class default I2C address is 0x68
  19. // specific I2C addresses may be passed as a parameter here
  20. // AD0 low = 0x68 (default for InvenSense evaluation board)
  21. // AD0 high = 0x69
  22. MPU6050 accelgyro;
  23. //MPU6050 accelgyro(0x69); // <-- use for AD0 high
  24.  
  25. int16_t ax, ay, az;
  26. int16_t gx, gy, gz;
  27.  
  28.  
  29.  
  30. // uncomment "OUTPUT_READABLE_ACCELGYRO" if you want to see a tab-separated
  31. // list of the accel X/Y/Z and then gyro X/Y/Z values in decimal. Easy to read,
  32. // not so easy to parse, and slow(er) over UART.
  33. #define OUTPUT_READABLE_ACCELGYRO
  34.  
  35. // uncomment "OUTPUT_BINARY_ACCELGYRO" to send all 6 axes of data as 16-bit
  36. // binary, one right after the other. This is very fast (as fast as possible
  37. // without compression or data loss), and easy to parse, but impossible to read
  38. // for a human.
  39. //#define OUTPUT_BINARY_ACCELGYRO
  40.  
  41.  
  42. #define LED_PIN 13
  43. bool blinkState = false;
  44.  
  45. void setup() {
  46.     // join I2C bus (I2Cdev library doesn't do this automatically)
  47.     #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
  48.         Wire.begin();
  49.     #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
  50.         Fastwire::setup(400, true);
  51.     #endif
  52.  
  53.     // initialize serial communication
  54.     // (38400 chosen because it works as well at 8MHz as it does at 16MHz, but
  55.     // it's really up to you depending on your project)
  56.     Serial.begin(115200);
  57.  
  58.     // initialize device
  59.     Serial.println("Initializing I2C devices...");
  60.     accelgyro.initialize();
  61.     accelgyro.setFullScaleAccelRange(3);
  62.     // verify connection
  63.     Serial.println("Testing device connections...");
  64.     Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
  65.  
  66.     // use the code below to change accel/gyro offset values
  67.     
  68.     Serial.println("Updating internal sensor offsets...");
  69.     // -76 -2359 1688 0 0 0
  70.     Serial.print(accelgyro.getXAccelOffset()); Serial.print("\t"); // -76
  71.     Serial.print(accelgyro.getYAccelOffset()); Serial.print("\t"); // -2359
  72.     Serial.print(accelgyro.getZAccelOffset()); Serial.print("\t"); // 1688
  73.     Serial.print(accelgyro.getXGyroOffset()); Serial.print("\t"); // 0
  74.     Serial.print(accelgyro.getYGyroOffset()); Serial.print("\t"); // 0
  75.     Serial.print(accelgyro.getZGyroOffset()); Serial.print("\t"); // 0
  76.     Serial.print("\n");
  77.     accelgyro.setXGyroOffset(110);
  78.     accelgyro.setYGyroOffset(3);
  79.     accelgyro.setZGyroOffset(-1);
  80.     accelgyro.setZAccelOffset(1566);
  81.     accelgyro.setXAccelOffset(-1011);
  82.     accelgyro.setYAccelOffset(-4102);
  83.     Serial.print(accelgyro.getXAccelOffset()); Serial.print("\t"); // -76
  84.     Serial.print(accelgyro.getYAccelOffset()); Serial.print("\t"); // -2359
  85.     Serial.print(accelgyro.getZAccelOffset()); Serial.print("\t"); // 1688
  86.     Serial.print(accelgyro.getXGyroOffset()); Serial.print("\t"); // 0
  87.     Serial.print(accelgyro.getYGyroOffset()); Serial.print("\t"); // 0
  88.     Serial.print(accelgyro.getZGyroOffset()); Serial.print("\t"); // 0
  89.     Serial.print("\n");
  90.  
  91.     // configure Arduino LED pin for output
  92.     pinMode(LED_PIN, OUTPUT);
  93. }
  94.  
  95. void loop() {
  96.     // read raw accel/gyro measurements from device
  97.     accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  98.  
  99.     // these methods (and a few others) are also available
  100.     //accelgyro.getAcceleration(&ax, &ay, &az);
  101.     //accelgyro.getRotation(&gx, &gy, &gz);
  102.  
  103.     #ifdef OUTPUT_READABLE_ACCELGYRO
  104.         // display tab-separated accel/gyro x/y/z values
  105.         Serial.print("a/g:\t");
  106.         Serial.print(ax); Serial.print("\t");
  107.         Serial.print(ay); Serial.print("\t");
  108.         Serial.print(az); Serial.print("\t");
  109.         Serial.print(gx); Serial.print("\t");
  110.         Serial.print(gy); Serial.print("\t");
  111.         Serial.println(gz);
  112.     #endif
  113.  
  114.     #ifdef OUTPUT_BINARY_ACCELGYRO
  115.         Serial.write((uint8_t)(ax >> 8)); Serial.write((uint8_t)(ax & 0xFF));
  116.         Serial.write((uint8_t)(ay >> 8)); Serial.write((uint8_t)(ay & 0xFF));
  117.         Serial.write((uint8_t)(az >> 8)); Serial.write((uint8_t)(az & 0xFF));
  118.         Serial.write((uint8_t)(gx >> 8)); Serial.write((uint8_t)(gx & 0xFF));
  119.         Serial.write((uint8_t)(gy >> 8)); Serial.write((uint8_t)(gy & 0xFF));
  120.         Serial.write((uint8_t)(gz >> 8)); Serial.write((uint8_t)(gz & 0xFF));
  121.     #endif
  122.  
  123.     // blink LED to indicate activity
  124.     blinkState = !blinkState;
  125.     digitalWrite(LED_PIN, blinkState);
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement