Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: "MPU6050 Setup"
- - Source Code compiled for: Arduino Mega
- - Source Code created on: 2024-06-21 05:38:13
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Create a head tracking system with MPU6050 */
- /* gyroscope (I2C: SDA D20, SCL D21, Interrupt D2) */
- /* and PPMEncoder library. Ensure accurate data */
- /* capture and encoding for real-time head movement */
- /* tracking. Send out data in ppm format. */
- /****** END SYSTEM REQUIREMENTS *****/
- /****** DEFINITION OF LIBRARIES *****/
- #include <Wire.h>
- #include <MPU6050_6Axis_MotionApps20.h> //https://github.com/jrowberg/i2cdevlib
- #include <PPMEncoder.h> //https://github.com/schinken/PPMEncoder
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void dmpDataReady(void);
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t gyro_MPU6050_Interrupt_PIN_D2 = 2;
- /***** DEFINITION OF I2C PINS *****/
- const uint8_t gyro_MPU6050_I2C_PIN_SDA_D20 = 20;
- const uint8_t gyro_MPU6050_I2C_PIN_SCL_D21 = 21;
- const uint8_t gyro_MPU6050_I2C_SLAVE_ADDRESS = 0x68;
- /***** DEFINITION OF PPM OUTPUT PIN *****/
- const uint8_t ppmOutputPin = 10;
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- MPU6050 mpu;
- /****** GLOBAL VARIABLES *****/
- bool blinkState = false;
- bool dmpReady = false;
- uint8_t mpuIntStatus, devStatus, fifoBuffer[64];
- uint16_t packetSize, fifoCount;
- Quaternion q;
- VectorFloat gravity;
- float ypr[3];
- volatile bool mpuInterrupt = false;
- /****** FUNCTION DEFINITIONS *****/
- void dmpDataReady() {
- mpuInterrupt = true;
- }
- void setup(void) {
- // put your setup code here, to run once:
- pinMode(gyro_MPU6050_Interrupt_PIN_D2, INPUT);
- pinMode(LED_BUILTIN, OUTPUT);
- // Initialize I2C communication
- Wire.begin();
- Wire.setClock(400000); // 400kHz I2C clock
- // Initialize serial communication
- Serial.begin(115200);
- while (!Serial);
- // Initialize MPU6050
- Serial.println(F("Initializing I2C devices..."));
- mpu.initialize();
- Serial.println(F("Testing device connections..."));
- Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));
- // Wait for user input to start DMP
- Serial.println(F("\nSend any character to begin DMP programming and demo: "));
- while (!Serial.available());
- while (Serial.available() && Serial.read());
- // Initialize DMP
- Serial.println(F("Initializing DMP..."));
- devStatus = mpu.dmpInitialize();
- // Supply your own gyro offsets here, scaled for min sensitivity
- mpu.setXGyroOffset(220);
- mpu.setYGyroOffset(76);
- mpu.setZGyroOffset(-85);
- mpu.setZAccelOffset(1788);
- // Check if DMP initialization was successful
- if (devStatus == 0) {
- mpu.CalibrateAccel(6);
- mpu.CalibrateGyro(6);
- mpu.PrintActiveOffsets();
- Serial.println(F("Enabling DMP..."));
- mpu.setDMPEnabled(true);
- // Enable interrupt detection
- Serial.print(F("Enabling interrupt detection (Arduino external interrupt "));
- Serial.print(digitalPinToInterrupt(gyro_MPU6050_Interrupt_PIN_D2));
- Serial.println(F(")..."));
- attachInterrupt(digitalPinToInterrupt(gyro_MPU6050_Interrupt_PIN_D2), dmpDataReady, RISING);
- mpuIntStatus = mpu.getIntStatus();
- Serial.println(F("DMP ready! Waiting for first interrupt..."));
- dmpReady = true;
- packetSize = mpu.dmpGetFIFOPacketSize();
- } else {
- Serial.print(F("DMP Initialization failed (code "));
- Serial.print(devStatus);
- Serial.println(F(")"));
- }
- // Initialize PPM Encoder
- ppmEncoder.begin(ppmOutputPin);
- }
- void loop(void) {
- // put your main code here, to run repeatedly:
- if (!dmpReady) return;
- // Check if there's a new DMP packet available
- if (mpu.dmpGetCurrentFIFOPacket(fifoBuffer)) {
- mpu.dmpGetQuaternion(&q, fifoBuffer);
- mpu.dmpGetGravity(&gravity, &q);
- mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
- Serial.print("ypr\t");
- Serial.print(ypr[0] * 180 / M_PI);
- Serial.print("\t");
- Serial.print(ypr[1] * 180 / M_PI);
- Serial.print("\t");
- Serial.println(ypr[2] * 180 / M_PI);
- // Encode Yaw, Pitch, and Roll into PPM channels
- ppmEncoder.setChannel(0, map(ypr[0] * 180 / M_PI, -180, 180, PPMEncoder::MIN, PPMEncoder::MAX));
- ppmEncoder.setChannel(1, map(ypr[1] * 180 / M_PI, -90, 90, PPMEncoder::MIN, PPMEncoder::MAX));
- ppmEncoder.setChannel(2, map(ypr[2] * 180 / M_PI, -90, 90, PPMEncoder::MIN, PPMEncoder::MAX));
- blinkState = !blinkState;
- digitalWrite(LED_BUILTIN, blinkState);
- }
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement