Advertisement
pleasedontcode

MPU6050 Orientation rev_02

Jun 5th, 2024
546
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: MPU6050 Orientation
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-06-05 21:55:33
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* read gyroscope at maximum rate and show on serial. */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /****** DEFINITION OF LIBRARIES *****/
  24. #include <Wire.h>
  25. #include <MPU6050.h>  // https://github.com/electroniccats/mpu6050
  26.  
  27. /****** FUNCTION PROTOTYPES *****/
  28. void setup(void);
  29. void loop(void);
  30. void dmpDataReady(void);
  31.  
  32. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  33. const uint8_t sensor_MPU6050_Interrupt_PIN_D2 = 2;
  34.  
  35. /***** DEFINITION OF I2C PINS *****/
  36. const uint8_t sensor_MPU6050_I2C_PIN_SDA_A4 = A4;
  37. const uint8_t sensor_MPU6050_I2C_PIN_SCL_A5 = A5;
  38. const uint8_t sensor_MPU6050_I2C_SLAVE_ADDRESS = 104;
  39.  
  40. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  41. MPU6050 mpu(sensor_MPU6050_I2C_SLAVE_ADDRESS);
  42.  
  43. /****** VARIABLES ******/
  44. bool dmpReady = false;  // set true if DMP init was successful
  45. uint8_t mpuIntStatus;   // holds actual interrupt status byte from MPU
  46. uint8_t devStatus;      // return status after each device operation (0 = success, !0 = error)
  47. uint16_t packetSize;    // expected DMP packet size (default is 42 bytes)
  48. uint16_t fifoCount;     // count of all bytes currently in FIFO
  49. uint8_t fifoBuffer[64]; // FIFO storage buffer
  50.  
  51. // orientation/motion vars
  52. Quaternion q;           // [w, x, y, z]         quaternion container
  53. VectorInt16 aa;         // [x, y, z]            accel sensor measurements
  54. VectorInt16 aaReal;     // [x, y, z]            gravity-free accel sensor measurements
  55. VectorInt16 aaWorld;    // [x, y, z]            world-frame accel sensor measurements
  56. VectorFloat gravity;    // [x, y, z]            gravity vector
  57. float ypr[3];           // [yaw, pitch, roll]   yaw/pitch/roll container and gravity vector
  58.  
  59. volatile bool mpuInterrupt = false;     // indicates whether MPU interrupt pin has gone high
  60.  
  61. void dmpDataReady() {
  62.     mpuInterrupt = true;
  63. }
  64.  
  65. void setup(void)
  66. {
  67.     // put your setup code here, to run once:
  68.     Wire.begin(sensor_MPU6050_I2C_PIN_SDA_A4, sensor_MPU6050_I2C_PIN_SCL_A5);
  69.     Wire.setClock(400000); // 400kHz I2C clock. Comment this line if having compilation difficulties
  70.  
  71.     Serial.begin(115200);
  72.     while (!Serial); // wait for Leonardo enumeration, others continue immediately
  73.  
  74.     Serial.println(F("Initializing I2C devices..."));
  75.     mpu.initialize();
  76.     pinMode(sensor_MPU6050_Interrupt_PIN_D2, INPUT);
  77.  
  78.     // verify connection
  79.     Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));
  80.  
  81.     // load and configure the DMP
  82.     Serial.println(F("Initializing DMP..."));
  83.     devStatus = mpu.dmpInitialize();
  84.  
  85.     // supply your own gyro offsets here, scaled for min sensitivity
  86.     mpu.setXGyroOffset(220);
  87.     mpu.setYGyroOffset(76);
  88.     mpu.setZGyroOffset(-85);
  89.     mpu.setZAccelOffset(1788); // 1688 factory default for my test chip
  90.  
  91.     // make sure it worked (returns 0 if so)
  92.     if (devStatus == 0) {
  93.         // turn on the DMP, now that it's ready
  94.         Serial.println(F("Enabling DMP..."));
  95.         mpu.setDMPEnabled(true);
  96.  
  97.         // enable Arduino interrupt detection
  98.         attachInterrupt(digitalPinToInterrupt(sensor_MPU6050_Interrupt_PIN_D2), dmpDataReady, RISING);
  99.         mpuIntStatus = mpu.getIntStatus();
  100.  
  101.         // set our DMP Ready flag so the main loop() function knows it's okay to use it
  102.         Serial.println(F("DMP ready! Waiting for first interrupt..."));
  103.         dmpReady = true;
  104.  
  105.         // get expected DMP packet size for later comparison
  106.         packetSize = mpu.dmpGetFIFOPacketSize();
  107.     } else {
  108.         // ERROR!
  109.         // 1 = initial memory load failed
  110.         // 2 = DMP configuration updates failed
  111.         // (if it's going to break, usually the code will be 1)
  112.         Serial.print(F("DMP Initialization failed (code "));
  113.         Serial.print(devStatus);
  114.         Serial.println(F(")"));
  115.     }
  116. }
  117.  
  118. void loop(void)
  119. {
  120.     // put your main code here, to run repeatedly:
  121.     if (!dmpReady) return;
  122.  
  123.     // wait for MPU interrupt or extra packet(s) available
  124.     while (!mpuInterrupt && fifoCount < packetSize) {
  125.         // other program behavior stuff here
  126.         // .
  127.         // .
  128.         // .
  129.         // if you are really paranoid you can frequently test in between other
  130.         // stuff to see if mpuInterrupt is true, and if so, "break;" from the
  131.         // while() loop to immediately process the MPU data
  132.         // .
  133.         // .
  134.         // .
  135.     }
  136.  
  137.     // reset interrupt flag and get INT_STATUS byte
  138.     mpuInterrupt = false;
  139.     mpuIntStatus = mpu.getIntStatus();
  140.  
  141.     // get current FIFO count
  142.     fifoCount = mpu.getFIFOCount();
  143.  
  144.     // check for overflow (this should never happen unless our code is too inefficient)
  145.     if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
  146.         // reset so we can continue cleanly
  147.         mpu.resetFIFO();
  148.         Serial.println(F("FIFO overflow!"));
  149.  
  150.     // otherwise, check for DMP data ready interrupt (this should happen frequently)
  151.     } else if (mpuIntStatus & 0x02) {
  152.         // wait for correct available data length, should be a VERY short wait
  153.         while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();
  154.  
  155.         // read a packet from FIFO
  156.         mpu.getFIFOBytes(fifoBuffer, packetSize);
  157.  
  158.         // track FIFO count here in case there is > 1 packet available
  159.         // (this lets us immediately read more without waiting for an interrupt)
  160.         fifoCount -= packetSize;
  161.  
  162.         // display Euler angles in degrees
  163.         mpu.dmpGetQuaternion(&q, fifoBuffer);
  164.         mpu.dmpGetGravity(&gravity, &q);
  165.         mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
  166.         Serial.print("ypr\t");
  167.         Serial.print(ypr[0] * 180/M_PI);
  168.         Serial.print("\t");
  169.         Serial.print(ypr[1] * 180/M_PI);
  170.         Serial.print("\t");
  171.         Serial.println(ypr[2] * 180/M_PI);
  172.  
  173.         // read raw gyroscope measurements at the maximum rate
  174.         VectorInt16 gyroscope;
  175.         mpu.getRotation(&gyroscope);
  176.         Serial.print("Gyroscope\t");
  177.         Serial.print("X: ");
  178.         Serial.print(gyroscope.x);
  179.         Serial.print("\tY: ");
  180.         Serial.print(gyroscope.y);
  181.         Serial.print("\tZ: ");
  182.         Serial.println(gyroscope.z);
  183.     }
  184. }
  185.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement