Advertisement
pleasedontcode

"MPU6050 PPM" rev_05

Jun 21st, 2024
485
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 07:44:46
  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.     /* when i press the push button, all gyro axis get */
  27.     /* set to center so all channels are centered with it */
  28. /****** END SYSTEM REQUIREMENTS *****/
  29.  
  30. /****** DEFINITION OF LIBRARIES *****/
  31. #include <Wire.h>
  32. #include <MPU6050_6Axis_MotionApps20.h>  //https://github.com/jrowberg/i2cdevlib
  33. #include "PPMEncoder.h"  //https://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. const uint8_t centerbutton_PushButton_PIN_D3 = 3;
  43. const uint8_t ppm_Output_PIN_D10 = 10;
  44.  
  45. /***** DEFINITION OF I2C PINS *****/
  46. const uint8_t gyro_MPU6050_I2C_PIN_SDA_D20 = 20;
  47. const uint8_t gyro_MPU6050_I2C_PIN_SCL_D21 = 21;
  48. const uint8_t gyro_MPU6050_I2C_SLAVE_ADDRESS = 104;
  49.  
  50. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  51. MPU6050 mpu;
  52. PPMEncoder ppmEncoder;
  53.  
  54. /****** GLOBAL VARIABLES *****/
  55. bool dmpReady = false;
  56. uint8_t mpuIntStatus, devStatus, fifoBuffer[64];
  57. uint16_t packetSize, fifoCount;
  58. Quaternion q;
  59. VectorFloat gravity;
  60. float ypr[3];
  61. volatile bool mpuInterrupt = false;
  62.  
  63. /****** INTERRUPT HANDLER *****/
  64. void dmpDataReady() {
  65.     mpuInterrupt = true;
  66. }
  67.  
  68. void setup(void)
  69. {
  70.     // put your setup code here, to run once:
  71.  
  72.     pinMode(gyro_MPU6050_Interrupt_PIN_D2, INPUT);
  73.     pinMode(centerbutton_PushButton_PIN_D3, INPUT_PULLUP);
  74.  
  75.     #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
  76.         Wire.begin();
  77.         Wire.setClock(400000);
  78.     #endif
  79.  
  80.     Serial.begin(115200);
  81.     while (!Serial);
  82.  
  83.     Serial.println(F("Initializing I2C devices..."));
  84.     mpu.initialize();
  85.     pinMode(gyro_MPU6050_Interrupt_PIN_D2, INPUT);
  86.  
  87.     Serial.println(F("Testing device connections..."));
  88.     Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));
  89.  
  90.     Serial.println(F("\nSend any character to begin DMP programming and demo: "));
  91.     while (!Serial.available());
  92.     while (Serial.available() && Serial.read());
  93.  
  94.     Serial.println(F("Initializing DMP..."));
  95.     devStatus = mpu.dmpInitialize();
  96.  
  97.     mpu.setXGyroOffset(220);
  98.     mpu.setYGyroOffset(76);
  99.     mpu.setZGyroOffset(-85);
  100.     mpu.setZAccelOffset(1788);
  101.  
  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.         Serial.print(F("Enabling interrupt detection (Arduino external interrupt "));
  110.         Serial.print(digitalPinToInterrupt(gyro_MPU6050_Interrupt_PIN_D2));
  111.         Serial.println(F(")..."));
  112.         attachInterrupt(digitalPinToInterrupt(gyro_MPU6050_Interrupt_PIN_D2), dmpDataReady, RISING);
  113.         mpuIntStatus = mpu.getIntStatus();
  114.  
  115.         Serial.println(F("DMP ready! Waiting for first interrupt..."));
  116.         dmpReady = true;
  117.         packetSize = mpu.dmpGetFIFOPacketSize();
  118.     } else {
  119.         Serial.print(F("DMP Initialization failed (code "));
  120.         Serial.print(devStatus);
  121.         Serial.println(F(")"));
  122.     }
  123.  
  124.     // Initialize PPM encoder
  125.     ppmEncoder.begin(ppm_Output_PIN_D10);
  126. }
  127.  
  128. void loop(void)
  129. {
  130.     // put your main code here, to run repeatedly:
  131.     if (!dmpReady) return;
  132.  
  133.     if (mpu.dmpGetCurrentFIFOPacket(fifoBuffer)) {
  134.         mpu.dmpGetQuaternion(&q, fifoBuffer);
  135.         mpu.dmpGetGravity(&gravity, &q);
  136.         mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
  137.         Serial.print("ypr\t");
  138.         Serial.print(ypr[0] * 180/M_PI);
  139.         Serial.print("\t");
  140.         Serial.print(ypr[1] * 180/M_PI);
  141.         Serial.print("\t");
  142.         Serial.println(ypr[2] * 180/M_PI);
  143.  
  144.         // Map Yaw, Pitch, Roll to PPM channels
  145.         ppmEncoder.setChannel(0, map(ypr[0] * 180/M_PI, -180, 180, PPMEncoder::MIN, PPMEncoder::MAX));
  146.         ppmEncoder.setChannel(1, map(ypr[1] * 180/M_PI, -90, 90, PPMEncoder::MIN, PPMEncoder::MAX));
  147.         ppmEncoder.setChannel(2, map(ypr[2] * 180/M_PI, -90, 90, PPMEncoder::MIN, PPMEncoder::MAX));
  148.  
  149.         // Check if the center button is pressed
  150.         if (digitalRead(centerbutton_PushButton_PIN_D3) == LOW) {
  151.             // Center all gyro axis
  152.             mpu.setXGyroOffset(0);
  153.             mpu.setYGyroOffset(0);
  154.             mpu.setZGyroOffset(0);
  155.         }
  156.     }
  157. }
  158.  
  159. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement