久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 4591|回復: 1
打印 上一主題 下一主題
收起左側

arduino可用的mpu6050 demo

[復制鏈接]
跳轉到指定樓層
樓主
ID:287855 發表于 2018-3-5 17:27 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
  1. // I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files
  2. // for both classes must be in the include path of your project
  3. #include "I2Cdev.h"
  4. #include "MPU6050_6Axis_MotionApps20.h"
  5. //#include "MPU6050.h" // not necessary if using MotionApps include file
  6. // Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation
  7. // is used in I2Cdev.h
  8. #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
  9.     #include "Wire.h"
  10. #endif
  11. // class default I2C address is 0x68
  12. // specific I2C addresses may be passed as a parameter here
  13. // AD0 low = 0x68 (default for SparkFun breakout and InvenSense evaluation board)
  14. // AD0 high = 0x69
  15. MPU6050 mpu;
  16. //MPU6050 mpu(0x69); // <-- use for AD0 high
  17. /* =========================================================================
  18.    NOTE: In addition to connection 3.3v, GND, SDA, and SCL, this sketch
  19.    depends on the MPU-6050's INT pin being connected to the Arduino's
  20.    external interrupt #0 pin. On the Arduino Uno and Mega 2560, this is
  21.    digital I/O pin 2.
  22. * ========================================================================= */
  23. /* =========================================================================
  24.    NOTE: Arduino v1.0.1 with the Leonardo board generates a compile error
  25.    when using Serial.write(buf, len). The Teapot output uses this method.
  26.    The solution requires a modification to the Arduino USBAPI.h file, which
  27.    is fortunately simple, but annoying. This will be fixed in the next IDE
  28.    release. For more info, see these links:
  29.    http://arduino.cc/forum/index.php/topic,109987.0.html
  30.    http://code.google.com/p/arduino/issues/detail?id=958
  31. * ========================================================================= */

  32. // uncomment "OUTPUT_READABLE_QUATERNION" if you want to see the actual
  33. // quaternion components in a [w, x, y, z] format (not best for parsing
  34. // on a remote host such as Processing or something though)
  35. //#define OUTPUT_READABLE_QUATERNION
  36. // uncomment "OUTPUT_READABLE_EULER" if you want to see Euler angles
  37. // (in degrees) calculated from the quaternions coming from the FIFO.
  38. // Note that Euler angles suffer from gimbal lock (for more info, see
  39. //#define OUTPUT_READABLE_EULER
  40. // uncomment "OUTPUT_READABLE_YAWPITCHROLL" if you want to see the yaw/
  41. // pitch/roll angles (in degrees) calculated from the quaternions coming
  42. // from the FIFO. Note this also requires gravity vector calculations.
  43. // Also note that yaw/pitch/roll angles suffer from gimbal lock (for
  44. #define OUTPUT_READABLE_YAWPITCHROLL
  45. // uncomment "OUTPUT_READABLE_REALACCEL" if you want to see acceleration
  46. // components with gravity removed. This acceleration reference frame is
  47. // not compensated for orientation, so +X is always +X according to the
  48. // sensor, just without the effects of gravity. If you want acceleration
  49. // compensated for orientation, us OUTPUT_READABLE_WORLDACCEL instead.
  50. //#define OUTPUT_READABLE_REALACCEL
  51. // uncomment "OUTPUT_READABLE_WORLDACCEL" if you want to see acceleration
  52. // components with gravity removed and adjusted for the world frame of
  53. // reference (yaw is relative to initial orientation, since no magnetometer
  54. // is present in this case). Could be quite handy in some cases.
  55. //#define OUTPUT_READABLE_WORLDACCEL
  56. // uncomment "OUTPUT_TEAPOT" if you want output that matches the
  57. // format used for the InvenSense teapot demo
  58. //#define OUTPUT_TEAPOT

  59. #define INTERRUPT_PIN 2  // use pin 2 on Arduino Uno & most boards
  60. #define LED_PIN 13 // (Arduino is 13, Teensy is 11, Teensy++ is 6)
  61. bool blinkState = false;
  62. // MPU control/status vars
  63. bool dmpReady = false;  // set true if DMP init was successful
  64. uint8_t mpuIntStatus;   // holds actual interrupt status byte from MPU
  65. uint8_t devStatus;      // return status after each device operation (0 = success, !0 = error)
  66. uint16_t packetSize;    // expected DMP packet size (default is 42 bytes)
  67. uint16_t fifoCount;     // count of all bytes currently in FIFO
  68. uint8_t fifoBuffer[64]; // FIFO storage buffer
  69. // orientation/motion vars
  70. Quaternion q;           // [w, x, y, z]         quaternion container
  71. VectorInt16 aa;         // [x, y, z]            accel sensor measurements
  72. VectorInt16 aaReal;     // [x, y, z]            gravity-free accel sensor measurements
  73. VectorInt16 aaWorld;    // [x, y, z]            world-frame accel sensor measurements
  74. VectorFloat gravity;    // [x, y, z]            gravity vector
  75. float euler[3];         // [psi, theta, phi]    Euler angle container
  76. float ypr[3];           // [yaw, pitch, roll]   yaw/pitch/roll container and gravity vector
  77. // packet structure for InvenSense teapot demo
  78. uint8_t teapotPacket[14] = { '

  79. , 0x02, 0,0, 0,0, 0,0, 0,0, 0x00, 0x00, '\r', '\n' };

  80. // ================================================================
  81. // ===               INTERRUPT DETECTION ROUTINE                ===
  82. // ================================================================
  83. volatile bool mpuInterrupt = false;     // indicates whether MPU interrupt pin has gone high
  84. void dmpDataReady() {
  85.     mpuInterrupt = true;
  86. }

  87. // ================================================================
  88. // ===                      INITIAL SETUP                       ===
  89. // ================================================================
  90. void setup() {
  91.     // join I2C bus (I2Cdev library doesn't do this automatically)
  92.     #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
  93.         Wire.begin();
  94.         Wire.setClock(400000); // 400kHz I2C clock. Comment this line if having compilation difficulties
  95.     #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
  96.         Fastwire::setup(400, true);
  97.     #endif
  98.     // initialize serial communication
  99.     // (115200 chosen because it is required for Teapot Demo output, but it's
  100.     // really up to you depending on your project)
  101.     Serial.begin(115200);
  102.     while (!Serial); // wait for Leonardo enumeration, others continue immediately
  103.     // NOTE: 8MHz or slower host processors, like the Teensy @ 3.3v or Ardunio
  104.     // Pro Mini running at 3.3v, cannot handle this baud rate reliably due to
  105.     // the baud timing being too misaligned with processor ticks. You must use
  106.     // 38400 or slower in these cases, or use some kind of external separate
  107.     // crystal solution for the UART timer.
  108.     // initialize device
  109.     Serial.println(F("Initializing I2C devices..."));
  110.     mpu.initialize();
  111.     pinMode(INTERRUPT_PIN, INPUT);
  112.     // verify connection
  113.     Serial.println(F("Testing device connections..."));
  114.     Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));
  115.     // wait for ready
  116.     Serial.println(F("\nSend any character to begin DMP programming and demo: "));
  117.     while (Serial.available() && Serial.read()); // empty buffer
  118.     while (!Serial.available());                 // wait for data
  119.     while (Serial.available() && Serial.read()); // empty buffer again
  120.     // load and configure the DMP
  121.     Serial.println(F("Initializing DMP..."));
  122.     devStatus = mpu.dmpInitialize();
  123.     // supply your own gyro offsets here, scaled for min sensitivity
  124.     mpu.setXGyroOffset(220);
  125.     mpu.setYGyroOffset(76);
  126.     mpu.setZGyroOffset(-85);
  127.     mpu.setZAccelOffset(1788); // 1688 factory default for my test chip
  128.     // make sure it worked (returns 0 if so)
  129.     if (devStatus == 0) {
  130.         // turn on the DMP, now that it's ready
  131.         Serial.println(F("Enabling DMP..."));
  132.         mpu.setDMPEnabled(true);
  133.         // enable Arduino interrupt detection
  134.         Serial.println(F("Enabling interrupt detection (Arduino external interrupt 0)..."));
  135.         attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), dmpDataReady, RISING);
  136.         mpuIntStatus = mpu.getIntStatus();
  137.         // set our DMP Ready flag so the main loop() function knows it's okay to use it
  138.         Serial.println(F("DMP ready! Waiting for first interrupt..."));
  139.         dmpReady = true;
  140.         // get expected DMP packet size for later comparison
  141.         packetSize = mpu.dmpGetFIFOPacketSize();
  142.     } else {
  143.         // ERROR!
  144.         // 1 = initial memory load failed
  145.         // 2 = DMP configuration updates failed
  146.         // (if it's going to break, usually the code will be 1)
  147.         Serial.print(F("DMP Initialization failed (code "));
  148.         Serial.print(devStatus);
  149.         Serial.println(F(")"));
  150.     }
  151.     // configure LED for output
  152.     pinMode(LED_PIN, OUTPUT);
  153. }

  154. // ================================================================
  155. // ===                    MAIN PROGRAM LOOP                     ===
  156. // ================================================================
  157. void loop() {
  158.     // if programming failed, don't try to do anything
  159.     if (!dmpReady) return;
  160.     // wait for MPU interrupt or extra packet(s) available
  161.     while (!mpuInterrupt && fifoCount < packetSize) {
  162.         // other program behavior stuff here
  163.         // .
  164.         // .
  165.         // .
  166.         // if you are really paranoid you can frequently test in between other
  167.         // stuff to see if mpuInterrupt is true, and if so, "break;" from the
  168.         // while() loop to immediately process the MPU data
  169.         // .
  170.         // .
  171.         // .
  172.     }
  173.     // reset interrupt flag and get INT_STATUS byte
  174.     mpuInterrupt = false;
  175.     mpuIntStatus = mpu.getIntStatus();
  176.     // get current FIFO count
  177.     fifoCount = mpu.getFIFOCount();
  178.     // check for overflow (this should never happen unless our code is too inefficient)
  179.     if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
  180.         // reset so we can continue cleanly
  181.         mpu.resetFIFO();
  182.         Serial.println(F("FIFO overflow!"));
  183.     // otherwise, check for DMP data ready interrupt (this should happen frequently)
  184.     } else if (mpuIntStatus & 0x02) {
  185.         // wait for correct available data length, should be a VERY short wait
  186.         while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();
  187.         // read a packet from FIFO
  188.         mpu.getFIFOBytes(fifoBuffer, packetSize);
  189.         
  190.         // track FIFO count here in case there is > 1 packet available
  191.         // (this lets us immediately read more without waiting for an interrupt)
  192.         fifoCount -= packetSize;
  193.         #ifdef OUTPUT_READABLE_QUATERNION
  194.             // display quaternion values in easy matrix form: w x y z
  195.             mpu.dmpGetQuaternion(&q, fifoBuffer);
  196.             Serial.print("quat\t");
  197.             Serial.print(q.w);
  198.             Serial.print("\t");
  199.             Serial.print(q.x);
  200.             Serial.print("\t");
  201.             Serial.print(q.y);
  202.             Serial.print("\t");
  203.             Serial.println(q.z);
  204.         #endif
  205.         #ifdef OUTPUT_READABLE_EULER
  206.             // display Euler angles in degrees
  207.             mpu.dmpGetQuaternion(&q, fifoBuffer);
  208.             mpu.dmpGetEuler(euler, &q);
  209.             Serial.print("euler\t");
  210.             Serial.print(euler[0] * 180/M_PI);
  211.             Serial.print("\t");
  212.             Serial.print(euler[1] * 180/M_PI);
  213.             Serial.print("\t");
  214.             Serial.println(euler[2] * 180/M_PI);
  215.         #endif
  216.         #ifdef OUTPUT_READABLE_YAWPITCHROLL
  217.             // display Euler angles in degrees
  218.             mpu.dmpGetQuaternion(&q, fifoBuffer);
  219.             mpu.dmpGetGravity(&gravity, &q);
  220.             mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
  221.             Serial.print("ypr\t");
  222.             Serial.print(ypr[0] * 180/M_PI);
  223.             Serial.print("\t");
  224.             Serial.print(ypr[1] * 180/M_PI);
  225.             Serial.print("\t");
  226.             Serial.println(ypr[2] * 180/M_PI);
  227.         #endif
  228.         #ifdef OUTPUT_READABLE_REALACCEL
  229.             // display real acceleration, adjusted to remove gravity
  230.             mpu.dmpGetQuaternion(&q, fifoBuffer);
  231.             mpu.dmpGetAccel(&aa, fifoBuffer);
  232.             mpu.dmpGetGravity(&gravity, &q);
  233.             mpu.dmpGetLinearAccel(&aaReal, &aa, &gravity);
  234.             Serial.print("areal\t");
  235.             Serial.print(aaReal.x);
  236.             Serial.print("\t");
  237.             Serial.print(aaReal.y);
  238.             Serial.print("\t");
  239.             Serial.println(aaReal.z);
  240.         #endif
  241.         #ifdef OUTPUT_READABLE_WORLDACCEL
  242.             // display initial world-frame acceleration, adjusted to remove gravity
  243.             // and rotated based on known orientation from quaternion
  244.             mpu.dmpGetQuaternion(&q, fifoBuffer);
  245.             mpu.dmpGetAccel(&aa, fifoBuffer);
  246.             mpu.dmpGetGravity(&gravity, &q);
  247.             mpu.dmpGetLinearAccel(&aaReal, &aa, &gravity);
  248.             mpu.dmpGetLinearAccelInWorld(&aaWorld, &aaReal, &q);
  249.             Serial.print("aworld\t");
  250.             Serial.print(aaWorld.x);
  251.             Serial.print("\t");
  252.             Serial.print(aaWorld.y);
  253.             Serial.print("\t");
  254.             Serial.println(aaWorld.z);
  255.         #endif
  256.    
  257.         #ifdef OUTPUT_TEAPOT
  258.             // display quaternion values in InvenSense Teapot demo format:
  259.             teapotPacket[2] = fifoBuffer[0];
  260.             teapotPacket[3] = fifoBuffer[1];
  261.             teapotPacket[4] = fifoBuffer[4];
  262.             teapotPacket[5] = fifoBuffer[5];
  263.             teapotPacket[6] = fifoBuffer[8];
  264.             teapotPacket[7] = fifoBuffer[9];
  265.             teapotPacket[8] = fifoBuffer[12];
  266.             teapotPacket[9] = fifoBuffer[13];
  267.             Serial.write(teapotPacket, 14);
  268.             teapotPacket[11]++; // packetCount, loops at 0xFF on purpose
  269.         #endif
  270.         // blink LED to indicate activity
  271.         blinkState = !blinkState;
  272.         digitalWrite(LED_PIN, blinkState);
  273.     }
  274. }
