极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 14046|回复: 11

mpu6050 DMP数据写SD卡出问题了,能帮忙看下吗?

[复制链接]
发表于 2013-11-5 11:33:58 | 显示全部楼层 |阅读模式
本帖最后由 龙翔竞天 于 2013-11-5 12:19 编辑

mpu6050设置用的是I2C库里面的例子mup6050_dmp.ino
输出的三轴加速度和陀螺仪值很稳定。
现在想把六个值写入SD卡中,一开始几个数据能够读取。后来就间断性的显示FIFO overflow!
不停的显示error opening datalog.txt
请大家帮忙看下,谢谢了
代码贴一下:

  1. // I2C device class (I2Cdev) demonstration Arduino sketch for MPU6050 class using DMP (MotionApps v2.0)
  2. // 6/21/2012 by Jeff Rowberg <[email protected]>
  3. // Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
  4. //
  5. // Changelog:
  6. // 2013-05-08 - added seamless Fastwire support
  7. // - added note about gyro calibration
  8. // 2012-06-21 - added note about Arduino 1.0.1 + Leonardo compatibility error
  9. // 2012-06-20 - improved FIFO overflow handling and simplified read process
  10. // 2012-06-19 - completely rearranged DMP initialization code and simplification
  11. // 2012-06-13 - pull gyro and accel data from FIFO packet instead of reading directly
  12. // 2012-06-09 - fix broken FIFO read sequence and change interrupt detection to RISING
  13. // 2012-06-05 - add gravity-compensated initial reference frame acceleration output
  14. // - add 3D math helper file to DMP6 example sketch
  15. // - add Euler output and Yaw/Pitch/Roll output formats
  16. // 2012-06-04 - remove accel offset clearing for better results (thanks Sungon Lee)
  17. // 2012-06-01 - fixed gyro sensitivity to be 2000 deg/sec instead of 250
  18. // 2012-05-30 - basic DMP initialization working

  19. /* ============================================
  20. I2Cdev device library code is placed under the MIT license
  21. Copyright (c) 2012 Jeff Rowberg

  22. Permission is hereby granted, free of charge, to any person obtaining a copy
  23. of this software and associated documentation files (the "Software"), to deal
  24. in the Software without restriction, including without limitation the rights
  25. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  26. copies of the Software, and to permit persons to whom the Software is
  27. furnished to do so, subject to the following conditions:

  28. The above copyright notice and this permission notice shall be included in
  29. all copies or substantial portions of the Software.

  30. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  31. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  32. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  33. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  34. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  35. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  36. THE SOFTWARE.
  37. ===============================================
  38. */

  39. // I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files
  40. // for both classes must be in the include path of your project
  41. #include "I2Cdev.h"
  42. #include <SD.h>
  43. const int chipSelect = 4; //设定CS接口
  44. #include "MPU6050_6Axis_MotionApps20.h"
  45. //#include "MPU6050.h" // not necessary if using MotionApps include file

  46. // Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation
  47. // is used in I2Cdev.h
  48. #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
  49. #include "Wire.h"
  50. #endif

  51. // class default I2C address is 0x68
  52. // specific I2C addresses may be passed as a parameter here
  53. // AD0 low = 0x68 (default for SparkFun breakout and InvenSense evaluation board)
  54. // AD0 high = 0x69
  55. MPU6050 mpu;
  56. //MPU6050 mpu(0x69); // <-- use for AD0 high

  57. /* =========================================================================
  58. NOTE: In addition to connection 3.3v, GND, SDA, and SCL, this sketch
  59. depends on the MPU-6050's INT pin being connected to the Arduino's
  60. external interrupt #0 pin. On the Arduino Uno and Mega 2560, this is
  61. digital I/O pin 2.
  62. * ========================================================================= */

  63. /* =========================================================================
  64. NOTE: Arduino v1.0.1 with the Leonardo board generates a compile error
  65. when using Serial.write(buf, len). The Teapot output uses this method.
  66. The solution requires a modification to the Arduino USBAPI.h file, which
  67. is fortunately simple, but annoying. This will be fixed in the next IDE
  68. release. For more info, see these links:

  69. http://arduino.cc/forum/index.php/topic,109987.0.html
  70. http://code.google.com/p/arduino/issues/detail?id=958
  71. * ========================================================================= */



  72. // uncomment "OUTPUT_READABLE_QUATERNION" if you want to see the actual
  73. // quaternion components in a [w, x, y, z] format (not best for parsing
  74. // on a remote host such as Processing or something though)
  75. //#define OUTPUT_READABLE_QUATERNION

  76. // uncomment "OUTPUT_READABLE_EULER" if you want to see Euler angles
  77. // (in degrees) calculated from the quaternions coming from the FIFO.
  78. // Note that Euler angles suffer from gimbal lock (for more info, see
  79. // http://en.wikipedia.org/wiki/Gimbal_lock)
  80. //#define OUTPUT_READABLE_EULER

  81. // uncomment "OUTPUT_READABLE_YAWPITCHROLL" if you want to see the yaw/
  82. // pitch/roll angles (in degrees) calculated from the quaternions coming
  83. // from the FIFO. Note this also requires gravity vector calculations.
  84. // Also note that yaw/pitch/roll angles suffer from gimbal lock (for
  85. // more info, see: http://en.wikipedia.org/wiki/Gimbal_lock)
  86. //#define OUTPUT_READABLE_YAWPITCHROLL

  87. // uncomment "OUTPUT_READABLE_REALACCEL" if you want to see acceleration
  88. // components with gravity removed. This acceleration reference frame is
  89. // not compensated for orientation, so +X is always +X according to the
  90. // sensor, just without the effects of gravity. If you want acceleration
  91. // compensated for orientation, us OUTPUT_READABLE_WORLDACCEL instead.
  92. #define OUTPUT_READABLE_REALACCEL

  93. // uncomment "OUTPUT_READABLE_WORLDACCEL" if you want to see acceleration
  94. // components with gravity removed and adjusted for the world frame of
  95. // reference (yaw is relative to initial orientation, since no magnetometer
  96. // is present in this case). Could be quite handy in some cases.
  97. //#define OUTPUT_READABLE_WORLDACCEL

  98. // uncomment "OUTPUT_TEAPOT" if you want output that matches the
  99. // format used for the InvenSense teapot demo
  100. //#define OUTPUT_TEAPOT



  101. #define LED_PIN 13 // (Arduino is 13, Teensy is 11, Teensy++ is 6)
  102. bool blinkState = false;

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

  110. // orientation/motion vars
  111. Quaternion q; // [w, x, y, z] quaternion container
  112. VectorInt16 aa; // [x, y, z] accel sensor measurements
  113. VectorInt16 aaReal; // [x, y, z] gravity-free accel sensor measurements
  114. VectorInt16 aaWorld; // [x, y, z] world-frame accel sensor measurements
  115. VectorFloat gravity; // [x, y, z] gravity vector
  116. float euler[3]; // [psi, theta, phi] Euler angle container
  117. float ypr[3]; // [yaw, pitch, roll] yaw/pitch/roll container and gravity vector

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

  120. // ================================================================
  121. // === INTERRUPT DETECTION ROUTINE ===
  122. // ================================================================

  123. volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high
  124. void dmpDataReady() {
  125. mpuInterrupt = true;
  126. }



  127. // ================================================================
  128. // === INITIAL SETUP ===
  129. // ================================================================

  130. void setup() {
  131. // join I2C bus (I2Cdev library doesn't do this automatically)
  132. #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
  133. Wire.begin();
  134. TWBR = 24; // 400kHz I2C clock (200kHz if CPU is 8MHz)
  135. #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
  136. Fastwire::setup(400, true);
  137. #endif

  138. // initialize serial communication
  139. // (115200 chosen because it is required for Teapot Demo output, but it's
  140. // really up to you depending on your project)
  141. Serial.begin(9600);
  142. while (!Serial); // wait for Leonardo enumeration, others continue immediately

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

  148. // initialize device
  149. Serial.println(F("Initializing I2C devices..."));
  150. mpu.initialize();

  151. // verify connection
  152. Serial.println(F("Testing device connections..."));
  153. Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));

  154. // wait for ready
  155. // Serial.println(F("\nSend any character to begin DMP programming and demo: "));
  156. //while (Serial.available() && Serial.read()); // empty buffer
  157. // while (!Serial.available()); // wait for data
  158. //while (Serial.available() && Serial.read()); // empty buffer again

  159. // load and configure the DMP
  160. Serial.println(F("Initializing DMP..."));
  161. devStatus = mpu.dmpInitialize();

  162. // supply your own gyro offsets here, scaled for min sensitivity
  163. mpu.setXGyroOffset(220);
  164. mpu.setYGyroOffset(76);
  165. mpu.setZGyroOffset(-85);
  166. mpu.setZAccelOffset(1788); // 1688 factory default for my test chip

  167. // make sure it worked (returns 0 if so)
  168. if (devStatus == 0) {
  169. // turn on the DMP, now that it's ready
  170. Serial.println(F("Enabling DMP..."));
  171. mpu.setDMPEnabled(true);

  172. // enable Arduino interrupt detection
  173. Serial.println(F("Enabling interrupt detection (Arduino external interrupt 0)..."));
  174. attachInterrupt(0, dmpDataReady, RISING);
  175. mpuIntStatus = mpu.getIntStatus();

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

  179. // get expected DMP packet size for later comparison
  180. packetSize = mpu.dmpGetFIFOPacketSize();
  181. } else {
  182. // ERROR!
  183. // 1 = initial memory load failed
  184. // 2 = DMP configuration updates failed
  185. // (if it's going to break, usually the code will be 1)
  186. Serial.print(F("DMP Initialization failed (code "));
  187. Serial.print(devStatus);
  188. Serial.println(F(")"));
  189. }

  190. // configure LED for output
  191. pinMode(LED_PIN, OUTPUT);
  192. pinMode(10, OUTPUT); //SD


  193. if (!SD.begin(chipSelect)) { //如果从CS口与SD卡通信失败,串口输出信息Card failed, or not present
  194. Serial.println("Card failed, or not present");
  195. return;
  196. }
  197. Serial.println("card initialized."); //与SD卡通信成功,串口输出信息card initialized.

  198. }



  199. // ================================================================
  200. // === MAIN PROGRAM LOOP ===
  201. // ================================================================

  202. void loop() {
  203. int16_t ax, ay, az;
  204. int16_t gx, gy, gz;
  205. File dataFile = SD.open("datalog.txt", FILE_WRITE);
  206. // if programming failed, don't try to do anything
  207. if (!dmpReady) return;

  208. // wait for MPU interrupt or extra packet(s) available
  209. while (!mpuInterrupt && fifoCount < packetSize) {
  210. // other program behavior stuff here
  211. // .
  212. // .
  213. // .
  214. // if you are really paranoid you can frequently test in between other
  215. // stuff to see if mpuInterrupt is true, and if so, "break;" from the
  216. // while() loop to immediately process the MPU data
  217. // .
  218. // .
  219. // .
  220. }

  221. // reset interrupt flag and get INT_STATUS byte
  222. mpuInterrupt = false;
  223. mpuIntStatus = mpu.getIntStatus();

  224. // get current FIFO count
  225. fifoCount = mpu.getFIFOCount();

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

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

  235. // read a packet from FIFO
  236. mpu.getFIFOBytes(fifoBuffer, packetSize);

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

  240. #ifdef OUTPUT_READABLE_QUATERNION
  241. // display quaternion values in easy matrix form: w x y z
  242. mpu.dmpGetQuaternion(&q, fifoBuffer);
  243. Serial.print("quat\t");
  244. Serial.print(q.w);
  245. Serial.print("\t");
  246. Serial.print(q.x);
  247. Serial.print("\t");
  248. Serial.print(q.y);
  249. Serial.print("\t");
  250. Serial.println(q.z);

  251. #endif

  252. #ifdef OUTPUT_READABLE_EULER
  253. // display Euler angles in degrees
  254. mpu.dmpGetQuaternion(&q, fifoBuffer);
  255. mpu.dmpGetEuler(euler, &q);
  256. Serial.print("euler\t");
  257. Serial.print(euler[0] * 180/M_PI);
  258. Serial.print("\t");
  259. Serial.print(euler[1] * 180/M_PI);
  260. Serial.print("\t");
  261. Serial.println(euler[2] * 180/M_PI);
  262. #endif

  263. #ifdef OUTPUT_READABLE_YAWPITCHROLL
  264. // display Euler angles in degrees
  265. mpu.dmpGetQuaternion(&q, fifoBuffer);
  266. mpu.dmpGetGravity(&gravity, &q);
  267. mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
  268. Serial.print("ypr\t");
  269. Serial.print(ypr[0] * 180/M_PI);
  270. Serial.print("\t");
  271. Serial.print(ypr[1] * 180/M_PI);
  272. Serial.print("\t");
  273. Serial.println(ypr[2] * 180/M_PI);
  274. #endif

  275. #ifdef OUTPUT_READABLE_REALACCEL
  276. // display real acceleration, adjusted to remove gravity
  277. mpu.dmpGetQuaternion(&q, fifoBuffer);
  278. mpu.dmpGetAccel(&aa, fifoBuffer);
  279. mpu.dmpGetGravity(&gravity, &q);
  280. mpu.dmpGetLinearAccel(&aaReal, &aa, &gravity);
  281. /* Serial.print("areal\t");
  282. Serial.print(aaReal.x/16384.00);
  283. Serial.print(",");
  284. Serial.print(aaReal.y/16384.00);
  285. Serial.print(",");
  286. Serial.println(aaReal.z/16384.00);
  287. Serial.print(",");
  288. */
  289. // Serial.print("aax\t");
  290. Serial.print(aa.x/16384.00);
  291. Serial.print(",");
  292. Serial.print(aa.y/16384.00);
  293. Serial.print(",");
  294. Serial.print(aa.z/16384.00);
  295. Serial.print(",");
  296. // Serial.print("gravity\t");
  297. Serial.print(gravity.x/131.00);
  298. Serial.print(",");
  299. Serial.print(gravity.y/131.00);
  300. Serial.print(",");
  301. Serial.println(gravity.z/131.00);



  302. #endif

  303. #ifdef OUTPUT_READABLE_WORLDACCEL
  304. // display initial world-frame acceleration, adjusted to remove gravity
  305. // and rotated based on known orientation from quaternion
  306. mpu.dmpGetQuaternion(&q, fifoBuffer);
  307. mpu.dmpGetAccel(&aa, fifoBuffer);
  308. mpu.dmpGetGravity(&gravity, &q);
  309. mpu.dmpGetLinearAccel(&aaReal, &aa, &gravity);
  310. mpu.dmpGetLinearAccelInWorld(&aaWorld, &aaReal, &q);
  311. Serial.print("aworld\t");
  312. Serial.print(aaWorld.x);
  313. Serial.print("\t");
  314. Serial.print(aaWorld.y);
  315. Serial.print("\t");
  316. Serial.println(aaWorld.z);

  317. #endif

  318. #ifdef OUTPUT_TEAPOT
  319. // display quaternion values in InvenSense Teapot demo format:
  320. teapotPacket[2] = fifoBuffer[0];
  321. teapotPacket[3] = fifoBuffer[1];
  322. teapotPacket[4] = fifoBuffer[4];
  323. teapotPacket[5] = fifoBuffer[5];
  324. teapotPacket[6] = fifoBuffer[8];
  325. teapotPacket[7] = fifoBuffer[9];
  326. teapotPacket[8] = fifoBuffer[12];
  327. teapotPacket[9] = fifoBuffer[13];
  328. Serial.write(teapotPacket, 14);
  329. teapotPacket[11]++; // packetCount, loops at 0xFF on purpose
  330. #endif
  331. if (dataFile) {
  332. ax=aa.x/16384.00;

  333. ay=aa.y/16384.00;

  334. az=aa.z/16384.00;

  335. gx=gravity.x/131.00;

  336. gy=gravity.y/131.00;

  337. gz=gravity.z/131.00;

  338. dataFile.print(ax);
  339. dataFile.print(",");
  340. dataFile.print(ay);
  341. dataFile.print(",");
  342. dataFile.print(az);
  343. dataFile.print(",");
  344. // Serial.print("gravity\t");
  345. dataFile.print(gx);
  346. dataFile.print(",");
  347. dataFile.print(gy);
  348. dataFile.print(",");
  349. dataFile.println(gz);
  350. // dataFile.print("\n\t");
  351. //dataFile.close();
  352. // 数组dataString输出到串口
  353. //Serial.println(dataString);
  354. }
  355. // 如果无法打开文件,串口输出错误信息error opening datalog.txt
  356. else {
  357. Serial.println("error opening datalog.txt");
  358. }

  359. // blink LED to indicate activity
  360. blinkState = !blinkState;
  361. digitalWrite(LED_PIN, blinkState);
  362. }
  363. }
复制代码

本帖子中包含更多资源

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

x
回复

使用道具 举报

发表于 2013-11-5 13:13:47 | 显示全部楼层
//dataFile.close();
回复 支持 反对

使用道具 举报

发表于 2013-11-5 13:15:14 | 显示全部楼层
我没有SD卡模块,但是我想这个问题应该和dataFile.close()被注释掉有关。
你试一下吧。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-11-5 14:44:19 | 显示全部楼层
Damn_intuition 发表于 2013-11-5 13:15
我没有SD卡模块,但是我想这个问题应该和dataFile.close()被注释掉有关。
你试一下吧。

注释掉了~没有什么效果
回复 支持 反对

使用道具 举报

发表于 2013-11-5 16:13:45 | 显示全部楼层
龙翔竞天 发表于 2013-11-5 14:44
注释掉了~没有什么效果

问题有可能就出在你注释掉了这些代码。
回复 支持 反对

使用道具 举报

发表于 2013-11-5 16:27:53 | 显示全部楼层
MPU的DMP融合成功了??
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-11-5 16:58:15 | 显示全部楼层
Damn_intuition 发表于 2013-11-5 16:13
问题有可能就出在你注释掉了这些代码。

好了~嘿嘿,一个小问题,试了N多次才发现,但是还是会时不时的出现fifo flow的错误。。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-11-5 16:59:22 | 显示全部楼层
学慧放弃 发表于 2013-11-5 16:27
MPU的DMP融合成功了??

从DMP出来的加速度和陀螺仪的值还是很稳定的。就是融合后的姿态数据有抖动现象,一直没找到原因。
回复 支持 反对

使用道具 举报

发表于 2013-11-5 21:15:54 | 显示全部楼层
龙翔竞天 发表于 2013-11-5 16:59
从DMP出来的加速度和陀螺仪的值还是很稳定的。就是融合后的姿态数据有抖动现象,一直没找到原因。

不是融合不成功?可以把你融合的给我看看吗??听说串口的不错耶,但是都没试过
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-11-7 10:33:16 | 显示全部楼层
学慧放弃 发表于 2013-11-5 21:15
不是融合不成功?可以把你融合的给我看看吗??听说串口的不错耶,但是都没试过

6050的范例里就是融合的算法啊
回复 支持 反对

使用道具 举报

发表于 2013-11-7 19:32:45 | 显示全部楼层
龙翔竞天 发表于 2013-11-7 10:33
6050的范例里就是融合的算法啊

但是DMP还没试过呢,都说融合不成功呢
回复 支持 反对

使用道具 举报

发表于 2014-4-22 11:12:39 | 显示全部楼层
你之前用mpu-6050测位移  效果怎么样
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-11 03:56 , Processed in 0.039828 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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