极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 31230|回复: 9

陀螺仪加速度计 MPU6050 启用DMP运算角度代码

[复制链接]
发表于 2013-9-5 11:28:01 | 显示全部楼层 |阅读模式
本人新人——废话不多说了……直接上代码……
官网也有这个代码,但是发这里大家用的方便……:)
记得把int接入D2口作为中断
接线和以前一样接arduino 的 I2C接口
SDL SCL两个口
3.3v电源

  1. // Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation
  2. // is used in I2Cdev.h
  3. #include "Wire.h"

  4. // I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files
  5. // for both classes must be in the include path of your project
  6. #include "I2Cdev.h"

  7. #include "MPU6050_6Axis_MotionApps20.h"
  8. MPU6050 mpu(0x68);

  9. // MPU control/status vars
  10. bool dmpReady = false;  // set true if DMP init was successful
  11. uint8_t mpuIntStatus;   // holds actual interrupt status byte from MPU
  12. uint8_t devStatus;      // return status after each device operation (0 = success, !0 = error)
  13. uint16_t packetSize;    // expected DMP packet size (default is 42 bytes)
  14. uint16_t fifoCount;     // count of all bytes currently in FIFO
  15. uint8_t fifoBuffer[64]; // FIFO storage buffer

  16. // orientation/motion vars
  17. Quaternion q;           // [w, x, y, z]         quaternion container
  18. VectorFloat gravity;    // [x, y, z]            gravity vector
  19. float ypr[3];           // [yaw, pitch, roll]   yaw/pitch/roll container and gravity vector


  20. // ================================================================
  21. // ===               INTERRUPT DETECTION ROUTINE                ===
  22. // ================================================================

  23. volatile bool mpuInterrupt = false;     // indicates whether MPU interrupt pin has gone high
  24. void dmpDataReady() {
  25.   mpuInterrupt = true;
  26. }



  27. // ================================================================
  28. // ===                      INITIAL SETUP                       ===
  29. // ================================================================

  30. void mpu_setup() {
  31.   //Serial.begin(115200);        // opens serial port, sets data rate to 9600 bps

  32.   // join I2C bus (I2Cdev library doesn't do this automatically)
  33.   Wire.begin();

  34.   // initialize device
  35.   Serial.println("Initializing I2C devices...");
  36.   mpu.initialize();

  37.   // verify connection
  38.   Serial.println("Testing device connections...");
  39.   Serial.println(mpu.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");

  40.   delay(2);

  41.   // load and configure the DMP
  42.   Serial.println("Initializing DMP...");
  43.   devStatus = mpu.dmpInitialize();

  44.   // make sure it worked (returns 0 if so)
  45.   if (devStatus == 0) {
  46.     // turn on the DMP, now that it's ready
  47.     Serial.println("Enabling DMP...");
  48.     mpu.setDMPEnabled(true);

  49.     // enable Arduino interrupt detection
  50.     Serial.println("Enabling interrupt detection (Arduino external interrupt 0)...");
  51.     attachInterrupt(0, dmpDataReady, RISING);
  52.     mpuIntStatus = mpu.getIntStatus();

  53.     // set our DMP Ready flag so the main loop() function knows it's okay to use it
  54.     Serial.println("DMP ready! Waiting for first interrupt...");
  55.     dmpReady = true;

  56.     // get expected DMP packet size for later comparison
  57.     packetSize = mpu.dmpGetFIFOPacketSize();
  58.   }
  59.   else {
  60.     // ERROR!
  61.     // 1 = initial memory load failed
  62.     // 2 = DMP configuration updates failed
  63.     // (if it's going to break, usually the code will be 1)
  64.     Serial.print("DMP Initialization failed (code ");
  65.     Serial.print(devStatus);
  66.     Serial.println(")");
  67.   }
  68. }

  69. void mpu_loop()
  70. {
  71.   float alpha,omiga;

  72.   // if programming failed, don't try to do anything
  73.   if (!dmpReady)
  74.     return;

  75.   // wait for MPU interrupt or extra packet(s) available
  76.   if (!mpuInterrupt && fifoCount < packetSize)
  77.     return;

  78.   // reset interrupt flag and get INT_STATUS byte
  79.   mpuInterrupt = false;
  80.   mpuIntStatus = mpu.getIntStatus();

  81.   // get current FIFO count
  82.   fifoCount = mpu.getFIFOCount();

  83.   // check for overflow (this should never happen unless our code is too inefficient)
  84.   if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
  85.     // reset so we can continue cleanly
  86.     mpu.resetFIFO();
  87.     Serial.println("FIFO overflow!");

  88.     // otherwise, check for DMP data ready interrupt (this should happen frequently)
  89.   }
  90.   else if (mpuIntStatus & 0x02) {
  91.     // wait for correct available data length, should be a VERY short wait
  92.     while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();

  93.     // read a packet from FIFO
  94.     mpu.getFIFOBytes(fifoBuffer, packetSize);

  95.     // track FIFO count here in case there is > 1 packet available
  96.     // (this lets us immediately read more without waiting for an interrupt)
  97.     fifoCount -= packetSize;

  98.     mpu.dmpGetQuaternion(&q, fifoBuffer);
  99.     mpu.dmpGetGravity(&gravity, &q);
  100.     mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);  //从DMP中取出Yaw、Pitch、Roll三个轴的角度,放入数组ypr。单位:弧度
  101.     alpha=-ypr[2] * 180/M_PI;

  102.     omiga=mpu.getRotationX()/16.4;        //配置是16位表示正负2000°/s, 65536/4000

  103.     Serial.print("Alpha ");
  104.     Serial.print(alpha);//x轴平行偏移角度
  105.     Serial.print("\tOmiga ");
  106.     Serial.println(omiga);//对应加速度力道

  107.   }
  108. }
复制代码


另外谁研究过dmp的电子罗盘接入?
请分享下
回复

使用道具 举报

发表于 2013-9-5 15:19:15 | 显示全部楼层
您好:
請問陀螺儀或加速器的中斷口是做什麼用的啊??

謝謝
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-9-5 15:34:52 | 显示全部楼层
TTTTTTT33 发表于 2013-9-5 15:19
您好:
請問陀螺儀或加速器的中斷口是做什麼用的啊??

中断功能很多,比如fifo缓冲溢出、数据准备完毕,都会使用这个中断进行触发,还有我记得文档内还有mpu的跌落中断,好像是当失控跌落的时候也会产生中断
使用mpu.getIntStatus(); 可以获取到此次中断的状态
回复 支持 反对

使用道具 举报

发表于 2013-9-5 23:32:17 | 显示全部楼层
有空找个机会测试一下楼主的代码,谢谢楼主分享
回复 支持 反对

使用道具 举报

发表于 2014-3-19 16:51:30 | 显示全部楼层
楼主,试过滚角Roll没?很怪异,数值乱变!
回复 支持 反对

使用道具 举报

发表于 2014-9-1 11:13:22 | 显示全部楼层
有空测试一下
回复 支持 反对

使用道具 举报

发表于 2014-10-11 22:19:20 | 显示全部楼层
好多东西没搞明白 尤其是中断
回复 支持 反对

使用道具 举报

发表于 2014-10-17 22:17:49 | 显示全部楼层
1.6.5版本编译通不过
回复 支持 反对

使用道具 举报

发表于 2015-4-7 23:10:18 | 显示全部楼层
敢问楼主的库在哪里下载?
回复 支持 反对

使用道具 举报

发表于 2016-2-20 09:08:20 | 显示全部楼层
DMP 运算角度的精度如何
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-15 13:34 , Processed in 0.144858 second(s), 26 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表