Advertisement
pleasedontcode

"MPU6050 Initialization" rev_02

Jun 16th, 2024
525
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-16 13:34:44
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* flight controller for brushed motors using radio */
  21.     /* frequency transmitters and receiver like flysky , */
  22.     /* radiolink */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <Wire.h>
  27. #include <FastIMU.h>    // https://github.com/LiquidCGS/FastIMU
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32.  
  33. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  34. const uint8_t gyro_MPU6050_Interrupt_PIN_D2 = 2;
  35.  
  36. /***** DEFINITION OF I2C PINS *****/
  37. const uint8_t gyro_MPU6050_I2C_PIN_SDA_A4 = A4;
  38. const uint8_t gyro_MPU6050_I2C_PIN_SCL_A5 = A5;
  39. const uint8_t gyro_MPU6050_I2C_SLAVE_ADDRESS = 0x68; // 104 in decimal
  40.  
  41. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  42. MPU6050 IMU; // Initialize MPU6050 with FastIMU library
  43.  
  44. /****** GLOBAL VARIABLES *****/
  45. bool dmpReady = false;  // set true if DMP init was successful
  46. uint8_t mpuIntStatus;   // holds actual interrupt status byte from MPU
  47. uint8_t devStatus;      // return status after each device operation (0 = success, !0 = error)
  48. uint16_t packetSize;    // expected DMP packet size (default is 42 bytes)
  49. uint16_t fifoCount;     // count of all bytes currently in FIFO
  50. uint8_t fifoBuffer[64]; // FIFO storage buffer
  51.  
  52. Quaternion q;           // [w, x, y, z] quaternion container
  53. VectorFloat gravity;    // [x, y, z] gravity vector
  54. float ypr[3];           // [yaw, pitch, roll] yaw/pitch/roll container and gravity vector
  55.  
  56. volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high
  57.  
  58. void dmpDataReady() {
  59.     mpuInterrupt = true;
  60. }
  61.  
  62. void setup(void) {
  63.     // put your setup code here, to run once:
  64.     Wire.begin();
  65.     Wire.setClock(400000); // 400kHz I2C clock. Comment this line if having compilation difficulties
  66.  
  67.     Serial.begin(115200);
  68.     while (!Serial); // wait for Leonardo enumeration, others continue immediately
  69.  
  70.     Serial.println(F("Initializing I2C devices..."));
  71.     calData calib = { 0 }; // Calibration data
  72.     int err = IMU.init(calib, gyro_MPU6050_I2C_SLAVE_ADDRESS);
  73.     if (err != 0) {
  74.         Serial.print(F("Error initializing IMU: "));
  75.         Serial.println(err);
  76.         while (true) { ; }
  77.     }
  78.  
  79.     pinMode(gyro_MPU6050_Interrupt_PIN_D2, INPUT);
  80.  
  81.     Serial.println(F("Testing device connections..."));
  82.     Serial.println(IMU.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));
  83.  
  84.     Serial.println(F("\nSend any character to begin DMP programming and demo: "));
  85.     while (!Serial.available());
  86.     while (Serial.available() && Serial.read());
  87.  
  88.     Serial.println(F("Initializing DMP..."));
  89.     devStatus = IMU.dmpInitialize();
  90.  
  91.     // supply your own gyro offsets here, scaled for min sensitivity
  92.     IMU.setXGyroOffset(220);
  93.     IMU.setYGyroOffset(76);
  94.     IMU.setZGyroOffset(-85);
  95.     IMU.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.         IMU.setDMPEnabled(true);
  102.  
  103.         // enable Arduino interrupt detection
  104.         Serial.print(F("Enabling interrupt detection (Arduino external interrupt "));
  105.         Serial.print(digitalPinToInterrupt(gyro_MPU6050_Interrupt_PIN_D2));
  106.         Serial.println(F(")..."));
  107.         attachInterrupt(digitalPinToInterrupt(gyro_MPU6050_Interrupt_PIN_D2), dmpDataReady, RISING);
  108.         mpuIntStatus = IMU.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 = IMU.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.     // configure LED for output
  127.     pinMode(LED_BUILTIN, OUTPUT);
  128. }
  129.  
  130. void loop(void) {
  131.     // if programming failed, don't try to do anything
  132.     if (!dmpReady) return;
  133.  
  134.     // wait for MPU interrupt or extra packet(s) available
  135.     while (!mpuInterrupt && fifoCount < packetSize) {
  136.         // other program behavior stuff here
  137.         // .
  138.         // .
  139.         // .
  140.         // if you are really paranoid you can frequently test in between other
  141.         // stuff to see if mpuInterrupt is true, and if so, "break;" from the
  142.         // while() loop to immediately process the MPU data
  143.     }
  144.  
  145.     // reset interrupt flag and get INT_STATUS byte
  146.     mpuInterrupt = false;
  147.     mpuIntStatus = IMU.getIntStatus();
  148.  
  149.     // get current FIFO count
  150.     fifoCount = IMU.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.         IMU.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 = IMU.getFIFOCount();
  162.  
  163.         // read a packet from FIFO
  164.         IMU.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.         IMU.dmpGetQuaternion(&q, fifoBuffer);
  171.         IMU.dmpGetGravity(&gravity, &q);
  172.         IMU.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.         // blink LED to indicate activity
  181.         digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
  182.     }
  183. }
  184.  
  185. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement