Advertisement
pleasedontcode

"MPU6050 Initialization" rev_02

Jun 5th, 2024
502
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 Initialization"
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-06-05 22:36:04
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Create a motion tracking application using the */
  21.     /* MPU6050 sensor. Configure I2C communication on */
  22.     /* pins A4 (SDA) and A5 (SCL), and use pin D2 as an */
  23.     /* interrupt to capture motion events. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <Wire.h>
  28. #include <MPU6050.h>  // https://github.com/electroniccats/mpu6050
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33.  
  34. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  35. const uint8_t sensor_MPU6050_Interrupt_PIN_D2 = 2;
  36.  
  37. /***** DEFINITION OF I2C PINS *****/
  38. const uint8_t sensor_MPU6050_I2C_PIN_SDA_A4 = A4;
  39. const uint8_t sensor_MPU6050_I2C_PIN_SCL_A5 = A5;
  40. const uint8_t sensor_MPU6050_I2C_SLAVE_ADDRESS = 104;
  41.  
  42. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  43. MPU6050 mpu(sensor_MPU6050_I2C_SLAVE_ADDRESS);  // Initialize MPU6050 with the I2C address
  44.  
  45. /****** GLOBAL VARIABLES *****/
  46. bool dmpReady = false;  // set true if DMP init was successful
  47. uint8_t mpuIntStatus;   // holds actual interrupt status byte from MPU
  48. uint8_t devStatus;      // return status after each device operation (0 = success, !0 = error)
  49. uint16_t packetSize;    // expected DMP packet size (default is 42 bytes)
  50. uint16_t fifoCount;     // count of all bytes currently in FIFO
  51. uint8_t fifoBuffer[64]; // FIFO storage buffer
  52.  
  53. // orientation/motion vars
  54. Quaternion q;           // [w, x, y, z]         quaternion container
  55. VectorFloat gravity;    // [x, y, z]            gravity vector
  56. float ypr[3];           // [yaw, pitch, roll]   yaw/pitch/roll container and gravity vector
  57.  
  58. volatile bool mpuInterrupt = false;  // indicates whether MPU interrupt pin has gone high
  59.  
  60. void dmpDataReady() {
  61.     mpuInterrupt = true;
  62. }
  63.  
  64. void setup(void) {
  65.     // put your setup code here, to run once:
  66.     pinMode(sensor_MPU6050_Interrupt_PIN_D2, INPUT);
  67.  
  68.     Wire.begin();
  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.     // initialize device
  75.     Serial.println(F("Initializing I2C devices..."));
  76.     mpu.initialize();
  77.  
  78.     // verify connection
  79.     Serial.println(F("Testing device connections..."));
  80.     Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));
  81.  
  82.     // wait for ready
  83.     Serial.println(F("\nSend any character to begin DMP programming and demo: "));
  84.     while (!Serial.available());
  85.     while (Serial.available() && Serial.read());  // empty buffer
  86.  
  87.     // load and configure the DMP
  88.     Serial.println(F("Initializing DMP..."));
  89.     devStatus = mpu.dmpInitialize();
  90.  
  91.     // supply your own gyro offsets here, scaled for min sensitivity
  92.     mpu.setXGyroOffset(220);
  93.     mpu.setYGyroOffset(76);
  94.     mpu.setZGyroOffset(-85);
  95.     mpu.setZAccelOffset(1788);  // 1688 factory default for my test chip
  96.  
  97.     // make sure it worked (returns 0 if so)
  98.     if (devStatus == 0) {
  99.         // turn on the DMP, now that it's ready
  100.         Serial.println(F("Enabling DMP..."));
  101.         mpu.setDMPEnabled(true);
  102.  
  103.         // enable Arduino interrupt detection
  104.         Serial.print(F("Enabling interrupt detection (Arduino external interrupt "));
  105.         Serial.print(digitalPinToInterrupt(sensor_MPU6050_Interrupt_PIN_D2));
  106.         Serial.println(F(")..."));
  107.         attachInterrupt(digitalPinToInterrupt(sensor_MPU6050_Interrupt_PIN_D2), dmpDataReady, RISING);
  108.         mpuIntStatus = mpu.getIntStatus();
  109.  
  110.         // set our DMP ready flag
  111.         Serial.println(F("DMP ready! Waiting for first interrupt..."));
  112.         dmpReady = true;
  113.  
  114.         // get expected DMP packet size for later comparison
  115.         packetSize = mpu.dmpGetFIFOPacketSize();
  116.     } else {
  117.         // ERROR!
  118.         // 1 = initial memory load failed
  119.         // 2 = DMP configuration updates failed
  120.         // (if it's going to break, usually the code will be 1)
  121.         Serial.print(F("DMP Initialization failed (code "));
  122.         Serial.print(devStatus);
  123.         Serial.println(F(")"));
  124.     }
  125. }
  126.  
  127. void loop(void) {
  128.     // put your main code here, to run repeatedly:
  129.     if (!dmpReady) return;
  130.  
  131.     // wait for MPU interrupt or extra packet(s) available
  132.     while (!mpuInterrupt && fifoCount < packetSize) {
  133.         // other program behavior stuff here
  134.         // .
  135.         // .
  136.         // .
  137.         // if you are really paranoid you can frequently test in between other
  138.         // stuff to see if mpuInterrupt is true, and if so, "break;" from the
  139.         // while() loop to immediately process the MPU data
  140.         // .
  141.         // .
  142.         // .
  143.     }
  144.  
  145.     // reset interrupt flag and get INT_STATUS byte
  146.     mpuInterrupt = false;
  147.     mpuIntStatus = mpu.getIntStatus();
  148.  
  149.     // get current FIFO count
  150.     fifoCount = mpu.getFIFOCount();
  151.  
  152.     // check for overflow (this should never happen unless our code is too inefficient)
  153.     if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
  154.         // reset so we can continue cleanly
  155.         mpu.resetFIFO();
  156.         Serial.println(F("FIFO overflow!"));
  157.  
  158.     // otherwise, check for DMP data ready interrupt (this should happen frequently)
  159.     } else if (mpuIntStatus & 0x02) {
  160.         // wait for correct available data length, should be a VERY short wait
  161.         while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();
  162.  
  163.         // read a packet from FIFO
  164.         mpu.getFIFOBytes(fifoBuffer, packetSize);
  165.  
  166.         // track FIFO count here in case there is > 1 packet available
  167.         // (this lets us immediately read more without waiting for an interrupt)
  168.         fifoCount -= packetSize;
  169.  
  170.         mpu.dmpGetQuaternion(&q, fifoBuffer);
  171.         mpu.dmpGetGravity(&gravity, &q);
  172.         mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
  173.         Serial.print("ypr\t");
  174.         Serial.print(ypr[0] * 180/M_PI);
  175.         Serial.print("\t");
  176.         Serial.print(ypr[1] * 180/M_PI);
  177.         Serial.print("\t");
  178.         Serial.println(ypr[2] * 180/M_PI);
  179.     }
  180. }
  181.  
  182. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement