{"id":886,"date":"2017-03-01T21:38:04","date_gmt":"2017-03-01T13:38:04","guid":{"rendered":"http:\/\/www.luwl.net\/?p=886"},"modified":"2017-03-15T19:39:24","modified_gmt":"2017-03-15T11:39:24","slug":"arduino-uno-mpu6050-%e9%99%80%e8%9e%ba%e4%bb%aa-%e8%bf%90%e7%94%a8%e5%8d%a1%e5%b0%94%e6%9b%bc%e6%bb%a4%e6%b3%a2%e5%a7%bf%e6%80%81%e8%a7%a3%e7%ae%97%e5%ae%9e%e9%aa%8c","status":"publish","type":"post","link":"http:\/\/www.luwl.net\/?p=886","title":{"rendered":"Arduino uno + mpu6050 \u9640\u87ba\u4eea \u8fd0\u7528\u5361\u5c14\u66fc\u6ee4\u6ce2\u59ff\u6001\u89e3\u7b97\u5b9e\u9a8c"},"content":{"rendered":"<p>\u5f88\u591a\u597d\u7684\u7f51\u5740\u65f6\u95f4\u957f\u4e86\u90fd\u5fd8\u8bb0\u4e86\u3002\u8fd9\u4e2a\u4e0d\u9519\u7684\u7f51\u5740\u4ee5\u540e\u90fd\u8981\u8bb0\u5f55\u4e0b\u6765\uff1a<\/p>\n<p>http:\/\/blog.csdn.net\/ling3ye\/article\/details\/51360568 ? ?<a href=\"http:\/\/blog.csdn.net\/ling3ye\/article\/details\/51360568\">Arduino uno + mpu6050 \u9640\u87ba\u4eea \u8fd0\u7528\u5361\u5c14\u66fc\u6ee4\u6ce2\u59ff\u6001\u89e3\u7b97\u5b9e\u9a8c<\/a><\/p>\n<p>&nbsp;<\/p>\n<h2>MPU6050\u516d\u8f74\u9640\u87ba\u4eea<\/h2>\n<p>\u4f5c\u7528\u4e8e\u56db\u8f74\u65e0\u4eba\u673a\uff0c\u5e73\u8861\u8f66\uff0c\u673a\u5668\u4eba\u7b49\u7b49\u7684\u7535\u5b50\u5b9e\u4f5c\u5f53\u4e2d\uff0c\u7528\u4e8e\u59ff\u6001\u5224\u65ad\uff0c\u638c\u63e1\u4e86\u53ef\u4ee5\u53d1\u6325\u81ea\u5df1\u7684\u60f3\u8c61\u5b8c\u6210\u66f4\u591a\u66f4\u6709\u8da3\u7684\u4f5c\u54c1\u3002<\/p>\n<p>\u672c\u4f8b\u7a0b\u8f93\u51faXYZ\u7684\u89d2\u5ea6\uff0c\u6b63\u8d1f90\u5ea6\u3002<\/p>\n<p>\u8fd0\u7528\u5361\u5c14\u66fc\u6ee4\u6ce2<a class=\"replace_word\" title=\"\u7b97\u6cd5\u4e0e\u6570\u636e\u7ed3\u6784\u77e5\u8bc6\u5e93\" href=\"http:\/\/lib.csdn.net\/base\/datastructure\" target=\"_blank\">\u7b97\u6cd5<\/a>\u89e3\u7b97\u59ff\u6001\uff0c\u611f\u89c9\u7b97\u662f\u6bd4\u8f83\u7a33\u5b9a\uff0c\u4f46\u597d\u50cf\u6709\u70b9\u504f\u79fb\u3002\u5927\u5bb6\u597d\u597d\u5b66\u4e60\u53c2\u8003\uff0c\u518d\u6539\u8fdb\u5427\u3002<\/p>\n<p>\u8f93\u51fa\u6548\u679c<\/p>\n<p>\u9996\u5148\u770b\u770b\u672c\u4f8b\u7a0bXYZ\u8f74\u7684\u8f93\u51fa\u6548\u679c\u56fe:<\/p>\n<p>\uff08\u65f6\u95f4\u66f2\u7ebf\u7684\u4f53\u73b0\u662f\uff1a\u9759\u6b62\u59ff\u6001\u2192\u6446\u52a8\u2192\u6062\u590d\u539f\u9759\u6b62\u59ff\u6001\u2192\u62cd\u52a8\u684c\u5b50\u2192\u9759\u6b62\u59ff\u6001\uff09<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" src=\"http:\/\/img.blog.csdn.net\/20160510093704265\" width=\"219\" height=\"124\" \/><\/p>\n<p>&nbsp;<\/p>\n<h2><a name=\"t1\"><\/a>Bom\u8868<\/h2>\n<p>Arduino Uno ? ? ? ? ? ? ? *1<\/p>\n<p>mpu6050 \u9640\u87ba\u4eea\u6a21\u5757 *1<\/p>\n<p>\u8df3\u7ebf ? ? ? ? ? ? ? ? ? ? ? ? ? ?\u82e5\u5e72<\/p>\n<h2><a name=\"t2\"><\/a>MPU6050 \u5f15\u811a\u8bf4\u660e<\/h2>\n<p>VCC ? ? ? ? ? ? 3.3-5V\uff08\u5185\u90e8\u6709\u7a33\u538b\u82af\u7247\uff09<\/p>\n<p>GND ? ? ? ? ? ? \u5730\u7ebf<br \/>\nSCL ? ? ? ? ? ? ?MPU6050\u4f5c\u4e3a\u4ece\u673a\u65f6IIC\u65f6\u949f\u7ebf<\/p>\n<p>SDA ? ? ? ? ? ? ?MPU6050\u4f5c\u4e3a\u4ece\u673a\u65f6IIC\u6570\u636e\u7ebf<\/p>\n<p>XCL ? ? ? ? ? ? ? MPU6050\u4f5c\u4e3a\u4e3b\u673a\u65f6IIC\u65f6\u949f\u7ebf<\/p>\n<p>XDA ? ? ? ? ? ? ?MPU6050\u4f5c\u4e3a\u4e3b\u673a\u65f6IIC\u6570\u636e\u7ebf<br \/>\nAD0 ? ? ? ? ? ? ?\u5730\u5740\u7ba1\u811a\uff0c\u8be5\u7ba1\u811a\u51b3\u5b9a\u4e86IIC\u5730\u5740\u7684\u6700\u4f4e\u4e00\u4f4d<\/p>\n<p>INT ? ? ? ? ? ? ? ?\u4e2d\u65ad\u5f15\u811a<\/p>\n<h2><a name=\"t3\"><\/a>\u63a5\u7ebf<\/h2>\n<p>Arduino uno+MPU6050\u63a5\u7ebf\u65b9\u5f0f\u5982\u4e0b<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/img.blog.csdn.net\/20160510093755668\" alt=\"\" width=\"251\" height=\"220\" \/><\/p>\n<h2><a name=\"t4\"><\/a>\u7a0b\u5e8f\u5b9e\u73b0<\/h2>\n<p>\u9996\u5148\u8981\u66f4\u65b0I2C\u5e93<\/p>\n<p>\u5728GITHUB\u627e\u5230\u7684I2C\u5e93<\/p>\n<p>\uff08\u7a0b\u5e8f\u6765\u6e90:?<a href=\"https:\/\/github.com\/jrowberg\/i2cdevlib\" target=\"_blank\">https:\/\/github.com\/jrowberg\/i2cdevlib<\/a>\uff09<\/p>\n<p>\u6253\u5f00,\u628aArduino\u6587\u4ef6\u5939\u91cc\u7684I2Cdev\uff0cMPU6050\u6587\u4ef6\u5939\u590d\u5236\u5230Arduino IDE\u7684\u5e93\u6587\u4ef6\u5939\u91cc<\/p>\n<p>(\u9ed8\u8ba4\u7684\u8def\u5f84\u662f\u8fd9\u4e2a C:\\Program Files (x86)\\Arduino\\libraries)<\/p>\n<p>&nbsp;<\/p>\n<p>\u5728GITHUB\u627e\u5230\u7684\u5361\u5c14\u66fc\u6ee4\u6ce2\u7a0b\u5e8f\uff08\u7a0b\u5e8f\u6765\u6e90: <a href=\"https:\/\/github.com\/wjjun\/MPU6050_Kalman\" target=\"_blank\">https:\/\/github.com\/wjjun\/MPU6050_Kalman<\/a>\uff09<\/p>\n<p>\u628a\u7a0b\u5e8f\u4e0a\u4f20\u5230\u677f\u5b50\u4e0a\uff0c\u6253\u5f00\u4e32\u53e3\u76d1\u89c6\u5668\uff0c\u5c31\u53ef\u4ee5\u770b\u5230\u4e00\u5806\u5806\u7684\u6570\u636e\u4e86<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n\r\n#include &quot;Wire.h&quot;\r\n#include &quot;I2Cdev.h&quot;\r\n#include &quot;MPU6050.h&quot;\r\n\r\nMPU6050 accelgyro;\r\n\r\nunsigned long now, lastTime = 0;\r\nfloat dt; \/\/\u5fae\u5206\u65f6\u95f4\r\n\r\nint16_t ax, ay, az, gx, gy, gz; \/\/\u52a0\u901f\u5ea6\u8ba1\u9640\u87ba\u4eea\u539f\u59cb\u6570\u636e\r\nfloat aax=0, aay=0,aaz=0, agx=0, agy=0, agz=0; \/\/\u89d2\u5ea6\u53d8\u91cf\r\nlong axo = 0, ayo = 0, azo = 0; \/\/\u52a0\u901f\u5ea6\u8ba1\u504f\u79fb\u91cf\r\nlong gxo = 0, gyo = 0, gzo = 0; \/\/\u9640\u87ba\u4eea\u504f\u79fb\u91cf\r\n\r\nfloat pi = 3.1415926;\r\nfloat AcceRatio = 16384.0; \/\/\u52a0\u901f\u5ea6\u8ba1\u6bd4\u4f8b\u7cfb\u6570\r\nfloat GyroRatio = 131.0; \/\/\u9640\u87ba\u4eea\u6bd4\u4f8b\u7cfb\u6570\r\n\r\nuint8_t n_sample = 8; \/\/\u52a0\u901f\u5ea6\u8ba1\u6ee4\u6ce2\u7b97\u6cd5\u91c7\u6837\u4e2a\u6570\r\nfloat aaxs&#x5B;8] = {0}, aays&#x5B;8] = {0}, aazs&#x5B;8] = {0}; \/\/x,y\u8f74\u91c7\u6837\u961f\u5217\r\nlong aax_sum, aay_sum,aaz_sum; \/\/x,y\u8f74\u91c7\u6837\u548c\r\n\r\nfloat a_x&#x5B;10]={0}, a_y&#x5B;10]={0},a_z&#x5B;10]={0} ,g_x&#x5B;10]={0} ,g_y&#x5B;10]={0},g_z&#x5B;10]={0}; \/\/\u52a0\u901f\u5ea6\u8ba1\u534f\u65b9\u5dee\u8ba1\u7b97\u961f\u5217\r\nfloat Px=1, Rx, Kx, Sx, Vx, Qx; \/\/x\u8f74\u5361\u5c14\u66fc\u53d8\u91cf\r\nfloat Py=1, Ry, Ky, Sy, Vy, Qy; \/\/y\u8f74\u5361\u5c14\u66fc\u53d8\u91cf\r\nfloat Pz=1, Rz, Kz, Sz, Vz, Qz; \/\/z\u8f74\u5361\u5c14\u66fc\u53d8\u91cf\r\n\r\nvoid setup()\r\n{\r\nWire.begin();\r\nSerial.begin(115200);\r\n\r\naccelgyro.initialize(); \/\/\u521d\u59cb\u5316\r\n\r\nunsigned short times = 200; \/\/\u91c7\u6837\u6b21\u6570\r\nfor(int i=0;i&lt;times;i++)\r\n{\r\naccelgyro.getMotion6(&amp;ax, &amp;ay, &amp;az, &amp;gx, &amp;gy, &amp;gz); \/\/\u8bfb\u53d6\u516d\u8f74\u539f\u59cb\u6570\u503c\r\naxo += ax; ayo += ay; azo += az; \/\/\u91c7\u6837\u548c\r\ngxo += gx; gyo += gy; gzo += gz;\r\n\r\n}\r\n\r\naxo \/= times; ayo \/= times; azo \/= times; \/\/\u8ba1\u7b97\u52a0\u901f\u5ea6\u8ba1\u504f\u79fb\r\ngxo \/= times; gyo \/= times; gzo \/= times; \/\/\u8ba1\u7b97\u9640\u87ba\u4eea\u504f\u79fb\r\n}\r\n\r\nvoid loop()\r\n{\r\nunsigned long now = millis(); \/\/\u5f53\u524d\u65f6\u95f4(ms)\r\ndt = (now - lastTime) \/ 1000.0; \/\/\u5fae\u5206\u65f6\u95f4(s)\r\nlastTime = now; \/\/\u4e0a\u4e00\u6b21\u91c7\u6837\u65f6\u95f4(ms)\r\n\r\naccelgyro.getMotion6(&amp;ax, &amp;ay, &amp;az, &amp;gx, &amp;gy, &amp;gz); \/\/\u8bfb\u53d6\u516d\u8f74\u539f\u59cb\u6570\u503c\r\n\r\nfloat accx = ax \/ AcceRatio; \/\/x\u8f74\u52a0\u901f\u5ea6\r\nfloat accy = ay \/ AcceRatio; \/\/y\u8f74\u52a0\u901f\u5ea6\r\nfloat accz = az \/ AcceRatio; \/\/z\u8f74\u52a0\u901f\u5ea6\r\n\r\naax = atan(accy \/ accz) * (-180) \/ pi; \/\/y\u8f74\u5bf9\u4e8ez\u8f74\u7684\u5939\u89d2\r\naay = atan(accx \/ accz) * 180 \/ pi; \/\/x\u8f74\u5bf9\u4e8ez\u8f74\u7684\u5939\u89d2\r\naaz = atan(accz \/ accy) * 180 \/ pi; \/\/z\u8f74\u5bf9\u4e8ey\u8f74\u7684\u5939\u89d2\r\n\r\naax_sum = 0; \/\/ \u5bf9\u4e8e\u52a0\u901f\u5ea6\u8ba1\u539f\u59cb\u6570\u636e\u7684\u6ed1\u52a8\u52a0\u6743\u6ee4\u6ce2\u7b97\u6cd5\r\naay_sum = 0;\r\naaz_sum = 0;\r\n\r\nfor(int i=1;i&lt;n_sample;i++)\r\n{\r\naaxs&#x5B;i-1] = aaxs&#x5B;i];\r\naax_sum += aaxs&#x5B;i] * i;\r\naays&#x5B;i-1] = aays&#x5B;i];\r\naay_sum += aays&#x5B;i] * i;\r\naazs&#x5B;i-1] = aazs&#x5B;i];\r\naaz_sum += aazs&#x5B;i] * i;\r\n\r\n}\r\n\r\naaxs&#x5B;n_sample-1] = aax;\r\naax_sum += aax * n_sample;\r\naax = (aax_sum \/ (11*n_sample\/2.0)) * 9 \/ 7.0; \/\/\u89d2\u5ea6\u8c03\u5e45\u81f30-90\u00b0\r\naays&#x5B;n_sample-1] = aay; \/\/\u6b64\u5904\u5e94\u7528\u5b9e\u9a8c\u6cd5\u53d6\u5f97\u5408\u9002\u7684\u7cfb\u6570\r\naay_sum += aay * n_sample; \/\/\u672c\u4f8b\u7cfb\u6570\u4e3a9\/7\r\naay = (aay_sum \/ (11*n_sample\/2.0)) * 9 \/ 7.0;\r\naazs&#x5B;n_sample-1] = aaz;\r\naaz_sum += aaz * n_sample;\r\naaz = (aaz_sum \/ (11*n_sample\/2.0)) * 9 \/ 7.0;\r\n\r\nfloat gyrox = - (gx-gxo) \/ GyroRatio * dt; \/\/x\u8f74\u89d2\u901f\u5ea6\r\nfloat gyroy = - (gy-gyo) \/ GyroRatio * dt; \/\/y\u8f74\u89d2\u901f\u5ea6\r\nfloat gyroz = - (gz-gzo) \/ GyroRatio * dt; \/\/z\u8f74\u89d2\u901f\u5ea6\r\nagx += gyrox; \/\/x\u8f74\u89d2\u901f\u5ea6\u79ef\u5206\r\nagy += gyroy; \/\/x\u8f74\u89d2\u901f\u5ea6\u79ef\u5206\r\nagz += gyroz;\r\n\r\n\/* kalman start *\/\r\nSx = 0; Rx = 0;\r\nSy = 0; Ry = 0;\r\nSz = 0; Rz = 0;\r\n\r\nfor(int i=1;i&lt;10;i++)\r\n{ \/\/\u6d4b\u91cf\u503c\u5e73\u5747\u503c\u8fd0\u7b97\r\na_x&#x5B;i-1] = a_x&#x5B;i]; \/\/\u5373\u52a0\u901f\u5ea6\u5e73\u5747\u503c\r\nSx += a_x&#x5B;i];\r\na_y&#x5B;i-1] = a_y&#x5B;i];\r\nSy += a_y&#x5B;i];\r\na_z&#x5B;i-1] = a_z&#x5B;i];\r\nSz += a_z&#x5B;i];\r\n\r\n}\r\n\r\na_x&#x5B;9] = aax;\r\nSx += aax;\r\nSx \/= 10; \/\/x\u8f74\u52a0\u901f\u5ea6\u5e73\u5747\u503c\r\na_y&#x5B;9] = aay;\r\nSy += aay;\r\nSy \/= 10; \/\/y\u8f74\u52a0\u901f\u5ea6\u5e73\u5747\u503c\r\na_z&#x5B;9] = aaz;\r\nSz += aaz;\r\nSz \/= 10;\r\n\r\nfor(int i=0;i&lt;10;i++)\r\n{\r\nRx += sq(a_x&#x5B;i] - Sx);\r\nRy += sq(a_y&#x5B;i] - Sy);\r\nRz += sq(a_z&#x5B;i] - Sz);\r\n\r\n}\r\n\r\nRx = Rx \/ 9; \/\/\u5f97\u5230\u65b9\u5dee\r\nRy = Ry \/ 9;\r\nRz = Rz \/ 9;\r\n\r\nPx = Px + 0.0025; \/\/ 0.0025\u5728\u4e0b\u9762\u6709\u8bf4\u660e...\r\nKx = Px \/ (Px + Rx); \/\/\u8ba1\u7b97\u5361\u5c14\u66fc\u589e\u76ca\r\nagx = agx + Kx * (aax - agx); \/\/\u9640\u87ba\u4eea\u89d2\u5ea6\u4e0e\u52a0\u901f\u5ea6\u8ba1\u901f\u5ea6\u53e0\u52a0\r\nPx = (1 - Kx) * Px; \/\/\u66f4\u65b0p\u503c\r\n\r\nPy = Py + 0.0025;\r\nKy = Py \/ (Py + Ry);\r\nagy = agy + Ky * (aay - agy);\r\nPy = (1 - Ky) * Py;\r\n\r\nPz = Pz + 0.0025;\r\nKz = Pz \/ (Pz + Rz);\r\nagz = agz + Kz * (aaz - agz);\r\nPz = (1 - Kz) * Pz;\r\n\r\n\/* kalman end *\/\r\nSerial.print(accx);Serial.print(&quot;,&quot;);\r\nSerial.print(accy);Serial.print(&quot;,&quot;);\r\nSerial.print(accz);Serial.print(&quot;,&quot;);\r\nSerial.print(gyrox);Serial.print(&quot;,&quot;);\r\nSerial.print(gyroy);Serial.print(&quot;,&quot;);\r\nSerial.print(gyroz);Serial.print(&quot;,&quot;);\r\nSerial.print(agx);Serial.print(&quot;,&quot;);\r\nSerial.print(agy);Serial.print(&quot;,&quot;);\r\nSerial.print(agz);Serial.println();\r\n\r\n}\r\n\r\n<\/pre>\n<p>\u60f3\u7528MPU6050\u6765\u6d4b\u8bd5\u632f\u52a8\u503c\uff0c\u632f\u52a8\u503c\u5e94\u8be5\u4eceMPU6050\u7684\u52a0\u901f\u5ea6\u503c\u53ef\u4ee5\u83b7\u53d6\u5230\u3002mpu6050\u539f\u59cb\u8f93\u51fa\u76846\u4e2a\u4fe1\u606f\u4e3axyz\u4e09\u8f74\u7684\u52a0\u901f\u5ea6\u548c\u89d2\u901f\u5ea6\u3002\u5e76\u4e0d\u80fd\u8f93\u51fa\u73b0\u5728\u7684\u59ff\u6001\u3002\u7528\u5361\u5c14\u66fc\u6ee4\u6ce2\u5f97\u5230\u7684\u6570\u636e\u662f\u59ff\u6001\u6570\u636e\u3002<\/p>\n<p>\u5668\u4ef6\u4e2d\u6587\u8d44\u6599<\/p>\n<p><a href=\"http:\/\/www.luwl.net\/?attachment_id=895\" rel=\"attachment wp-att-895\">MPU-6000.6050\u4e2d\u6587\u8d44\u6599<\/a><\/p>\n<p><a href=\"http:\/\/www.luwl.net\/?attachment_id=896\" rel=\"attachment wp-att-896\">MPU6050\u4e2d\u6587\u5bc4\u5b58\u5668\u624b\u518c<\/a><\/p>\n<p><a href=\"http:\/\/www.luwl.net\/?attachment_id=924\" rel=\"attachment wp-att-924\">MPU6050\u6559\u7a0b<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f88\u591a\u597d\u7684\u7f51\u5740\u65f6\u95f4\u957f\u4e86\u90fd\u5fd8\u8bb0\u4e86\u3002\u8fd9\u4e2a\u4e0d\u9519\u7684\u7f51\u5740\u4ee5\u540e\u90fd\u8981\u8bb0\u5f55\u4e0b\u6765\uff1a http:\/\/b &hellip; <a href=\"http:\/\/www.luwl.net\/?p=886\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-886","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/www.luwl.net\/index.php?rest_route=\/wp\/v2\/posts\/886","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.luwl.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.luwl.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.luwl.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.luwl.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=886"}],"version-history":[{"count":6,"href":"http:\/\/www.luwl.net\/index.php?rest_route=\/wp\/v2\/posts\/886\/revisions"}],"predecessor-version":[{"id":925,"href":"http:\/\/www.luwl.net\/index.php?rest_route=\/wp\/v2\/posts\/886\/revisions\/925"}],"wp:attachment":[{"href":"http:\/\/www.luwl.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.luwl.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=886"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.luwl.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}