极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 12637|回复: 7

MPU6050零基础 打飞机?

[复制链接]
发表于 2014-10-3 15:48:19 | 显示全部楼层 |阅读模式


processing 下载地址(跟arduino一样,解压就能用):
https://processing.org/download/?processing


processing画图用到的的库在这里下:
http://hg.postspectacular.com/toxiclibs/downloads/


processing的库放在这里:
processing-2.2.1\modes\java\libraries

    // open the serial port
    port = new Serial(this, "COM3", 115200); //processing程序MPUTeapot.pde很关键的一个地方,串口号、波特率


打飞机arduino端:
https://github.com/jrowberg/i2cdevlib/tree/master/Arduino/MPU6050


不用中断的arduino程序:

  1. #include "I2Cdev.h"

  2. #include "MPU6050_6Axis_MotionApps20.h"
  3. //#include "MPU6050.h" // not necessary if using MotionApps include file
  4. #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
  5.     #include "Wire.h"
  6. #endif

  7. MPU6050 mpu;
  8. //MPU6050 mpu(0x69); // <-- use for AD0 high

  9. // uncomment "OUTPUT_TEAPOT" if you want output that matches the
  10. // format used for the InvenSense teapot demo
  11. #define OUTPUT_TEAPOT


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

  19. // orientation/motion vars
  20. Quaternion q;           // [w, x, y, z]         quaternion container
  21. VectorInt16 aa;         // [x, y, z]            accel sensor measurements
  22. VectorInt16 aaReal;     // [x, y, z]            gravity-free accel sensor measurements
  23. VectorInt16 aaWorld;    // [x, y, z]            world-frame accel sensor measurements
  24. VectorFloat gravity;    // [x, y, z]            gravity vector
  25. float euler[3];         // [psi, theta, phi]    Euler angle container
  26. float ypr[3];           // [yaw, pitch, roll]   yaw/pitch/roll container and gravity vector

  27. // packet structure for InvenSense teapot demo
  28. uint8_t teapotPacket[14] = { '$', 0x02, 0,0, 0,0, 0,0, 0,0, 0x00, 0x00, '\r', '\n' };



  29. // ================================================================
  30. // ===               INTERRUPT DETECTION ROUTINE                ===
  31. // ================================================================

  32. volatile bool mpuInterrupt = false;     // indicates whether MPU interrupt pin has gone high
  33. void dmpDataReady() {
  34.     mpuInterrupt = true;
  35. }



  36. // ================================================================
  37. // ===                      INITIAL SETUP                       ===
  38. // ================================================================

  39. void setup() {
  40.     // join I2C bus (I2Cdev library doesn't do this automatically)
  41.     #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
  42.         Wire.begin();
  43.         TWBR = 24; // 400kHz I2C clock (200kHz if CPU is 8MHz)
  44.     #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
  45.         Fastwire::setup(400, true);
  46.     #endif

  47.     // initialize serial communication
  48.     // (115200 chosen because it is required for Teapot Demo output, but it's
  49.     // really up to you depending on your project)
  50.     Serial.begin(115200);
  51.   //  while (!Serial); // wait for Leonardo enumeration, others continue immediately

  52.     // NOTE: 8MHz or slower host processors, like the Teensy @ 3.3v or Ardunio
  53.     // Pro Mini running at 3.3v, cannot handle this baud rate reliably due to
  54.     // the baud timing being too misaligned with processor ticks. You must use
  55.     // 38400 or slower in these cases, or use some kind of external separate
  56.     // crystal solution for the UART timer.

  57.     // initialize device
  58.     Serial.println(F("Initializing I2C devices..."));
  59.     mpu.initialize();

  60.     // verify connection
  61.     Serial.println(F("Testing device connections..."));
  62.     Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));

  63.     // wait for ready
  64.     Serial.println(F("\nSend any character to begin DMP programming and demo: "));
  65.     //while (Serial.available() && Serial.read()); // empty buffer
  66.     //while (!Serial.available());                 // wait for data
  67.     //while (Serial.available() && Serial.read()); // empty buffer again

  68.     // load and configure the DMP
  69.     //Serial.println(F("Initializing DMP..."));
  70.     devStatus = mpu.dmpInitialize();

  71.     // supply your own gyro offsets here, scaled for min sensitivity
  72.     mpu.setXGyroOffset(220);
  73.     mpu.setYGyroOffset(76);
  74.     mpu.setZGyroOffset(-85);
  75.     mpu.setZAccelOffset(1788); // 1688 factory default for my test chip

  76.     // make sure it worked (returns 0 if so)
  77.     if (devStatus == 0) {
  78.         // turn on the DMP, now that it's ready
  79.         Serial.println(F("Enabling DMP..."));
  80.         mpu.setDMPEnabled(true);

  81.         // enable Arduino interrupt detection
  82.         Serial.println(F("Enabling interrupt detection (Arduino external interrupt 0)..."));
  83.         attachInterrupt(0, dmpDataReady, RISING);
  84.         mpuIntStatus = mpu.getIntStatus();

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

  88.         // get expected DMP packet size for later comparison
  89.         packetSize = mpu.dmpGetFIFOPacketSize();
  90.     } else {
  91.         // ERROR!
  92.         // 1 = initial memory load failed
  93.         // 2 = DMP configuration updates failed
  94.         // (if it's going to break, usually the code will be 1)
  95.         Serial.print(F("DMP Initialization failed (code "));
  96.         Serial.print(devStatus);
  97.         Serial.println(F(")"));
  98.     }


  99. }



  100. // ================================================================
  101. // ===                    MAIN PROGRAM LOOP                     ===
  102. // ================================================================

  103. void loop() {
  104.     // if programming failed, don't try to do anything
  105.     if (!dmpReady) return;



  106.     // reset interrupt flag and get INT_STATUS byte
  107.     mpuInterrupt = false;
  108.     mpuIntStatus = mpu.getIntStatus();

  109.     // get current FIFO count
  110.     fifoCount = mpu.getFIFOCount();

  111.     // check for overflow (this should never happen unless our code is too inefficient)
  112.     if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
  113.         // reset so we can continue cleanly
  114.         mpu.resetFIFO();
  115.         Serial.println(F("FIFO overflow!"));

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

  120.         // read a packet from FIFO
  121.         mpu.getFIFOBytes(fifoBuffer, packetSize);
  122.         
  123.         // track FIFO count here in case there is > 1 packet available
  124.         // (this lets us immediately read more without waiting for an interrupt)
  125.         fifoCount -= packetSize;


  126.    
  127.         #ifdef OUTPUT_TEAPOT
  128.             // display quaternion values in InvenSense Teapot demo format:
  129.             teapotPacket[2] = fifoBuffer[0];
  130.             teapotPacket[3] = fifoBuffer[1];
  131.             teapotPacket[4] = fifoBuffer[4];
  132.             teapotPacket[5] = fifoBuffer[5];
  133.             teapotPacket[6] = fifoBuffer[8];
  134.             teapotPacket[7] = fifoBuffer[9];
  135.             teapotPacket[8] = fifoBuffer[12];
  136.             teapotPacket[9] = fifoBuffer[13];
  137.             Serial.write(teapotPacket, 14);
  138.             teapotPacket[11]++; // packetCount, loops at 0xFF on purpose
  139.         #endif


  140.     }
  141. }
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

 楼主| 发表于 2014-10-3 16:19:44 | 显示全部楼层
PS:arduino Pro mini 吧波特率改为38400效果更好
回复 支持 反对

使用道具 举报

发表于 2014-10-3 20:36:20 | 显示全部楼层
楼主很强大,给楼主点赞先。
回复 支持 反对

使用道具 举报

发表于 2014-10-4 09:02:08 | 显示全部楼层
没看懂,具体讲的是什么?
回复 支持 反对

使用道具 举报

发表于 2014-10-4 16:41:16 | 显示全部楼层
你写的东西玩过的人一眼就知道是什么,没玩过的也看不明白,因该写的再明白些
回复 支持 反对

使用道具 举报

发表于 2014-10-4 21:43:09 | 显示全部楼层
讲解讲解?
回复 支持 反对

使用道具 举报

发表于 2014-10-5 22:05:52 | 显示全部楼层
求超详细的说明哦!
回复 支持 反对

使用道具 举报

发表于 2015-2-24 20:52:44 | 显示全部楼层
你这个代码就是用了dmp中断的啊, 和官方库里的没什么不同 .(手册上没有介绍这个dmp中断,应该是中断标志寄存器的第1位)
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-17 10:57 , Processed in 0.057458 second(s), 26 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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