Advertisement
pleasedontcode

"PPM Conversion" rev_07

Jun 21st, 2024
444
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 Conversion"
  13.     - Source Code NOT compiled for: Arduino Mega
  14.     - Source Code created on: 2024-06-21 07:52:13
  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.     /* convert potentiometer input to ppm signal as well */
  27. /****** END SYSTEM REQUIREMENTS *****/
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30. #include <Wire.h>
  31. #include <MPU6050.h>    //https://github.com/electroniccats/mpu6050
  32. #include <PPMEncoder.h> //http://github.com/schinken/PPMEncoder
  33.  
  34. /****** FUNCTION PROTOTYPES *****/
  35. void setup(void);
  36. void loop(void);
  37. void dmpDataReady(void);
  38.  
  39. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  40. const uint8_t gyro_MPU6050_Interrupt_PIN_D2 = 2;
  41.  
  42. /***** DEFINITION OF ANALOG INPUT PINS *****/
  43. const uint8_t steering_Potentiometer_Vout_PIN_A0 = A0;
  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(gyro_MPU6050_I2C_SLAVE_ADDRESS); // Initialize MPU6050 with the specified I2C address
  52. PPMEncoder ppmEncoder; // Initialize PPMEncoder
  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. Quaternion q;           // [w, x, y, z]         quaternion container
  63. VectorFloat gravity;    // [x, y, z]            gravity vector
  64. float ypr[3];           // [yaw, pitch, roll]   yaw/pitch/roll container and gravity vector
  65.  
  66. volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high
  67.  
  68. void dmpDataReady() {
  69.     mpuInterrupt = true;
  70. }
  71.  
  72. void setup(void)
  73. {
  74.     // put your setup code here, to run once:
  75.  
  76.     pinMode(gyro_MPU6050_Interrupt_PIN_D2, INPUT);
  77.     pinMode(steering_Potentiometer_Vout_PIN_A0, INPUT);
  78.  
  79.     // Initialize I2C communication
  80.     Wire.begin();
  81.     Wire.setClock(400000); // Set I2C clock to 400kHz
  82.  
  83.     // Initialize MPU6050
  84.     Serial.begin(115200);
  85.     while (!Serial); // Wait for the serial port to open (for Leonardo/Micro)
  86.     Serial.println(F("Initializing I2C devices..."));
  87.     mpu.initialize();
  88.  
  89.     // Verify connection
  90.     Serial.println(F("Testing device connections..."));
  91.     Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));
  92.  
  93.     // Initialize DMP
  94.     Serial.println(F("Initializing DMP..."));
  95.     devStatus = mpu.dmpInitialize();
  96.  
  97.     // supply your own gyro offsets here, scaled for min sensitivity
  98.     mpu.setXGyroOffset(220);
  99.     mpu.setYGyroOffset(76);
  100.     mpu.setZGyroOffset(-85);
  101.     mpu.setZAccelOffset(1788); // 1688 factory default for my test chip
  102.  
  103.     // make sure it worked (returns 0 if so)
  104.     if (devStatus == 0) {
  105.         // turn on the DMP, now that it's ready
  106.         Serial.println(F("Enabling DMP..."));
  107.         mpu.setDMPEnabled(true);
  108.  
  109.         // enable Arduino interrupt detection
  110.         Serial.print(F("Enabling interrupt detection (Arduino external interrupt "));
  111.         Serial.print(digitalPinToInterrupt(gyro_MPU6050_Interrupt_PIN_D2));
  112.         Serial.println(F(")..."));
  113.         attachInterrupt(digitalPinToInterrupt(gyro_MPU6050_Interrupt_PIN_D2), dmpDataReady, RISING);
  114.         mpuIntStatus = mpu.getIntStatus();
  115.  
  116.         // set our DMP ready flag
  117.         Serial.println(F("DMP ready! Waiting for first interrupt..."));
  118.         dmpReady = true;
  119.  
  120.         // get expected DMP packet size for later comparison
  121.         packetSize = mpu.dmpGetFIFOPacketSize();
  122.     } else {
  123.         // ERROR!
  124.         // 1 = initial memory load failed
  125.         // 2 = DMP configuration updates failed
  126.         // (if it's going to break, usually the code will be 1)
  127.         Serial.print(F("DMP Initialization failed (code "));
  128.         Serial.print(devStatus);
  129.         Serial.println(F(")"));
  130.     }
  131.  
  132.     // Initialize PPM Encoder
  133.     ppmEncoder.begin(10); // Assuming pin 10 for PPM output
  134. }
  135.  
  136. void loop(void)
  137. {
  138.     // put your main code here, to run repeatedly:
  139.  
  140.     // if programming failed, don't try to do anything
  141.     if (!dmpReady) return;
  142.  
  143.     // wait for MPU interrupt or extra packet(s) available
  144.     while (!mpuInterrupt && fifoCount < packetSize) {
  145.         // other program behavior stuff here
  146.         // .
  147.         // .
  148.         // .
  149.         // if you are really paranoid you can frequently test in between other
  150.         // stuff to see if mpuInterrupt is true, and if so, "break;" from the
  151.         // while() loop to immediately process the MPU data
  152.         // .
  153.         // .
  154.         // .
  155.     }
  156.  
  157.     // reset interrupt flag and get INT_STATUS byte
  158.     mpuInterrupt = false;
  159.     mpuIntStatus = mpu.getIntStatus();
  160.  
  161.     // get current FIFO count
  162.     fifoCount = mpu.getFIFOCount();
  163.  
  164.     // check for overflow (this should never happen unless our code is too inefficient)
  165.     if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
  166.         // reset so we can continue cleanly
  167.         mpu.resetFIFO();
  168.         Serial.println(F("FIFO overflow!"));
  169.  
  170.     // otherwise, check for DMP data ready interrupt (this should happen frequently)
  171.     } else if (mpuIntStatus & 0x02) {
  172.         // wait for correct available data length, should be a VERY short wait
  173.         while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();
  174.  
  175.         // read a packet from FIFO
  176.         mpu.getFIFOBytes(fifoBuffer, packetSize);
  177.  
  178.         // track FIFO count here in case there is > 1 packet available
  179.         // (this lets us immediately read more without waiting for an interrupt)
  180.         fifoCount -= packetSize;
  181.  
  182.         mpu.dmpGetQuaternion(&q, fifoBuffer);
  183.         mpu.dmpGetGravity(&gravity, &q);
  184.         mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
  185.         Serial.print("ypr\t");
  186.         Serial.print(ypr[0] * 180/M_PI);
  187.         Serial.print("\t");
  188.         Serial.print(ypr[1] * 180/M_PI);
  189.         Serial.print("\t");
  190.         Serial.println(ypr[2] * 180/M_PI);
  191.  
  192.         // Convert Yaw, Pitch, Roll to PPM signals
  193.         ppmEncoder.setChannel(0, map(ypr[0] * 180/M_PI, -180, 180, PPMEncoder::MIN, PPMEncoder::MAX));
  194.         ppmEncoder.setChannel(1, map(ypr[1] * 180/M_PI, -90, 90, PPMEncoder::MIN, PPMEncoder::MAX));
  195.         ppmEncoder.setChannel(2, map(ypr[2] * 180/M_PI, -90, 90, PPMEncoder::MIN, PPMEncoder::MAX));
  196.     }
  197.  
  198.     // Read potentiometer value and convert to PPM signal
  199.     int potValue = analogRead(steering_Potentiometer_Vout_PIN_A0);
  200.     ppmEncoder.setChannel(3, map(potValue, 0, 1023, PPMEncoder::MIN, PPMEncoder::MAX));
  201. }
  202.  
  203. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement