Advertisement
pleasedontcode

"MPU6050 PPM" rev_03

Jun 21st, 2024
463
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 PPM"
  13.     - Source Code NOT compiled for: Arduino Mega
  14.     - Source Code created on: 2024-06-21 05:55:47
  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.     /* calibrate gyro all center on start. all ppm */
  27.     /* channel should be in center on start. */
  28. /****** END SYSTEM REQUIREMENTS *****/
  29.  
  30. /****** DEFINITION OF LIBRARIES *****/
  31. #include <Wire.h>
  32. #include <MPU6050_6Axis_MotionApps20.h>  // Include the correct MPU6050 library with DMP support
  33. #include "PPMEncoder.h"  // Include the PPMEncoder library
  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 I2C PINS *****/
  44. const uint8_t gyro_MPU6050_I2C_PIN_SDA_D20 = 20;
  45. const uint8_t gyro_MPU6050_I2C_PIN_SCL_D21 = 21;
  46. const uint8_t gyro_MPU6050_I2C_SLAVE_ADDRESS = 0x68;  // Correct I2C address for MPU6050
  47.  
  48. /***** DEFINITION OF PPM ENCODER PIN *****/
  49. const uint8_t ppmEncoderOutputPin = 10;
  50.  
  51. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  52. MPU6050 mpu;  // Define the MPU6050 object
  53. PPMEncoder ppmEncoder;  // Define the PPMEncoder object
  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. // Orientation/motion vars
  64. Quaternion q;           // [w, x, y, z]         quaternion container
  65. VectorFloat gravity;    // [x, y, z]            gravity vector
  66. float ypr[3];           // [yaw, pitch, roll]   yaw/pitch/roll container and gravity vector
  67.  
  68. volatile bool mpuInterrupt = false;  // Indicates whether MPU interrupt pin has gone high
  69.  
  70. void dmpDataReady() {
  71.     mpuInterrupt = true;
  72. }
  73.  
  74. void setup(void) {
  75.     // Initialize PPM Encoder
  76.     ppmEncoder.begin(ppmEncoderOutputPin);
  77.  
  78.     // Set all PPM channels to center on start
  79.     for (int i = 0; i < ppmEncoder.getChannelCount(); i++) {
  80.         ppmEncoder.setChannel(i, PPMEncoder::CENTER);
  81.     }
  82.  
  83.     pinMode(gyro_MPU6050_Interrupt_PIN_D2, INPUT);
  84.  
  85.     // Initialize I2C
  86.     Wire.begin();
  87.     Wire.setClock(400000);  // 400kHz I2C clock. Comment this line if having compilation difficulties
  88.  
  89.     // Initialize serial communication
  90.     Serial.begin(115200);
  91.     while (!Serial);  // Wait for Leonardo enumeration, others continue immediately
  92.  
  93.     // Initialize the MPU6050
  94.     Serial.println(F("Initializing I2C devices..."));
  95.     mpu.initialize();
  96.     pinMode(gyro_MPU6050_Interrupt_PIN_D2, INPUT);
  97.  
  98.     // Verify connection
  99.     Serial.println(F("Testing device connections..."));
  100.     Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));
  101.  
  102.     // Wait for ready
  103.     Serial.println(F("\nSend any character to begin DMP programming and demo: "));
  104.     while (!Serial.available());
  105.     while (Serial.available() && Serial.read());  // Empty buffer
  106.  
  107.     // Load and configure the DMP
  108.     Serial.println(F("Initializing DMP..."));
  109.     devStatus = mpu.dmpInitialize();
  110.  
  111.     // Supply your own gyro offsets here, scaled for min sensitivity
  112.     mpu.setXGyroOffset(220);
  113.     mpu.setYGyroOffset(76);
  114.     mpu.setZGyroOffset(-85);
  115.     mpu.setZAccelOffset(1788);  // 1688 factory default for my test chip
  116.  
  117.     // Make sure it worked (returns 0 if so)
  118.     if (devStatus == 0) {
  119.         // Calibration Time: generate offsets and calibrate our MPU6050
  120.         mpu.CalibrateAccel(6);
  121.         mpu.CalibrateGyro(6);
  122.         mpu.PrintActiveOffsets();
  123.         Serial.println(F("Enabling DMP..."));
  124.         mpu.setDMPEnabled(true);
  125.  
  126.         // Enable Arduino interrupt detection
  127.         Serial.print(F("Enabling interrupt detection (Arduino external interrupt "));
  128.         Serial.print(digitalPinToInterrupt(gyro_MPU6050_Interrupt_PIN_D2));
  129.         Serial.println(F(")..."));
  130.         attachInterrupt(digitalPinToInterrupt(gyro_MPU6050_Interrupt_PIN_D2), dmpDataReady, RISING);
  131.         mpuIntStatus = mpu.getIntStatus();
  132.  
  133.         // Set our DMP ready flag so the main loop() function knows it's okay to use it
  134.         Serial.println(F("DMP ready! Waiting for first interrupt..."));
  135.         dmpReady = true;
  136.  
  137.         // Get expected DMP packet size for later comparison
  138.         packetSize = mpu.dmpGetFIFOPacketSize();
  139.     } else {
  140.         // ERROR!
  141.         // 1 = initial memory load failed
  142.         // 2 = DMP configuration updates failed
  143.         // (if it's going to break, usually the code will be 1)
  144.         Serial.print(F("DMP Initialization failed (code "));
  145.         Serial.print(devStatus);
  146.         Serial.println(F(")"));
  147.     }
  148. }
  149.  
  150. void loop(void) {
  151.     // If programming failed, don't try to do anything
  152.     if (!dmpReady) return;
  153.  
  154.     // Wait for MPU interrupt or extra packet(s) available
  155.     while (!mpuInterrupt && fifoCount < packetSize) {
  156.         // Do nothing
  157.     }
  158.  
  159.     // Reset interrupt flag and get INT_STATUS byte
  160.     mpuInterrupt = false;
  161.     mpuIntStatus = mpu.getIntStatus();
  162.  
  163.     // Get current FIFO count
  164.     fifoCount = mpu.getFIFOCount();
  165.  
  166.     // Check for overflow (this should never happen unless our code is too inefficient)
  167.     if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
  168.         // Reset so we can continue cleanly
  169.         mpu.resetFIFO();
  170.         Serial.println(F("FIFO overflow!"));
  171.  
  172.     // Otherwise, check for DMP data ready interrupt (this should happen frequently)
  173.     } else if (mpuIntStatus & 0x02) {
  174.         // Wait for correct available data length, should be a VERY short wait
  175.         while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();
  176.  
  177.         // Read a packet from FIFO
  178.         mpu.getFIFOBytes(fifoBuffer, packetSize);
  179.  
  180.         // Track FIFO count here in case there is > 1 packet available
  181.         // (this lets us immediately read more without waiting for an interrupt)
  182.         fifoCount -= packetSize;
  183.  
  184.         mpu.dmpGetQuaternion(&q, fifoBuffer);
  185.         mpu.dmpGetGravity(&gravity, &q);
  186.         mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
  187.         Serial.print("ypr\t");
  188.         Serial.print(ypr[0] * 180/M_PI);
  189.         Serial.print("\t");
  190.         Serial.print(ypr[1] * 180/M_PI);
  191.         Serial.print("\t");
  192.         Serial.println(ypr[2] * 180/M_PI);
  193.  
  194.         // Map the yaw, pitch, and roll to PPM channels
  195.         ppmEncoder.setChannel(0, map(ypr[0] * 180/M_PI, -180, 180, PPMEncoder::MIN, PPMEncoder::MAX));
  196.         ppmEncoder.setChannel(1, map(ypr[1] * 180/M_PI, -90, 90, PPMEncoder::MIN, PPMEncoder::MAX));
  197.         ppmEncoder.setChannel(2, map(ypr[2] * 180/M_PI, -90, 90, PPMEncoder::MIN, PPMEncoder::MAX));
  198.     }
  199. }
  200.  
  201. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement