Advertisement
pleasedontcode

"MPU6050 Bluetooth" rev_04

Jun 9th, 2024
278
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 Bluetooth"
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-06-09 21:24:02
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Design a Bluetooth head tracker for OpenTrack with */
  21.     /* MPU6050 and HC-06 modules. Provide thorough code */
  22.     /* documentation and comments detailing each code */
  23.     /* segment's purpose and the hardware connections */
  24.     /* required. */
  25. /****** SYSTEM REQUIREMENT 2 *****/
  26.     /* Develop a Bluetooth head tracker for OpenTrack */
  27.     /* with MPU6050 and HC-06 modules. Include */
  28.     /* comprehensive code documentation, comments on */
  29.     /* hardware connections, and utilize an LED for */
  30.     /* status updates. */
  31. /****** END SYSTEM REQUIREMENTS *****/
  32.  
  33. /****** DEFINITION OF LIBRARIES *****/
  34. #include <Wire.h>
  35. #include <SoftwareSerial.h>
  36. #include <MPU6050_6Axis_MotionApps20.h> //https://github.com/jrowberg/i2cdevlib
  37.  
  38. /****** FUNCTION PROTOTYPES *****/
  39. void setup(void);
  40. void loop(void);
  41. void dmpDataReady(void);
  42. void updateOutputs(void);
  43.  
  44. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  45. const uint8_t accelerometer_MPU6050_Interrupt_PIN_D2 = 2;
  46.  
  47. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  48. const uint8_t statusLED_LED_PIN_D3 = 3;
  49.  
  50. /***** DEFINITION OF Software Serial *****/
  51. const uint8_t bluetooth_HC05_mySerial_PIN_SERIAL_TX_A0 = A0;
  52. const uint8_t bluetooth_HC05_mySerial_PIN_SERIAL_RX_A1 = A1;
  53. const uint8_t bluetooth_HC05_mySerial_PIN_SERIAL_TX_A2 = A2;
  54. const uint8_t bluetooth_HC05_mySerial_PIN_SERIAL_RX_A3 = A3;
  55. SoftwareSerial bluetooth_HC05_mySerial(bluetooth_HC05_mySerial_PIN_SERIAL_RX_A1, bluetooth_HC05_mySerial_PIN_SERIAL_TX_A0);
  56. SoftwareSerial bluetooth_HC05_mySerial_2(bluetooth_HC05_mySerial_PIN_SERIAL_RX_A3, bluetooth_HC05_mySerial_PIN_SERIAL_TX_A2);
  57.  
  58. /***** DEFINITION OF I2C PINS *****/
  59. const uint8_t accelerometer_MPU6050_I2C_PIN_SDA_A4 = A4;
  60. const uint8_t accelerometer_MPU6050_I2C_PIN_SCL_A5 = A5;
  61. const uint8_t accelerometer_MPU6050_I2C_SLAVE_ADDRESS = 104;
  62.  
  63. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  64. /***** used to store raw data *****/
  65. bool statusLED_LED_PIN_D3_rawData = 0;
  66.  
  67. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  68. /***** used to store data after characteristic curve transformation *****/
  69. float statusLED_LED_PIN_D3_phyData = 0.0;
  70.  
  71. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  72. MPU6050 mpu;
  73.  
  74. /****** GLOBAL VARIABLES *****/
  75. bool blinkState = false;
  76. bool dmpReady = false;
  77. uint8_t mpuIntStatus, devStatus, fifoBuffer[64];
  78. uint16_t packetSize, fifoCount;
  79. Quaternion q;
  80. VectorFloat gravity;
  81. float ypr[3];
  82.  
  83. volatile bool mpuInterrupt = false;
  84. void dmpDataReady() { mpuInterrupt = true; }
  85.  
  86. void setup(void)
  87. {
  88.     // Initialize digital pins
  89.     pinMode(accelerometer_MPU6050_Interrupt_PIN_D2, INPUT);
  90.     pinMode(statusLED_LED_PIN_D3, OUTPUT);
  91.  
  92.     // Initialize Bluetooth serial communication
  93.     bluetooth_HC05_mySerial.begin(9600);
  94.     bluetooth_HC05_mySerial_2.begin(9600);
  95.  
  96.     // Initialize I2C communication
  97.     Wire.begin();
  98.     Wire.setClock(400000);
  99.  
  100.     // Initialize Serial communication for debugging
  101.     Serial.begin(115200);
  102.     while (!Serial);
  103.  
  104.     // Initialize MPU6050
  105.     Serial.println(F("Initializing I2C devices..."));
  106.     mpu.initialize();
  107.     pinMode(accelerometer_MPU6050_Interrupt_PIN_D2, INPUT);
  108.  
  109.     // Verify connection to MPU6050
  110.     Serial.println(F("Testing device connections..."));
  111.     Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));
  112.  
  113.     // Wait for user input to begin DMP programming
  114.     Serial.println(F("\nSend any character to begin DMP programming and demo: "));
  115.     while (!Serial.available());
  116.     while (Serial.available() && Serial.read());
  117.  
  118.     // Initialize DMP
  119.     Serial.println(F("Initializing DMP..."));
  120.     devStatus = mpu.dmpInitialize();
  121.  
  122.     // Set offsets for MPU6050
  123.     mpu.setXGyroOffset(220);
  124.     mpu.setYGyroOffset(76);
  125.     mpu.setZGyroOffset(-85);
  126.     mpu.setZAccelOffset(1788);
  127.  
  128.     // Check if DMP initialization was successful
  129.     if (devStatus == 0) {
  130.         mpu.CalibrateAccel(6);
  131.         mpu.CalibrateGyro(6);
  132.         mpu.PrintActiveOffsets();
  133.         Serial.println(F("Enabling DMP..."));
  134.         mpu.setDMPEnabled(true);
  135.  
  136.         // Enable interrupt detection
  137.         Serial.print(F("Enabling interrupt detection (Arduino external interrupt "));
  138.         Serial.print(digitalPinToInterrupt(accelerometer_MPU6050_Interrupt_PIN_D2));
  139.         Serial.println(F(")..."));
  140.         attachInterrupt(digitalPinToInterrupt(accelerometer_MPU6050_Interrupt_PIN_D2), dmpDataReady, RISING);
  141.         mpuIntStatus = mpu.getIntStatus();
  142.  
  143.         Serial.println(F("DMP ready! Waiting for first interrupt..."));
  144.         dmpReady = true;
  145.         packetSize = mpu.dmpGetFIFOPacketSize();
  146.     } else {
  147.         Serial.print(F("DMP Initialization failed (code "));
  148.         Serial.print(devStatus);
  149.         Serial.println(F(")"));
  150.     }
  151. }
  152.  
  153. void loop(void)
  154. {
  155.     // Main code to run repeatedly
  156.     if (!dmpReady) return;
  157.  
  158.     // Check if we have a new DMP packet
  159.     if (mpu.dmpGetCurrentFIFOPacket(fifoBuffer)) {
  160.         mpu.dmpGetQuaternion(&q, fifoBuffer);
  161.         mpu.dmpGetGravity(&gravity, &q);
  162.         mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
  163.        
  164.         // Send yaw, pitch, and roll data over Serial
  165.         Serial.print("ypr\t");
  166.         Serial.print(ypr[0] * 180/M_PI);
  167.         Serial.print("\t");
  168.         Serial.print(ypr[1] * 180/M_PI);
  169.         Serial.print("\t");
  170.         Serial.println(ypr[2] * 180/M_PI);
  171.  
  172.         // Send yaw, pitch, and roll data over Bluetooth
  173.         bluetooth_HC05_mySerial.print("ypr\t");
  174.         bluetooth_HC05_mySerial.print(ypr[0] * 180/M_PI);
  175.         bluetooth_HC05_mySerial.print("\t");
  176.         bluetooth_HC05_mySerial.print(ypr[1] * 180/M_PI);
  177.         bluetooth_HC05_mySerial.print("\t");
  178.         bluetooth_HC05_mySerial.println(ypr[2] * 180/M_PI);
  179.  
  180.         // Toggle LED state
  181.         blinkState = !blinkState;
  182.         digitalWrite(statusLED_LED_PIN_D3, blinkState);
  183.     }
  184.  
  185.     updateOutputs(); // Refresh output data
  186. }
  187.  
  188. void updateOutputs()
  189. {
  190.     // Update the status LED based on raw data
  191.     digitalWrite(statusLED_LED_PIN_D3, statusLED_LED_PIN_D3_rawData);
  192. }
  193.  
  194. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement