Advertisement
pleasedontcode

"PPM Encoding" rev_01

Jun 21st, 2024
558
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: "PPM Encoding"
  13.     - Source Code NOT compiled for: Arduino Mega
  14.     - Source Code created on: 2024-06-21 05:34:07
  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/your-PPMEncoder-library
  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. PPMEncoder ppmEncoder;
  51.  
  52. /****** GLOBAL VARIABLES *****/
  53. bool blinkState = false;
  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.  
  61. volatile bool mpuInterrupt = false;
  62.  
  63. /****** FUNCTION DEFINITIONS *****/
  64. void dmpDataReady() {
  65.     mpuInterrupt = true;
  66. }
  67.  
  68. void setup(void) {
  69.     // put your setup code here, to run once:
  70.     pinMode(gyro_MPU6050_Interrupt_PIN_D2, INPUT);
  71.     pinMode(LED_BUILTIN, OUTPUT);
  72.  
  73.     // Initialize I2C communication
  74.     Wire.begin();
  75.     Wire.setClock(400000);  // 400kHz I2C clock
  76.  
  77.     // Initialize serial communication
  78.     Serial.begin(115200);
  79.     while (!Serial);
  80.  
  81.     // Initialize MPU6050
  82.     Serial.println(F("Initializing I2C devices..."));
  83.     mpu.initialize();
  84.     Serial.println(F("Testing device connections..."));
  85.     Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));
  86.  
  87.     // Wait for user input to start DMP
  88.     Serial.println(F("\nSend any character to begin DMP programming and demo: "));
  89.     while (!Serial.available());
  90.     while (Serial.available() && Serial.read());
  91.  
  92.     // Initialize DMP
  93.     Serial.println(F("Initializing DMP..."));
  94.     devStatus = mpu.dmpInitialize();
  95.  
  96.     // Supply your own gyro offsets here, scaled for min sensitivity
  97.     mpu.setXGyroOffset(220);
  98.     mpu.setYGyroOffset(76);
  99.     mpu.setZGyroOffset(-85);
  100.     mpu.setZAccelOffset(1788);
  101.  
  102.     // Check if DMP initialization was successful
  103.     if (devStatus == 0) {
  104.         mpu.CalibrateAccel(6);
  105.         mpu.CalibrateGyro(6);
  106.         mpu.PrintActiveOffsets();
  107.         Serial.println(F("Enabling DMP..."));
  108.         mpu.setDMPEnabled(true);
  109.  
  110.         // Enable interrupt detection
  111.         Serial.print(F("Enabling interrupt detection (Arduino external interrupt "));
  112.         Serial.print(digitalPinToInterrupt(gyro_MPU6050_Interrupt_PIN_D2));
  113.         Serial.println(F(")..."));
  114.         attachInterrupt(digitalPinToInterrupt(gyro_MPU6050_Interrupt_PIN_D2), dmpDataReady, RISING);
  115.         mpuIntStatus = mpu.getIntStatus();
  116.  
  117.         Serial.println(F("DMP ready! Waiting for first interrupt..."));
  118.         dmpReady = true;
  119.         packetSize = mpu.dmpGetFIFOPacketSize();
  120.     } else {
  121.         Serial.print(F("DMP Initialization failed (code "));
  122.         Serial.print(devStatus);
  123.         Serial.println(F(")"));
  124.     }
  125.  
  126.     // Initialize PPM Encoder
  127.     ppmEncoder.begin(ppmOutputPin);
  128. }
  129.  
  130. void loop(void) {
  131.     // put your main code here, to run repeatedly:
  132.     if (!dmpReady) return;
  133.  
  134.     // Check if there's a new DMP packet available
  135.     if (mpu.dmpGetCurrentFIFOPacket(fifoBuffer)) {
  136.         mpu.dmpGetQuaternion(&q, fifoBuffer);
  137.         mpu.dmpGetGravity(&gravity, &q);
  138.         mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
  139.         Serial.print("ypr\t");
  140.         Serial.print(ypr[0] * 180 / M_PI);
  141.         Serial.print("\t");
  142.         Serial.print(ypr[1] * 180 / M_PI);
  143.         Serial.print("\t");
  144.         Serial.println(ypr[2] * 180 / M_PI);
  145.  
  146.         // Encode Yaw, Pitch, and Roll into PPM channels
  147.         ppmEncoder.setChannel(0, map(ypr[0] * 180 / M_PI, -180, 180, PPMEncoder::MIN, PPMEncoder::MAX));
  148.         ppmEncoder.setChannel(1, map(ypr[1] * 180 / M_PI, -90, 90, PPMEncoder::MIN, PPMEncoder::MAX));
  149.         ppmEncoder.setChannel(2, map(ypr[2] * 180 / M_PI, -90, 90, PPMEncoder::MIN, PPMEncoder::MAX));
  150.  
  151.         blinkState = !blinkState;
  152.         digitalWrite(LED_BUILTIN, blinkState);
  153.     }
  154. }
  155.  
  156. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement