{"id":260,"date":"2017-04-10T14:57:48","date_gmt":"2017-04-10T11:57:48","guid":{"rendered":"https:\/\/www.neyaptik.com\/blog\/?p=260"},"modified":"2017-04-10T14:57:48","modified_gmt":"2017-04-10T11:57:48","slug":"mpu6050-kalibrasyon","status":"publish","type":"post","link":"https:\/\/www.neyaptik.com\/blog\/mpu6050-kalibrasyon\/","title":{"rendered":"MPU6050 Kalibrasyon"},"content":{"rendered":"<p>InvenSense&#8217;nin \u00fcretti\u011fi bu d\u00fc\u015f\u00fck fiyatl\u0131, geli\u015fmi\u015f jiroskoplar\u0131 inceliyordum bir s\u00fcredir. Sapmalar\u0131 minimuma getirmek ad\u0131na kalibrasyon yapmak gerekli gibi. Mevcut kalibrasyonu 3-4 derecelik bir sapma g\u00f6steriyordu bendeki.<\/p>\n<p>MPU6050 ve di\u011fer I2C cihazlar i\u00e7in \u00f6nerdi\u011fim I2Cdevlib forumlar\u0131ndan ald\u0131m bu \u00f6rne\u011fi.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-261\" src=\"https:\/\/www.neyaptik.com\/blog\/wp-content\/uploads\/2017\/04\/Screenshot_12-300x93.png\" alt=\"\" width=\"300\" height=\"93\" srcset=\"https:\/\/www.neyaptik.com\/blog\/wp-content\/uploads\/2017\/04\/Screenshot_12-300x93.png 300w, https:\/\/www.neyaptik.com\/blog\/wp-content\/uploads\/2017\/04\/Screenshot_12.png 353w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>\u015eimdi bakal\u0131m kalibrasyon s\u00fcrecine.<\/p>\n<h2>\u00d6ncelikle,<\/h2>\n<p>Arduino ve MPU6050 aras\u0131ndaki ba\u011flant\u0131y\u0131 sa\u011flay\u0131n.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-262 size-large\" src=\"https:\/\/www.neyaptik.com\/blog\/wp-content\/uploads\/2017\/04\/MPU-6050-Board-GY-521-Acelerometro-Giroscopio_bb-1024x718.png\" alt=\"\" width=\"1024\" height=\"718\" srcset=\"https:\/\/www.neyaptik.com\/blog\/wp-content\/uploads\/2017\/04\/MPU-6050-Board-GY-521-Acelerometro-Giroscopio_bb-1024x718.png 1024w, https:\/\/www.neyaptik.com\/blog\/wp-content\/uploads\/2017\/04\/MPU-6050-Board-GY-521-Acelerometro-Giroscopio_bb-300x210.png 300w, https:\/\/www.neyaptik.com\/blog\/wp-content\/uploads\/2017\/04\/MPU-6050-Board-GY-521-Acelerometro-Giroscopio_bb-768x539.png 768w, https:\/\/www.neyaptik.com\/blog\/wp-content\/uploads\/2017\/04\/MPU-6050-Board-GY-521-Acelerometro-Giroscopio_bb.png 1125w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/> <img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-263 size-large\" src=\"https:\/\/www.neyaptik.com\/blog\/wp-content\/uploads\/2017\/04\/IMG_41121-e1491825304925-1024x987.jpg\" alt=\"\" width=\"1024\" height=\"987\" srcset=\"https:\/\/www.neyaptik.com\/blog\/wp-content\/uploads\/2017\/04\/IMG_41121-e1491825304925-1024x987.jpg 1024w, https:\/\/www.neyaptik.com\/blog\/wp-content\/uploads\/2017\/04\/IMG_41121-e1491825304925-300x289.jpg 300w, https:\/\/www.neyaptik.com\/blog\/wp-content\/uploads\/2017\/04\/IMG_41121-e1491825304925-768x740.jpg 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>Ard\u0131ndan uygun ve d\u00fcz bir zemin bulup bant yard\u0131m\u0131yla tutturun.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-265\" src=\"https:\/\/www.neyaptik.com\/blog\/wp-content\/uploads\/2017\/04\/IMG_41141-e1491824808291-300x225.jpg\" alt=\"\" width=\"300\" height=\"225\" srcset=\"https:\/\/www.neyaptik.com\/blog\/wp-content\/uploads\/2017\/04\/IMG_41141-e1491824808291-300x225.jpg 300w, https:\/\/www.neyaptik.com\/blog\/wp-content\/uploads\/2017\/04\/IMG_41141-e1491824808291-768x576.jpg 768w, https:\/\/www.neyaptik.com\/blog\/wp-content\/uploads\/2017\/04\/IMG_41141-e1491824808291-1024x768.jpg 1024w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/> <img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-264\" src=\"https:\/\/www.neyaptik.com\/blog\/wp-content\/uploads\/2017\/04\/IMG_41131-e1491824794292-300x225.jpg\" alt=\"\" width=\"300\" height=\"225\" srcset=\"https:\/\/www.neyaptik.com\/blog\/wp-content\/uploads\/2017\/04\/IMG_41131-e1491824794292-300x225.jpg 300w, https:\/\/www.neyaptik.com\/blog\/wp-content\/uploads\/2017\/04\/IMG_41131-e1491824794292-768x576.jpg 768w, https:\/\/www.neyaptik.com\/blog\/wp-content\/uploads\/2017\/04\/IMG_41131-e1491824794292-1024x768.jpg 1024w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>Gelelim yaz\u0131l\u0131m k\u0131sm\u0131na,<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-266 size-large\" src=\"https:\/\/www.neyaptik.com\/blog\/wp-content\/uploads\/2017\/04\/Screenshot_10-1024x548.png\" alt=\"\" width=\"1024\" height=\"548\" srcset=\"https:\/\/www.neyaptik.com\/blog\/wp-content\/uploads\/2017\/04\/Screenshot_10-1024x548.png 1024w, https:\/\/www.neyaptik.com\/blog\/wp-content\/uploads\/2017\/04\/Screenshot_10-300x161.png 300w, https:\/\/www.neyaptik.com\/blog\/wp-content\/uploads\/2017\/04\/Screenshot_10-768x411.png 768w, https:\/\/www.neyaptik.com\/blog\/wp-content\/uploads\/2017\/04\/Screenshot_10.png 1128w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/> <img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-267 size-large\" src=\"https:\/\/www.neyaptik.com\/blog\/wp-content\/uploads\/2017\/04\/Screenshot_11-1024x547.png\" alt=\"\" width=\"1024\" height=\"547\" srcset=\"https:\/\/www.neyaptik.com\/blog\/wp-content\/uploads\/2017\/04\/Screenshot_11-1024x547.png 1024w, https:\/\/www.neyaptik.com\/blog\/wp-content\/uploads\/2017\/04\/Screenshot_11-300x160.png 300w, https:\/\/www.neyaptik.com\/blog\/wp-content\/uploads\/2017\/04\/Screenshot_11-768x410.png 768w, https:\/\/www.neyaptik.com\/blog\/wp-content\/uploads\/2017\/04\/Screenshot_11.png 1364w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>\u00d6rnek olarak bu offset de\u011ferlerini DMP6 \u00f6rne\u011finde kulland\u0131m. Bu \u00f6rnek MPU6050 k\u00fct\u00fcphanesinde mevcut.<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Arduino sketch that returns calibration offsets for MPU6050 \/\/   Version 1.1  (31th January 2014)\r\n\/\/ Done by Luis R\u00f3denas &lt;luisrodenaslorda@gmail.com&gt;\r\n\/\/ Based on the I2Cdev library and previous work by Jeff Rowberg &lt;jeff@rowberg.net&gt;\r\n\/\/ Updates (of the library) should (hopefully) always be available at https:\/\/github.com\/jrowberg\/i2cdevlib\r\n\r\n\/\/ These offsets were meant to calibrate MPU6050's internal DMP, but can be also useful for reading sensors. \r\n\/\/ The effect of temperature has not been taken into account so I can't promise that it will work if you \r\n\/\/ calibrate indoors and then use it outdoors. Best is to calibrate and use at the same room temperature.\r\n\r\n\/* ==========  LICENSE  ==================================\r\n I2Cdev device library code is placed under the MIT license\r\n Copyright (c) 2011 Jeff Rowberg\r\n \r\n Permission is hereby granted, free of charge, to any person obtaining a copy\r\n of this software and associated documentation files (the \"Software\"), to deal\r\n in the Software without restriction, including without limitation the rights\r\n to use, copy, modify, merge, publish, distribute, sublicense, and\/or sell\r\n copies of the Software, and to permit persons to whom the Software is\r\n furnished to do so, subject to the following conditions:\r\n \r\n The above copyright notice and this permission notice shall be included in\r\n all copies or substantial portions of the Software.\r\n \r\n THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r\n IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r\n FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r\n AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r\n LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r\n OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r\n THE SOFTWARE.\r\n =========================================================\r\n *\/\r\n\r\n\/\/ I2Cdev and MPU6050 must be installed as libraries\r\n#include \"I2Cdev.h\"\r\n#include \"MPU6050.h\"\r\n#include \"Wire.h\"\r\n\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/   CONFIGURATION   \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\/\/Change this 3 variables if you want to fine tune the skecth to your needs.\r\nint buffersize=1000;     \/\/Amount of readings used to average, make it higher to get more precision but sketch will be slower  (default:1000)\r\nint acel_deadzone=8;     \/\/Acelerometer error allowed, make it lower to get more precision, but sketch may not converge  (default:8)\r\nint giro_deadzone=1;     \/\/Giro error allowed, make it lower to get more precision, but sketch may not converge  (default:1)\r\n\r\n\/\/ default I2C address is 0x68\r\n\/\/ specific I2C addresses may be passed as a parameter here\r\n\/\/ AD0 low = 0x68 (default for InvenSense evaluation board)\r\n\/\/ AD0 high = 0x69\r\n\/\/MPU6050 accelgyro;\r\nMPU6050 accelgyro(0x68); \/\/ &lt;-- use for AD0 high\r\n\r\nint16_t ax, ay, az,gx, gy, gz;\r\n\r\nint mean_ax,mean_ay,mean_az,mean_gx,mean_gy,mean_gz,state=0;\r\nint ax_offset,ay_offset,az_offset,gx_offset,gy_offset,gz_offset;\r\n\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/   SETUP   \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\nvoid setup() {\r\n  \/\/ join I2C bus (I2Cdev library doesn't do this automatically)\r\n  Wire.begin();\r\n  \/\/ COMMENT NEXT LINE IF YOU ARE USING ARDUINO DUE\r\n  TWBR = 24; \/\/ 400kHz I2C clock (200kHz if CPU is 8MHz). Leonardo measured 250kHz.\r\n\r\n  \/\/ initialize serial communication\r\n  Serial.begin(115200);\r\n\r\n  \/\/ initialize device\r\n  accelgyro.initialize();\r\n\r\n  \/\/ wait for ready\r\n  while (Serial.available() &amp;&amp; Serial.read()); \/\/ empty buffer\r\n  while (!Serial.available()){\r\n    Serial.println(F(\"Send any character to start sketch.\\n\"));\r\n    delay(1500);\r\n  }                \r\n  while (Serial.available() &amp;&amp; Serial.read()); \/\/ empty buffer again\r\n\r\n  \/\/ start message\r\n  Serial.println(\"\\nMPU6050 Calibration Sketch\");\r\n  delay(2000);\r\n  Serial.println(\"\\nYour MPU6050 should be placed in horizontal position, with package letters facing up. \\nDon't touch it until you see a finish message.\\n\");\r\n  delay(3000);\r\n  \/\/ verify connection\r\n  Serial.println(accelgyro.testConnection() ? \"MPU6050 connection successful\" : \"MPU6050 connection failed\");\r\n  delay(1000);\r\n  \/\/ reset offsets\r\n  accelgyro.setXAccelOffset(0);\r\n  accelgyro.setYAccelOffset(0);\r\n  accelgyro.setZAccelOffset(0);\r\n  accelgyro.setXGyroOffset(0);\r\n  accelgyro.setYGyroOffset(0);\r\n  accelgyro.setZGyroOffset(0);\r\n}\r\n\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/   LOOP   \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\nvoid loop() {\r\n  if (state==0){\r\n    Serial.println(\"\\nReading sensors for first time...\");\r\n    meansensors();\r\n    state++;\r\n    delay(1000);\r\n  }\r\n\r\n  if (state==1) {\r\n    Serial.println(\"\\nCalculating offsets...\");\r\n    calibration();\r\n    state++;\r\n    delay(1000);\r\n  }\r\n\r\n  if (state==2) {\r\n    meansensors();\r\n    Serial.println(\"\\nFINISHED!\");\r\n    Serial.print(\"\\nSensor readings with offsets:\\t\");\r\n    Serial.print(mean_ax); \r\n    Serial.print(\"\\t\");\r\n    Serial.print(mean_ay); \r\n    Serial.print(\"\\t\");\r\n    Serial.print(mean_az); \r\n    Serial.print(\"\\t\");\r\n    Serial.print(mean_gx); \r\n    Serial.print(\"\\t\");\r\n    Serial.print(mean_gy); \r\n    Serial.print(\"\\t\");\r\n    Serial.println(mean_gz);\r\n    Serial.print(\"Your offsets:\\t\");\r\n    Serial.print(ax_offset); \r\n    Serial.print(\"\\t\");\r\n    Serial.print(ay_offset); \r\n    Serial.print(\"\\t\");\r\n    Serial.print(az_offset); \r\n    Serial.print(\"\\t\");\r\n    Serial.print(gx_offset); \r\n    Serial.print(\"\\t\");\r\n    Serial.print(gy_offset); \r\n    Serial.print(\"\\t\");\r\n    Serial.println(gz_offset); \r\n    Serial.println(\"\\nData is printed as: acelX acelY acelZ giroX giroY giroZ\");\r\n    Serial.println(\"Check that your sensor readings are close to 0 0 16384 0 0 0\");\r\n    Serial.println(\"If calibration was succesful write down your offsets so you can set them in your projects using something similar to mpu.setXAccelOffset(youroffset)\");\r\n    while (1);\r\n  }\r\n}\r\n\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/   FUNCTIONS   \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\nvoid meansensors(){\r\n  long i=0,buff_ax=0,buff_ay=0,buff_az=0,buff_gx=0,buff_gy=0,buff_gz=0;\r\n\r\n  while (i&lt;(buffersize+101)){\r\n    \/\/ read raw accel\/gyro measurements from device\r\n    accelgyro.getMotion6(&amp;ax, &amp;ay, &amp;az, &amp;gx, &amp;gy, &amp;gz);\r\n    \r\n    if (i&gt;100 &amp;&amp; i&lt;=(buffersize+100)){ \/\/First 100 measures are discarded\r\n      buff_ax=buff_ax+ax;\r\n      buff_ay=buff_ay+ay;\r\n      buff_az=buff_az+az;\r\n      buff_gx=buff_gx+gx;\r\n      buff_gy=buff_gy+gy;\r\n      buff_gz=buff_gz+gz;\r\n    }\r\n    if (i==(buffersize+100)){\r\n      mean_ax=buff_ax\/buffersize;\r\n      mean_ay=buff_ay\/buffersize;\r\n      mean_az=buff_az\/buffersize;\r\n      mean_gx=buff_gx\/buffersize;\r\n      mean_gy=buff_gy\/buffersize;\r\n      mean_gz=buff_gz\/buffersize;\r\n    }\r\n    i++;\r\n    delay(2); \/\/Needed so we don't get repeated measures\r\n  }\r\n}\r\n\r\nvoid calibration(){\r\n  ax_offset=-mean_ax\/8;\r\n  ay_offset=-mean_ay\/8;\r\n  az_offset=(16384-mean_az)\/8;\r\n\r\n  gx_offset=-mean_gx\/4;\r\n  gy_offset=-mean_gy\/4;\r\n  gz_offset=-mean_gz\/4;\r\n  while (1){\r\n    int ready=0;\r\n    accelgyro.setXAccelOffset(ax_offset);\r\n    accelgyro.setYAccelOffset(ay_offset);\r\n    accelgyro.setZAccelOffset(az_offset);\r\n\r\n    accelgyro.setXGyroOffset(gx_offset);\r\n    accelgyro.setYGyroOffset(gy_offset);\r\n    accelgyro.setZGyroOffset(gz_offset);\r\n\r\n    meansensors();\r\n    Serial.println(\"...\");\r\n\r\n    if (abs(mean_ax)&lt;=acel_deadzone) ready++;\r\n    else ax_offset=ax_offset-mean_ax\/acel_deadzone;\r\n\r\n    if (abs(mean_ay)&lt;=acel_deadzone) ready++;\r\n    else ay_offset=ay_offset-mean_ay\/acel_deadzone;\r\n\r\n    if (abs(16384-mean_az)&lt;=acel_deadzone) ready++;\r\n    else az_offset=az_offset+(16384-mean_az)\/acel_deadzone;\r\n\r\n    if (abs(mean_gx)&lt;=giro_deadzone) ready++;\r\n    else gx_offset=gx_offset-mean_gx\/(giro_deadzone+1);\r\n\r\n    if (abs(mean_gy)&lt;=giro_deadzone) ready++;\r\n    else gy_offset=gy_offset-mean_gy\/(giro_deadzone+1);\r\n\r\n    if (abs(mean_gz)&lt;=giro_deadzone) ready++;\r\n    else gz_offset=gz_offset-mean_gz\/(giro_deadzone+1);\r\n\r\n    if (ready==6) break;\r\n  }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>InvenSense&#8217;nin \u00fcretti\u011fi bu d\u00fc\u015f\u00fck fiyatl\u0131, geli\u015fmi\u015f jiroskoplar\u0131 inceliyordum bir s\u00fcredir. Sapmalar\u0131 minimuma getirmek ad\u0131na kalibrasyon yapmak gerekli gibi. Mevcut kalibrasyonu 3-4 derecelik bir sapma g\u00f6steriyordu bendeki. MPU6050 ve di\u011fer I2C cihazlar i\u00e7in \u00f6nerdi\u011fim I2Cdevlib forumlar\u0131ndan&#8230;<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":262,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[105,122],"tags":[],"_links":{"self":[{"href":"https:\/\/www.neyaptik.com\/blog\/wp-json\/wp\/v2\/posts\/260"}],"collection":[{"href":"https:\/\/www.neyaptik.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.neyaptik.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.neyaptik.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.neyaptik.com\/blog\/wp-json\/wp\/v2\/comments?post=260"}],"version-history":[{"count":2,"href":"https:\/\/www.neyaptik.com\/blog\/wp-json\/wp\/v2\/posts\/260\/revisions"}],"predecessor-version":[{"id":269,"href":"https:\/\/www.neyaptik.com\/blog\/wp-json\/wp\/v2\/posts\/260\/revisions\/269"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.neyaptik.com\/blog\/wp-json\/wp\/v2\/media\/262"}],"wp:attachment":[{"href":"https:\/\/www.neyaptik.com\/blog\/wp-json\/wp\/v2\/media?parent=260"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.neyaptik.com\/blog\/wp-json\/wp\/v2\/categories?post=260"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.neyaptik.com\/blog\/wp-json\/wp\/v2\/tags?post=260"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}