Advertisement
pleasedontcode

"Motion Bluetooth" rev_01

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