Advertisement
Guest User

MPU6050 by Luis Ródenas

a guest
Oct 11th, 2015
752
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.84 KB | None | 0 0
  1. // Arduino sketch that returns calibration offsets for MPU6050 // Version 1.1 (31th January 2014)
  2. // Done by Luis Ródenas <luisrodenaslorda@gmail.com>
  3. // Based on the I2Cdev library and previous work by Jeff Rowberg <jeff@rowberg.net>
  4. // Updates (of the library) should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
  5.  
  6. // These offsets were meant to calibrate MPU6050's internal DMP, but can be also useful for reading sensors.
  7. // The effect of temperature has not been taken into account so I can't promise that it will work if you
  8. // calibrate indoors and then use it outdoors. Best is to calibrate and use at the same room temperature.
  9.  
  10. /* ========== LICENSE ==================================
  11. I2Cdev device library code is placed under the MIT license
  12. Copyright (c) 2011 Jeff Rowberg
  13.  
  14. Permission is hereby granted, free of charge, to any person obtaining a copy
  15. of this software and associated documentation files (the "Software"), to deal
  16. in the Software without restriction, including without limitation the rights
  17. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  18. copies of the Software, and to permit persons to whom the Software is
  19. furnished to do so, subject to the following conditions:
  20.  
  21. The above copyright notice and this permission notice shall be included in
  22. all copies or substantial portions of the Software.
  23.  
  24. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  25. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  27. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  28. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  29. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  30. THE SOFTWARE.
  31. =========================================================
  32. */
  33.  
  34. // I2Cdev and MPU6050 must be installed as libraries
  35. #include "I2Cdev.h"
  36. #include "MPU6050.h"
  37. #include "Wire.h"
  38.  
  39. /////////////////////////////////// CONFIGURATION /////////////////////////////
  40. //Change this 3 variables if you want to fine tune the skecth to your needs.
  41. int buffersize=1000; //Amount of readings used to average, make it higher to get more precision but sketch will be slower (default:1000)
  42. int acel_deadzone=8; //Acelerometer error allowed, make it lower to get more precision, but sketch may not converge (default:8)
  43. int giro_deadzone=1; //Giro error allowed, make it lower to get more precision, but sketch may not converge (default:1)
  44.  
  45. // default I2C address is 0x68
  46. // specific I2C addresses may be passed as a parameter here
  47. // AD0 low = 0x68 (default for InvenSense evaluation board)
  48. // AD0 high = 0x69
  49. //MPU6050 accelgyro;
  50. MPU6050 accelgyro(0x68); // <-- use for AD0 high
  51.  
  52. int16_t ax, ay, az,gx, gy, gz;
  53.  
  54. int mean_ax,mean_ay,mean_az,mean_gx,mean_gy,mean_gz,state=0;
  55. int ax_offset,ay_offset,az_offset,gx_offset,gy_offset,gz_offset;
  56.  
  57. /////////////////////////////////// SETUP ////////////////////////////////////
  58. void setup() {
  59. // join I2C bus (I2Cdev library doesn't do this automatically)
  60. Wire.begin();
  61. // COMMENT NEXT LINE IF YOU ARE USING ARDUINO DUE
  62. TWBR = 24; // 400kHz I2C clock (200kHz if CPU is 8MHz). Leonardo measured 250kHz.
  63.  
  64. // initialize serial communication
  65. Serial.begin(115200);
  66.  
  67. // initialize device
  68. accelgyro.initialize();
  69.  
  70. // wait for ready
  71. while (Serial.available() && Serial.read()); // empty buffer
  72. while (!Serial.available()){
  73. Serial.println(F("Send any character to start sketch.\n"));
  74. delay(1500);
  75. }
  76. while (Serial.available() && Serial.read()); // empty buffer again
  77.  
  78. // start message
  79. Serial.println("\nMPU6050 Calibration Sketch");
  80. delay(2000);
  81. 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");
  82. delay(3000);
  83. // verify connection
  84. Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
  85. delay(1000);
  86. // reset offsets
  87. accelgyro.setXAccelOffset(0);
  88. accelgyro.setYAccelOffset(0);
  89. accelgyro.setZAccelOffset(0);
  90. accelgyro.setXGyroOffset(0);
  91. accelgyro.setYGyroOffset(0);
  92. accelgyro.setZGyroOffset(0);
  93. }
  94.  
  95. /////////////////////////////////// LOOP ////////////////////////////////////
  96. void loop() {
  97. if (state==0){
  98. Serial.println("\nReading sensors for first time...");
  99. meansensors();
  100. state++;
  101. delay(1000);
  102. }
  103.  
  104. if (state==1) {
  105. Serial.println("\nCalculating offsets...");
  106. calibration();
  107. state++;
  108. delay(1000);
  109. }
  110.  
  111. if (state==2) {
  112. meansensors();
  113. Serial.println("\nFINISHED!");
  114. Serial.print("\nSensor readings with offsets:\t");
  115. Serial.print(mean_ax);
  116. Serial.print("\t");
  117. Serial.print(mean_ay);
  118. Serial.print("\t");
  119. Serial.print(mean_az);
  120. Serial.print("\t");
  121. Serial.print(mean_gx);
  122. Serial.print("\t");
  123. Serial.print(mean_gy);
  124. Serial.print("\t");
  125. Serial.println(mean_gz);
  126. Serial.print("Your offsets:\t");
  127. Serial.print(ax_offset);
  128. Serial.print("\t");
  129. Serial.print(ay_offset);
  130. Serial.print("\t");
  131. Serial.print(az_offset);
  132. Serial.print("\t");
  133. Serial.print(gx_offset);
  134. Serial.print("\t");
  135. Serial.print(gy_offset);
  136. Serial.print("\t");
  137. Serial.println(gz_offset);
  138. Serial.println("\nData is printed as: acelX acelY acelZ giroX giroY giroZ");
  139. Serial.println("Check that your sensor readings are close to 0 0 16384 0 0 0");
  140. 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)");
  141. while (1);
  142. }
  143. }
  144.  
  145. /////////////////////////////////// FUNCTIONS ////////////////////////////////////
  146. void meansensors(){
  147. long i=0,buff_ax=0,buff_ay=0,buff_az=0,buff_gx=0,buff_gy=0,buff_gz=0;
  148.  
  149. while (i<(buffersize+101)){
  150. // read raw accel/gyro measurements from device
  151. accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  152.  
  153. if (i>100 && i<=(buffersize+100)){ //First 100 measures are discarded
  154. buff_ax=buff_ax+ax;
  155. buff_ay=buff_ay+ay;
  156. buff_az=buff_az+az;
  157. buff_gx=buff_gx+gx;
  158. buff_gy=buff_gy+gy;
  159. buff_gz=buff_gz+gz;
  160. }
  161. if (i==(buffersize+100)){
  162. mean_ax=buff_ax/buffersize;
  163. mean_ay=buff_ay/buffersize;
  164. mean_az=buff_az/buffersize;
  165. mean_gx=buff_gx/buffersize;
  166. mean_gy=buff_gy/buffersize;
  167. mean_gz=buff_gz/buffersize;
  168. }
  169. i++;
  170. delay(2); //Needed so we don't get repeated measures
  171. }
  172. }
  173.  
  174. void calibration(){
  175. ax_offset=-mean_ax/8;
  176. ay_offset=-mean_ay/8;
  177. az_offset=(16384-mean_az)/8;
  178.  
  179. gx_offset=-mean_gx/4;
  180. gy_offset=-mean_gy/4;
  181. gz_offset=-mean_gz/4;
  182. while (1){
  183. int ready=0;
  184. accelgyro.setXAccelOffset(ax_offset);
  185. accelgyro.setYAccelOffset(ay_offset);
  186. accelgyro.setZAccelOffset(az_offset);
  187.  
  188. accelgyro.setXGyroOffset(gx_offset);
  189. accelgyro.setYGyroOffset(gy_offset);
  190. accelgyro.setZGyroOffset(gz_offset);
  191.  
  192. meansensors();
  193. Serial.println("...");
  194.  
  195. if (abs(mean_ax)<=acel_deadzone) ready++;
  196. else ax_offset=ax_offset-mean_ax/acel_deadzone;
  197.  
  198. if (abs(mean_ay)<=acel_deadzone) ready++;
  199. else ay_offset=ay_offset-mean_ay/acel_deadzone;
  200.  
  201. if (abs(16384-mean_az)<=acel_deadzone) ready++;
  202. else az_offset=az_offset+(16384-mean_az)/acel_deadzone;
  203.  
  204. if (abs(mean_gx)<=giro_deadzone) ready++;
  205. else gx_offset=gx_offset-mean_gx/(giro_deadzone+1);
  206.  
  207. if (abs(mean_gy)<=giro_deadzone) ready++;
  208. else gy_offset=gy_offset-mean_gy/(giro_deadzone+1);
  209.  
  210. if (abs(mean_gz)<=giro_deadzone) ready++;
  211. else gz_offset=gz_offset-mean_gz/(giro_deadzone+1);
  212.  
  213. if (ready==6) break;
  214. }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement