Advertisement
MrLunk

MPU6050 Arduino camera / platform stabiliser stabilisation

Jan 21st, 2018
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.53 KB | None | 0 0
  1. #include "I2Cdev.h"
  2. #include "MPU6050_6Axis_MotionApps20.h"
  3. #include <Servo.h>
  4.  
  5. #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
  6. #include "Wire.h"
  7. #endif
  8.  
  9. MPU6050 mpu;
  10. //MPU6050 mpu(0x69); // <-- use for AD0 high
  11.  
  12. bool dmpReady = false;
  13. uint8_t mpuIntStatus;
  14. uint8_t devStatus;
  15. uint16_t packetSize;
  16. uint16_t fifoCount;
  17. uint8_t fifoBuffer[64];
  18.  
  19. Quaternion q;
  20. VectorInt16 aa;
  21. VectorInt16 aaReal;
  22. VectorInt16 aaWorld;
  23. VectorFloat gravity;
  24. float euler[3];
  25. float ypr[3];
  26.  
  27. volatile bool mpuInterrupt = false;
  28. void dmpDataReady() {
  29.   mpuInterrupt = true;
  30. }
  31.  
  32. Servo myservo1;
  33. Servo myservo2;
  34.  
  35. void setup() {
  36. #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
  37.   Wire.begin();
  38.   TWBR = 24;
  39. #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
  40.   Fastwire::setup(400, true);
  41. #endif
  42.  
  43.   Serial.begin(115200);
  44.   delay(200);
  45.  
  46.   Serial.println(F("Initializing I2C devices..."));
  47.   mpu.initialize();
  48.  
  49.   Serial.println(F("Testing device connections..."));
  50.   Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));
  51.  
  52.   Serial.println(F("Initializing DMP..."));
  53.   devStatus = mpu.dmpInitialize();
  54.  
  55.   // Your own gyro offsets from calibration.
  56.   mpu.setXGyroOffset(-23);
  57.   mpu.setYGyroOffset(81);
  58.   mpu.setZGyroOffset(40);
  59.   mpu.setZAccelOffset(1864);
  60.  
  61.   if (devStatus == 0) {
  62.     Serial.println(F("Enabling DMP..."));
  63.     mpu.setDMPEnabled(true);
  64.  
  65.     Serial.println(F("Enabling interrupt detection (Arduino external interrupt 0)...busy..."));
  66.     attachInterrupt(0, dmpDataReady, RISING);
  67.     mpuIntStatus = mpu.getIntStatus();
  68.  
  69.     Serial.println(F("DMP ready! Waiting for first interrupt..."));
  70.     dmpReady = true;
  71.  
  72.    packetSize = mpu.dmpGetFIFOPacketSize();
  73.   }
  74.  
  75.   myservo1.attach(3);
  76.   myservo2.attach(4);
  77. }
  78.  
  79. void loop() {
  80.   if (!dmpReady) return;
  81.  
  82.   while (!mpuInterrupt && fifoCount < packetSize) {
  83.     // just wait...
  84.   }
  85.  
  86.   mpuInterrupt = false;
  87.   mpuIntStatus = mpu.getIntStatus();
  88.  
  89.   fifoCount = mpu.getFIFOCount();
  90.  
  91.   if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
  92.     mpu.resetFIFO();
  93.     Serial.println(F("FIFO overflow!"));
  94.  
  95.   } else if (mpuIntStatus & 0x02) {
  96.     while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();
  97.  
  98.     mpu.getFIFOBytes(fifoBuffer, packetSize);
  99.     fifoCount -= packetSize;
  100.  
  101.     mpu.dmpGetQuaternion(&q, fifoBuffer);
  102.     mpu.dmpGetGravity(&gravity, &q);
  103.     mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
  104.  
  105.     myservo2.write(90 + (ypr[1] * 180 / M_PI));
  106.     myservo1.write(90 + (ypr[2] * 180 / M_PI));
  107.  
  108.   }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement