Advertisement
pleasedontcode

"Motion Bluetooth" rev_03

Jun 9th, 2024
276
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: "Motion Bluetooth"
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-06-09 21:09:44
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* make a bluetooth headtracker to use with opentrack */
  21.     /* software */
  22. /****** SYSTEM REQUIREMENT 2 *****/
  23.     /* make a headtracker for use with opentrack, using */
  24.     /* the mpu6050 and hc-06 bluetooth module */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Wire.h>
  29. #include <SoftwareSerial.h>
  30. #include <MPU6050_6Axis_MotionApps20.h> // Use the correct library for DMP functions
  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 accelerometer_MPU6050_Interrupt_PIN_D2 = 2;
  39.  
  40. /***** DEFINITION OF Software Serial *****/
  41. const uint8_t bluetooth_HC05_mySerial_PIN_SERIAL_TX_A0 = A0;
  42. const uint8_t bluetooth_HC05_mySerial_PIN_SERIAL_RX_A1 = A1;
  43. const uint8_t bluetooth_HC05_mySerial_PIN_SERIAL_TX_A2 = A2;
  44. const uint8_t bluetooth_HC05_mySerial_PIN_SERIAL_RX_A3 = A3;
  45. SoftwareSerial bluetooth_HC05_mySerial1(bluetooth_HC05_mySerial_PIN_SERIAL_RX_A1, bluetooth_HC05_mySerial_PIN_SERIAL_TX_A0);
  46. SoftwareSerial bluetooth_HC05_mySerial2(bluetooth_HC05_mySerial_PIN_SERIAL_RX_A3, bluetooth_HC05_mySerial_PIN_SERIAL_TX_A2);
  47.  
  48. /***** DEFINITION OF I2C PINS *****/
  49. const uint8_t accelerometer_MPU6050_I2C_PIN_SDA_A4 = A4;
  50. const uint8_t accelerometer_MPU6050_I2C_PIN_SCL_A5 = A5;
  51. const uint8_t accelerometer_MPU6050_I2C_SLAVE_ADDRESS = 104;
  52.  
  53. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  54. MPU6050 mpu(accelerometer_MPU6050_I2C_SLAVE_ADDRESS);
  55.  
  56. /****** VARIABLES *****/
  57. bool dmpReady = false;  // set true if DMP init was successful
  58. uint8_t mpuIntStatus;   // holds actual interrupt status byte from MPU
  59. uint8_t devStatus;      // return status after each device operation (0 = success, !0 = error)
  60. uint16_t packetSize;    // expected DMP packet size (default is 42 bytes)
  61. uint16_t fifoCount;     // count of all bytes currently in FIFO
  62. uint8_t fifoBuffer[64]; // FIFO storage buffer
  63.  
  64. // orientation/motion vars
  65. Quaternion q;           // [w, x, y, z]         quaternion container
  66. VectorFloat gravity;    // [x, y, z]            gravity vector
  67. float ypr[3];           // [yaw, pitch, roll]   yaw/pitch/roll container and gravity vector
  68.  
  69. volatile bool mpuInterrupt = false;     // indicates whether MPU interrupt pin has gone high
  70.  
  71. void dmpDataReady() {
  72.     mpuInterrupt = true;
  73. }
  74.  
  75. void setup(void)
  76. {
  77.     // put your setup code here, to run once:
  78.  
  79.     // Initialize I2C communication
  80.     Wire.begin();
  81.     Wire.setClock(400000); // Set I2C frequency to 400kHz
  82.  
  83.     // Initialize Serial communication for debugging
  84.     Serial.begin(115200);
  85.     while (!Serial);
  86.  
  87.     // Initialize MPU6050
  88.     Serial.println(F("Initializing I2C devices..."));
  89.     mpu.initialize();
  90.  
  91.     // Verify connection
  92.     Serial.println(F("Testing device connections..."));
  93.     Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));
  94.  
  95.     // Set up interrupt pin
  96.     pinMode(accelerometer_MPU6050_Interrupt_PIN_D2, INPUT);
  97.  
  98.     // Initialize Bluetooth Serial communication
  99.     bluetooth_HC05_mySerial1.begin(9600);
  100.     bluetooth_HC05_mySerial2.begin(9600);
  101.  
  102.     // Initialize DMP
  103.     Serial.println(F("Initializing DMP..."));
  104.     devStatus = mpu.dmpInitialize();
  105.  
  106.     // supply your own gyro offsets here, scaled for min sensitivity
  107.     mpu.setXGyroOffset(220);
  108.     mpu.setYGyroOffset(76);
  109.     mpu.setZGyroOffset(-85);
  110.     mpu.setZAccelOffset(1788); // 1688 factory default for my test chip
  111.  
  112.     // make sure it worked (returns 0 if so)
  113.     if (devStatus == 0) {
  114.         // turn on the DMP, now that it's ready
  115.         Serial.println(F("Enabling DMP..."));
  116.         mpu.setDMPEnabled(true);
  117.  
  118.         // enable Arduino interrupt detection
  119.         Serial.print(F("Enabling interrupt detection (Arduino external interrupt "));
  120.         Serial.print(digitalPinToInterrupt(accelerometer_MPU6050_Interrupt_PIN_D2));
  121.         Serial.println(F(")..."));
  122.         attachInterrupt(digitalPinToInterrupt(accelerometer_MPU6050_Interrupt_PIN_D2), dmpDataReady, RISING);
  123.         mpuIntStatus = mpu.getIntStatus();
  124.  
  125.         // set our DMP ready flag so the main loop() function knows it's okay to use it
  126.         Serial.println(F("DMP ready! Waiting for first interrupt..."));
  127.         dmpReady = true;
  128.  
  129.         // get expected DMP packet size for later comparison
  130.         packetSize = mpu.dmpGetFIFOPacketSize();
  131.     } else {
  132.         // ERROR!
  133.         // 1 = initial memory load failed
  134.         // 2 = DMP configuration updates failed
  135.         // (if it's going to break, usually the code will be 1)
  136.         Serial.print(F("DMP Initialization failed (code "));
  137.         Serial.print(devStatus);
  138.         Serial.println(F(")"));
  139.     }
  140. }
  141.  
  142. void loop(void)
  143. {
  144.     // if programming failed, don't try to do anything
  145.     if (!dmpReady) return;
  146.  
  147.     // wait for MPU interrupt or extra packet(s) available
  148.     while (!mpuInterrupt && fifoCount < packetSize) {
  149.         // other program behavior stuff here
  150.         // .
  151.         // .
  152.         // .
  153.         // if you are really paranoid you can frequently test in between other
  154.         // stuff to see if mpuInterrupt is true, and if so, "break;" from the
  155.         // while() loop to immediately process the MPU data
  156.         // .
  157.         // .
  158.         // .
  159.     }
  160.  
  161.     // reset interrupt flag and get INT_STATUS byte
  162.     mpuInterrupt = false;
  163.     mpuIntStatus = mpu.getIntStatus();
  164.  
  165.     // get current FIFO count
  166.     fifoCount = mpu.getFIFOCount();
  167.  
  168.     // check for overflow (this should never happen unless our code is too inefficient)
  169.     if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
  170.         // reset so we can continue cleanly
  171.         mpu.resetFIFO();
  172.         Serial.println(F("FIFO overflow!"));
  173.  
  174.     // otherwise, check for DMP data ready interrupt (this should happen frequently)
  175.     } else if (mpuIntStatus & 0x02) {
  176.         // wait for correct available data length, should be a VERY short wait
  177.         while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();
  178.  
  179.         // read a packet from FIFO
  180.         mpu.getFIFOBytes(fifoBuffer, packetSize);
  181.        
  182.         // track FIFO count here in case there is > 1 packet available
  183.         // (this lets us immediately read more without waiting for an interrupt)
  184.         fifoCount -= packetSize;
  185.  
  186.         mpu.dmpGetQuaternion(&q, fifoBuffer);
  187.         mpu.dmpGetGravity(&gravity, &q);
  188.         mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
  189.         Serial.print("ypr\t");
  190.         Serial.print(ypr[0] * 180/M_PI);
  191.         Serial.print("\t");
  192.         Serial.print(ypr[1] * 180/M_PI);
  193.         Serial.print("\t");
  194.         Serial.println(ypr[2] * 180/M_PI);
  195.  
  196.         // Send data over Bluetooth
  197.         bluetooth_HC05_mySerial1.print("ypr\t");
  198.         bluetooth_HC05_mySerial1.print(ypr[0] * 180/M_PI);
  199.         bluetooth_HC05_mySerial1.print("\t");
  200.         bluetooth_HC05_mySerial1.print(ypr[1] * 180/M_PI);
  201.         bluetooth_HC05_mySerial1.print("\t");
  202.         bluetooth_HC05_mySerial1.println(ypr[2] * 180/M_PI);
  203.  
  204.         bluetooth_HC05_mySerial2.print("ypr\t");
  205.         bluetooth_HC05_mySerial2.print(ypr[0] * 180/M_PI);
  206.         bluetooth_HC05_mySerial2.print("\t");
  207.         bluetooth_HC05_mySerial2.print(ypr[1] * 180/M_PI);
  208.         bluetooth_HC05_mySerial2.print("\t");
  209.         bluetooth_HC05_mySerial2.println(ypr[2] * 180/M_PI);
  210.     }
  211. }
  212.  
  213. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement