Advertisement
pleasedontcode

"Sensor Initialization" rev_05

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