復制代碼


MPU6050.zip

69.64 KB, 下載次數: 30, 下載積分: 黑幣 -5

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

沙發
ID:79544 發表于 2019-10-15 11:50 | 只看該作者
樓主在嗎?串口在嗎沒有輸出啊?
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 一级免费毛片 | 最近日韩中文字幕 | 久久精品亚洲一区二区三区浴池 | 亚洲视频中文字幕 | 亚洲免费在线播放 | 紧缚调教一区二区三区视频 | 日韩毛片免费看 | 免费成人高清在线视频 | 操久久 | 免费观看一级特黄欧美大片 | 中文在线一区二区 | 精品一区二区av | 成人激情视频在线 | 欧美jizzhd精品欧美巨大免费 | 在线观看av网站永久 | 欧美性久久 | 麻豆国产一区二区三区四区 | 国产区精品视频 | 夜夜爽99久久国产综合精品女不卡 | 亚洲国产网站 | 国产精品九九视频 | 国产欧美日韩视频 | 亚洲欧美日韩电影 | 国产精品海角社区在线观看 | 超碰婷婷 | 欧洲精品一区 | 亚洲精品久久久久久一区二区 | 国产在线二区 | 久久国产精品精品国产色婷婷 | 国产日韩精品一区二区 | 日韩一区二区三区在线观看 | 国产精品日韩一区二区 | 自拍偷拍亚洲欧美 | 国产成人精品一区二 | 国产一区二区三区四区五区3d | 国产极品粉嫩美女呻吟在线看人 | 亚洲高清电影 | 亚洲精品一区二区网址 | 亚洲免费人成在线视频观看 | 欧美综合久久 | 久久精品欧美一区二区三区不卡 |