Advertisement
pleasedontcode

"MPU6050 Initialization" rev_13

Jun 22nd, 2024
591
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 compiled for: Arduino Mega
  14.     - Source Code created on: 2024-06-22 05:51:59
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* convert mpu6050 data to ppm and send out for head */
  21.     /* tracking */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /****** DEFINITION OF LIBRARIES *****/
  25. #include <Wire.h>
  26. #include <PPMEncoder.h>  //http://github.com/schinken/PPMEncoder
  27. #include <MPU6050_6Axis_MotionApps20.h>  //https://github.com/jrowberg/i2cdevlib
  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_D20 = 20;
  38. const uint8_t gyro_MPU6050_I2C_PIN_SCL_D21 = 21;
  39. const uint8_t gyro_MPU6050_I2C_SLAVE_ADDRESS = 104;
  40.  
  41. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  42. //PPMEncoder ppmEncoder;  // Removed this instantiation
  43. MPU6050 mpu;  // Instantiate the MPU6050 object
  44.  
  45. /****** 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. Quaternion q;           // [w, x, y, z]         quaternion container
  54. VectorFloat gravity;    // [x, y, z]            gravity vector
  55. float ypr[3];           // [yaw, pitch, roll]   yaw/pitch/roll container and gravity vector
  56.  
  57. volatile bool mpuInterrupt = false;  // Indicates whether MPU interrupt pin has gone high
  58.  
  59. void dmpDataReady() {
  60.     mpuInterrupt = true;
  61. }
  62.  
  63. void setup(void) {
  64.   // Initialize I2C
  65.   Wire.begin();
  66.   Wire.setClock(400000);  // 400kHz I2C clock
  67.  
  68.   // Initialize serial communication
  69.   Serial.begin(115200);
  70.   while (!Serial);
  71.  
  72.   // Initialize MPU6050
  73.   Serial.println(F("Initializing I2C devices..."));
  74.   mpu.initialize();
  75.   pinMode(gyro_MPU6050_Interrupt_PIN_D2, INPUT);
  76.  
  77.   // Verify connection
  78.   Serial.println(F("Testing device connections..."));
  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.       // Calibration Time: generate offsets and calibrate our MPU6050
  94.       mpu.CalibrateAccel(6);
  95.       mpu.CalibrateGyro(6);
  96.       mpu.PrintActiveOffsets();
  97.       Serial.println(F("Enabling DMP..."));
  98.       mpu.setDMPEnabled(true);
  99.  
  100.       // Enable Arduino interrupt detection
  101.       Serial.print(F("Enabling interrupt detection (Arduino external interrupt "));
  102.       Serial.print(digitalPinToInterrupt(gyro_MPU6050_Interrupt_PIN_D2));
  103.       Serial.println(F(")..."));
  104.       attachInterrupt(digitalPinToInterrupt(gyro_MPU6050_Interrupt_PIN_D2), dmpDataReady, RISING);
  105.       mpuIntStatus = mpu.getIntStatus();
  106.  
  107.       // Set our DMP ready flag
  108.       Serial.println(F("DMP ready! Waiting for first interrupt..."));
  109.       dmpReady = true;
  110.  
  111.       // Get expected DMP packet size for later comparison
  112.       packetSize = mpu.dmpGetFIFOPacketSize();
  113.   } else {
  114.       // ERROR!
  115.       // 1 = initial memory load failed
  116.       // 2 = DMP configuration updates failed
  117.       // (if it's going to break, usually the code will be 1)
  118.       Serial.print(F("DMP Initialization failed (code "));
  119.       Serial.print(devStatus);
  120.       Serial.println(F(")"));
  121.   }
  122.  
  123.   // Initialize the PPM encoder on pin 10
  124.   ppmEncoder.begin(10);
  125. }
  126.  
  127. void loop(void) {
  128.   // If DMP is not ready, exit
  129.   if (!dmpReady) return;
  130.  
  131.   // Wait for MPU interrupt or extra packet(s) available
  132.   while (!mpuInterrupt && fifoCount < packetSize) {
  133.       // Do nothing
  134.   }
  135.  
  136.   // Reset interrupt flag and get INT_STATUS byte
  137.   mpuInterrupt = false;
  138.   mpuIntStatus = mpu.getIntStatus();
  139.  
  140.   // Get current FIFO count
  141.   fifoCount = mpu.getFIFOCount();
  142.  
  143.   // Check for overflow (this should never happen unless our code is too inefficient)
  144.   if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
  145.       // Reset so we can continue cleanly
  146.       mpu.resetFIFO();
  147.       Serial.println(F("FIFO overflow!"));
  148.  
  149.   // Otherwise, check for DMP data ready interrupt (this should happen frequently)
  150.   } else if (mpuIntStatus & 0x02) {
  151.       // Wait for correct available data length, should be a VERY short wait
  152.       while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();
  153.  
  154.       // Read a packet from FIFO
  155.       mpu.getFIFOBytes(fifoBuffer, packetSize);
  156.  
  157.       // Track FIFO count here in case there is > 1 packet available
  158.       // (this lets us immediately read more without waiting for an interrupt)
  159.       fifoCount -= packetSize;
  160.  
  161.       // Get Yaw, Pitch, and Roll values
  162.       mpu.dmpGetQuaternion(&q, fifoBuffer);
  163.       mpu.dmpGetGravity(&gravity, &q);
  164.       mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
  165.  
  166.       // Convert Yaw, Pitch, and Roll to PPM signals
  167.       int yawPPM = map(ypr[0] * 180/M_PI, -180, 180, PPMEncoder::MIN, PPMEncoder::MAX);
  168.       int pitchPPM = map(ypr[1] * 180/M_PI, -180, 180, PPMEncoder::MIN, PPMEncoder::MAX);
  169.       int rollPPM = map(ypr[2] * 180/M_PI, -180, 180, PPMEncoder::MIN, PPMEncoder::MAX);
  170.  
  171.       // Set PPM channels
  172.       ppmEncoder.setChannel(0, yawPPM);
  173.       ppmEncoder.setChannel(1, pitchPPM);
  174.       ppmEncoder.setChannel(2, rollPPM);
  175.  
  176.       // Print Yaw, Pitch, and Roll values for debugging
  177.       Serial.print("ypr\t");
  178.       Serial.print(ypr[0] * 180/M_PI);
  179.       Serial.print("\t");
  180.       Serial.print(ypr[1] * 180/M_PI);
  181.       Serial.print("\t");
  182.       Serial.println(ypr[2] * 180/M_PI);
  183.   }
  184. }
  185.  
  186. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement