Advertisement
pleasedontcode

"MPU6050 PPM" rev_06

Jun 21st, 2024
478
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 compiled for: Arduino Mega
  14.     - Source Code created on: 2024-06-21 07:46:57
  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.  
  53. /****** GLOBAL VARIABLES *****/
  54. bool dmpReady = false;
  55. uint8_t mpuIntStatus, devStatus, fifoBuffer[64];
  56. uint16_t packetSize, fifoCount;
  57. Quaternion q;
  58. VectorFloat gravity;
  59. float ypr[3];
  60. volatile bool mpuInterrupt = false;
  61.  
  62. /****** INTERRUPT HANDLER *****/
  63. void dmpDataReady() {
  64.     mpuInterrupt = true;
  65. }
  66.  
  67. void setup(void)
  68. {
  69.     // put your setup code here, to run once:
  70.  
  71.     pinMode(gyro_MPU6050_Interrupt_PIN_D2, INPUT);
  72.     pinMode(centerbutton_PushButton_PIN_D3, INPUT_PULLUP);
  73.  
  74.     #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
  75.         Wire.begin();
  76.         Wire.setClock(400000);
  77.     #endif
  78.  
  79.     Serial.begin(115200);
  80.     while (!Serial);
  81.  
  82.     Serial.println(F("Initializing I2C devices..."));
  83.     mpu.initialize();
  84.     pinMode(gyro_MPU6050_Interrupt_PIN_D2, INPUT);
  85.  
  86.     Serial.println(F("Testing device connections..."));
  87.     Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));
  88.  
  89.     Serial.println(F("\nSend any character to begin DMP programming and demo: "));
  90.     while (!Serial.available());
  91.     while (Serial.available() && Serial.read());
  92.  
  93.     Serial.println(F("Initializing DMP..."));
  94.     devStatus = mpu.dmpInitialize();
  95.  
  96.     mpu.setXGyroOffset(220);
  97.     mpu.setYGyroOffset(76);
  98.     mpu.setZGyroOffset(-85);
  99.     mpu.setZAccelOffset(1788);
  100.  
  101.     if (devStatus == 0) {
  102.         mpu.CalibrateAccel(6);
  103.         mpu.CalibrateGyro(6);
  104.         mpu.PrintActiveOffsets();
  105.         Serial.println(F("Enabling DMP..."));
  106.         mpu.setDMPEnabled(true);
  107.  
  108.         Serial.print(F("Enabling interrupt detection (Arduino external interrupt "));
  109.         Serial.print(digitalPinToInterrupt(gyro_MPU6050_Interrupt_PIN_D2));
  110.         Serial.println(F(")..."));
  111.         attachInterrupt(digitalPinToInterrupt(gyro_MPU6050_Interrupt_PIN_D2), dmpDataReady, RISING);
  112.         mpuIntStatus = mpu.getIntStatus();
  113.  
  114.         Serial.println(F("DMP ready! Waiting for first interrupt..."));
  115.         dmpReady = true;
  116.         packetSize = mpu.dmpGetFIFOPacketSize();
  117.     } else {
  118.         Serial.print(F("DMP Initialization failed (code "));
  119.         Serial.print(devStatus);
  120.         Serial.println(F(")"));
  121.     }
  122.  
  123.     // Initialize PPM encoder
  124.     ppmEncoder.begin(ppm_Output_PIN_D10);
  125. }
  126.  
  127. void loop(void)
  128. {
  129.     // put your main code here, to run repeatedly:
  130.     if (!dmpReady) return;
  131.  
  132.     if (mpu.dmpGetCurrentFIFOPacket(fifoBuffer)) {
  133.         mpu.dmpGetQuaternion(&q, fifoBuffer);
  134.         mpu.dmpGetGravity(&gravity, &q);
  135.         mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
  136.         Serial.print("ypr\t");
  137.         Serial.print(ypr[0] * 180/M_PI);
  138.         Serial.print("\t");
  139.         Serial.print(ypr[1] * 180/M_PI);
  140.         Serial.print("\t");
  141.         Serial.println(ypr[2] * 180/M_PI);
  142.  
  143.         // Map Yaw, Pitch, Roll to PPM channels
  144.         ppmEncoder.setChannel(0, map(ypr[0] * 180/M_PI, -180, 180, PPMEncoder::MIN, PPMEncoder::MAX));
  145.         ppmEncoder.setChannel(1, map(ypr[1] * 180/M_PI, -90, 90, PPMEncoder::MIN, PPMEncoder::MAX));
  146.         ppmEncoder.setChannel(2, map(ypr[2] * 180/M_PI, -90, 90, PPMEncoder::MIN, PPMEncoder::MAX));
  147.  
  148.         // Check if the center button is pressed
  149.         if (digitalRead(centerbutton_PushButton_PIN_D3) == LOW) {
  150.             // Center all gyro axis
  151.             mpu.setXGyroOffset(0);
  152.             mpu.setYGyroOffset(0);
  153.             mpu.setZGyroOffset(0);
  154.         }
  155.     }
  156. }
  157.  
  158. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement