Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "I2Cdev.h"
- #include "MPU6050_6Axis_MotionApps20.h"
- #include <Servo.h>
- #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
- #include "Wire.h"
- #endif
- MPU6050 mpu;
- //MPU6050 mpu(0x69); // <-- use for AD0 high
- bool dmpReady = false;
- uint8_t mpuIntStatus;
- uint8_t devStatus;
- uint16_t packetSize;
- uint16_t fifoCount;
- uint8_t fifoBuffer[64];
- Quaternion q;
- VectorInt16 aa;
- VectorInt16 aaReal;
- VectorInt16 aaWorld;
- VectorFloat gravity;
- float euler[3];
- float ypr[3];
- volatile bool mpuInterrupt = false;
- void dmpDataReady() {
- mpuInterrupt = true;
- }
- Servo myservo1;
- Servo myservo2;
- void setup() {
- #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
- Wire.begin();
- TWBR = 24;
- #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
- Fastwire::setup(400, true);
- #endif
- Serial.begin(115200);
- delay(200);
- 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"));
- Serial.println(F("Initializing DMP..."));
- devStatus = mpu.dmpInitialize();
- // Your own gyro offsets from calibration.
- mpu.setXGyroOffset(-23);
- mpu.setYGyroOffset(81);
- mpu.setZGyroOffset(40);
- mpu.setZAccelOffset(1864);
- if (devStatus == 0) {
- Serial.println(F("Enabling DMP..."));
- mpu.setDMPEnabled(true);
- Serial.println(F("Enabling interrupt detection (Arduino external interrupt 0)...busy..."));
- attachInterrupt(0, dmpDataReady, RISING);
- mpuIntStatus = mpu.getIntStatus();
- Serial.println(F("DMP ready! Waiting for first interrupt..."));
- dmpReady = true;
- packetSize = mpu.dmpGetFIFOPacketSize();
- }
- myservo1.attach(3);
- myservo2.attach(4);
- }
- void loop() {
- if (!dmpReady) return;
- while (!mpuInterrupt && fifoCount < packetSize) {
- // just wait...
- }
- mpuInterrupt = false;
- mpuIntStatus = mpu.getIntStatus();
- fifoCount = mpu.getFIFOCount();
- if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
- mpu.resetFIFO();
- Serial.println(F("FIFO overflow!"));
- } else if (mpuIntStatus & 0x02) {
- while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();
- mpu.getFIFOBytes(fifoBuffer, packetSize);
- fifoCount -= packetSize;
- mpu.dmpGetQuaternion(&q, fifoBuffer);
- mpu.dmpGetGravity(&gravity, &q);
- mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
- myservo2.write(90 + (ypr[1] * 180 / M_PI));
- myservo1.write(90 + (ypr[2] * 180 / M_PI));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement