Advertisement
pleasedontcode

"MPU6050 Initialization" rev_03

Jun 16th, 2024
534
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 Initialization"
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-06-16 14:26:09
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* flight controller for brushed motors using radio */
  21.     /* frequency transmitters and receiver like flysky , */
  22.     /* radiolink */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <Wire.h>
  27. #include <FastIMU.h>    // https://github.com/LiquidCGS/FastIMU
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32.  
  33. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  34. const uint8_t gyro_MPU6050_Interrupt_PIN_D2 = 2;
  35.  
  36. /***** DEFINITION OF I2C PINS *****/
  37. const uint8_t gyro_MPU6050_I2C_PIN_SDA_A4 = A4;
  38. const uint8_t gyro_MPU6050_I2C_PIN_SCL_A5 = A5;
  39. const uint8_t gyro_MPU6050_I2C_SLAVE_ADDRESS = 0x68; // 104 in decimal
  40.  
  41. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  42. MPU6050 IMU; // Initialize MPU6050 with FastIMU library
  43.  
  44. /****** GLOBAL VARIABLES *****/
  45. bool dmpReady = false;  // set true if DMP init was successful
  46. uint16_t packetSize;    // expected DMP packet size (default is 42 bytes)
  47. uint8_t fifoBuffer[64]; // FIFO storage buffer
  48.  
  49. Quaternion q;           // [w, x, y, z] quaternion container
  50. float ypr[3];           // [yaw, pitch, roll] yaw/pitch/roll container and gravity vector
  51.  
  52. volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high
  53.  
  54. void dmpDataReady() {
  55.     mpuInterrupt = true;
  56. }
  57.  
  58. void setup(void) {
  59.     // put your setup code here, to run once:
  60.     Wire.begin();
  61.     Wire.setClock(400000); // 400kHz I2C clock. Comment this line if having compilation difficulties
  62.  
  63.     Serial.begin(115200);
  64.     while (!Serial); // wait for Leonardo enumeration, others continue immediately
  65.  
  66.     Serial.println(F("Initializing I2C devices..."));
  67.     calData calib = { 0 }; // Calibration data
  68.     int err = IMU.init(calib, gyro_MPU6050_I2C_SLAVE_ADDRESS);
  69.     if (err != 0) {
  70.         Serial.print(F("Error initializing IMU: "));
  71.         Serial.println(err);
  72.         while (true) { ; }
  73.     }
  74.  
  75.     pinMode(gyro_MPU6050_Interrupt_PIN_D2, INPUT);
  76.  
  77.     Serial.println(F("Testing device connections..."));
  78.     Serial.println(IMU.init(calib, gyro_MPU6050_I2C_SLAVE_ADDRESS) == 0 ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));
  79.  
  80.     Serial.println(F("\nSend any character to begin DMP programming and demo: "));
  81.     while (!Serial.available());
  82.     while (Serial.available() && Serial.read());
  83.  
  84.     Serial.println(F("Initializing DMP..."));
  85.     IMU.setGyroRange(250); // Set gyro range to ±250 degrees/second
  86.     IMU.setAccelRange(2);  // Set accelerometer range to ±2g
  87.  
  88.     // configure LED for output
  89.     pinMode(LED_BUILTIN, OUTPUT);
  90. }
  91.  
  92. void loop(void) {
  93.     // wait for MPU interrupt or extra packet(s) available
  94.     IMU.update();
  95.  
  96.     AccelData accel;
  97.     GyroData gyro;
  98.     MagData mag;
  99.  
  100.     IMU.getAccel(&accel);
  101.     IMU.getGyro(&gyro);
  102.     if (IMU.hasMagnetometer()) {
  103.         IMU.getMag(&mag);
  104.     }
  105.  
  106.     Serial.print("Accel: ");
  107.     Serial.print(accel.accelX);
  108.     Serial.print(", ");
  109.     Serial.print(accel.accelY);
  110.     Serial.print(", ");
  111.     Serial.print(accel.accelZ);
  112.     Serial.print(" | Gyro: ");
  113.     Serial.print(gyro.gyroX);
  114.     Serial.print(", ");
  115.     Serial.print(gyro.gyroY);
  116.     Serial.print(", ");
  117.     Serial.print(gyro.gyroZ);
  118.     if (IMU.hasMagnetometer()) {
  119.         Serial.print(" | Mag: ");
  120.         Serial.print(mag.magX);
  121.         Serial.print(", ");
  122.         Serial.print(mag.magY);
  123.         Serial.print(", ");
  124.         Serial.print(mag.magZ);
  125.     }
  126.     Serial.println();
  127.  
  128.     // blink LED to indicate activity
  129.     digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
  130. }
  131.  
  132. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement