Advertisement
pleasedontcode

"MPU6050 Setup" rev_09

Jun 21st, 2024
506
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 Setup"
  13.     - Source Code compiled for: Arduino Mega
  14.     - Source Code created on: 2024-06-21 07:57:44
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Create a head tracking system with MPU6050 */
  21.     /* gyroscope (I2C: SDA D20, SCL D21, Interrupt D2) */
  22.     /* and PPMEncoder library. Ensure accurate data */
  23.     /* capture and encoding for real-time head movement */
  24.     /* tracking. Send out data in ppm format. */
  25. /****** SYSTEM REQUIREMENT 2 *****/
  26.     /* convert potentiometer input to ppm signal as well */
  27. /****** END SYSTEM REQUIREMENTS *****/
  28.  
  29.  
  30. /****** DEFINITION OF LIBRARIES *****/
  31. #include <Wire.h>
  32. #include <MPU6050_6Axis_MotionApps20.h> // Include the DMP header for MPU6050
  33. #include <PPMEncoder.h> //http://github.com/schinken/PPMEncoder
  34.  
  35. /****** FUNCTION PROTOTYPES *****/
  36. void setup(void);
  37. void loop(void);
  38. void dmpDataReady(void);
  39.  
  40. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  41. const uint8_t gyro_MPU6050_Interrupt_PIN_D2 = 2;
  42.  
  43. /***** DEFINITION OF ANALOG INPUT PINS *****/
  44. const uint8_t steering_Potentiometer_Vout_PIN_A0 = A0;
  45.  
  46. /***** DEFINITION OF I2C PINS *****/
  47. const uint8_t gyro_MPU6050_I2C_PIN_SDA_D20 = 20;
  48. const uint8_t gyro_MPU6050_I2C_PIN_SCL_D21 = 21;
  49. const uint8_t gyro_MPU6050_I2C_SLAVE_ADDRESS = 104;
  50.  
  51. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  52. MPU6050 mpu(gyro_MPU6050_I2C_SLAVE_ADDRESS); // Initialize MPU6050 with the specified I2C address
  53. // PPMEncoder ppmEncoder; // Remove this line
  54.  
  55. /****** GLOBAL VARIABLES *****/
  56. bool dmpReady = false;  // set true if DMP init was successful
  57. uint8_t mpuIntStatus;   // holds actual interrupt status byte from MPU
  58. uint8_t devStatus;      // return status after each device operation (0 = success, !0 = error)
  59. uint16_t packetSize;    // expected DMP packet size (default is 42 bytes)
  60. uint16_t fifoCount;     // count of all bytes currently in FIFO
  61. uint8_t fifoBuffer[64]; // FIFO storage buffer
  62.  
  63. Quaternion q;           // [w, x, y, z]         quaternion container
  64. VectorFloat gravity;    // [x, y, z]            gravity vector
  65. float ypr[3];           // [yaw, pitch, roll]   yaw/pitch/roll container and gravity vector
  66.  
  67. volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high
  68.  
  69. void dmpDataReady() {
  70.     mpuInterrupt = true;
  71. }
  72.  
  73. void setup(void)
  74. {
  75.     // put your setup code here, to run once:
  76.  
  77.     pinMode(gyro_MPU6050_Interrupt_PIN_D2, INPUT);
  78.     pinMode(steering_Potentiometer_Vout_PIN_A0, INPUT);
  79.  
  80.     // Initialize I2C communication
  81.     Wire.begin();
  82.     Wire.setClock(400000); // Set I2C clock to 400kHz
  83.  
  84.     // Initialize MPU6050
  85.     Serial.begin(115200);
  86.     while (!Serial); // Wait for the serial port to open (for Leonardo/Micro)
  87.     Serial.println(F("Initializing I2C devices..."));
  88.     mpu.initialize();
  89.  
  90.     // Verify connection
  91.     Serial.println(F("Testing device connections..."));
  92.     Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));
  93.  
  94.     // Initialize DMP
  95.     Serial.println(F("Initializing DMP..."));
  96.     devStatus = mpu.dmpInitialize();
  97.  
  98.     // supply your own gyro offsets here, scaled for min sensitivity
  99.     mpu.setXGyroOffset(220);
  100.     mpu.setYGyroOffset(76);
  101.     mpu.setZGyroOffset(-85);
  102.     mpu.setZAccelOffset(1788); // 1688 factory default for my test chip
  103.  
  104.     // make sure it worked (returns 0 if so)
  105.     if (devStatus == 0) {
  106.         // turn on the DMP, now that it's ready
  107.         Serial.println(F("Enabling DMP..."));
  108.         mpu.setDMPEnabled(true);
  109.  
  110.         // enable Arduino interrupt detection
  111.         Serial.print(F("Enabling interrupt detection (Arduino external interrupt "));
  112.         Serial.print(digitalPinToInterrupt(gyro_MPU6050_Interrupt_PIN_D2));
  113.         Serial.println(F(")..."));
  114.         attachInterrupt(digitalPinToInterrupt(gyro_MPU6050_Interrupt_PIN_D2), dmpDataReady, RISING);
  115.         mpuIntStatus = mpu.getIntStatus();
  116.  
  117.         // set our DMP ready flag
  118.         Serial.println(F("DMP ready! Waiting for first interrupt..."));
  119.         dmpReady = true;
  120.  
  121.         // get expected DMP packet size for later comparison
  122.         packetSize = mpu.dmpGetFIFOPacketSize();
  123.     } else {
  124.         // ERROR!
  125.         // 1 = initial memory load failed
  126.         // 2 = DMP configuration updates failed
  127.         // (if it's going to break, usually the code will be 1)
  128.         Serial.print(F("DMP Initialization failed (code "));
  129.         Serial.print(devStatus);
  130.         Serial.println(F(")"));
  131.     }
  132.  
  133.     // Initialize PPM Encoder
  134.     ppmEncoder.begin(10); // Assuming pin 10 for PPM output
  135. }
  136.  
  137. void loop(void)
  138. {
  139.     // put your main code here, to run repeatedly:
  140.  
  141.     // if programming failed, don't try to do anything
  142.     if (!dmpReady) return;
  143.  
  144.     // wait for MPU interrupt or extra packet(s) available
  145.     while (!mpuInterrupt && fifoCount < packetSize) {
  146.         // other program behavior stuff here
  147.         // .
  148.         // .
  149.         // .
  150.         // if you are really paranoid you can frequently test in between other
  151.         // stuff to see if mpuInterrupt is true, and if so, "break;" from the
  152.         // while() loop to immediately process the MPU data
  153.         // .
  154.         // .
  155.         // .
  156.     }
  157.  
  158.     // reset interrupt flag and get INT_STATUS byte
  159.     mpuInterrupt = false;
  160.     mpuIntStatus = mpu.getIntStatus();
  161.  
  162.     // get current FIFO count
  163.     fifoCount = mpu.getFIFOCount();
  164.  
  165.     // check for overflow (this should never happen unless our code is too inefficient)
  166.     if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
  167.         // reset so we can continue cleanly
  168.         mpu.resetFIFO();
  169.         Serial.println(F("FIFO overflow!"));
  170.  
  171.     // otherwise, check for DMP data ready interrupt (this should happen frequently)
  172.     } else if (mpuIntStatus & 0x02) {
  173.         // wait for correct available data length, should be a VERY short wait
  174.         while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();
  175.  
  176.         // read a packet from FIFO
  177.         mpu.getFIFOBytes(fifoBuffer, packetSize);
  178.  
  179.         // track FIFO count here in case there is > 1 packet available
  180.         // (this lets us immediately read more without waiting for an interrupt)
  181.         fifoCount -= packetSize;
  182.  
  183.         mpu.dmpGetQuaternion(&q, fifoBuffer);
  184.         mpu.dmpGetGravity(&gravity, &q);
  185.         mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
  186.         Serial.print("ypr\t");
  187.         Serial.print(ypr[0] * 180/M_PI);
  188.         Serial.print("\t");
  189.         Serial.print(ypr[1] * 180/M_PI);
  190.         Serial.print("\t");
  191.         Serial.println(ypr[2] * 180/M_PI);
  192.  
  193.         // Convert Yaw, Pitch, Roll to PPM signals
  194.         ppmEncoder.setChannel(0, map(ypr[0] * 180/M_PI, -180, 180, PPMEncoder::MIN, PPMEncoder::MAX));
  195.         ppmEncoder.setChannel(1, map(ypr[1] * 180/M_PI, -90, 90, PPMEncoder::MIN, PPMEncoder::MAX));
  196.         ppmEncoder.setChannel(2, map(ypr[2] * 180/M_PI, -90, 90, PPMEncoder::MIN, PPMEncoder::MAX));
  197.     }
  198.  
  199.     // Read potentiometer value and convert to PPM signal
  200.     int potValue = analogRead(steering_Potentiometer_Vout_PIN_A0);
  201.     ppmEncoder.setChannel(3, map(potValue, 0, 1023, PPMEncoder::MIN, PPMEncoder::MAX));
  202. }
  203.  
  204. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement