Advertisement
pleasedontcode

"MPU6050 PPM Encoder" rev_04

Jun 21st, 2024
518
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 Encoder"
  13.     - Source Code compiled for: Arduino Mega
  14.     - Source Code created on: 2024-06-21 06:00:23
  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.  
  54. /****** GLOBAL VARIABLES *****/
  55. bool dmpReady = false;  // Set true if DMP init was successful
  56. uint8_t mpuIntStatus;   // Holds actual interrupt status byte from MPU
  57. uint8_t devStatus;      // Return status after each device operation (0 = success, !0 = error)
  58. uint16_t packetSize;    // Expected DMP packet size (default is 42 bytes)
  59. uint16_t fifoCount;     // Count of all bytes currently in FIFO
  60. uint8_t fifoBuffer[64]; // FIFO storage buffer
  61.  
  62. // Orientation/motion vars
  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.     // Initialize PPM Encoder
  75.     ppmEncoder.begin(ppmEncoderOutputPin);
  76.  
  77.     // Set all PPM channels to center on start
  78.     for (int i = 0; i < PPM_DEFAULT_CHANNELS; i++) {
  79.         ppmEncoder.setChannel(i, (PPMEncoder::MIN + PPMEncoder::MAX) / 2);
  80.     }
  81.  
  82.     pinMode(gyro_MPU6050_Interrupt_PIN_D2, INPUT);
  83.  
  84.     // Initialize I2C
  85.     Wire.begin();
  86.     Wire.setClock(400000);  // 400kHz I2C clock. Comment this line if having compilation difficulties
  87.  
  88.     // Initialize serial communication
  89.     Serial.begin(115200);
  90.     while (!Serial);  // Wait for Leonardo enumeration, others continue immediately
  91.  
  92.     // Initialize the MPU6050
  93.     Serial.println(F("Initializing I2C devices..."));
  94.     mpu.initialize();
  95.     pinMode(gyro_MPU6050_Interrupt_PIN_D2, INPUT);
  96.  
  97.     // Verify connection
  98.     Serial.println(F("Testing device connections..."));
  99.     Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));
  100.  
  101.     // Wait for ready
  102.     Serial.println(F("\nSend any character to begin DMP programming and demo: "));
  103.     while (!Serial.available());
  104.     while (Serial.available() && Serial.read());  // Empty buffer
  105.  
  106.     // Load and configure the DMP
  107.     Serial.println(F("Initializing DMP..."));
  108.     devStatus = mpu.dmpInitialize();
  109.  
  110.     // Supply your own gyro offsets here, scaled for min sensitivity
  111.     mpu.setXGyroOffset(220);
  112.     mpu.setYGyroOffset(76);
  113.     mpu.setZGyroOffset(-85);
  114.     mpu.setZAccelOffset(1788);  // 1688 factory default for my test chip
  115.  
  116.     // Make sure it worked (returns 0 if so)
  117.     if (devStatus == 0) {
  118.         // Calibration Time: generate offsets and calibrate our MPU6050
  119.         mpu.CalibrateAccel(6);
  120.         mpu.CalibrateGyro(6);
  121.         mpu.PrintActiveOffsets();
  122.         Serial.println(F("Enabling DMP..."));
  123.         mpu.setDMPEnabled(true);
  124.  
  125.         // Enable Arduino interrupt detection
  126.         Serial.print(F("Enabling interrupt detection (Arduino external interrupt "));
  127.         Serial.print(digitalPinToInterrupt(gyro_MPU6050_Interrupt_PIN_D2));
  128.         Serial.println(F(")..."));
  129.         attachInterrupt(digitalPinToInterrupt(gyro_MPU6050_Interrupt_PIN_D2), dmpDataReady, RISING);
  130.         mpuIntStatus = mpu.getIntStatus();
  131.  
  132.         // Set our DMP ready flag so the main loop() function knows it's okay to use it
  133.         Serial.println(F("DMP ready! Waiting for first interrupt..."));
  134.         dmpReady = true;
  135.  
  136.         // Get expected DMP packet size for later comparison
  137.         packetSize = mpu.dmpGetFIFOPacketSize();
  138.     } else {
  139.         // ERROR!
  140.         // 1 = initial memory load failed
  141.         // 2 = DMP configuration updates failed
  142.         // (if it's going to break, usually the code will be 1)
  143.         Serial.print(F("DMP Initialization failed (code "));
  144.         Serial.print(devStatus);
  145.         Serial.println(F(")"));
  146.     }
  147. }
  148.  
  149. void loop(void) {
  150.     // If programming failed, don't try to do anything
  151.     if (!dmpReady) return;
  152.  
  153.     // Wait for MPU interrupt or extra packet(s) available
  154.     while (!mpuInterrupt && fifoCount < packetSize) {
  155.         // Do nothing
  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.         // Map the yaw, pitch, and roll to PPM channels
  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.  
  200. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement