Advertisement
pleasedontcode

"MPU6050 Setup" rev_02

Jun 21st, 2024
544
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 05:38:13
  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. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Wire.h>
  29. #include <MPU6050_6Axis_MotionApps20.h>  //https://github.com/jrowberg/i2cdevlib
  30. #include <PPMEncoder.h>  //https://github.com/schinken/PPMEncoder
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35. void dmpDataReady(void);
  36.  
  37. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  38. const uint8_t gyro_MPU6050_Interrupt_PIN_D2 = 2;
  39.  
  40. /***** DEFINITION OF I2C PINS *****/
  41. const uint8_t gyro_MPU6050_I2C_PIN_SDA_D20 = 20;
  42. const uint8_t gyro_MPU6050_I2C_PIN_SCL_D21 = 21;
  43. const uint8_t gyro_MPU6050_I2C_SLAVE_ADDRESS = 0x68;
  44.  
  45. /***** DEFINITION OF PPM OUTPUT PIN *****/
  46. const uint8_t ppmOutputPin = 10;
  47.  
  48. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  49. MPU6050 mpu;
  50.  
  51. /****** GLOBAL VARIABLES *****/
  52. bool blinkState = false;
  53. bool dmpReady = false;
  54. uint8_t mpuIntStatus, devStatus, fifoBuffer[64];
  55. uint16_t packetSize, fifoCount;
  56. Quaternion q;
  57. VectorFloat gravity;
  58. float ypr[3];
  59.  
  60. volatile bool mpuInterrupt = false;
  61.  
  62. /****** FUNCTION DEFINITIONS *****/
  63. void dmpDataReady() {
  64.     mpuInterrupt = true;
  65. }
  66.  
  67. void setup(void) {
  68.     // put your setup code here, to run once:
  69.     pinMode(gyro_MPU6050_Interrupt_PIN_D2, INPUT);
  70.     pinMode(LED_BUILTIN, OUTPUT);
  71.  
  72.     // Initialize I2C communication
  73.     Wire.begin();
  74.     Wire.setClock(400000);  // 400kHz I2C clock
  75.  
  76.     // Initialize serial communication
  77.     Serial.begin(115200);
  78.     while (!Serial);
  79.  
  80.     // Initialize MPU6050
  81.     Serial.println(F("Initializing I2C devices..."));
  82.     mpu.initialize();
  83.     Serial.println(F("Testing device connections..."));
  84.     Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));
  85.  
  86.     // Wait for user input to start DMP
  87.     Serial.println(F("\nSend any character to begin DMP programming and demo: "));
  88.     while (!Serial.available());
  89.     while (Serial.available() && Serial.read());
  90.  
  91.     // Initialize DMP
  92.     Serial.println(F("Initializing DMP..."));
  93.     devStatus = mpu.dmpInitialize();
  94.  
  95.     // Supply your own gyro offsets here, scaled for min sensitivity
  96.     mpu.setXGyroOffset(220);
  97.     mpu.setYGyroOffset(76);
  98.     mpu.setZGyroOffset(-85);
  99.     mpu.setZAccelOffset(1788);
  100.  
  101.     // Check if DMP initialization was successful
  102.     if (devStatus == 0) {
  103.         mpu.CalibrateAccel(6);
  104.         mpu.CalibrateGyro(6);
  105.         mpu.PrintActiveOffsets();
  106.         Serial.println(F("Enabling DMP..."));
  107.         mpu.setDMPEnabled(true);
  108.  
  109.         // Enable interrupt detection
  110.         Serial.print(F("Enabling interrupt detection (Arduino external interrupt "));
  111.         Serial.print(digitalPinToInterrupt(gyro_MPU6050_Interrupt_PIN_D2));
  112.         Serial.println(F(")..."));
  113.         attachInterrupt(digitalPinToInterrupt(gyro_MPU6050_Interrupt_PIN_D2), dmpDataReady, RISING);
  114.         mpuIntStatus = mpu.getIntStatus();
  115.  
  116.         Serial.println(F("DMP ready! Waiting for first interrupt..."));
  117.         dmpReady = true;
  118.         packetSize = mpu.dmpGetFIFOPacketSize();
  119.     } else {
  120.         Serial.print(F("DMP Initialization failed (code "));
  121.         Serial.print(devStatus);
  122.         Serial.println(F(")"));
  123.     }
  124.  
  125.     // Initialize PPM Encoder
  126.     ppmEncoder.begin(ppmOutputPin);
  127. }
  128.  
  129. void loop(void) {
  130.     // put your main code here, to run repeatedly:
  131.     if (!dmpReady) return;
  132.  
  133.     // Check if there's a new DMP packet available
  134.     if (mpu.dmpGetCurrentFIFOPacket(fifoBuffer)) {
  135.         mpu.dmpGetQuaternion(&q, fifoBuffer);
  136.         mpu.dmpGetGravity(&gravity, &q);
  137.         mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
  138.         Serial.print("ypr\t");
  139.         Serial.print(ypr[0] * 180 / M_PI);
  140.         Serial.print("\t");
  141.         Serial.print(ypr[1] * 180 / M_PI);
  142.         Serial.print("\t");
  143.         Serial.println(ypr[2] * 180 / M_PI);
  144.  
  145.         // Encode Yaw, Pitch, and Roll into PPM channels
  146.         ppmEncoder.setChannel(0, map(ypr[0] * 180 / M_PI, -180, 180, PPMEncoder::MIN, PPMEncoder::MAX));
  147.         ppmEncoder.setChannel(1, map(ypr[1] * 180 / M_PI, -90, 90, PPMEncoder::MIN, PPMEncoder::MAX));
  148.         ppmEncoder.setChannel(2, map(ypr[2] * 180 / M_PI, -90, 90, PPMEncoder::MIN, PPMEncoder::MAX));
  149.  
  150.         blinkState = !blinkState;
  151.         digitalWrite(LED_BUILTIN, blinkState);
  152.     }
  153. }
  154.  
  155. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement